Advertisement
Guest User

Sqslfd*

a guest
Jul 11th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import socket
  4. import string
  5. import random
  6. import hashlib
  7. import time
  8. import sys
  9.  
  10.  
  11. def completeGamePacketEncrypt(buf,session,is_session_packet):
  12. session_number = (((session >> 6) & 0xFF) & 0x80000003)
  13.  
  14. if(session_number < 0):
  15. session_number = (((session_number - 1) | 0xFFFFFFFC) + 1)
  16. session_key = (session & 0xFF)
  17.  
  18. if(is_session_packet):
  19. session_number = -1
  20. if (session_number == 0):
  21. for i in range(len(buf)):
  22. buf[i] = (buf[i] + (session_key + 0x40))
  23.  
  24. elif(session_number == 1):
  25. for i in range(len(buf)):
  26. buf[i] = (buf[i] - (session_key + 0x40))
  27. elif(session_number == 2):
  28. for i in range(len(buf)):
  29. buf[i] = (buf[i] ^ 0xC3) + (session_key + 0x40)
  30. elif(session_number == 3):
  31. for i in range(len(buf)):
  32. buf[i] = (buf[i] ^ 0xC3) - (session_key + 0x40)
  33. else:
  34. for i in range(len(buf)):
  35. buf[i] = buf[i] + 0x0F
  36.  
  37.  
  38. def transform(chaine):
  39. final = ""
  40. for i in range (len(chaine)):
  41. c = chaine[i]
  42. if (c == '#'):
  43. final += '0'
  44. else:
  45. n = ord(c)
  46. #!(n-= 0x20)
  47. if(not(n - 0x20) or (n + 0xF1) < 0 or (n - 0xB) < 0 or not(n - 0xC5)):
  48. final += '1'
  49. else:
  50. final += '0'
  51. return final
  52.  
  53.  
  54.  
  55. def encryptworld(identifier, session):
  56. table = [83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
  57. 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
  58. 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
  59. 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
  60. 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
  61. 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
  62. 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
  63. 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
  64. 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
  65. 227, 228, 229, 230, 231, 232, 233, 234, 235, 236 ]
  66.  
  67. str_enc = chr(154) # 0x9A
  68.  
  69. temp = str(identifier[0])+str(identifier[1])
  70. str_enc += str(chr(table[int(temp)])); # example --> 0xA6
  71. temp = str(identifier[2])+str(identifier[3])
  72. str_enc += str(chr(table[int(temp)]));# example --> 0x84
  73.  
  74. switcher = {
  75. 0 :chr(80),
  76. 1 :chr(96),
  77. 2 :chr(112),
  78. 3 :chr(128),
  79. 4 :chr(144),
  80. 5 :chr(160),
  81. 6 :chr(176),
  82. 7 :chr(192),
  83. 8 :chr(208),
  84. 9 :chr(224)
  85. }
  86. str_enc+=str(switcher.get(int(identifier[4]), ""))
  87.  
  88. temp = str(session[0])+str(session[1])
  89. str_enc += str(chr(table[int(temp)])); # example --> 0x64
  90. temp = str(session[2])+str(session[3])
  91. str_enc += str(chr(table[int(temp)]));# example --> 0x86
  92.  
  93. switcher = {
  94. 0 :chr(79),
  95. 1 :chr(95),
  96. 2 :chr(111),
  97. 3 :chr(127),
  98. 4 :chr(143),
  99. 5 :chr(159),
  100. 6 :chr(175),
  101. 7 :chr(191),
  102. 8 :chr(207),
  103. 9 :chr(223)
  104. }
  105. str_enc+=str(switcher.get(int(session[4]), ""))
  106. str_enc+=str(chr(14))
  107. res = bytearray(str_enc)
  108. return res
  109.  
  110.  
  111.  
  112. def decryptLoginPacket(buf):
  113. tmp = ""
  114. for i in range(len(buf)):
  115. tmp += chr(ord(buf[i]) - 0xF)
  116. return tmp[:-1]
  117.  
  118. def encryptLoginPacket(buf):
  119.  
  120. return bytearray([((ord(x) ^ 0xC3) + 0xF) % 256 for x in buf + '\n'])
  121.  
  122. def genLoginPacket(username, password, game_version, game_hash):
  123. res = 'NoS0575 '
  124. res += str(random.randrange(10000, 8888888))
  125. res += ' '
  126. res += username
  127. res += ' '
  128. res += hashlib.sha512(password.encode('utf-8')).hexdigest().upper()
  129. res += ' 000'
  130. res += '027AF'
  131. res += '\x0b'
  132. res += game_version
  133. res += ' 0 '
  134. res += hashlib.md5((game_hash+username).encode('utf-8')).hexdigest().upper()
  135. return res
  136.  
  137. #NoS0575 665638 ssss 092BCBA17FDEE6FF9C3CE4262CD48BB3D9BFB042D8D14E2DC7B35A0EE3F92D0C040EA49A1820D6EA3F86EA159B8C2D3590ED7DF4FB4AC20BB0CE35BE6D80E1AB 009027AF 0.9.3.3088 0 5E242D4D5C6EBFB9176C887078084537
  138.  
  139. def send(nb,s,session,user,passwd):
  140.  
  141. packet = oldencrypt(str(session),int(session),True)
  142. s.send(packet)
  143. ''' packet = oldencrypt(user,int(session),False)
  144. s.send(packet)
  145. packet =oldencrypt(passwd,int(session),False)
  146. s.send(packet)'''
  147. time.sleep(0.1)
  148.  
  149. def oldencrypt(buf, session, is_session_packet):
  150. packet_length = len(buf)
  151. packet_mask = "11111" #transform(buf)
  152. print(packet_mask)
  153. output = []
  154. sequences = 0
  155. sequence_counter = 0
  156. last_position = 0
  157. current_position = 0
  158. length = 0
  159. current_byte = 0
  160. while current_position <= packet_length:
  161. last_position = current_position
  162. while (current_position < packet_length) and (packet_mask[current_position] == '0'):
  163. current_position += 1
  164.  
  165. if (current_position):
  166. length = (current_position - last_position)
  167. sequences = (length / 0x7E)
  168. for i in range (length):
  169. if (i == (sequence_counter * 0x7E)) :
  170. if not(sequences):
  171. output.append(length - i)
  172. else:
  173. output.append(0x7E)
  174. sequences-=1
  175. sequence_counter+=1
  176.  
  177. output.append(ord(buf[last_position]) ^ 0xFF)
  178.  
  179. last_position += 1
  180. if(current_position >= packet_length):
  181. break
  182. last_position = current_position
  183. while(current_position < packet_length) and (packet_mask[current_position] == '1'):
  184. current_position+=1
  185.  
  186. if(current_position):
  187. length = (current_position - last_position)
  188. sequences = (length / 0x7E)
  189. for i in range(length):
  190.  
  191. if(i == (sequence_counter * 0x7E)):
  192.  
  193. if(not(sequences)):
  194.  
  195. output.append((length - i) | 0x80)
  196. else:
  197.  
  198. output.append(0x7E | 0x80)
  199. sequences-=1
  200. sequence_counter+=1
  201.  
  202. current_byte = int(buf[last_position])
  203.  
  204. if (current_byte == 0x20):
  205. current_byte=1
  206. elif (current_byte == 0x2D):
  207. current_byte = 2
  208. elif (current_byte== 0x2E):
  209. current_byte = 3
  210. elif (current_byte== 0xFF):
  211. current_byte = 0xE
  212. else:
  213. print( "before: " + str(current_byte))
  214. current_byte-= 0x2C
  215. print("after: " + str(current_byte))
  216. if(current_byte != 0x00):
  217.  
  218. if(i % 2 == 0):
  219. output.append(current_byte << 4)
  220. #print("Modulo" + str(current_byte << 4))
  221. else:
  222. output[len(output)-1] = output[len(output)-1] | current_byte
  223. #output.back() |= current_byte;
  224.  
  225.  
  226. last_position +=1
  227. output.append(0xff)
  228. print("Before :" + str(list(output)))
  229. completeGamePacketEncrypt(output,session,is_session_packet)
  230. output = [x % 256 for x in output]
  231. print("After :" + str(list(output)))
  232. return bytearray(output)
  233.  
  234.  
  235. TCP_IP = 'login.nostale.gfsrv.net'
  236. TCP_PORT = 4002
  237. BUFFER_SIZE = 4096
  238.  
  239. USERNAME = 'socket1'
  240. PASSWORD = 'socket'
  241. GAME_VERSION = '0.9.3.3088'
  242. GAME_HASH = 'CDB86A1430332375FB8B05B1A5C45755F807636A5832DDF13143AF01CCCB1695' # md5(NostaleClientX.exe)+(NostaleClient.exe) : http://onlinemd5.com/
  243.  
  244. fpacket = genLoginPacket(USERNAME, PASSWORD, GAME_VERSION, GAME_HASH)
  245.  
  246. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  247. s.connect((TCP_IP, TCP_PORT))
  248. b = encryptLoginPacket(fpacket)
  249. #print(list(b))
  250. s.send(encryptLoginPacket(fpacket))
  251.  
  252. data = s.recv(BUFFER_SIZE)
  253. s.close()
  254.  
  255. login = decryptLoginPacket(data)
  256. #print("\nReceived Data: %s" % login)
  257. server3info =login.split(' ')[6] #failc4 = deja loggé
  258. debug = int(sys.argv[1])
  259. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  260.  
  261. if debug == 0:
  262. ip3=server3info.split(':')[0]
  263. session = login.split(' ')[2]
  264. port3=server3info.split(':')[1]
  265. print("IP: " + ip3 +"\n" + "PORT : " + port3 + "\nSession ID: " + str(session))
  266. s.connect((ip3, int(port3)))
  267. send(1,s,session,USERNAME,PASSWORD)
  268. print(s.recv(1024))
  269. s.close()
  270. elif debug == 1:
  271. ip3="192.168.1.27"
  272. port3=4033
  273. session = 33247
  274. print("IP: " + ip3 +"\n" + "PORT : " + str(port3) + "\nSession ID: " + str(session))
  275. s.connect((ip3, int(port3)))
  276. send(1,s,session,USERNAME,PASSWORD)
  277. print(s.recv(1024))
  278. s.close()
  279.  
  280. """
  281. print("IP: " + ip3 +"\n" + "PORT : " + str(port3) + "\nSession ID: " + str(session))
  282. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  283. s.connect((ip3, int(port3)))
  284. send(1,s,session,USERNAME,PASSWORD)
  285. print ("TEST : " + s.recv(1024))"""
  286. #data = s.recv() AFTER...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement