Advertisement
Guest User

Crypto PYTHON

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