[FEATURE] Запись последнего HWID в users

This commit is contained in:
Gravit 2020-05-28 22:50:54 +07:00
parent c353de9267
commit 898c6a33a1
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 59 additions and 21 deletions

View file

@ -3,7 +3,6 @@
import pro.gravit.launcher.events.request.GetSecureLevelInfoRequestEvent; import pro.gravit.launcher.events.request.GetSecureLevelInfoRequestEvent;
import pro.gravit.launcher.events.request.HardwareReportRequestEvent; import pro.gravit.launcher.events.request.HardwareReportRequestEvent;
import pro.gravit.launcher.events.request.VerifySecureLevelKeyRequestEvent; import pro.gravit.launcher.events.request.VerifySecureLevelKeyRequestEvent;
import pro.gravit.launcher.request.secure.HardwareReportRequest;
import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.Reconfigurable;
import pro.gravit.launchserver.auth.protect.hwid.HWIDException; import pro.gravit.launchserver.auth.protect.hwid.HWIDException;
import pro.gravit.launchserver.auth.protect.hwid.HWIDProvider; import pro.gravit.launchserver.auth.protect.hwid.HWIDProvider;
@ -51,12 +50,17 @@ public void onHardwareReport(HardwareReportResponse response, Client client) {
return; return;
} }
try { try {
if(!client.isAuth || client.trustLevel == null || client.trustLevel.publicKey == null)
{
response.sendError("Access denied");
return;
}
provider.normalizeHardwareInfo(response.hardware); provider.normalizeHardwareInfo(response.hardware);
LogHelper.debug("[HardwareInfo] HardwareInfo received"); LogHelper.debug("[HardwareInfo] HardwareInfo received");
boolean needCreate = !provider.addPublicKeyToHardwareInfo(response.hardware, client.trustLevel.publicKey); boolean needCreate = !provider.addPublicKeyToHardwareInfo(response.hardware, client.trustLevel.publicKey, client);
LogHelper.debug("[HardwareInfo] HardwareInfo needCreate: %s", needCreate ? "true" : "false"); LogHelper.debug("[HardwareInfo] HardwareInfo needCreate: %s", needCreate ? "true" : "false");
if(needCreate) if(needCreate)
provider.createHardwareInfo(response.hardware, client.trustLevel.publicKey); provider.createHardwareInfo(response.hardware, client.trustLevel.publicKey, client);
client.trustLevel.hardwareInfo = response.hardware; client.trustLevel.hardwareInfo = response.hardware;
} catch (HWIDException e) { } catch (HWIDException e) {
throw new SecurityException(e.getMessage()); throw new SecurityException(e.getMessage());
@ -75,7 +79,7 @@ public VerifySecureLevelKeyRequestEvent onSuccessVerify(Client client) {
else else
{ {
try { try {
client.trustLevel.hardwareInfo = provider.findHardwareInfoByPublicKey(client.trustLevel.publicKey); client.trustLevel.hardwareInfo = provider.findHardwareInfoByPublicKey(client.trustLevel.publicKey, client);
if(client.trustLevel.hardwareInfo == null) //HWID not found? if(client.trustLevel.hardwareInfo == null) //HWID not found?
return new VerifySecureLevelKeyRequestEvent(true); return new VerifySecureLevelKeyRequestEvent(true);
} catch (HWIDException e) { } catch (HWIDException e) {

View file

@ -1,8 +1,8 @@
package pro.gravit.launchserver.auth.protect.hwid; package pro.gravit.launchserver.auth.protect.hwid;
import pro.gravit.launcher.request.secure.HardwareReportRequest; import pro.gravit.launcher.request.secure.HardwareReportRequest;
import pro.gravit.launchserver.auth.handler.AuthHandler;
import pro.gravit.launchserver.helper.DamerauHelper; import pro.gravit.launchserver.helper.DamerauHelper;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.utils.ProviderMap; import pro.gravit.utils.ProviderMap;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
@ -20,9 +20,9 @@ public static void registerProviders() {
registredProv = true; registredProv = true;
} }
} }
public abstract HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey) throws HWIDException; public abstract HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey, Client client) throws HWIDException;
public abstract void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException; public abstract void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException;
public abstract boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException; public abstract boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException;
public void normalizeHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo) public void normalizeHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo)
{ {

View file

@ -2,6 +2,7 @@
import pro.gravit.launcher.request.secure.HardwareReportRequest; import pro.gravit.launcher.request.secure.HardwareReportRequest;
import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.Reconfigurable;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.utils.command.Command; import pro.gravit.utils.command.Command;
import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.command.SubCommand;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
@ -65,7 +66,7 @@ public MemoryHWIDEntity(HardwareReportRequest.HardwareInfo hardware, byte[] publ
public Set<MemoryHWIDEntity> db = ConcurrentHashMap.newKeySet(); public Set<MemoryHWIDEntity> db = ConcurrentHashMap.newKeySet();
@Override @Override
public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey) throws HWIDException { public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey, Client client) throws HWIDException {
for(MemoryHWIDEntity e : db) { for(MemoryHWIDEntity e : db) {
if(Arrays.equals(e.publicKey, publicKey)) if(Arrays.equals(e.publicKey, publicKey))
{ {
@ -77,12 +78,12 @@ public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] pub
} }
@Override @Override
public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException { public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException {
db.add(new MemoryHWIDEntity(hardwareInfo, publicKey)); db.add(new MemoryHWIDEntity(hardwareInfo, publicKey));
} }
@Override @Override
public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException { public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException {
boolean isAlreadyWarning = false; boolean isAlreadyWarning = false;
for(MemoryHWIDEntity e : db) { for(MemoryHWIDEntity e : db) {
HardwareInfoCompareResult result = compareHardwareInfo(e.hardware, hardwareInfo); HardwareInfoCompareResult result = compareHardwareInfo(e.hardware, hardwareInfo);

View file

@ -2,6 +2,7 @@
import pro.gravit.launcher.request.secure.HardwareReportRequest; import pro.gravit.launcher.request.secure.HardwareReportRequest;
import pro.gravit.launchserver.auth.MySQLSourceConfig; import pro.gravit.launchserver.auth.MySQLSourceConfig;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
@ -14,24 +15,47 @@ public class MysqlHWIDProvider extends HWIDProvider {
public double warningSpoofingLevel = -1.0; public double warningSpoofingLevel = -1.0;
public double criticalCompareLevel = 1.0; public double criticalCompareLevel = 1.0;
public String tableHWID = "hwids";
public String tableHWIDLog = "hwidLog";
public String tableUsers;
public String usersNameColumn;
public String usersHWIDColumn;
private String sqlFindByPublicKey;
private String sqlFindByHardware;
private String sqlCreateHardware;
private String sqlCreateHWIDLog;
private String sqlUpdateHardware;
private String sqlUpdateUsers;
@Override @Override
public void init() { public void init() {
sqlFindByPublicKey = String.format("SELECT hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, id, banned FROM %s WHERE `publicKey` = ?", tableHWID);
sqlFindByHardware = String.format("SELECT hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, id, banned FROM %s", tableHWID);
sqlCreateHardware = String.format("INSERT INTO `%s` (`publickey`, `hwDiskId`, `baseboardSerialNumber`, `displayId`, `bitness`, `totalMemory`, `logicalProcessors`, `physicalProcessors`, `processorMaxFreq`, `battery`, `banned`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '0')", tableHWID);
sqlCreateHWIDLog = String.format("INSERT INTO %s (`hwidId`, `newPublicKey`) VALUES (?, ?)", tableHWIDLog);
sqlUpdateHardware = String.format("UPDATE %s SET `publicKey` = ? WHERE `id` = ?", tableHWID);
if(tableUsers != null && usersHWIDColumn != null && usersNameColumn != null)
{
sqlUpdateUsers = String.format("UPDATE %s SET `%s` = ? WHERE `%s` = ?", tableUsers, usersHWIDColumn, usersNameColumn);
}
} }
@Override @Override
public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey) throws HWIDException { public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey, Client client) throws HWIDException {
try(Connection connection = mySQLHolder.getConnection()) try(Connection connection = mySQLHolder.getConnection())
{ {
PreparedStatement s = connection.prepareStatement("SELECT hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, banned FROM hwids WHERE `publicKey` = ?"); PreparedStatement s = connection.prepareStatement(sqlFindByPublicKey);
s.setBlob(1, new ByteArrayInputStream(publicKey)); s.setBlob(1, new ByteArrayInputStream(publicKey));
ResultSet set = s.executeQuery(); ResultSet set = s.executeQuery();
if(set.next()) if(set.next())
{ {
if(set.getBoolean(10)) //isBanned if(set.getBoolean(11)) //isBanned
{ {
throw new SecurityException("You HWID banned"); throw new SecurityException("You HWID banned");
} }
long id = set.getLong(10);
setUserHardwareId(connection, client.username, id);
return fetchHardwareInfo(set); return fetchHardwareInfo(set);
} }
else else
@ -59,10 +83,10 @@ private HardwareReportRequest.HardwareInfo fetchHardwareInfo(ResultSet set) thro
} }
@Override @Override
public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException { public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException {
try(Connection connection = mySQLHolder.getConnection()) 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); PreparedStatement s = connection.prepareStatement(sqlCreateHardware, Statement.RETURN_GENERATED_KEYS);
s.setBlob(1, new ByteArrayInputStream(publicKey)); s.setBlob(1, new ByteArrayInputStream(publicKey));
s.setString(2, hardwareInfo.hwDiskId); s.setString(2, hardwareInfo.hwDiskId);
s.setString(3, hardwareInfo.baseboardSerialNumber); s.setString(3, hardwareInfo.baseboardSerialNumber);
@ -77,6 +101,7 @@ public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo,
try (ResultSet generatedKeys = s.getGeneratedKeys()) { try (ResultSet generatedKeys = s.getGeneratedKeys()) {
if (generatedKeys.next()) { if (generatedKeys.next()) {
writeHwidLog(connection, generatedKeys.getLong(1), publicKey); writeHwidLog(connection, generatedKeys.getLong(1), publicKey);
setUserHardwareId(connection, client.username, generatedKeys.getLong(1));
} }
} }
} catch (SQLException throwables) { } catch (SQLException throwables) {
@ -86,10 +111,10 @@ public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo,
} }
@Override @Override
public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey) throws HWIDException { public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException {
try(Connection connection = mySQLHolder.getConnection()) try(Connection connection = mySQLHolder.getConnection())
{ {
PreparedStatement s = connection.prepareStatement("SELECT hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, id, banned FROM hwids"); PreparedStatement s = connection.prepareStatement(sqlFindByHardware);
ResultSet set = s.executeQuery(); ResultSet set = s.executeQuery();
while(set.next()) while(set.next())
{ {
@ -104,6 +129,7 @@ public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo har
} }
writeHwidLog(connection, id, publicKey); writeHwidLog(connection, id, publicKey);
changePublicKey(connection, id, publicKey); changePublicKey(connection, id, publicKey);
setUserHardwareId(connection, client.username, id);
return true; return true;
} }
} }
@ -115,17 +141,24 @@ public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo har
return false; return false;
} }
private void changePublicKey(Connection connection, long id, byte[] publicKey) throws SQLException { private void changePublicKey(Connection connection, long id, byte[] publicKey) throws SQLException {
PreparedStatement s = connection.prepareStatement("UPDATE hwids SET `publicKey` = ? WHERE `id` = ?"); PreparedStatement s = connection.prepareStatement(sqlUpdateHardware);
s.setBlob(1, new ByteArrayInputStream(publicKey)); s.setBlob(1, new ByteArrayInputStream(publicKey));
s.setLong(2, id); s.setLong(2, id);
s.executeUpdate(); s.executeUpdate();
} }
private void writeHwidLog(Connection connection, long hwidId, byte[] newPublicKey) throws SQLException { private void writeHwidLog(Connection connection, long hwidId, byte[] newPublicKey) throws SQLException {
PreparedStatement s = connection.prepareStatement("INSERT INTO hwidLog (`hwidId`, `newPublicKey`) VALUES (?, ?)"); PreparedStatement s = connection.prepareStatement(sqlCreateHWIDLog);
s.setLong(1, hwidId); s.setLong(1, hwidId);
s.setBlob(2, new ByteArrayInputStream(newPublicKey)); s.setBlob(2, new ByteArrayInputStream(newPublicKey));
s.executeUpdate(); s.executeUpdate();
} }
private void setUserHardwareId(Connection connection, String username, long hwidId) throws SQLException {
if(sqlUpdateUsers == null || username == null) return;
PreparedStatement s = connection.prepareStatement(sqlUpdateUsers);
s.setLong(1, hwidId);
s.setString(2, username);
s.executeUpdate();
}
@Override @Override
public void close() { public void close() {