Add Hwid class for "nibble hwid" part of "remote host"

This commit is contained in:
P0nk
2021-06-29 20:26:14 +02:00
parent d34798649b
commit 828c3c5345
10 changed files with 73 additions and 54 deletions

View File

@@ -89,6 +89,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
public static final String CLIENT_NIBBLEHWID = "HWID2";
public static final String CLIENT_REMOTE_ADDRESS = "REMOTE_IP";
private Hwid hwid;
private String remoteAddress;
private volatile boolean inTransition;
@@ -116,7 +117,6 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
private int pinattempt = 0;
private String pic = "";
private int picattempt = 0;
private String hwid = null;
private byte csattempt = 0;
private byte gender = -1;
private boolean disconnecting = false;
@@ -260,6 +260,14 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
return session;
}
public Hwid getHwid() {
return hwid;
}
public void setHwid(Hwid hwid) {
this.hwid = hwid;
}
public String getRemoteAddress() {
return remoteAddress;
}
@@ -387,7 +395,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
boolean ret = false;
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM hwidbans WHERE hwid LIKE ?")) {
ps.setString(1, hwid);
ps.setString(1, hwid.hwid());
try (ResultSet rs = ps.executeQuery()) {
if (rs != null && rs.next()) {
@@ -445,7 +453,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
hwid = rs.getString("hwid");
hwid = new Hwid(rs.getString("hwid"));
}
}
}
@@ -477,7 +485,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO hwidbans (hwid) VALUES (?)")) {
ps.setString(1, hwid);
ps.setString(1, hwid.hwid());
ps.executeUpdate();
}
} catch (SQLException e) {
@@ -739,30 +747,25 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
return ipAddress;
}
public void updateHWID(String newHwid) {
String[] split = newHwid.split("_");
if (split.length > 1 && split[1].length() == 8) {
StringBuilder hwid = new StringBuilder();
String convert = split[1];
public void updateHwid(String hwidClientString) {
final Hwid hwid;
try {
hwid = Hwid.fromClientString(hwidClientString);
} catch (IllegalArgumentException e) {
log.warn("Failed to create hwid from client string: {}", hwidClientString, e);
this.disconnect(false, false);
return;
}
int len = convert.length();
for (int i = len - 2; i >= 0; i -= 2) {
hwid.append(convert, i, i + 2);
}
hwid.insert(4, "-");
this.hwid = hwid;
this.hwid = hwid.toString();
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET hwid = ? WHERE id = ?")) {
ps.setString(1, this.hwid);
ps.setInt(2, accId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
this.disconnect(false, false); // Invalid HWID...
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET hwid = ? WHERE id = ?")) {
ps.setString(1, hwid.hwid());
ps.setInt(2, accId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@@ -1173,14 +1176,6 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
}, TimeUnit.SECONDS.toMillis(15));
}
public String getHWID() {
return hwid;
}
public void setHWID(String hwid) {
this.hwid = hwid;
}
public Set<String> getMacs() {
return Collections.unmodifiableSet(macs);
}

View File

@@ -31,6 +31,7 @@ import net.server.PlayerBuffValueHolder;
import net.server.Server;
import net.server.channel.Channel;
import net.server.channel.CharacterIdChannelPair;
import net.server.coordinator.session.Hwid;
import net.server.coordinator.session.MapleSessionCoordinator;
import net.server.coordinator.world.MapleEventRecallCoordinator;
import net.server.guild.MapleAlliance;

View File

@@ -2,10 +2,30 @@ package net.server.coordinator.session;
import java.util.regex.Pattern;
public class Hwid {
private static final Pattern VALID_HWID_PATTERN = Pattern.compile("[0-9A-F]{12}_[0-9A-F]{8}");
public record Hwid (String hwid) {
private static final int HWID_LENGTH = 8;
// First part is a mac address (without dashes), second part is the hwid
private static final Pattern VALID_RAW_HWID_PATTERN = Pattern.compile("[0-9A-F]{12}_[0-9A-F]{8}");
public static boolean isValidHwid(String hwid) {
return VALID_HWID_PATTERN.matcher(hwid).matches();
public static boolean isValidRawHwid(String rawHwid) {
return VALID_RAW_HWID_PATTERN.matcher(rawHwid).matches();
}
public static Hwid fromClientString(String clientString) throws IllegalArgumentException {
String[] split = clientString.split("_");
if (split.length != 2 || split[1].length() != HWID_LENGTH) {
throw new IllegalArgumentException("Hwid validation failed for hwid: " + clientString);
}
StringBuilder newHwid = new StringBuilder();
String convert = split[1];
int len = convert.length();
for (int i = len - 2; i >= 0; i -= 2) {
newHwid.append(convert, i, i + 2);
}
newHwid.insert(4, "-");
return new Hwid(newHwid.toString());
}
}

View File

@@ -297,8 +297,9 @@ public class MapleSessionCoordinator {
return null;
}
int hwidLen = remoteHwid.length();
if (hwidLen <= 8) {
final int hwidLen = remoteHwid.length();
final boolean isOnlyNibbleHwid = hwidLen <= 8;
if (isOnlyNibbleHwid) {
session.setAttribute(MapleClient.CLIENT_NIBBLEHWID, remoteHwid);
} else {
session.setAttribute(MapleClient.CLIENT_HWID, remoteHwid);
@@ -326,9 +327,11 @@ public class MapleSessionCoordinator {
}
String hwid = (String) session.removeAttribute(MapleClient.CLIENT_NIBBLEHWID); // making sure to clean up calls to this function on login phase
// TODO: client.setNibbleHwid(null);
onlineRemoteHwids.remove(hwid);
hwid = (String) session.removeAttribute(MapleClient.CLIENT_HWID);
// TODO: client.setHwid(null);
onlineRemoteHwids.remove(hwid);
if (client != null) {

View File

@@ -46,10 +46,10 @@ public final class CharSelectedHandler extends AbstractMaplePacketHandler {
case REMOTE_NO_MATCH:
return 17;
case COORDINATOR_ERROR:
return 8;
default:
return 9;
}
@@ -62,13 +62,13 @@ public final class CharSelectedHandler extends AbstractMaplePacketHandler {
String macs = slea.readMapleAsciiString();
String hwid = slea.readMapleAsciiString();
if (!Hwid.isValidHwid(hwid)) {
if (!Hwid.isValidRawHwid(hwid)) {
c.announce(MaplePacketCreator.getAfterLoginError(17));
return;
}
c.updateMacs(macs);
c.updateHWID(hwid);
c.updateHwid(hwid);
IoSession session = c.getSession();
AntiMulticlientResult res = MapleSessionCoordinator.getInstance().attemptGameSession(session, c.getAccID(), hwid);

View File

@@ -43,13 +43,13 @@ public class CharSelectedWithPicHandler extends AbstractMaplePacketHandler {
String macs = slea.readMapleAsciiString();
String hwid = slea.readMapleAsciiString();
if (!Hwid.isValidHwid(hwid)) {
if (!Hwid.isValidRawHwid(hwid)) {
c.announce(MaplePacketCreator.getAfterLoginError(17));
return;
}
c.updateMacs(macs);
c.updateHWID(hwid);
c.updateHwid(hwid);
IoSession session = c.getSession();

View File

@@ -43,13 +43,13 @@ public final class RegisterPicHandler extends AbstractMaplePacketHandler {
String macs = slea.readMapleAsciiString();
String hwid = slea.readMapleAsciiString();
if (!Hwid.isValidHwid(hwid)) {
if (!Hwid.isValidRawHwid(hwid)) {
c.announce(MaplePacketCreator.getAfterLoginError(17));
return;
}
c.updateMacs(macs);
c.updateHWID(hwid);
c.updateHwid(hwid);
IoSession session = c.getSession();
AntiMulticlientResult res = MapleSessionCoordinator.getInstance().attemptGameSession(session, c.getAccID(), hwid);

View File

@@ -44,13 +44,13 @@ public final class ViewAllCharRegisterPicHandler extends AbstractMaplePacketHand
String mac = slea.readMapleAsciiString();
String hwid = slea.readMapleAsciiString();
if (!Hwid.isValidHwid(hwid)) {
if (!Hwid.isValidRawHwid(hwid)) {
c.announce(MaplePacketCreator.getAfterLoginError(17));
return;
}
c.updateMacs(mac);
c.updateHWID(hwid);
c.updateHwid(hwid);
if (c.hasBannedMac() || c.hasBannedHWID()) {
MapleSessionCoordinator.getInstance().closeSession(c.getSession(), true);

View File

@@ -64,13 +64,13 @@ public final class ViewAllCharSelectedHandler extends AbstractMaplePacketHandler
String macs = slea.readMapleAsciiString();
String hwid = slea.readMapleAsciiString();
if (!Hwid.isValidHwid(hwid)) {
if (!Hwid.isValidRawHwid(hwid)) {
c.announce(MaplePacketCreator.getAfterLoginError(17));
return;
}
c.updateMacs(macs);
c.updateHWID(hwid);
c.updateHwid(hwid);
if (c.hasBannedMac() || c.hasBannedHWID()) {
MapleSessionCoordinator.getInstance().closeSession(c.getSession(), true);

View File

@@ -46,13 +46,13 @@ public class ViewAllCharSelectedWithPicHandler extends AbstractMaplePacketHandle
String macs = slea.readMapleAsciiString();
String hwid = slea.readMapleAsciiString();
if (!Hwid.isValidHwid(hwid)) {
if (!Hwid.isValidRawHwid(hwid)) {
c.announce(MaplePacketCreator.getAfterLoginError(17));
return;
}
c.updateMacs(macs);
c.updateHWID(hwid);
c.updateHwid(hwid);
if (c.hasBannedMac() || c.hasBannedHWID()) {
MapleSessionCoordinator.getInstance().closeSession(c.getSession(), true);