Advertisement
Guest User

Encrypt Finish

a guest
Jul 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import socket
  3. import string
  4. import random
  5. import hashlib
  6.  
  7.  
  8. def completeGamePacketEncrypt(buf,session,is_session_packet):
  9. session_number = (((session >> 6) & 0xFF) & 0x80000003)
  10.  
  11. if(session_number < 0):
  12. session_number = (((session_number - 1) | 0xFFFFFFFC) + 1)
  13. session_key = (session & 0xFF)
  14.  
  15. if(is_session_packet):
  16. session_number = -1
  17. if (session_number == 0):
  18. for i in range(len(buf)):
  19. buf[i] = (buf[i] + (session_key + 0x40))
  20.  
  21. elif(session_number == 1):
  22. for i in range(len(buf)):
  23. buf[i] = (buf[i] - (session_key + 0x40))
  24. elif(session_number == 2):
  25. for i in range(len(buf)):
  26. buf[i] = (buf[i] ^ 0xC3) + (session_key + 0x40)
  27. elif(session_number == 3):
  28. for i in range(len(buf)):
  29. buf[i] = (buf[i] ^ 0xC3) - (session_key + 0x40)
  30. else:
  31. for i in range(len(buf)):
  32. buf[i] = buf[i] + 0x0F
  33.  
  34.  
  35. def transform(chaine):
  36. final = ""
  37. for i in range (len(chaine)):
  38. c = chaine[i]
  39. if (c == '#'):
  40. final += '0'
  41. else:
  42. n = ord(c)
  43. #!(n-= 0x20)
  44. if(not(n - 0x20) or (n + 0xF1) < 0 or (n - 0xB) < 0 or not(n - 0xC5)):
  45. final += '1'
  46. else:
  47. final += '0'
  48. return final
  49.  
  50.  
  51.  
  52. def encryptworld(buf, session, is_session_packet):
  53. packet_length = len(buf)
  54. packet_mask = transform(buf)
  55. output = []
  56. sequences = 0
  57. sequence_counter = 0
  58. last_position = 0
  59. current_position = 0
  60. length = 0
  61. current_byte = 0
  62. while current_position <= packet_length:
  63. last_position = current_position
  64. while (current_position < packet_length) and (packet_mask[current_position] == '0'):
  65. current_position += 1
  66.  
  67. if (current_position):
  68. length = (current_position - last_position)
  69. sequences = (length / 0x7E)
  70. for i in range (length):
  71. if (i == (sequence_counter * 0x7E)) :
  72. if not(sequences):
  73. output.append(length - i)
  74. else:
  75. output.append(0x7E)
  76. sequences-=1
  77. sequence_counter+=1
  78.  
  79.  
  80. output.append(ord(buf[last_position]) ^ 0xFF)
  81.  
  82. last_position += 1
  83. if(current_position >= packet_length):
  84. break
  85. last_position = current_position
  86. while(current_position < packet_length) and (packet_mask[current_position] == '1'):
  87. current_position+=1
  88.  
  89. if(current_position):
  90. length = (current_position - last_position)
  91. sequences = (length / 0x7E)
  92. for i in range(length):
  93.  
  94. if(i == (sequence_counter * 0x7E)):
  95.  
  96. if(not(sequences)):
  97.  
  98. output.append((length - i) | 0x80)
  99. else:
  100.  
  101. output.append(0x7E | 0x80)
  102. sequences-=1
  103. sequence_counter+=1
  104.  
  105. current_byte = buf[last_position]
  106. if (current_byte == 0x20):
  107. current_byte=1
  108. elif (current_byte == 0x2D):
  109. current_byte = 2
  110. elif (current_byte== 0x2E):
  111. current_byte = 3
  112. elif (current_byte== 0xFF):
  113. current_byte = 0xE
  114. else:
  115. current_byte-= 0x2C
  116.  
  117. if(current_byte != 0x00):
  118.  
  119. if(i % 2 == 0):
  120. output.append(current_byte << 4)
  121. print(current_byte)
  122. else:
  123. output[len(output)-1] = output[len(output)-1] | current_byte
  124. #output.back() |= current_byte;
  125.  
  126.  
  127. last_position +=1
  128. output.append(0xff)
  129. print( "Before :complete -> " + str(output))
  130. completeGamePacketEncrypt(output,session,is_session_packet)
  131.  
  132. output = [x % 256 for x in output]
  133. print("After : complete -> " + str(output))
  134. return bytearray(output)
  135.  
  136. def decryptLoginPacket(buf):
  137. tmp = ""
  138. for i in range(len(buf)):
  139. tmp += chr(ord(buf[i]) - 0xF)
  140. return tmp[:-1]
  141.  
  142. def encryptLoginPacket(buf):
  143.  
  144. return bytearray([((ord(x) ^ 0xC3) + 0xF) % 256 for x in buf + '\n'])
  145.  
  146. def genLoginPacket(username, password, game_version, game_hash):
  147. res = 'NoS0575 '
  148. res += str(random.randrange(10000, 8888888))
  149. res += ' '
  150. res += username
  151. res += ' '
  152. res += hashlib.sha512(password.encode('utf-8')).hexdigest().upper()
  153. res += ' 000'
  154. res += '027AF'
  155. res += '\x0b'
  156. res += game_version
  157. res += ' 0 '
  158. res += hashlib.md5((game_hash+username).encode('utf-8')).hexdigest().upper()
  159. return res
  160.  
  161. #NoS0575 665638 ssss 092BCBA17FDEE6FF9C3CE4262CD48BB3D9BFB042D8D14E2DC7B35A0EE3F92D0C040EA49A1820D6EA3F86EA159B8C2D3590ED7DF4FB4AC20BB0CE35BE6D80E1AB 009027AF 0.9.3.3088 0 5E242D4D5C6EBFB9176C887078084537
  162.  
  163. TCP_IP = 'login.nostale.gfsrv.net'
  164. TCP_PORT = 4002
  165. BUFFER_SIZE = 4096
  166.  
  167. USERNAME = 'socket1'
  168. PASSWORD = 'socket'
  169. GAME_VERSION = '0.9.3.3088'
  170. GAME_HASH = 'CDB86A1430332375FB8B05B1A5C45755F807636A5832DDF13143AF01CCCB1695' # md5(NostaleClientX.exe)+(NostaleClient.exe) : http://onlinemd5.com/
  171.  
  172. fpacket = genLoginPacket(USERNAME, PASSWORD, GAME_VERSION, GAME_HASH)
  173.  
  174. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  175. s.connect((TCP_IP, TCP_PORT))
  176. s.send(encryptLoginPacket(fpacket))
  177.  
  178. data = s.recv(BUFFER_SIZE)
  179. s.close()
  180.  
  181. #print("\nReceived Data: %s" % data)
  182. login = decryptLoginPacket(data)
  183. server3info =login.split(' ')[6]
  184. ip3=server3info.split(':')[0]
  185. port3=server3info.split(':')[1]
  186. session = int(login.split(' ')[2])
  187. print("IP: " + ip3 +"\n" + "PORT : " + port3 + "\nSession ID: " + str(session))
  188. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  189. s.connect((ip3, int(port3)))
  190.  
  191. s.send(encryptworld(str(session),session,True))
  192. s.send(encryptworld(USERNAME,session,False))
  193. s.send(encryptworld(PASSWORD,session,False))
  194.  
  195.  
  196. print ("TEST : " + s.recv(1024))
  197. #data = s.recv() AFTER...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement