[FIX] MySQLCoreProvider HWID fix

This commit is contained in:
Gravita 2021-06-22 11:13:12 +07:00
parent 0c281ab50d
commit 81be9d21de
6 changed files with 129 additions and 122 deletions

View file

@ -177,107 +177,109 @@ public void invoke(String... args) throws Exception {
} }
{ {
var instance = isSupport(AuthSupportHardware.class); var instance = isSupport(AuthSupportHardware.class);
map.put("gethardwarebyid", new SubCommand("[id]", "get hardware by id") { if (instance != null) {
@Override map.put("gethardwarebyid", new SubCommand("[id]", "get hardware by id") {
public void invoke(String... args) throws Exception { @Override
verifyArgs(args, 1); public void invoke(String... args) throws Exception {
UserHardware hardware = instance.getHardwareInfoById(args[0]); verifyArgs(args, 1);
if (hardware == null) { UserHardware hardware = instance.getHardwareInfoById(args[0]);
logger.info("UserHardware {} not found", args[0]); if (hardware == null) {
} else { logger.info("UserHardware {} not found", args[0]);
logger.info("UserHardware: {}", hardware); } else {
logger.info("UserHardware: {}", hardware);
}
} }
} });
}); map.put("gethardwarebydata", new SubCommand("[json data]", "fulltext search hardware by json data(slow)") {
map.put("gethardwarebydata", new SubCommand("[json data]", "fulltext search hardware by json data(slow)") { @Override
@Override public void invoke(String... args) throws Exception {
public void invoke(String... args) throws Exception { verifyArgs(args, 1);
verifyArgs(args, 1); UserHardware hardware = instance.getHardwareInfoByData(Launcher.gsonManager.gson.fromJson(args[0], HardwareReportRequest.HardwareInfo.class));
UserHardware hardware = instance.getHardwareInfoByData(Launcher.gsonManager.gson.fromJson(args[0], HardwareReportRequest.HardwareInfo.class)); if (hardware == null) {
if (hardware == null) { logger.info("UserHardware {} not found", args[0]);
logger.info("UserHardware {} not found", args[0]); } else {
} else { logger.info("UserHardware: {}", hardware);
logger.info("UserHardware: {}", hardware); }
} }
} });
}); map.put("getuserhardware", new SubCommand("[username]", "get hardware by username") {
map.put("getuserhardware", new SubCommand("[username]", "get hardware by username") { @Override
@Override public void invoke(String... args) throws Exception {
public void invoke(String... args) throws Exception { verifyArgs(args, 1);
verifyArgs(args, 1); User user = getUserByUUID(UUID.fromString(args[0]));
User user = getUserByUUID(UUID.fromString(args[0])); if (user == null) {
if (user == null) { logger.info("User {} not found", args[0]);
logger.info("User {} not found", args[0]); }
UserSupportHardware hardware = instance.fetchUserHardware(user);
if (hardware == null) {
logger.error("Method fetchUserHardware return null");
return;
}
UserHardware userHardware = hardware.getHardware();
if (userHardware == null) {
logger.info("User {} not contains hardware info", args[0]);
} else {
logger.info("UserHardware: {}", userHardware);
logger.info("HardwareInfo(JSON): {}", Launcher.gsonManager.gson.toJson(userHardware.getHardwareInfo()));
}
} }
UserSupportHardware hardware = instance.fetchUserHardware(user); });
if (hardware == null) { map.put("findmulti", new SubCommand("[hardware id]", "get all users in one hardware id") {
logger.error("Method fetchUserHardware return null"); @Override
return; public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
UserHardware hardware = instance.getHardwareInfoById(args[0]);
if (hardware == null) {
logger.info("UserHardware {} not found", args[0]);
return;
}
Iterable<User> users = instance.getUsersByHardwareInfo(hardware);
for (User user : users) {
logger.info("User {}", user);
}
} }
UserHardware userHardware = hardware.getHardware(); });
if (userHardware == null) { map.put("banhardware", new SubCommand("[hardware id]", "ban hardware by id") {
logger.info("User {} not contains hardware info", args[0]); @Override
} else { public void invoke(String... args) throws Exception {
logger.info("UserHardware: {}", userHardware); verifyArgs(args, 1);
logger.info("HardwareInfo(JSON): {}", Launcher.gsonManager.gson.toJson(userHardware.getHardwareInfo())); UserHardware hardware = instance.getHardwareInfoById(args[0]);
if (hardware == null) {
logger.info("UserHardware {} not found", args[0]);
return;
}
instance.banHardware(hardware);
logger.info("UserHardware {} banned", args[0]);
} }
} });
}); map.put("unbanhardware", new SubCommand("[hardware id]", "ban hardware by id") {
map.put("findmulti", new SubCommand("[hardware id]", "get all users in one hardware id") { @Override
@Override public void invoke(String... args) throws Exception {
public void invoke(String... args) throws Exception { verifyArgs(args, 1);
verifyArgs(args, 1); UserHardware hardware = instance.getHardwareInfoById(args[0]);
UserHardware hardware = instance.getHardwareInfoById(args[0]); if (hardware == null) {
if (hardware == null) { logger.info("UserHardware {} not found", args[0]);
logger.info("UserHardware {} not found", args[0]); return;
return; }
instance.unbanHardware(hardware);
logger.info("UserHardware {} unbanned", args[0]);
} }
Iterable<User> users = instance.getUsersByHardwareInfo(hardware); });
for (User user : users) { map.put("comparehardware", new SubCommand("[json data 1] [json data 2]", "compare hardware info") {
logger.info("User {}", user.getUsername()); @Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 2);
HardwareReportRequest.HardwareInfo hardware1 = Launcher.gsonManager.gson.fromJson(args[0], HardwareReportRequest.HardwareInfo.class);
HardwareReportRequest.HardwareInfo hardware2 = Launcher.gsonManager.gson.fromJson(args[1], HardwareReportRequest.HardwareInfo.class);
HWIDProvider.HardwareInfoCompareResult result = instance.compareHardwareInfo(hardware1, hardware2);
if (result == null) {
logger.error("Method compareHardwareInfo return null");
return;
}
logger.info("Compare result: {} Spoof: {} first {} second", result.compareLevel, result.firstSpoofingLevel, result.secondSpoofingLevel);
} }
} });
}); }
map.put("banhardware", new SubCommand("[hardware id]", "ban hardware by id") {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
UserHardware hardware = instance.getHardwareInfoById(args[0]);
if (hardware == null) {
logger.info("UserHardware {} not found", args[0]);
return;
}
instance.banHardware(hardware);
logger.info("UserHardware {} banned", args[0]);
}
});
map.put("unbanhardware", new SubCommand("[hardware id]", "ban hardware by id") {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
UserHardware hardware = instance.getHardwareInfoById(args[0]);
if (hardware == null) {
logger.info("UserHardware {} not found", args[0]);
return;
}
instance.unbanHardware(hardware);
logger.info("UserHardware {} unbanned", args[0]);
}
});
map.put("comparehardware", new SubCommand("[json data 1] [json data 2]", "compare hardware info") {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 2);
HardwareReportRequest.HardwareInfo hardware1 = Launcher.gsonManager.gson.fromJson(args[0], HardwareReportRequest.HardwareInfo.class);
HardwareReportRequest.HardwareInfo hardware2 = Launcher.gsonManager.gson.fromJson(args[1], HardwareReportRequest.HardwareInfo.class);
HWIDProvider.HardwareInfoCompareResult result = instance.compareHardwareInfo(hardware1, hardware2);
if (result == null) {
logger.error("Method compareHardwareInfo return null");
return;
}
logger.info("Compare result: {} Spoof: {} first {} second", result.compareLevel, result.firstSpoofingLevel, result.secondSpoofingLevel);
}
});
} }
return map; return map;
} }

