-
Notifications
You must be signed in to change notification settings - Fork 65
/
client.py
283 lines (254 loc) · 8.17 KB
/
client.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
import socket, struct, time
from hashlib import md5
import sys
import urllib2
class ChallengeException (Exception):
def __init__(self):
pass
class LoginException (Exception):
def __init__(self):
pass
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("0.0.0.0", 61440))
s.settimeout(3)
SALT = ''
UNLIMITED_RETRY = True
EXCEPTION = False
DEBUG = True
server = "10.100.61.3" # "auth.jlu.edu.cn"
username = "YOURUSERNAME"
password = "YOURPASSWORD"
host_name = "LIYUANYUAN"
host_os = "8089D"
mac = 0xffffffffffff
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)
except:
if DEBUG:
print '[challenge] timeout, retrying...'
continue
if address == (svr, 61440):
break;
else:
continue
if DEBUG:
print '[DEBUG] challenge:\n' + data.encode('hex')
if data[0] != '\x02':
raise ChallengeException
print '[challenge] challenge packet sent.'
return data[4:8]
def md5sum(s):
m = 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 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 += '\xdc\02'
data += random + '\x00' * 6
data += tail
data += '\x00' * 4
#data += struct.pack("!H",0xdc02)
if type == 3:
foo = '\x31\x8c\x21\x3e' #CONSTANT
#CRC
crc = packet_CRC(data+foo)
data += struct.pack("!I",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():
#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
import random
ran = random.randint(0,0xFFFF)
ran += random.randint(1,10)
packet = keep_alive_package_builder(0,dump(ran),'\x00'*4,1,True)
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
ran += random.randint(1,10)
packet = keep_alive_package_builder(1,dump(ran),'\x00'*4,1,False)
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
tail = data[16:20]
ran += random.randint(1,10)
packet = keep_alive_package_builder(2,dump(ran),tail,3,False)
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
tail = data[16:20]
print "[keep-alive2] keep-alive2 loop was in daemon."
i = 1
while True:
try:
time.sleep(5)
ran += random.randint(1,10)
packet = keep_alive_package_builder(2,dump(ran),tail,1,False)
#print 'DEBUG: keep_alive2,packet 4\n',packet.encode('hex')
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
tail = data[16:20]
#print 'DEBUG: keep_alive2,packet 4 return\n',data.encode('hex')
ran += random.randint(1,10)
packet = keep_alive_package_builder(2,dump(ran),tail,3,False)
#print 'DEBUG: keep_alive2,packet 5\n',packet.encode('hex')
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
tail = data[16:20]
#print 'DEBUG: keep_alive2,packet 5 return\n',data.encode('hex')
i = i+1
i = i % (0xFF)
check_online = urllib2.urlopen('http://10.100.61.3')
foo = check_online.read()
if 'login.jlu.edu.cn' in foo:
print '[keep_alive2] offline.relogin...'
break;
#MODIFIED END
'''
if i % 10 == 0:
check_online = urllib2.urlopen('http://10.100.61.3')
foo = check_online.read()
if 'login.jlu.edu.cn' in foo:
print '[keep_alive2] offline.relogin...'
break;
'''
except:
pass
import re
def checksum(s):
ret = 1234
for i in re.findall('....', s):
ret ^= int(i[::-1].encode('hex'), 16)
ret = (1968 * ret) & 0xffffffff
return struct.pack('<I', ret)
def mkpkt(salt, usr, pwd, mac):
data = '\x03\01\x00'+chr(len(usr)+20)
data += md5sum('\x03\x01'+salt+pwd)
data += usr.ljust(36, '\x00')
data += '\x00\x00'
data += dump(int(data[4:10].encode('hex'),16)^mac).rjust(6,'\x00')
data += md5sum("\x01" + pwd + salt + '\x00'*4)
data += '\x01\x31\x8c\x31\x4e' + '\00'*12
data += md5sum(data + '\x14\x00\x07\x0b')[:8] + '\x01'+'\x00'*4
data += host_name.ljust(71, '\x00')
data += '\x01' + host_os.ljust(128, '\x00')
data += '\x6d\x00\x00'+chr(len(pwd))
data += ror(md5sum('\x03\x01'+salt+pwd), pwd)
data += '\x02\x0c'
data += checksum(data+'\x01\x26\x07\x11\x00\x00'+dump(mac))
data += "\x00\x00" + dump(mac)
return data
def login(usr, pwd, svr):
import random
global SALT
i = 0
while True:
try:
try:
salt = challenge(svr,time.time()+random.randint(0xF,0xFF))
except ChallengeException:
if DEBUG:
print 'challenge packet exception'
continue
SALT = salt
packet = mkpkt(salt, usr, pwd, mac)
s.sendto(packet, (svr, 61440))
data, address = s.recvfrom(1024)
except:
print "[login] recvfrom timeout,retrying..."
continue
print '[login] packet sent.'
if address == (svr, 61440):
if data[0] == '\x05' and i >= 5 and UNLIMITED_RETRY == False:
print '[login] wrong password, retried ' + str(i) +' times.'
sys.exit(1)
elif data[0] == '\x05':
print "[login] wrong password."
i = i + 1
time.sleep(i*1.618)
elif data[0] != '\x02':
print "[login] server return exception.retry"
if DEBUG:
print '[login] last packet server returned:\n' + data.encode('hex')
time.sleep(1)
raise LoginException
continue;
break;
else:
if i >= 5 and UNLIMITED_RETRY == False :
print '[login] packet received error, maybe you are under attacking'
sys.exit(1)
else:
i = i + 1
print '[login] package error, retrying...'
print '[login] login sent'
return data[-22:-6]
import httplib
def info(ip):
c = httplib.HTTPConnection(ip, 80, timeout=10)
c.request("GET", "")
r = c.getresponse()
if r.status != 200:
return None
s = r.read()
data = dict()
data["flux"] = int(s[s.index("flow='")+6:s.index("';fsele=")])
data["time"] = int(s[s.index("time='")+6:s.index("';flow")])
return data
def keep_alive1(salt,tail,pwd,svr):
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'
print '[keep_alive1] keep_alive1,sent'
s.sendto(data, (svr, 61440))
try:
data, address = s.recvfrom(1024)
print '[keep_alive1] keep_alive1,server received'
except:
print '[keep_alive1] Timeout!'
pass
def main():
print "auth svr:"+server+"\nusername:"+username+"\npassword:"+"********"+"\nmac:"+str(hex(mac))
print "os:MSDOS 8.0"+"\nhostname: localhost"
print "DrCOM Auth Router Ver 1.2"
print "Version feature:\n[1] Auto Anti droping connection\n[2] Stronger exception handling."
while True:
try:
package_tail = login(username, password, server)
except LoginException:
continue
keep_alive2()
if __name__ == "__main__":
main()