-
Notifications
You must be signed in to change notification settings - Fork 10
/
latest-wired.py
executable file
·574 lines (535 loc) · 18.2 KB
/
latest-wired.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import struct
import time
import hashlib
import sys
import os
import random
import traceback
import re
keep_alive1_mod = False #If you have trouble at KEEPALIVE1, turn this value to True
nic_name = '' #Indicate your nic, e.g. 'eth0.2'.nic_name
bind_ip = '0.0.0.0'
class ChallengeException (Exception):
def __init__(self):
pass
class LoginException (Exception):
def __init__(self):
pass
def bind_nic():
try:
import fcntl
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
return get_ip_address(nic_name)
except ImportError as e:
print('Indicate nic feature need to be run under Unix based system.')
return '0.0.0.0'
except IOError as e:
print(nic_name + 'is unacceptable !')
return '0.0.0.0'
finally:
return '0.0.0.0'
if nic_name != '':
bind_ip = bind_nic()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((bind_ip, 61440))
s.settimeout(3)
SALT = ''
IS_TEST = False
# specified fields based on version
CONF = "/etc/drcom.conf"
UNLIMITED_RETRY = True
EXCEPTION = False
DEBUG = False #log saves to file
LOG_PATH = '/var/log/drcom_client.log'
if IS_TEST:
DEBUG = True
LOG_PATH = 'drcom_client.log'
regex = re.compile('bcecb2e2b7a2cfd620((3[0-9])+)20b4ce2c20((3[0-9])+)20b4cebaf3b4a6c0ed')
def log(*args, **kwargs):
s = ' '.join(args)
print(s)
if DEBUG:
with open(LOG_PATH,'a') as f:
f.write(s + '\n')
kicking_search = regex.search(s)
if kicking_search and kicking_search.group(1) == kicking_search.group(3):
log('[auto-relogin] Need to relogin now, or will be kicked!')
sys.exit(1)
def challenge(svr,ran):
while True:
t = struct.pack("<H", int(ran)%(0xFFFF))
s.sendto("\x01\x02"+t+"\x09"+"\x00"*15, (svr, 61440))
try:
data, address = s.recvfrom(1024)
log('[challenge] recv',data.encode('hex'))
except:
log('[challenge] timeout, retrying...')
continue
if address == (svr, 61440):
break
else:
continue
log('[DEBUG] challenge:\n' + data.encode('hex'))
if data[0] != '\x02':
raise ChallengeException
log('[challenge] challenge packet sent.')
return data[4:8]
def md5sum(s):
m = hashlib.md5()
m.update(s)
return m.digest()
def dump(n):
s = '%x' % n
if len(s) & 1:
s = '0' + s
return s.decode('hex')
def ror(md5, pwd):
ret = ''
for i in range(len(pwd)):
x = ord(md5[i]) ^ ord(pwd[i])
ret += chr(((x<<3)&0xFF) + (x>>5))
return ret
def gen_crc(data, encrypt_type):
DRCOM_DIAL_EXT_PROTO_CRC_INIT = 20000711
ret = ''
if encrypt_type == 0:
# ���ܷ�ʽ��
return struct.pack('<I',DRCOM_DIAL_EXT_PROTO_CRC_INIT) + struct.pack('<I',126)
elif encrypt_type == 1:
# ���ܷ�ʽΪ md5
foo = hashlib.md5(data).digest()
ret += foo[2]
ret += foo[3]
ret += foo[8]
ret += foo[9]
ret += foo[5]
ret += foo[6]
ret += foo[13]
ret += foo[14]
return ret
elif encrypt_type == 2:
# md4
foo = hashlib.new('md4', data).digest()
ret += foo[1]
ret += foo[2]
ret += foo[8]
ret += foo[9]
ret += foo[4]
ret += foo[5]
ret += foo[11]
ret += foo[12]
return ret
elif encrypt_type == 3:
# sha1
foo = hashlib.sha1(data).digest()
ret += foo[2]
ret += foo[3]
ret += foo[9]
ret += foo[10]
ret += foo[5]
ret += foo[6]
ret += foo[15]
ret += foo[16]
return ret
def keep_alive_package_builder(number,random,tail,type=1,first=False):
data = '\x07'+ chr(number) + '\x28\x00\x0b' + chr(type)
if first :
data += '\x0f\x27'
else:
data += KEEP_ALIVE_VERSION
data += '\x2f\x12' + '\x00' * 6
data += tail
data += '\x00' * 4
#data += struct.pack("!H",0xdc02)
if type == 3:
foo = ''.join([chr(int(i)) for i in host_ip.split('.')]) # host_ip
#CRC
# edited on 2014/5/12, filled zeros to checksum
# crc = packet_CRC(data+foo)
crc = '\x00' * 4
#data += struct.pack("!I",crc) + foo + '\x00' * 8
data += crc + foo + '\x00' * 8
else: #packet type = 1
data += '\x00' * 16
return data
# def packet_CRC(s):
# ret = 0
# for i in re.findall('..', s):
# ret ^= struct.unpack('>h', i)[0]
# ret &= 0xFFFF
# ret = ret * 0x2c7
# return ret
def keep_alive2(*args):
#first keep_alive:
#number = number (mod 7)
#status = 1: first packet user sended
# 2: first packet user recieved
# 3: 2nd packet user sended
# 4: 2nd packet user recieved
# Codes for test
tail = ''
packet = ''
svr = server
ran = random.randint(0,0xFFFF)
ran += random.randint(1,10)
# 2014/10/15 add by latyas, maybe svr sends back a file packet
svr_num = 0
packet = keep_alive_package_builder(svr_num,dump(ran),'\x00'*4,1,True)
while True:
log('[keep-alive2] send1',packet.encode('hex'))
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
log('[keep-alive2] recv1',data.encode('hex'))
if data.startswith('\x07\x00\x28\x00') or data.startswith('\x07' + chr(svr_num) + '\x28\x00'):
break
elif data[0] == '\x07' and data[2] == '\x10':
log('[keep-alive2] recv file, resending..')
svr_num = svr_num + 1
# packet = keep_alive_package_builder(svr_num,dump(ran),'\x00'*4,1, False)
break
else:
log('[keep-alive2] recv1/unexpected',data.encode('hex'))
#log('[keep-alive2] recv1',data.encode('hex'))
ran += random.randint(1,10)
packet = keep_alive_package_builder(svr_num, dump(ran),'\x00'*4,1,False)
log('[keep-alive2] send2',packet.encode('hex'))
s.sendto(packet, (svr, 61440))
while True:
data, address = s.recvfrom(1024)
if data[0] == '\x07':
svr_num = svr_num + 1
break
else:
log('[keep-alive2] recv2/unexpected',data.encode('hex'))
log('[keep-alive2] recv2',data.encode('hex'))
tail = data[16:20]
ran += random.randint(1,10)
packet = keep_alive_package_builder(svr_num,dump(ran),tail,3,False)
log('[keep-alive2] send3',packet.encode('hex'))
s.sendto(packet, (svr, 61440))
while True:
data, address = s.recvfrom(1024)
if data[0] == '\x07':
svr_num = svr_num + 1
break
else:
log('[keep-alive2] recv3/unexpected',data.encode('hex'))
log('[keep-alive2] recv3',data.encode('hex'))
tail = data[16:20]
log("[keep-alive2] keep-alive2 loop was in daemon.")
i = svr_num
while True:
try:
time.sleep(20)
keep_alive1(*args)
ran += random.randint(1,10)
packet = keep_alive_package_builder(i,dump(ran),tail,1,False)
#log('DEBUG: keep_alive2,packet 4\n',packet.encode('hex'))
log('[keep_alive2] send',str(i),packet.encode('hex'))
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
log('[keep_alive2] recv',data.encode('hex'))
tail = data[16:20]
#log('DEBUG: keep_alive2,packet 4 return\n',data.encode('hex'))
ran += random.randint(1,10)
packet = keep_alive_package_builder(i+1,dump(ran),tail,3,False)
#log('DEBUG: keep_alive2,packet 5\n',packet.encode('hex'))
s.sendto(packet, (svr, 61440))
log('[keep_alive2] send',str(i+1),packet.encode('hex'))
data, address = s.recvfrom(1024)
log('[keep_alive2] recv',data.encode('hex'))
tail = data[16:20]
#log('DEBUG: keep_alive2,packet 5 return\n',data.encode('hex'))
i = (i+2) % 0xFF
except:
log('[keep_alive2] error', 'raise Exception to main()')
raise
def checksum(s):
ret = 1234
x = 0
for i in [x*4 for x in range(0, -(-len(s)//4))]:
ret ^= int(s[i:i+4].ljust(4, '\x00')[::-1].encode('hex'), 16)
ret = (1968 * ret) & 0xffffffff
return struct.pack('<I', ret)
def mkpkt(salt, usr, pwd, mac):
'''
struct _tagLoginPacket
{
struct _tagDrCOMHeader Header;
unsigned char PasswordMd5[MD5_LEN];
char Account[ACCOUNT_MAX_LEN];
unsigned char ControlCheckStatus;
unsigned char AdapterNum;
unsigned char MacAddrXORPasswordMD5[MAC_LEN];
unsigned char PasswordMd5_2[MD5_LEN];
unsigned char HostIpNum;
unsigned int HostIPList[HOST_MAX_IP_NUM];
unsigned char HalfMD5[8];
unsigned char DogFlag;
unsigned int unkown2;
struct _tagHostInfo HostInfo;
unsigned char ClientVerInfoAndInternetMode;
unsigned char DogVersion;
};
'''
data = '\x03\x01\x00' + chr(len(usr) + 20)
data += md5sum('\x03\x01' + salt + pwd)
data += usr.ljust(36, '\x00')
data += CONTROLCHECKSTATUS
data += ADAPTERNUM
data += dump(int(data[4:10].encode('hex'),16)^mac).rjust(6, '\x00') #mac xor md51
data += md5sum("\x01" + pwd + salt + '\x00' * 4) #md52
data += '\x01' # number of ip
data += ''.join([chr(int(i)) for i in host_ip.split('.')]) #x.x.x.x ->
data += '\00' * 4 #your ipaddress 2
data += '\00' * 4 #your ipaddress 3
data += '\00' * 4 #your ipaddress 4
data += md5sum(data + '\x14\x00\x07\x0B')[:8] #md53
data += IPDOG
data += '\x00'*4 # unknown2
'''
struct _tagOSVERSIONINFO
{
unsigned int OSVersionInfoSize;
unsigned int MajorVersion;
unsigned int MinorVersion;
unsigned int BuildNumber;
unsigned int PlatformID;
char ServicePack[128];
};
struct _tagHostInfo
{
char HostName[HOST_NAME_MAX_LEN];
unsigned int DNSIP1;
unsigned int DHCPServerIP;
unsigned int DNSIP2;
unsigned int WINSIP1;
unsigned int WINSIP2;
struct _tagDrCOM_OSVERSIONINFO OSVersion;
};
'''
data += host_name.ljust(32, '\x00') # _tagHostInfo.HostName
data += ''.join([chr(int(i)) for i in PRIMARY_DNS.split('.')]) # _tagHostInfo.DNSIP1
data += ''.join([chr(int(i)) for i in dhcp_server.split('.')]) # _tagHostInfo.DHCPServerIP
data += '\x00\x00\x00\x00' # _tagHostInfo.DNSIP2
data += '\x00' * 4 # _tagHostInfo.WINSIP1
data += '\x00' * 4 # _tagHostInfo.WINSIP2
data += '\x94\x00\x00\x00' # _tagHostInfo.OSVersion.OSVersionInfoSize
data += '\x05\x00\x00\x00' # _tagHostInfo.OSVersion.MajorVersion
data += '\x01\x00\x00\x00' # _tagHostInfo.OSVersion.MinorVersion
data += '\x28\x0A\x00\x00' # _tagHostInfo.OSVersion.BuildNumber
data += '\x02\x00\x00\x00' # _tagHostInfo.OSVersion.PlatformID
# _tagHostInfo.OSVersion.ServicePack
data += host_os.ljust(32, '\x00')
data += '\x00' * 96
# END OF _tagHostInfo
data += AUTH_VERSION
if ror_version:
'''
struct _tagLDAPAuth
{
unsigned char Code;
unsigned char PasswordLen;
unsigned char Password[MD5_LEN];
};
'''
data += '\x00' # _tagLDAPAuth.Code
data += chr(len(pwd)) # _tagLDAPAuth.PasswordLen
data += ror(md5sum('\x03\x01' + salt + pwd), pwd) # _tagLDAPAuth.Password
'''
struct _tagDrcomAuthExtData
{
unsigned char Code;
unsigned char Len;
unsigned long CRC;
unsigned short Option;
unsigned char AdapterAddress[MAC_LEN];
};
'''
data += '\x02' # _tagDrcomAuthExtData.Code
data += '\x0C' # _tagDrcomAuthExtData.Len
data += checksum(data + '\x01\x26\x07\x11\x00\x00' + dump(mac)) # _tagDrcomAuthExtData.CRC
data += '\x00\x00' # _tagDrcomAuthExtData.Option
data += dump(mac) # _tagDrcomAuthExtData.AdapterAddress
# END OF _tagDrcomAuthExtData
if ror_version:
data += '\x00' * (8 - len(pwd))
if len(pwd)%2:
data += '\x00'
else:
data += '\x00' # auto logout / default: False
data += '\x00' # broadcast mode / default : False
data += '\xE9\x13' #unknown, filled numbers randomly =w=
log('[mkpkt]',data.encode('hex'))
return data
def login(usr, pwd, svr):
global SALT
global AUTH_INFO
i = 0
timeoutcount = 0
while True:
salt = challenge(svr,time.time()+random.randint(0xF,0xFF))
SALT = salt
packet = mkpkt(salt, usr, pwd, mac)
log('[login] send',packet.encode('hex'))
s.sendto(packet, (svr, 61440))
log('[login] packet sent.')
try:
data, address = s.recvfrom(1024)
log('[login] recv',data.encode('hex'))
if address == (svr, 61440) :
if data[0] == '\x04':
log('[login] loged in')
AUTH_INFO = data[23:39]
break
else:
log('[login] login failed.')
if IS_TEST:
time.sleep(3)
else:
time.sleep(30)
continue
else:
if i >= 5 and UNLIMITED_RETRY == False :
log('[login] exception occured.')
sys.exit(1)
else:
i += 1
continue
except socket.timeout as e:
print(e)
log('[login] recv timeout.')
timeoutcount += 1
if timeoutcount >= 5:
log('[login] recv timeout exception occured 5 times.')
sys.exit(1)
else:
continue
log('[login] login sent')
#0.8 changed:
return data[23:39]
#return data[-22:-6]
def logout(usr, pwd, svr, mac, auth_info):
salt = challenge(svr, time.time()+random.randint(0xF, 0xFF))
if salt:
data = '\x06\x01\x00' + chr(len(usr) + 20)
data += md5sum('\x03\x01' + salt + pwd)
data += usr.ljust(36, '\x00')
data += CONTROLCHECKSTATUS
data += ADAPTERNUM
data += dump(int(data[4:10].encode('hex'),16)^mac).rjust(6, '\x00')
# data += '\x44\x72\x63\x6F' # Drco
data += auth_info
s.send(data)
data, address = s.recvfrom(1024)
if data[:1] == '\x04':
log('[logout_auth] logouted.')
def keep_alive1(salt,tail,pwd,svr):
if keep_alive1_mod:
res=''
while True:
s.sendto('\x07' + struct.pack('!B',int(time.time())%0xFF) + '\x08\x00\x01\x00\x00\x00', (svr, 61440))
log('[keep_alive1_challenge] keep_alive1_challenge packet sent.')
try:
res, address = s.recvfrom(1024)
log('[keep_alive1_challenge] recv', res.encode('hex'))
except:
log('[keep_alive1_challenge] timeout, retrying...')
continue
if address == (svr, 61440):
if res[0] == '\x07':
break
else:
raise ChallengeException
else:
continue
seed = res[8:12]
# encrypt_type = int(res[5].encode('hex'))
encrypt_type = struct.unpack('<I', seed)[0] & 3
crc = gen_crc(seed, encrypt_type)
data = '\xFF' + '\x00'*7 + seed + crc + tail + struct.pack('!H', int(time.time())%0xFFFF)
log('[keep_alive1] send', data.encode('hex'))
s.sendto(data, (svr, 61440))
while True:
try:
res, address = s.recvfrom(1024)
if res[0] == '\x07':
break
else:
log('[keep-alive1]recv/not expected', res.encode('hex'))
except:
log('[keep-alive1] error')
raise
log('[keep-alive1]recv', res.encode('hex'))
else:
foo = struct.pack('!H',int(time.time())%0xFFFF)
data = '\xff' + md5sum('\x03\x01'+salt+pwd) + '\x00\x00\x00'
data += tail
data += foo + '\x00\x00\x00\x00'
log('[keep_alive1] send',data.encode('hex'))
s.sendto(data, (svr, 61440))
while True:
try:
data, address = s.recvfrom(1024)
if data[0] == '\x07':
break
else:
log('[keep-alive1]recv/not expected',data.encode('hex'))
except:
log('[keep_alive1] error', 'raise Exception to main() or keep_alive2()')
raise
log('[keep-alive1] recv', data.encode('hex'))
def empty_socket_buffer():
#empty buffer for some fucking schools
log('starting to empty socket buffer')
try:
while True:
data, address = s.recvfrom(1024)
log('recived sth unexpected',data.encode('hex'))
if s == '':
break
except:
# get exception means it has done.
log('exception in empty_socket_buffer')
pass
log('emptyed')
def daemon():
with open('/var/run/jludrcom.pid','w') as f:
f.write(str(os.getpid()))
def main():
if not IS_TEST:
daemon()
execfile(CONF, globals())
log("auth svr: " + server + "\nusername: " + username + "\npassword: " + password + "\nmac: " + str(hex(mac))[:-1])
log("bind ip: " + bind_ip)
while True:
try:
package_tail = login(username, password, server)
except LoginException:
continue
log('package_tail',package_tail.encode('hex'))
#keep_alive1 is fucking bullshit!
empty_socket_buffer()
try:
keep_alive1(SALT,package_tail,password,server)
keep_alive2(SALT,package_tail,password,server)
except Exception as e:
print(e)
log('[main] error', 'something was wrong in keep_alives, do everything again')
log(traceback.format_exc())
time.sleep(3)
#empty socket buffer before relogin
empty_socket_buffer()
continue
if __name__ == "__main__":
main()