View file

@ -22,7 +22,7 @@
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.Arrays; import java.util.Base64;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -61,11 +61,11 @@ public class MySQLCoreProvider extends AuthCoreProvider implements AuthSupportHa
private transient String updateAuthSQL; private transient String updateAuthSQL;
private transient String updateServerIDSQL; private transient String updateServerIDSQL;
public String defaultQueryByUUIDSQL; public String customQueryByUUIDSQL;
public String defaultQueryByUsernameSQL; public String customQueryByUsernameSQL;
public String defaultQueryByLoginSQL; public String customQueryByLoginSQL;
public String defaultUpdateAuthSQL; public String customUpdateAuthSQL;
public String defaultUpdateServerIdSQL; public String customUpdateServerIdSQL;
@Override @Override
public User getUserByUsername(String username) { public User getUserByUsername(String username) {
@ -143,23 +143,26 @@ public void init(LaunchServer server) {
if (table == null) logger.error("table cannot be null"); if (table == null) logger.error("table cannot be null");
// Prepare SQL queries // Prepare SQL queries
String userInfoCols = String.format("%s, %s, %s, %s, %s, %s", uuidColumn, usernameColumn, accessTokenColumn, serverIDColumn, passwordColumn, hardwareIdColumn); String userInfoCols = String.format("%s, %s, %s, %s, %s, %s", uuidColumn, usernameColumn, accessTokenColumn, serverIDColumn, passwordColumn, hardwareIdColumn);
queryByUUIDSQL = defaultQueryByUUIDSQL != null ? defaultQueryByUUIDSQL : String.format("SELECT %s FROM %s WHERE %s=? LIMIT 1", userInfoCols, queryByUUIDSQL = customQueryByUUIDSQL != null ? customQueryByUUIDSQL : String.format("SELECT %s FROM %s WHERE %s=? LIMIT 1", userInfoCols,
table, uuidColumn); table, uuidColumn);
queryByUsernameSQL = defaultQueryByUsernameSQL != null ? defaultQueryByUsernameSQL : String.format("SELECT %s FROM %s WHERE %s=? LIMIT 1", queryByUsernameSQL = customQueryByUsernameSQL != null ? customQueryByUsernameSQL : String.format("SELECT %s FROM %s WHERE %s=? LIMIT 1",
userInfoCols, table, usernameColumn); userInfoCols, table, usernameColumn);
queryByLoginSQL = defaultQueryByLoginSQL != null ? defaultQueryByLoginSQL : queryByUsernameSQL; queryByLoginSQL = customQueryByLoginSQL != null ? customQueryByLoginSQL : queryByUsernameSQL;
updateAuthSQL = defaultUpdateAuthSQL != null ? defaultUpdateAuthSQL : String.format("UPDATE %s SET %s=?, %s=NULL WHERE %s=? LIMIT 1", updateAuthSQL = customUpdateAuthSQL != null ? customUpdateAuthSQL : String.format("UPDATE %s SET %s=?, %s=NULL WHERE %s=? LIMIT 1",
table, accessTokenColumn, serverIDColumn, uuidColumn); table, accessTokenColumn, serverIDColumn, uuidColumn);
updateServerIDSQL = defaultUpdateServerIdSQL != null ? defaultUpdateServerIdSQL : String.format("UPDATE %s SET %s=? WHERE %s=? LIMIT 1", updateServerIDSQL = customUpdateServerIdSQL != null ? customUpdateServerIdSQL : String.format("UPDATE %s SET %s=? WHERE %s=? LIMIT 1",
table, serverIDColumn, uuidColumn); table, serverIDColumn, uuidColumn);
String hardwareInfoCols = "id, hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, id, graphicCard, banned"; String hardwareInfoCols = "id, hwDiskId, baseboardSerialNumber, displayId, bitness, totalMemory, logicalProcessors, physicalProcessors, processorMaxFreq, battery, id, graphicCard, banned, publicKey";
if(sqlFindHardwareByPublicKey == null) sqlFindHardwareByPublicKey = String.format("SELECT %s FROM %s WHERE `publicKey` = ?", hardwareInfoCols, tableHWID); if(sqlFindHardwareByPublicKey == null) sqlFindHardwareByPublicKey = String.format("SELECT %s FROM %s WHERE `publicKey` = ?", hardwareInfoCols, tableHWID);
if(sqlFindHardwareById == null) sqlFindHardwareById = String.format("SELECT %s FROM %s WHERE `id` = ?", hardwareInfoCols, tableHWID); if(sqlFindHardwareById == null) sqlFindHardwareById = String.format("SELECT %s FROM %s WHERE `id` = ?", hardwareInfoCols, tableHWID);
if(sqlUsersByHwidId == null) sqlUsersByHwidId = String.format("SELECT %s FROM %s WHERE `%s` = ?", userInfoCols, table, hardwareIdColumn); if(sqlUsersByHwidId == null) sqlUsersByHwidId = String.format("SELECT %s FROM %s WHERE `%s` = ?", userInfoCols, table, hardwareIdColumn);
if(sqlFindHardwareByData == null) sqlFindHardwareByData = String.format("SELECT %s FROM %s", hardwareInfoCols, tableHWID); if (sqlFindHardwareByData == null)
if(sqlCreateHardware == null) sqlCreateHardware = String.format("INSERT INTO `%s` (`publickey`, `hwDiskId`, `baseboardSerialNumber`, `displayId`, `bitness`, `totalMemory`, `logicalProcessors`, `physicalProcessors`, `processorMaxFreq`, `battery`, `graphicCard`, `banned`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '0')", tableHWID); sqlFindHardwareByData = String.format("SELECT %s FROM %s", hardwareInfoCols, tableHWID);
if(sqlCreateHWIDLog == null) sqlCreateHWIDLog = String.format("INSERT INTO %s (`hwidId`, `newPublicKey`) VALUES (?, ?)", tableHWIDLog); if (sqlCreateHardware == null)
sqlCreateHardware = String.format("INSERT INTO `%s` (`publickey`, `hwDiskId`, `baseboardSerialNumber`, `displayId`, `bitness`, `totalMemory`, `logicalProcessors`, `physicalProcessors`, `processorMaxFreq`, `graphicCard`, `battery`, `banned`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '0')", tableHWID);
if (sqlCreateHWIDLog == null)
sqlCreateHWIDLog = String.format("INSERT INTO %s (`hwidId`, `newPublicKey`) VALUES (?, ?)", tableHWIDLog);
if(sqlUpdateHardwarePublicKey == null) sqlUpdateHardwarePublicKey = String.format("UPDATE %s SET `publicKey` = ? WHERE `id` = ?", tableHWID); if(sqlUpdateHardwarePublicKey == null) sqlUpdateHardwarePublicKey = String.format("UPDATE %s SET `publicKey` = ? WHERE `id` = ?", tableHWID);
sqlUpdateHardwareBanned = String.format("UPDATE %s SET `banned` = ? WHERE `id` = ?", tableHWID); sqlUpdateHardwareBanned = String.format("UPDATE %s SET `banned` = ? WHERE `id` = ?", tableHWID);
sqlUpdateUsers = String.format("UPDATE %s SET `%s` = ? WHERE `%s` = ?", table, hardwareIdColumn, uuidColumn); sqlUpdateUsers = String.format("UPDATE %s SET `%s` = ? WHERE `%s` = ?", table, hardwareIdColumn, uuidColumn);
@ -256,7 +259,7 @@ public UserHardware getHardwareInfoByPublicKey(byte[] publicKey) {
} }
} }
} catch (SQLException | IOException throwables) { } catch (SQLException | IOException throwables) {
logger.error(throwables); logger.error("SQL Error", throwables);
return null; return null;
} }
} }
@ -275,7 +278,7 @@ public UserHardware getHardwareInfoByData(HardwareReportRequest.HardwareInfo inf
} }
} }
} catch (SQLException | IOException throwables) { } catch (SQLException | IOException throwables) {
logger.error(throwables); logger.error("SQL Error", throwables);
} }
return null; return null;
} }
@ -293,7 +296,7 @@ public UserHardware getHardwareInfoById(String id) {
} }
} }
} catch (SQLException | IOException throwables) { } catch (SQLException | IOException throwables) {
logger.error(throwables); logger.error("SQL Error", throwables);
return null; return null;
} }
} }
@ -323,7 +326,7 @@ public UserHardware createHardwareInfo(HardwareReportRequest.HardwareInfo hardwa
} }
return null; return null;
} catch (SQLException throwables) { } catch (SQLException throwables) {
logger.error(throwables); logger.error("SQL Error", throwables);
return null; return null;
} }
} }
@ -337,7 +340,7 @@ public void connectUserAndHardware(User user, UserHardware hardware) {
try (Connection connection = mySQLHolder.getConnection()) { try (Connection connection = mySQLHolder.getConnection()) {
setUserHardwareId(connection, user.getUUID(), mySQLUserHardware.id); setUserHardwareId(connection, user.getUUID(), mySQLUserHardware.id);
} catch (SQLException throwables) { } catch (SQLException throwables) {
logger.error(throwables); logger.error("SQL Error", throwables);
} }
} }
@ -363,7 +366,7 @@ public Iterable<User> getUsersByHardwareInfo(UserHardware hardware) {
s.setLong(1, Long.parseLong(hardware.getId())); s.setLong(1, Long.parseLong(hardware.getId()));
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
try (ResultSet set = s.executeQuery()) { try (ResultSet set = s.executeQuery()) {
while (set.next()) { while (!set.isLast()) {
users.add(constructUser(set)); users.add(constructUser(set));
} }
} }
@ -503,7 +506,7 @@ public boolean isBanned() {
public String toString() { public String toString() {
return "MySQLUserHardware{" + return "MySQLUserHardware{" +
"hardwareInfo=" + hardwareInfo + "hardwareInfo=" + hardwareInfo +
", publicKey=" + (publicKey == null ? null : SecurityHelper.toHex(publicKey)) + ", publicKey=" + (publicKey == null ? null : new String(Base64.getEncoder().encode(publicKey))) +
", id=" + id + ", id=" + id +
", banned=" + banned + ", banned=" + banned +
'}'; '}';

View file

@ -48,7 +48,7 @@ public void invoke(String... args) {
logger.info("Data: checkSign {} | auth_id {}", client.checkSign ? "true" : "false", logger.info("Data: checkSign {} | auth_id {}", client.checkSign ? "true" : "false",
client.auth_id); client.auth_id);
if (client.trustLevel != null) { if (client.trustLevel != null) {
logger.info("trustLevel | key {} | pubkey {}", client.trustLevel.keyChecked ? "checked" : "unchecked", client.trustLevel.publicKey == null ? "null" : Base64.getEncoder().encode(client.trustLevel.publicKey)); logger.info("trustLevel | key {} | pubkey {}", client.trustLevel.keyChecked ? "checked" : "unchecked", client.trustLevel.publicKey == null ? "null" : new String(Base64.getEncoder().encode(client.trustLevel.publicKey)));
} }
logger.info("Permissions: {} (permissions {} | flags {})", client.permissions == null ? "null" : client.permissions.toString(), client.permissions == null ? 0 : client.permissions.permissions, client.permissions == null ? 0 : client.permissions.flags); logger.info("Permissions: {} (permissions {} | flags {})", client.permissions == null ? "null" : client.permissions.toString(), client.permissions == null ? 0 : client.permissions.permissions, client.permissions == null ? 0 : client.permissions.flags);
} }

View file

@ -84,7 +84,10 @@ public final void setFlag(long mask, boolean value) {
@Override @Override
public String toString() { public String toString() {
return String.format("permissions %d | flags %d", permissions, flags); return "ClientPermissions{" +
"permissions=" + permissions +
", flags=" + flags +
'}';
} }
public enum PermissionConsts { public enum PermissionConsts {

View file

@ -2,9 +2,8 @@
import pro.gravit.launcher.events.request.HardwareReportRequestEvent; import pro.gravit.launcher.events.request.HardwareReportRequestEvent;
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.utils.helper.SecurityHelper;
import java.util.Arrays; import java.util.Base64;
public class HardwareReportRequest extends Request<HardwareReportRequestEvent> { public class HardwareReportRequest extends Request<HardwareReportRequestEvent> {
public HardwareInfo hardware; public HardwareInfo hardware;
@ -36,7 +35,7 @@ public String toString() {
", processorMaxFreq=" + processorMaxFreq + ", processorMaxFreq=" + processorMaxFreq +
", battery=" + battery + ", battery=" + battery +
", hwDiskId='" + hwDiskId + '\'' + ", hwDiskId='" + hwDiskId + '\'' +
", displayId=" + (displayId == null ? null : SecurityHelper.toHex(displayId)) + ", displayId=" + (displayId == null ? null : new String(Base64.getEncoder().encode(displayId))) +
", baseboardSerialNumber='" + baseboardSerialNumber + '\'' + ", baseboardSerialNumber='" + baseboardSerialNumber + '\'' +
", graphicCard='" + graphicCard + '\'' + ", graphicCard='" + graphicCard + '\'' +
'}'; '}';

@ -1 +1 @@
Subproject commit e8bc82be4d0fb86041fe464cad32314430552386 Subproject commit 0472083e090ff249cb6795d209c34ad79f391f7a