Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package silentium.protection;
- import java.nio.ByteBuffer;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.Map;
- import javolution.util.FastList;
- import javolution.util.FastMap;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import silentium.commons.database.DatabaseFactory;
- import silentium.gameserver.Announcements;
- import silentium.gameserver.AuthServerThread;
- import silentium.gameserver.model.actor.instance.L2PcInstance;
- import silentium.gameserver.network.L2GameClient;
- import silentium.gameserver.network.L2GameClient.IExReader;
- import silentium.gameserver.network.serverpackets.GameGuardQuery;
- import silentium.gameserver.network.serverpackets.LeaveWorld;
- public class CatsGuard
- {
- protected static final Logger _log = LoggerFactory.getLogger(CatsGuard.class.getName());
- private class CatsGuardReader implements IExReader
- {
- private RC4 _crypt;
- private final L2GameClient _client;
- private int _prevcode = 0;
- private final byte[] buffer = new byte[4];
- private int _state;
- protected boolean _checkChar;
- protected CatsGuardReader(L2GameClient cl)
- {
- _state = 0;
- _client = cl;
- }
- public void setKey(int data[])
- {
- String key = "";
- for (int i = 0; i < 10; i++)
- {
- key += String.format("%X%X", data[1], ProtectionProperties.SERVER_KEY);
- }
- _crypt = new RC4(key, false);
- _state = 1;
- }
- @Override
- public int read(ByteBuffer buf)
- {
- int opcode = 0;
- if (_state == 0)
- {
- opcode = buf.get() & 0xff;
- if (opcode != 0xca)
- {
- illegalAction(_client, "Invalid opcode on pre-auth state");
- return 0;
- }
- }
- else
- {
- if (buf.remaining() < 4)
- {
- illegalAction(_client, "Invalid block size on authed state");
- }
- else
- {
- buf.get(buffer);
- opcode = decryptPacket(buffer) & 0xff;
- }
- }
- return opcode;
- }
- private int decryptPacket(byte[] packet)
- {
- packet = _crypt.rc4(packet);
- int crc = CRC16.calc(new byte[]
- {
- (byte) (_prevcode & 0xff),
- packet[1]
- });
- int read_crc = (((packet[3] & 0xff) << 8) & 0xff00) | (packet[2] & 0xff);
- if (crc != read_crc)
- {
- illegalAction(_client, "CRC error");
- return 0;
- }
- _prevcode = packet[1] & 0xff;
- return _prevcode;
- }
- @Override
- public void checkChar(L2PcInstance cha)
- {
- if (!_checkChar || (cha == null))
- {
- return;
- }
- if (ProtectionProperties.ALLOW_GM_FROM_BANNED_HWID && cha.isGM())
- {
- return;
- }
- if (ProtectionProperties.LOG_OPTION.contains("BANNED"))
- {
- _log.info("CatsGuard: Client " + cha.getClient() + " try to log with banned hwid.");
- }
- cha.closeNetConnection(true);
- }
- }
- private Map<String, Integer> _connections;
- private final List<String> _premium = new FastList<>();
- private List<String> _bannedhwid;
- private CatsGuard()
- {
- ProtectionProperties.init();
- if (ProtectionProperties.SERVER_KEY == 0)
- {
- return;
- }
- _connections = new FastMap<>();
- _bannedhwid = new FastList<>();
- try (Connection con = DatabaseFactory.getConnection();
- PreparedStatement stm = con.prepareStatement("select * from banned_hwid");
- ResultSet rs = stm.executeQuery())
- {
- while (rs.next())
- {
- _bannedhwid.add(rs.getString(1));
- }
- }
- catch (Exception e)
- {
- if (e.getClass().getSimpleName().equals("MySQLSyntaxErrorException"))
- {
- try (Connection con = DatabaseFactory.getConnection();
- PreparedStatement stmt = con.prepareStatement("create table `banned_hwid` (`hwid` varchar(64) not null primary key)");)
- {
- stmt.execute();
- }
- catch (Exception ex)
- {
- _log.warn("", ex);
- }
- }
- }
- _log.info("CatsGuard: Loaded " + _bannedhwid.size() + " banned hwid(s)");
- _log.info("CatsGuard: Ready");
- }
- public boolean isEnabled()
- {
- return ProtectionProperties.ENABLED;
- }
- public void ban(L2PcInstance player)
- {
- ban(player.getHWid());
- }
- public void ban(String hwid)
- {
- if (!ProtectionProperties.ENABLED)
- {
- return;
- }
- synchronized (_bannedhwid)
- {
- if (_bannedhwid.contains(hwid))
- {
- return;
- }
- _bannedhwid.add(hwid);
- try
- {
- Connection con = DatabaseFactory.getConnection();
- PreparedStatement stm = con.prepareStatement("insert into banned_hwid values(?)");
- stm.setString(1, hwid);
- stm.execute();
- stm.close();
- con.close();
- }
- catch (SQLException e)
- {
- _log.warn("CatsGuard: Unable to store banned hwid");
- }
- }
- }
- protected void illegalAction(L2GameClient cl, String reason)
- {
- if ((cl.getActiveChar() != null) && ProtectionProperties.ANNOUNCE_HACK)
- {
- Announcements.announceToAll("Player " + cl.getActiveChar().getName() + " used unlegal soft!");
- }
- if (ProtectionProperties.ON_HACK_ATTEMP.equals("hwidban") && (cl.getHWid() != null))
- {
- ban(cl.getHWid());
- }
- else if (ProtectionProperties.ON_HACK_ATTEMP.equals("jail") && (cl.getActiveChar() != null))
- {
- cl.getActiveChar().isInJail();
- }
- else if (ProtectionProperties.ON_HACK_ATTEMP.equals("ban") && (cl.getActiveChar() != null))
- {
- AuthServerThread.getInstance().sendAccessLevel(cl.getAccountName(), -100);
- }
- _log.info("CatsGuard: Client " + cl + " use illegal software and will " + ProtectionProperties.ON_HACK_ATTEMP + "ed. Reason: " + reason);
- cl.close(new LeaveWorld());
- }
- public void initSession(L2GameClient cl)
- {
- if (!ProtectionProperties.ENABLED)
- {
- return;
- }
- cl.sendPacket(GameGuardQuery.STATIC_PACKET);
- cl._reader = new CatsGuardReader(cl);
- }
- public void doneSession(L2GameClient cl)
- {
- if (!ProtectionProperties.ENABLED)
- {
- return;
- }
- if (cl.getHWid() != null)
- {
- _premium.remove(cl.getHWid());
- if (_connections.containsKey(cl.getHWid()))
- {
- int nwnd = _connections.get(cl.getHWid());
- if (nwnd == 0)
- {
- _connections.remove(cl.getHWid());
- }
- else
- {
- _connections.put(cl.getHWid(), --nwnd);
- }
- }
- }
- cl._reader = null;
- }
- public void initSession(L2GameClient cl, int[] data)
- {
- if (!ProtectionProperties.ENABLED)
- {
- return;
- }
- if (data[0] != ProtectionProperties.SERVER_KEY)
- {
- if (ProtectionProperties.LOG_OPTION.contains("NOPROTECT"))
- {
- _log.info("CatsGuard: Client " + cl + " try to log with no CatsGuard");
- }
- cl.close(new LeaveWorld());
- return;
- }
- String hwid = String.format("%x", data[3]);
- if (cl._reader == null)
- {
- if (ProtectionProperties.LOG_OPTION.contains("HACK"))
- {
- _log.info("CatsGuard: Client " + cl + " has no pre-authed state");
- }
- cl.close(new LeaveWorld());
- return;
- }
- if (_bannedhwid.contains(hwid))
- {
- ((CatsGuardReader) cl._reader)._checkChar = true;
- }
- if (!_connections.containsKey(hwid))
- {
- _connections.put(hwid, 0);
- }
- int nwindow = _connections.get(hwid);
- int max = ProtectionProperties.MAX_SESSIONS;
- if (_premium.contains(hwid))
- {
- max = ProtectionProperties.MAX_PREMIUM_SESSIONS;
- }
- if ((max > 0) && (++nwindow > max))
- {
- if (ProtectionProperties.LOG_OPTION.contains("SESSIONS"))
- {
- _log.info("CatsGuard: To many sessions from hwid " + hwid);
- }
- cl.close(new LeaveWorld());
- return;
- }
- if (!_premium.contains(hwid))
- {
- _premium.add(hwid);
- }
- _connections.put(hwid, nwindow);
- cl.setHWID(hwid);
- ((CatsGuardReader) cl._reader).setKey(data);
- if (ProtectionProperties.LOG_SESSIONS)
- {
- _log.info("Client " + cl.getAccountName() + " connected with hwid " + cl.getHWid());
- }
- }
- private static CatsGuard _instance;
- public static CatsGuard getInstance()
- {
- if (_instance == null)
- {
- _instance = new CatsGuard();
- }
- return _instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement