Advertisement
Guest User

Untitled

a guest
Feb 9th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. package lasteam.protection;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. import org.apache.log4j.Logger;
  12.  
  13.  
  14.  
  15. import javolution.util.FastList;
  16. import javolution.util.FastMap;
  17.  
  18. import ru.catssoftware.L2DatabaseFactory;
  19. import ru.catssoftware.config.L2Properties;
  20. import ru.catssoftware.gameserver.Announcements;
  21. import ru.catssoftware.gameserver.banmanager.BanManager;
  22. // import ru.catssoftware.gameserver.gmaccess.gmCache;
  23. import ru.catssoftware.gameserver.gmaccess.gmCache;
  24. import ru.catssoftware.gameserver.gmaccess.gmController;
  25. import ru.catssoftware.gameserver.model.actor.instance.L2PcInstance;
  26. import ru.catssoftware.gameserver.network.Disconnection;
  27. import ru.catssoftware.gameserver.network.L2GameClient;
  28. import ru.catssoftware.gameserver.network.L2GameClient.IExReader;
  29. import ru.catssoftware.gameserver.network.serverpackets.GameGuardQuery;
  30. import ru.catssoftware.util.Console;
  31.  
  32. public class CatsGuard {
  33. private static Logger _log = Logger.getLogger("CatsGuard");
  34. private class CatsGuardReader implements IExReader {
  35. private RC4 _crypt;
  36. private L2GameClient _client;
  37. private int _prevcode = 0;
  38. private byte []buffer = new byte[4];
  39. private int _state;
  40. private boolean _checkChar;
  41. private CatsGuardReader(L2GameClient cl) {
  42. _state = 0;
  43. _client = cl;
  44. }
  45.  
  46. private void setKey(int data[]) {
  47. String key = "";
  48. for(int i=0;i<10;i++)
  49. key+=String.format("%X%X", data[1],_SERVER_KEY);
  50. _crypt = new RC4(key,false);
  51. _state = 1;
  52. }
  53. public int read(ByteBuffer buf) {
  54. int opcode = 0;
  55. if(_state==0) {
  56. opcode = buf.get() & 0xff;
  57. if(opcode!=0xca) {
  58. illegalAction(_client,"Invalid opcode on pre-auth state");
  59. return 0;
  60. }
  61. } else {
  62. if(buf.remaining()<4)
  63. illegalAction(_client,"Invalid block size on authed state");
  64. else {
  65. buf.get(buffer);
  66. opcode = decryptPacket(buffer) & 0xff;
  67. }
  68. }
  69. return opcode;
  70. }
  71. private int decryptPacket(byte [] packet) {
  72. packet = _crypt.rc4(packet);
  73. int crc = CRC16.calc(new byte[] { (byte)(_prevcode & 0xff),packet[1]});
  74. int read_crc = (((packet[3] & 0xff) << 8) & 0xff00) | (packet[2] & 0xff);
  75. if(crc!= read_crc ) {
  76. illegalAction(_client,"CRC error");
  77. return 0;
  78. }
  79. _prevcode = packet[1] & 0xff;
  80. return _prevcode;
  81. }
  82.  
  83. @Override
  84. public void checkChar(L2PcInstance cha) {
  85. if(!_checkChar || cha == null)
  86. return;
  87. if(ALLOW_GM_FROM_BANNED_HWID && gmCache.getInstance().isGm(cha.getObjectId()))
  88. return;
  89. if(LOG_OPTION.contains("BANNED"))
  90. _log.info("CatsGuard: Client "+cha.getClient()+" try to log with banned hwid");
  91. new Disconnection(cha.getClient()).defaultSequence(false);
  92. }
  93.  
  94. }
  95. private static CatsGuard _instance;
  96. public static CatsGuard getInstance() {
  97. if(_instance==null)
  98. _instance = new CatsGuard();
  99. return _instance;
  100. }
  101. private Map<String, Integer> _connections;
  102. private List<String> _premium = new FastList<String>();
  103. private List<String> _bannedhwid;
  104. private static boolean ENABLED = false;
  105. private static int _SERVER_KEY;
  106. private int MAX_SESSIONS;
  107. private int MAX_PREMIUM_SESSIONS;
  108. private String LOG_OPTION;
  109. private boolean ANNOUNCE_HACK;
  110. private String ON_HACK_ATTEMP;
  111. private boolean ALLOW_GM_FROM_BANNED_HWID;
  112. private boolean LOG_SESSIONS;
  113. private CatsGuard() {
  114. Console.printSection("CatsGuard");
  115. try {
  116. if(_SERVER_KEY == 0)
  117. return;
  118. L2Properties p = new L2Properties("./config/main/catsguard.properties");
  119. ENABLED = Boolean.parseBoolean(p.getProperty("Enabled","true")) && ENABLED;
  120. if(!ENABLED) {
  121. _log.info("CatsGuard: disabled");
  122. return;
  123. }
  124. LOG_OPTION = p.getProperty("LogOption","NOSPS HACK");
  125. MAX_SESSIONS = Integer.parseInt(p.getProperty("MaxSessionsFromHWID","-1"));
  126. MAX_PREMIUM_SESSIONS = Integer.parseInt(p.getProperty("MaxSessionsForPremium","-1"));
  127. ANNOUNCE_HACK = Boolean.parseBoolean(p.getProperty("AnnounceHackAttempt","true"));
  128. ON_HACK_ATTEMP = p.getProperty("OnHackAttempt","kick");
  129. ALLOW_GM_FROM_BANNED_HWID = Boolean.parseBoolean(p.getProperty("AllowGMFromBannedHWID","false")); _connections = new FastMap<String, Integer>().setShared(true);
  130. LOG_SESSIONS = Boolean.parseBoolean(p.getProperty("LogSessions","false"));
  131. _bannedhwid = new FastList<String>();
  132. Connection con = L2DatabaseFactory.getInstance().getConnection();
  133. PreparedStatement stm = con.prepareStatement("select * from banned_hwid");
  134. try {
  135. ResultSet rs = stm.executeQuery();
  136. while(rs.next())
  137. _bannedhwid.add(rs.getString(1));
  138. rs.close();
  139. } catch(Exception e) {
  140. if (e.getClass().getSimpleName().equals("MySQLSyntaxErrorException")) {
  141. stm.close();
  142. stm = con.prepareStatement("create table `banned_hwid` (`hwid` varchar(64) not null primary key)");
  143. stm.execute();
  144. }
  145. }
  146. stm.close();
  147. con.close();
  148. gmController.getInstance().regCommand(new GatsGuardHandler());
  149. _log.info("CatsGuard: Loaded "+_bannedhwid.size()+" banned hwid(s)");
  150. _log.info("CatsGuard: Ready");
  151. } catch(Exception e) {
  152. _log.warn("CatsGuard: Error while loading ./config/main/catsguard.properties",e);
  153. ENABLED = false;
  154. }
  155. }
  156.  
  157. public boolean isEnabled() {
  158. return ENABLED;
  159. }
  160. public void ban(L2PcInstance player) {
  161. ban(player.getHWid());
  162. }
  163. public void ban(String hwid) {
  164. if(!ENABLED)
  165. return;
  166. synchronized(_bannedhwid) {
  167. if(_bannedhwid.contains(hwid))
  168. return;
  169. _bannedhwid.add(hwid);
  170. try {
  171. Connection con = L2DatabaseFactory.getInstance().getConnection();
  172. PreparedStatement stm = con.prepareStatement("insert into banned_hwid values(?)");
  173. stm.setString(1, hwid);
  174. stm.execute();
  175. stm.close();
  176. con.close();
  177. } catch(SQLException e) {
  178. _log.error("CatsGuard: Unable to store banned hwid",e);
  179. }
  180. }
  181. }
  182. private void illegalAction(L2GameClient cl, String reason) {
  183. if(cl.getActiveChar()!=null && ANNOUNCE_HACK)
  184. Announcements.getInstance().announceToAll("Игрок "+cl.getActiveChar().getName()+" использует недопустимое ПО!");
  185. if(ON_HACK_ATTEMP.equals("hwidban") && cl.getHWid()!=null)
  186. ban(cl.getHWid());
  187. else if(ON_HACK_ATTEMP.equals("jail") && cl.getActiveChar()!=null)
  188. BanManager.getInstance().jailPlayer(null, cl.getActiveChar(), -1, true);
  189. else if(ON_HACK_ATTEMP.equals("ban") && cl.getActiveChar()!=null)
  190. BanManager.getInstance().banAccount(null, cl.getActiveChar());
  191. _log.info("CatsGuard: Client "+cl+" use illegal software and will "+ON_HACK_ATTEMP+"ed. Reason: "+reason);
  192. new Disconnection(cl).close(false);
  193. }
  194. public void unban(String hwid) {
  195. if(!ENABLED)
  196. return;
  197. synchronized(_bannedhwid) {
  198. _bannedhwid.remove(hwid);
  199. }
  200. try {
  201. Connection con = L2DatabaseFactory.getInstance().getConnection();
  202. PreparedStatement stm = con.prepareStatement("delete from banned_hwid where hwid=?");
  203. stm.setString(1, hwid);
  204. stm.execute();
  205. stm.close();
  206. con.close();
  207. } catch(SQLException e) {
  208. _log.error("CatsGuard: Unable to clear banned hwid",e);
  209. }
  210.  
  211. }
  212. public void initSession(L2GameClient cl) {
  213. if(!ENABLED )
  214. return;
  215. cl.sendPacket(GameGuardQuery.STATIC_PACKET);
  216. cl._reader = new CatsGuardReader(cl);
  217. }
  218.  
  219. public void doneSession(L2GameClient cl) {
  220. if(!ENABLED)
  221. return;
  222. if(cl.getHWid()!=null) {
  223. _premium.remove(cl.getHWid());
  224. if(_connections.containsKey(cl.getHWid())) {
  225. int nwnd = _connections.get(cl.getHWid());
  226. if(nwnd==0)
  227. _connections.remove(cl.getHWid());
  228. else
  229. _connections.put(cl.getHWid(),--nwnd);
  230. }
  231. }
  232. cl._reader = null;
  233. }
  234.  
  235. public void initSession(L2GameClient cl, int [] data) {
  236. if(!ENABLED)
  237. return;
  238. if(data[0]!=_SERVER_KEY) {
  239. if(LOG_OPTION.contains("NOPROTECT"))
  240. _log.info("CatsGuard: Client "+cl+" try to log with no CatsGuard");
  241. new Disconnection(cl).defaultSequence(false);
  242. return;
  243. }
  244. String hwid = String.format("%x", data[3]);
  245. if(cl._reader==null) {
  246. if(LOG_OPTION.contains("HACK"))
  247. _log.info("CatsGuard: Client "+cl+" has no pre-authed state");
  248. new Disconnection(cl).defaultSequence(false);
  249. return;
  250. }
  251. if(_bannedhwid.contains(hwid)) {
  252. ((CatsGuardReader) cl._reader)._checkChar = true;
  253. }
  254. if(!_connections.containsKey(hwid))
  255. _connections.put(hwid,0);
  256. int nwindow = _connections.get(hwid);
  257. int max = MAX_SESSIONS;
  258. if (_premium.contains(hwid))
  259. max = MAX_PREMIUM_SESSIONS;
  260. if(max > 0 && ++nwindow>max) {
  261. if(LOG_OPTION.contains("SESSIONS"))
  262. _log.info("CatsGuard: To many sessions from hwid "+hwid);
  263. new Disconnection(cl).defaultSequence(false);
  264. return;
  265. }
  266. if (cl.getAccountData().getLong("premium",0)>System.currentTimeMillis() && !_premium.contains(hwid))
  267. _premium.add(hwid);
  268. _connections.put(hwid, nwindow);
  269. cl.setHWID(hwid);
  270. ((CatsGuardReader) cl._reader).setKey(data);
  271. if(LOG_SESSIONS)
  272. _log.info("Client "+cl.getAccountName()+" ["+cl.getHostAddress()+"] connected with hwid "+cl.getHWid());
  273.  
  274. }
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement