From 72a8c03296df8b28b24b4bbeef15144d481cdda8 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 24 May 2020 03:17:00 +0700 Subject: [PATCH] [FEATURE] MysqlHWIDProvider --- .../auth/protect/hwid/HWIDProvider.java | 11 ++ .../auth/protect/hwid/MemoryHWIDProvider.java | 1 + .../auth/protect/hwid/MysqlHWIDProvider.java | 134 ++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MysqlHWIDProvider.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java index a1b90816..d9ce4f95 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java @@ -16,6 +16,7 @@ public static void registerProviders() { if(!registredProv) { providers.register("memory", MemoryHWIDProvider.class); + providers.register("mysql", MysqlHWIDProvider.class); registredProv = true; } } @@ -104,4 +105,14 @@ protected void printHardwareInfo(LogHelper.Level logLevel, HardwareReportRequest LogHelper.log(logLevel, String.format("[HardwareInfo] Memory max: %d | battery %s", info.totalMemory, info.battery ? "true" : "false") , false); LogHelper.log(logLevel, String.format("[HardwareInfo] HWDiskID %s | baseboardSerialNumber %s | displayId hash: %s", info.hwDiskId, info.baseboardSerialNumber, SecurityHelper.toHex(SecurityHelper.digest(SecurityHelper.DigestAlgorithm.MD5, info.displayId))) , false); } + + public void init() + { + + } + + public void close() + { + + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MemoryHWIDProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MemoryHWIDProvider.java index a175957d..e87b536c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MemoryHWIDProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MemoryHWIDProvider.java @@ -94,6 +94,7 @@ public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo har if(result.compareLevel > criticalCompareLevel) { LogHelper.debug("HardwareInfo publicKey change: compareLevel %d", result.compareLevel); + if(e.banned) throw new HWIDException("You HWID banned"); e.publicKey = publicKey; return true; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MysqlHWIDProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MysqlHWIDProvider.java new file mode 100644 index 00000000..ceea81ee --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/MysqlHWIDProvider.java @@ -0,0 +1,134 @@ +package pro.gravit.launchserver.auth.protect.hwid; + +import pro.gravit.launcher.request.secure.HardwareReportRequest; +import pro.gravit.launchserver.auth.MySQLSourceConfig; +import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.LogHelper; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.sql.*; + +public class MysqlHWIDProvider extends HWIDProvider { + public MySQLSourceConfig mySQLHolder; + public double warningSpoofingLevel = -1.0; + public double criticalCompareLevel = 1.0; + + @Override + public void init() { + + } + + @Override + public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey) throws HWIDException { + try(Connection connection = mySQLHolder.getConnection()) + { + PreparedStatement s = connection.prepareStatement("SELECT hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, banned FROM hwids WHERE `publicKey` = ?"); + s.setBlob(1, new ByteArrayInputStream(publicKey)); + ResultSet set = s.executeQuery(); + if(set.next()) + { + if(set.getBoolean(10)) //isBanned + { + throw new SecurityException("You HWID banned"); + } + return fetchHardwareInfo(set); + } + else + { + return null; + } + } catch (SQLException | IOException throwables) { + LogHelper.error(throwables); + throw new SecurityException("SQL error. Please try again later"); + } + } + + private HardwareReportRequest.HardwareInfo fetchHardwareInfo(ResultSet set) throws SQLException, IOException { + HardwareReportRequest.HardwareInfo hardwareInfo = new HardwareReportRequest.HardwareInfo(); + hardwareInfo.hwDiskId = set.getString(1); + hardwareInfo.baseboardSerialNumber = set.getString(2); + hardwareInfo.displayId = IOHelper.read(set.getBlob(3).getBinaryStream()); + hardwareInfo.bitness = set.getInt(4); + hardwareInfo.totalMemory = set.getLong(5); + hardwareInfo.logicalProcessors = set.getInt(6); + hardwareInfo.physicalProcessors = set.getInt(7); + hardwareInfo.processorMaxFreq = set.getLong(8); + hardwareInfo.battery = set.getBoolean(9); + return hardwareInfo; + } + + @Override + public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException { + try(Connection connection = mySQLHolder.getConnection()) + { + PreparedStatement s = connection.prepareStatement("INSERT INTO `hwids` (`publickey`, `hwDiskId`, `baseboardSerialNumber`, `displayId`, `bitness`, `totalMemory`, `logicalProcessors`, `physicalProcessors`, `processorMaxFreq`, `battery`, `banned`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '0');", Statement.RETURN_GENERATED_KEYS); + s.setBlob(1, new ByteArrayInputStream(publicKey)); + s.setString(2, hardwareInfo.hwDiskId); + s.setString(3, hardwareInfo.baseboardSerialNumber); + s.setBlob(4, new ByteArrayInputStream(hardwareInfo.displayId)); + s.setInt(5, hardwareInfo.bitness); + s.setLong(6, hardwareInfo.totalMemory); + s.setInt(7, hardwareInfo.logicalProcessors); + s.setInt(8, hardwareInfo.physicalProcessors); + s.setLong(9, hardwareInfo.processorMaxFreq); + s.setBoolean(10, hardwareInfo.battery); + s.executeUpdate(); + try (ResultSet generatedKeys = s.getGeneratedKeys()) { + if (generatedKeys.next()) { + writeHwidLog(connection, generatedKeys.getLong(1), publicKey); + } + } + } catch (SQLException throwables) { + LogHelper.error(throwables); + throw new SecurityException("SQL error. Please try again later"); + } + } + + @Override + public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException { + try(Connection connection = mySQLHolder.getConnection()) + { + PreparedStatement s = connection.prepareStatement("SELECT hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, id, banned FROM hwids"); + ResultSet set = s.executeQuery(); + while(set.next()) + { + HardwareReportRequest.HardwareInfo hw = fetchHardwareInfo(set); + long id = set.getLong(10); + HardwareInfoCompareResult result = compareHardwareInfo(hw, hardwareInfo); + if(result.compareLevel > criticalCompareLevel) + { + if(set.getBoolean(11)) //isBanned + { + throw new SecurityException("You HWID banned"); + } + writeHwidLog(connection, id, publicKey); + changePublicKey(connection, id, publicKey); + return true; + } + } + } catch (SQLException | IOException throwables) + { + LogHelper.error(throwables); + throw new SecurityException("SQL error. Please try again later"); + } + return false; + } + private void changePublicKey(Connection connection, long id, byte[] publicKey) throws SQLException { + PreparedStatement s = connection.prepareStatement("UPDATE hwids SET `publicKey` = ? WHERE `id` = ?"); + s.setBlob(1, new ByteArrayInputStream(publicKey)); + s.setLong(2, id); + s.executeUpdate(); + } + private void writeHwidLog(Connection connection, long hwidId, byte[] newPublicKey) throws SQLException { + PreparedStatement s = connection.prepareStatement("INSERT INTO hwidLog (`hwidId`, `newPublicKey`) VALUES (?, ?)"); + s.setLong(1, hwidId); + s.setBlob(2, new ByteArrayInputStream(newPublicKey)); + s.executeUpdate(); + } + + @Override + public void close() { + mySQLHolder.close(); + } +}