Guest User

Untitled

a guest
Jul 28th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6.  
  7. namespace GCKELogin
  8. {
  9. class GCKLoginParser1453 : GCKPacketFilterLog, GCKILoginParser
  10. {
  11. private Dictionary<byte, GCKLoginServer.LoginParseMethod> ParsingDict;
  12. public Dictionary<byte, GCKLoginServer.LoginParseMethod> ParsingDictionary()
  13. {
  14. return ParsingDict;
  15. }
  16.  
  17. private int packetDataReadStartIndex(long user)
  18. {
  19. if (IsClientEncrypted(user))
  20. {
  21. return 9;
  22. }
  23. else
  24. {
  25. return 5;
  26. }
  27. }
  28.  
  29. private int packetDataWriteStartIndex(long user)
  30. {
  31. if (IsClientEncrypted(user))
  32. {
  33. return 4;
  34. }
  35. else
  36. {
  37. return 4;
  38. }
  39. }
  40.  
  41. private const byte PacketVersionRequest = 0x01;
  42. private const byte PacketDownloadInfoRequest = 0x02;
  43. private const byte PacketCreateCharacter = 0x03;
  44. private const byte PacketNationselect = 0x05;
  45. private const byte PacketGetCharacters = 0x0c;
  46. private const byte PacketGetEncryptionKey = 0xf2;
  47. private const byte PacketLogInRequest = 0xf3;
  48. private const byte PacketMGameLogInRequest = 0xf4;
  49. private const byte PacketServerList = 0xf5;
  50. private const byte PacketIntroNotice = 0xf6;
  51.  
  52. private Dictionary<long, byte[]> clientEncryptionKeys = new Dictionary<long, byte[]>();
  53. private Dictionary<long, byte[]> serverEncryptionKeys = new Dictionary<long, byte[]>();
  54. private List<long> encryptedUsers = new List<long>();
  55.  
  56. public GCKLoginParser1453()
  57. {
  58. ParsingDict = new Dictionary<byte, GCKLoginServer.LoginParseMethod>();
  59. ParsingDict.Add(PacketVersionRequest, VersionRequest);
  60. ParsingDict.Add(PacketDownloadInfoRequest, DownloadInfoRequest);
  61. ParsingDict.Add(PacketCreateCharacter, CreateCharacter);
  62. ParsingDict.Add(PacketNationselect, Nationselect);
  63. ParsingDict.Add(PacketGetCharacters, GetCharacters);
  64. ParsingDict.Add(PacketGetEncryptionKey, GetEncryptionKey);
  65. ParsingDict.Add(PacketLogInRequest, LogInRequest);
  66. ParsingDict.Add(PacketMGameLogInRequest, MGameLogInRequest);
  67. ParsingDict.Add(PacketServerList, ServerList);
  68. ParsingDict.Add(PacketIntroNotice, IntroNotice);
  69. }
  70.  
  71. public void RemoveUser(long userId)
  72. {
  73. lock (clientEncryptionKeys)
  74. {
  75. clientEncryptionKeys.Remove(userId);
  76. }
  77. lock (serverEncryptionKeys)
  78. {
  79. serverEncryptionKeys.Remove(userId);
  80. }
  81. lock (encryptedUsers)
  82. {
  83. encryptedUsers.Remove(userId);
  84. }
  85. }
  86. /*
  87. public static void CreateNewServerListPacketData()
  88. {
  89. GCKAccountDatabaseConnection.GetNewGameServerList();
  90. GCKAccountDatabaseConnection.GameServerInfo[] gameServers= GCKAccountDatabaseConnection.GetGameServerList();
  91. List<byte[]> serverPacketBytes = new List<byte[]>();
  92. foreach (GCKAccountDatabaseConnection.GameServerInfo gameServer in gameServers)
  93. {
  94. byte[] thisServerBytes = new byte[528];
  95.  
  96. thisServerBytes[0] = (byte)gameServer.LoginPacketName.Length;
  97. byte[] nameBytes = UTF8Encoding.UTF8.GetBytes(gameServer.LoginPacketName);
  98. Array.Copy(nameBytes, 0, thisServerBytes, 1, nameBytes.Length);
  99.  
  100. thisServerBytes[256] = (byte)gameServer.Address.Length;
  101. byte[] addressBytes = UTF8Encoding.UTF8.GetBytes(gameServer.Address);
  102. Array.Copy(addressBytes, 0, thisServerBytes, 257, addressBytes.Length);
  103.  
  104. byte[] serverNumberBytes = BitConverter.GetBytes(gameServer.Number);
  105. Array.Copy(serverNumberBytes, 0, thisServerBytes, 514, serverNumberBytes.Length);
  106.  
  107. byte[] serverGroupBytes = BitConverter.GetBytes(gameServer.GroupNumber);
  108. Array.Copy(serverGroupBytes, 0, thisServerBytes, 516, serverGroupBytes.Length);
  109.  
  110. byte[] serverSlotBytes = BitConverter.GetBytes(gameServer.Slots);
  111. Array.Copy(serverSlotBytes, 0, thisServerBytes, 520, serverSlotBytes.Length);
  112.  
  113. byte[] serverAvailableBytes = BitConverter.GetBytes(gameServer.Available);
  114. Array.Copy(serverAvailableBytes, 0, thisServerBytes, 524, serverAvailableBytes.Length);
  115.  
  116. serverPacketBytes.Add(thisServerBytes);
  117. }
  118. if (serverPacketBytes.Count > 0)
  119. {
  120. byte[] newPacketBytes = new byte[(528 * serverPacketBytes.Count) + 7];
  121. newPacketBytes[0] = 0x00;
  122. newPacketBytes[1] = 0x02;
  123. newPacketBytes[2] = 0x1E;
  124. newPacketBytes[3] = 0xFC;
  125. newPacketBytes[4] = PacketServerList;
  126. newPacketBytes[6] = (byte)serverPacketBytes.Count;
  127.  
  128. for (int i = 0; i < serverPacketBytes.Count; i++)
  129. {
  130. Array.Copy(serverPacketBytes[i], 0, newPacketBytes, (528 * i) + 7, 528);
  131. }
  132. serverListPacketData = newPacketBytes;
  133. }
  134. }
  135. */
  136. private bool PreParse(long user, Socket socket, byte[] packetData, ref int packetLength, string text)
  137. {
  138. Log(packetData, packetLength, String.Format("User: {0} {1}", user, text));
  139. return true;
  140. }
  141.  
  142. public bool VersionRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
  143. {
  144. if (!PreParse(user, socket, packetData, ref packetLength, "Version Request")) return false;
  145. byte[] versionBytes = BitConverter.GetBytes(Settings.LoginVersion);
  146. int outIndex = packetDataWriteStartIndex(user);
  147. packetData[outIndex] = PacketVersionRequest;
  148. packetData[outIndex + 1] = versionBytes[0];
  149. packetData[outIndex + 2] = versionBytes[1];
  150. packetLength = 3;
  151. return true;
  152. }
  153.  
  154. public bool DownloadInfoRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
  155. {
  156. if (!PreParse(user, socket, packetData, ref packetLength, "Download Info Request")) return false;
  157. return false;
  158. }
  159.  
  160. public bool CreateCharacter(long user, Socket socket, byte[] packetData, ref int packetLength)
  161. {
  162. if (!PreParse(user, socket, packetData, ref packetLength, "Create Character")) return false;
  163. return false;
  164. }
  165.  
  166. public bool Nationselect(long user, Socket socket, byte[] packetData, ref int packetLength)
  167. {
  168. if (!PreParse(user, socket, packetData, ref packetLength, "Nation Select")) return false;
  169.  
  170. int outIndex = packetDataWriteStartIndex(user) ;
  171. packetData[outIndex] = PacketNationselect;
  172. packetData[outIndex + 1]++;
  173. packetLength = 2;
  174. return true;
  175. }
  176.  
  177. public bool GetCharacters(long user, Socket socket, byte[] packetData, ref int packetLength)
  178. {
  179. if (!PreParse(user, socket, packetData, ref packetLength, "Get Characters")) return false;
  180. return false;
  181. }
  182.  
  183. public bool IsClientEncrypted(long user)
  184. {
  185. lock (encryptedUsers)
  186. {
  187. return encryptedUsers.Contains(user);
  188. }
  189. }
  190.  
  191. public byte[] GetClientEncryptionKey(long user)
  192. {
  193. lock (clientEncryptionKeys)
  194. {
  195. if (clientEncryptionKeys.ContainsKey(user))
  196. return clientEncryptionKeys[user];
  197. else
  198. return null;
  199. }
  200. }
  201. public byte[] GetServerEncryptionKey(long user)
  202. {
  203. lock (serverEncryptionKeys)
  204. {
  205. if (serverEncryptionKeys.ContainsKey(user))
  206. return serverEncryptionKeys[user];
  207. else
  208. return null;
  209. }
  210. }
  211.  
  212. private static Random encKeyRand = new Random();
  213. public bool GetEncryptionKey(long user, Socket socket, byte[] packetData, ref int packetLength)
  214. {
  215. if (!PreParse(user, socket, packetData, ref packetLength, "Encryption Key Request")) return false;
  216. //9
  217. int outIndex = packetDataWriteStartIndex(user);
  218. packetData[outIndex] = PacketGetEncryptionKey; outIndex++;
  219.  
  220. byte[] clientKey = new byte[] { 0xaf, 0x6f, 0x80, 0x1f, 0xa7, 0x50, 0xb5, 0x3a };
  221.  
  222. encKeyRand.NextBytes(clientKey);
  223.  
  224. byte[] serverKey = Settings.GetServerEncryptionKey(clientKey);
  225.  
  226. packetData[outIndex] = clientKey[0]; outIndex++;
  227. packetData[outIndex] = clientKey[1]; outIndex++;
  228. packetData[outIndex] = clientKey[2]; outIndex++;
  229. packetData[outIndex] = clientKey[3]; outIndex++;
  230. packetData[outIndex] = clientKey[4]; outIndex++;
  231. packetData[outIndex] = clientKey[5]; outIndex++;
  232. packetData[outIndex] = clientKey[6]; outIndex++;
  233. packetData[outIndex] = clientKey[7]; outIndex++;
  234. packetLength = outIndex - packetDataWriteStartIndex(user);
  235.  
  236. lock (clientEncryptionKeys)
  237. {
  238. clientEncryptionKeys.Add(user, clientKey);
  239. }
  240. lock (serverEncryptionKeys)
  241. {
  242. serverEncryptionKeys.Add(user, serverKey);
  243. }
  244. lock (encryptedUsers)
  245. {
  246. encryptedUsers.Add(user);
  247. }
  248.  
  249. return true;
  250. }
  251.  
  252.  
  253.  
  254. public bool LogInRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
  255. {
  256. if (!PreParse(user, socket, packetData, ref packetLength, "Log In Request")) return false;
  257.  
  258. int currentPacketIndex = packetDataReadStartIndex(user);
  259.  
  260. ushort usernameLength = BitConverter.ToUInt16(packetData, currentPacketIndex);
  261. currentPacketIndex += 2;
  262. if ((usernameLength > Settings.MaxUsernameLength) || (usernameLength < Settings.MinUsernameLength))
  263. {
  264. return sendLoginReturnPacket(user, socket, packetData, ref packetLength, 2);
  265. }
  266. string username = UTF8Encoding.UTF8.GetString(packetData, currentPacketIndex, usernameLength);
  267. currentPacketIndex += usernameLength;
  268.  
  269. ushort passwordLength = BitConverter.ToUInt16(packetData, currentPacketIndex);
  270. currentPacketIndex += 2;
  271. if ((passwordLength > Settings.MaxPasswordLength) || (passwordLength < Settings.MinPasswordLength))
  272. {
  273. return sendLoginReturnPacket(user, socket, packetData, ref packetLength, 2);
  274. }
  275. string password = UTF8Encoding.UTF8.GetString(packetData, currentPacketIndex, passwordLength);
  276. currentPacketIndex += passwordLength;
  277.  
  278. Log(String.Format("Login: {0} : {1}", username, password));
  279.  
  280. int loginRetVal = 0;
  281. if ((loginRetVal = GCKAccountDatabaseConnection.Login(username, password, true)) == 1)
  282. {
  283. //need to check if the person is a current user
  284. string serverIp = "";
  285. int serverNumber = 0;
  286. if (GCKAccountDatabaseConnection.IsCurrentUser(username, ref serverIp, ref serverNumber))
  287. {
  288. return sendLoginReturnPacket(user, socket, packetData, ref packetLength, 5);
  289. }
  290. else
  291. {
  292. //successful login
  293. int outIndex = packetDataWriteStartIndex(user);
  294. packetData[outIndex] = 0xFC; outIndex++;
  295. packetData[outIndex] = 0x1E; outIndex++;
  296. packetData[outIndex] = 0x01; outIndex++;
  297. packetData[outIndex] = 0x00; outIndex++;
  298. packetData[outIndex] = 0x00; outIndex++;
  299. packetData[outIndex] = PacketLogInRequest; outIndex++;
  300. packetData[outIndex] = 0x01; outIndex++;
  301. packetData[outIndex] = 0xFF; outIndex++;
  302. packetData[outIndex] = 0xFF; outIndex++;
  303. byte[] nameLengthBytes = BitConverter.GetBytes(usernameLength);
  304. packetData[outIndex] = nameLengthBytes[0]; outIndex++;
  305. packetData[outIndex] = nameLengthBytes[1]; outIndex++;
  306. byte[] usernameBytes = UTF8Encoding.UTF8.GetBytes(username);
  307. Array.Copy(usernameBytes, 0, packetData, outIndex, usernameBytes.Length);
  308. packetLength = outIndex + usernameLength - packetDataWriteStartIndex(user);
  309. return true;
  310. }
  311. }
  312. else
  313. {
  314. return sendLoginReturnPacket(user, socket, packetData, ref packetLength, (byte)loginRetVal);
  315. }
  316. }
  317.  
  318. private bool sendLoginReturnPacket(long user, Socket socket, byte[] packetData, ref int packetLength, byte value)
  319. {
  320. int outIndex = packetDataWriteStartIndex(user);
  321. packetData[outIndex] = 0xfc; outIndex++;
  322. packetData[outIndex] = 0x1e; outIndex++;
  323. packetData[outIndex] = 0x01; outIndex++;
  324. packetData[outIndex] = 0x00; outIndex++;
  325. packetData[outIndex] = 0x00; outIndex++;
  326. packetData[outIndex] = 0xf3; outIndex++;
  327. packetData[outIndex] = value; outIndex++;
  328. packetData[outIndex] = 0x00; outIndex++;
  329. packetData[outIndex] = 0x00; outIndex++;
  330. packetData[outIndex] = 0x00; outIndex++;
  331. packetData[outIndex] = 0x00; outIndex++;
  332. packetLength = outIndex - packetDataWriteStartIndex(user);
  333. return true;
  334. }
  335.  
  336. public bool MGameLogInRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
  337. {
  338. if (!PreParse(user, socket, packetData, ref packetLength, "MGame Log In Request")) return false;
  339. return false;
  340. }
  341.  
  342. public bool ServerList(long user, Socket socket, byte[] packetData, ref int packetLength)
  343. {
  344. if (!PreParse(user, socket, packetData, ref packetLength, "Server List Request")) return false;
  345.  
  346. GCKAccountDatabaseConnection.GameServerInfo[] gameServers = GCKAccountDatabaseConnection.GameServers;
  347.  
  348. int outIndex = packetDataWriteStartIndex(user);
  349. packetData[outIndex] = 0xfc; outIndex++;
  350. packetData[outIndex] = 0x1e; outIndex++;
  351. packetData[outIndex] = 0x02; outIndex++;
  352. packetData[outIndex] = 0x00; outIndex++;
  353.  
  354. packetData[outIndex] = 0x00; outIndex++;
  355.  
  356. packetData[outIndex] = PacketServerList; outIndex++;
  357.  
  358. packetData[outIndex] = (byte)gameServers.Length; outIndex++;
  359.  
  360. //uint lastServerGroup = 0;
  361. for (int i = 0; i < gameServers.Length; i++)
  362. {
  363. byte[] ipLengthBytes = BitConverter.GetBytes((short)gameServers[i].Address.Length);
  364. Array.Copy(ipLengthBytes, 0, packetData, outIndex, ipLengthBytes.Length);
  365. outIndex += ipLengthBytes.Length;
  366.  
  367. byte[] ipBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].Address);
  368. Array.Copy(ipBytes, 0, packetData, outIndex, ipBytes.Length);
  369. outIndex += ipBytes.Length;
  370.  
  371. byte[] nameLengthBytes = BitConverter.GetBytes((short)gameServers[i].LoginPacketName.Length);
  372. Array.Copy(nameLengthBytes, 0, packetData, outIndex, nameLengthBytes.Length);
  373. outIndex += nameLengthBytes.Length;
  374.  
  375. byte[] nameBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].LoginPacketName);
  376. Array.Copy(nameBytes, 0, packetData, outIndex, nameBytes.Length);
  377. outIndex += nameBytes.Length;
  378.  
  379. byte[] availableBytes = BitConverter.GetBytes((ushort)(gameServers[i].Slots - gameServers[i].Available));
  380. packetData[outIndex] = availableBytes[0]; packetData[outIndex + 1] = availableBytes[1];
  381. outIndex += 2;
  382.  
  383. byte[] numberBytes = BitConverter.GetBytes((ushort)gameServers[i].Number);
  384. packetData[outIndex] = numberBytes[0]; packetData[outIndex + 1] = numberBytes[1];
  385. outIndex += 2;
  386.  
  387. byte[] groupNumberBytes = BitConverter.GetBytes((ushort)gameServers[i].GroupNumber);
  388. packetData[outIndex] = groupNumberBytes[0]; packetData[outIndex + 1] = groupNumberBytes[1];
  389. outIndex += 2;
  390.  
  391. //if (lastServerGroup != gameServers[i].GroupNumber)
  392. //{
  393. byte[] karusKingLengthBytes = BitConverter.GetBytes((short)gameServers[i].KarusKing.Length);
  394. Array.Copy(karusKingLengthBytes, 0, packetData, outIndex, karusKingLengthBytes.Length);
  395. outIndex += karusKingLengthBytes.Length;
  396.  
  397. byte[] karusKingBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].KarusKing);
  398. Array.Copy(karusKingBytes, 0, packetData, outIndex, karusKingBytes.Length);
  399. outIndex += karusKingBytes.Length;
  400.  
  401.  
  402. byte[] karusTextLengthBytes = BitConverter.GetBytes((short)gameServers[i].KarusText.Length);
  403. Array.Copy(karusTextLengthBytes, 0, packetData, outIndex, karusTextLengthBytes.Length);
  404. outIndex += karusTextLengthBytes.Length;
  405.  
  406. byte[] karusTextBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].KarusText);
  407. Array.Copy(karusTextBytes, 0, packetData, outIndex, karusTextBytes.Length);
  408. outIndex += karusTextBytes.Length;
  409.  
  410.  
  411. byte[] elmoKingLengthBytes = BitConverter.GetBytes((short)gameServers[i].ElmoKing.Length);
  412. Array.Copy(elmoKingLengthBytes, 0, packetData, outIndex, elmoKingLengthBytes.Length);
  413. outIndex += elmoKingLengthBytes.Length;
  414.  
  415. byte[] elmoKingBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].ElmoKing);
  416. Array.Copy(elmoKingBytes, 0, packetData, outIndex, elmoKingBytes.Length);
  417. outIndex += elmoKingBytes.Length;
  418.  
  419.  
  420. byte[] elmoTextLengthBytes = BitConverter.GetBytes((short)gameServers[i].ElmoText.Length);
  421. Array.Copy(elmoTextLengthBytes, 0, packetData, outIndex, elmoTextLengthBytes.Length);
  422. outIndex += elmoTextLengthBytes.Length;
  423.  
  424. byte[] elmoTextBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].ElmoText);
  425. Array.Copy(elmoTextBytes, 0, packetData, outIndex, elmoTextBytes.Length);
  426. outIndex += elmoTextBytes.Length;
  427.  
  428. //lastServerGroup = gameServers[i].GroupNumber;
  429. //}
  430.  
  431. /*byte[] unknownBytes = BitConverter.GetBytes((ushort)0);
  432. packetData[outIndex] = unknownBytes[0]; packetData[outIndex + 1] = unknownBytes[1];
  433. outIndex += 2;*/
  434.  
  435. /*byte[] slotsBytes = BitConverter.GetBytes((ushort)gameServers[i].Slots);
  436. packetData[outIndex] = slotsBytes[0]; packetData[outIndex + 1] = slotsBytes[1];
  437. outIndex += 2;*/
  438. }
  439.  
  440. packetLength = outIndex - packetDataWriteStartIndex(user);
  441. return true;
  442. }
  443.  
  444. public bool IntroNotice(long user, Socket socket, byte[] packetData, ref int packetLength)
  445. {
  446. if (!PreParse(user, socket, packetData, ref packetLength, "Intro Notice Request")) return false;
  447.  
  448. //12:21:31 PM 12: Server To Client 127.0.0.1:19100 - 127.0.0.1:1508 Len 39: AA 55 21 00 F6 0E 00 4F 6D 66 67 20 49 72 6F 6E 20 70 77 6E 73 0E 00 4F 6D 66 67 20 49 72 6F 6E 20 70 77 6E 73 55 AA
  449. int outIndex = packetDataWriteStartIndex(user);
  450.  
  451. packetData[outIndex] = 0xfc; outIndex++;
  452. packetData[outIndex] = 0x1e; outIndex++;
  453. packetData[outIndex] = 0x03; outIndex++;
  454. packetData[outIndex] = 0x00; outIndex++;
  455.  
  456. packetData[outIndex] = 0x00; outIndex++;
  457. packetData[outIndex] = PacketIntroNotice; outIndex++;
  458.  
  459. byte[] introNoticeLengthBytes = BitConverter.GetBytes((ushort)Settings.IntroNoticeString.Length);
  460. Array.Copy(introNoticeLengthBytes, 0, packetData, outIndex, introNoticeLengthBytes.Length);
  461. outIndex += introNoticeLengthBytes.Length;
  462.  
  463. byte[] introNoticeBytes = UTF8Encoding.UTF8.GetBytes(Settings.IntroNoticeString);
  464. Array.Copy(introNoticeBytes, 0, packetData, outIndex, introNoticeBytes.Length);
  465. outIndex += introNoticeBytes.Length;
  466.  
  467. int newsLength = 0;
  468. string[] newsItems = GCKAccountDatabaseConnection.NewsTexts;
  469. for (int i = 0; i < newsItems.Length; i++)
  470. {
  471. newsLength += newsItems[i].Length;
  472. }
  473. newsLength += ((newsItems.Length - 1) * 2);
  474.  
  475. byte[] newsLengthBytes = BitConverter.GetBytes((ushort)newsLength);
  476. Array.Copy(newsLengthBytes, 0, packetData, outIndex, newsLengthBytes.Length);
  477. outIndex += newsLengthBytes.Length;
  478.  
  479. for (int i = 0; i < newsItems.Length; i++)
  480. {
  481. byte[] newsItemStringBytes = UTF8Encoding.UTF8.GetBytes(newsItems[i]);
  482. Array.Copy(newsItemStringBytes, 0, packetData, outIndex, newsItemStringBytes.Length);
  483. outIndex += newsItemStringBytes.Length;
  484.  
  485. packetData[outIndex] = 0x00; outIndex++;
  486. packetData[outIndex] = 0x0a; outIndex++;
  487. }
  488.  
  489. packetLength = outIndex - packetDataWriteStartIndex(user) - 2; //the 2 is for the last 0x00 0x0a that doesn't get put there
  490. return true;
  491. }
  492. }
  493. }
Add Comment
Please, Sign In to add comment