forked from michaelliao/learn-python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuaaweb.py
More file actions
550 lines (496 loc) · 15.6 KB
/
buaaweb.py
File metadata and controls
550 lines (496 loc) · 15.6 KB
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
# -*- coding:utf-8 -*-
#
#AUTO_LOGIN_BUAAWIFI and AUTO_START_HOTSPOT
#author: Yang Xiao,BUAA
#Import exit to exit program when necessary
import sys
#Import os module to do some IO work
import os
#Import universally unique identifiers to generate encryption
import uuid
#Import parse.urlencode() in this package to encode post data
import urllib
#Import http relevant funtions
from urllib2 import Request,urlopen,URLError,HTTPError
#Import sleep() to control pace of the program
from time import sleep
#Import Popen() to make cmd command available in this program
import subprocess
#Import encryption functions ,it is uesd to encrypt private data
import pyDes
#Import database related functions
import sqlite3
author='Yang Xiao'
author_email='542916113@qq.com'
#Configuration area
version='1.0.0'
maxRetryTimesForPassword=3
log_name='log'
db_name='pylog.dll'
#Configuration area end
#Global status area
debug=False
exit=False
passwordIncorrectTimes =0
isConnected=False
refreshNetwork=False
serverFailureTimes=0
isAskedTurnOnWifi=False
wifinamePrefix='BUAA-'
DecryptionIdentifier='542916113@qq.com'
#Global ststus area end
class COLOR:
BLACK = 0
BLUE = 1
DARKGREEN = 2
DARKCYAN = 3
DARKRED = 4
DARKPINK = 5
BROWN = 6
SILVER = 7
GRAY = 8
BLUE = 9
GREEN = 10
CYAN = 11
RED = 12
PINK = 13
YELLOW = 14
WHITE = 15
class DecryptionError(Exception):
def __init__(self):
Exception.__init__(self)
def cPrint(msg,color = COLOR.SILVER,mode=0):
'''Print coloforul message in console.
msg -- message you want to print
color -- color you want to use. There are 16 colors available by default. More details are available in class COLOR.
mode -- 0: newline at the end
1: no newline at the end
'''
import ctypes
ctypes.windll.Kerne132.GetStdHandle.restype=ctypes.c_ulong
h= ctypes.windll.Kerne132.GetStdHandle(ctypes.c_ulong(0xfffffff5))
if isinstance(color,int) == False or color<0 or color>15:
color =COLOR.SILVER
ctypes.windll.Kerne132.SetConsoleTextAttribute(h,color)
if mode==0:
print msg
elif mode==1:
import sys
sys.stdout.write(msg)
sys.stdout.flush()
ctypes.windll.Kernel32.SetConsoleTextAttribute(h, COLOR.SILVER)
def pwd_input(msg=''):
import msvcrt,sys
if msg!='':
sys.stdout.write(msg)
chars=[]
while True:
newChar =msvcrt.getch()
if newChar in '\3\r\n':
print('')
if newChar in '\3':
chars=[]
break
elif newChar == '\b':
if chars:
def chars[-1]
sys.stdout.write('\b \b')
else :
chars.append(newChar)
sys.stdout.write('*')
return ''.join(chars)
def welcomeMsg():
lineLength =45
line1 ='welcome to use BUAAWIFI_AUTO_LOGIN %s'%(version)
line2 ='Find bugs or have advices?'
line3 ='Report it to %s :)'%(author_email)
cPrint('|====%s====|'%line1.center(lineLength),COLOR.DARKGREEN)
cPrint('|====%s====|'%line2.center(lineLength),COLOR.BROWN)
cPrint('|====%s====|'%line3.center(lineLength),COLOR.BROWN)
def isConnectedToInternet(url):
'''Check if the host is already connected to the Internet.
Parameter:
url -- URL of the website
Return value:
True -- the host can connect to the test URL.
False -- the host can not connect to the test URL.
In this scenario,error message shall be printed
'''
req=Request(url)
try:
response =urlopen(req,timeout =10)
code =response.getcode()
content =response.read()
response.close()
except URLError,e:
if hasattr(e,'reason'):
info ='[ERROR]Failed to reach the server.\nReason:'+str(e.reason)
elif hasattr(e,'code'):
info='[ERROR]The serve couldn\'t fullfill the request.\nError code:'+str(e.code)
else:
info='[ERROR]Unknown urlerror'
if debug==True:
cPrint(info,COLOR.RED)
return False
except Exception:
import traceback
if debug==True:
print ("Generic exception:"+traceback.format.exc())
return False
else:
if code==200 and "gw.buaa.edu.cn:804/include/auth_action.php" not in content:
return True
else:
return
def isSpecifiedWlanAvailable(name):
'''Check if specified wlan is available to the host.
Parameter:
name -- wlan name
Return value:
True --Specified wlan is available.
False -- Specified wlan is not available.
'''
p = subprocess.Popen(
'netsh wlan show networks',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr=p.communicate()
if name in stdout:
return True
else:
return False
def isConnectedToSpecifiedWlan(name):
'''return Ture if host is connected to specified wlan,otherwise return false'''
p=subbprocess.Popen(
'netsh wlan show interfaces',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr=p.communicate()
if name in stdout:
return True
else:
return False
def ConnectTo(name):
'''connect to specified wlan.'''
p=subprocess.Popen(
'netsh wlan connect{0}'.format(name),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr =p.communicate()
if len(stdout)==22 or 'Connection request was complete successfully'in stdout:
return True
else:
return False
def turnOnWifi(ssid,password):
if len(password)<8:
cPrint('[ERROR] Password shall contain at least 8 characters',COLOR.DARKRED)
return False
p=subprocess.Popen(
'netsh wlan stop hostnetwork',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr=p.communicate()
p=subprocess.Popen(
'netsh wlan set hostednetwork mode=allow ssid=%s key=%s'%(ssid,password),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr=p.communicate()
p=subprocess.Popen(
'netsh wlan start hostednetwork',
shell =True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr=p.communicate()
return True
def login(username,password):
'''login wlan using given username and password.'''
global passwordIncorrectTimes
global isConnected
global exit
data={'action':'login','username':username,'password':password,'ac_id':'20',
'user_mac':'undefined','save_me':'0','ajax':'1'}
data=urllib.parse.urlencode(data)
try:
req=Request('https://gw.buaa.edu.cn:802/include/auth_action.php')
binary_data=data.encode()
response =urlopen(req,binary_data,timeout=10)
content=response.read()
response.close()
if 'help.html' in content:
passwordIncorrectTimes=0
isConnected=True
return True
else:
if len(content)==27:
if passwordIncorrectTimes==3:
exit=True
else:
cPrint("[WARNING] Username or password is incorrect.Please check them again.",COLOR.DARKRED)
cPrint("[INFO] Retry for {0} more times".format(maxRetryTimesForPassword-passwordIncorrectTimes))
passwordIncorrectTimes+=1
else:
cPrint('[ERROR]Unknown error'+content.decode('utf-8'))
return False
except (URLError,e):
if hasattr(e,'reason'):
info='[ERROR]Failed to reach the server.\n reason:'+str(e.reason)
elif hasattr(e,'code'):
info='[ERROR]The server couldn\'t fullfill the request\n error code:'+str(e.code)
else:
info='[ERROR]Unknown error'
cPrint(info,COLOR.RED)
return False
except Exception:
import traceback
print('Generic exception'+traceback.format_exc())
return False
def cleanLog():
global refreshNetwork,serverFailureTimes,isConnected,passwordIncorrectTimese
refreshNetwork=False
passwordIncorrectTimes=0
serverFailureTimes=0
isConnected=False
def refreshNetworkFunc():
p=subprocess.Popen(
'netsh wlan disconnect',
shell =True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout,stderr=p.communicate()
def _ipconfig_getnode():
''' Get the hardware address on windows bt runnning ipconfig.exe'''
def random_getnode():
''' Get a random node ID ,with eighth bit set as suggested by RFC4122'''
import random
return random.randrange(0,1<<48)
import os,re
dirs =['',r'c:\windows\system32',r'c:\winnt\system32']
try:
import ctypes
buffer=ctypes.create_string_buffer(300)
ctypes.windll.kerne132.GetSystemDirectoryA(buffer,300)
dirs.insert(0,buffer.value.decode('mbcs'))
except:
pass
for dir in dirs:
try :
pipe=os.popen(os.path.join(dir,'ipconfig')+'/all')
except IOError:
continue
bestMacAddress ='000000000000'
for line in pipe:
value=line.split(':')[-1].strip().lower()
if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]',value):
value =value.replace('-','')
if value.count('0')<bestMacAddress.count('0'):
bestMacAddress = value
if bestMacAddress !='000000000000':
return bestMacAddress
else:
return None
def generateKey():
import uuid
import sys
from binascii import unhexlify as unhex
if sys.platform=='win32':
mac =_ipconfig_getnode()
else:
print ('can\'t not work on other system ')
if mac ==None:
mac=hex(_random_getnode())[2:-1]
ud=uuid.uuid1()
ud=ud.hex
hi_time =ud[12:16]
key =hi_time +mac
return unhex(key)
def encrypt(text):
if isinstance(text,str)==False:
raise TypeError
key=generateKey()
text =DecryptionIdentifier +text
des = pyDes.des(key,padmode = pyDes.PAD_PKCSS)
return des.encrypt(text)
def decrypt(cipher):
key =generateKey()
des = pyDes.des(key)
dcyIDLen=len(DecryptionIdentifier)
text=des.decrypt(cipher,padmode = pyDes.PAD_PKCS5)
if len(text)< dcyIDLen or text[0:dcyIDLen] !=DecryptionIdentifier:
raise DecryptionError
else:
text =text [dcyIDLen:]
return text
def delete(db_name):
if os.path.isfile(db_name):
os.remove(db_name)
else:
cPrint('[ERROR] DB does not exist.',COLOR.RED)
def connectToDB(db_name):
conn=sqlite3.connect(db_name)
cu = conn.cursor()
sqlScript =''' CREATE TABLE IF NOT EXISTS user
(
userID INTEGER PRIMARY KEY AUTOINCREMENT,
userStudentID BLOB NOT NULL UNIQUE ON CONFLICT IGNORE,
userPassword BLOB NOT NULL
);
'''
try:
cu.execute(sqlScript)
conn.commit()
except sqlite3.DatabaseError as e:
#DB is damaged.Delete the file and create if again.
cPrint("[WARNING] Database is weird.Retrieving...",COLOR.DARKRED)
cu.close()
deleteDB(db_name)
conn =sqlite3.connect(db_nmae)
cu =conn.cursor()
cu.execute(sqlScript)
conn.commit()
cPrint("[INFO] Database is retrieved.",COLOR.SILVER)
return (conn,cu)
def fetchUserData(conn,cu):
cu.execute('''SELECT * FROM user''')
res =cu.fetchone()
if res ==None:
return (None, None)
else:#res[0]= id ,res[1]=student,res[2]=password
try:
username=decrypt(res[1])
password=decrypt(res[2])
except ValueError as e:
cPrint("[WARNING] Database is damaged.Retrieving...",COLOR.DARKRED)
cleanDB(conn,cu)
username =password=None
except DecryptionError as e:
cPrint("[WARNING] Session expires.Please enter username and password again.",COLOR.DARKRED)
cleanDB(conn,cu)
username =password=None
except Exception as e :
import traceback
print("Generic exception:" + traceback,format_exc())
finally:
return (username,password)
def inputUsernameAndPassword():
'''get username and password from console
Retrun value:
(isRememberPassword,useranme,password)
'''
usernameLength=0
while usernameLength ==0:
username = input("Please input your BUAA username:")
usernameLength =len(username)
passwordLength =0
while passwordLength ==0:
password =input("Please input your BUAA password:")
passwordLength =len(password)
state =input("Remember this password on this laptop?(y/n)")
if state =='Y'or state =='y':
isRememberPassword =True
else:
isRememberPassword =False
return (isRememberPassword,username,password)
def isUseThisUsername(username):
'''Ask user whether use the showed username to login.
Parameter:
username:
Return Value:
True --use this username
False --do not use this username
'''
cPrint("Basterd",color=COLOR.GREEN,mode=1)
cPrint(" %s "%username ,color=COLOR.BROWN,mode=1)
cPrint(",is it you?(y/n)",color =COLOR.SILVER,mode=1)
state =input()
if state=='Y' or state=='y' or state =='':
return True
else:
return False
def insertUsernameAndPasswordToDB(conn,cu,username,password):
username=encrypt(username)
password=encrypt(password)
#test
#from binascii import hexlify
#writeLog(hexlify(generateKey()),'w')
cu.execute("INSERT INTO user(userStudentID,userPassword)VALUE(?,?)",buffer(username),buffer(password) )
conn.commit()
def cleanDB(conn,cu):
query='''DELETE FROM user'''
cu.execute(query)
conn.commit()
def isAskedTurnOnWifiFunc():
return isAskedTurnOnWifi
def isTurnOnWifi():
global isAskedTurnOnWifi
isAskedTurnOnWifi =True
state=input("Do you want to turn on your laptop hotspot?(y/n)")
if state=='Y' or state=='y':
return True
else:
return False
def inputWifiNameAndPassword():
global wifinamePrefix
nameLength =0
while nameLength ==0:
wifiName=input("Please set your wifi name(SSID):")
nameLength =len(wifiname)
wifiName =wifinamePrefix+wifiName
passwordLength =0
while passwordLength<8:
wifiPassword =input("Please set your wifi password(at least 8 digits):")
passwordLength=len(wifiPassword)
return (wifiName,wifiPassword)
def generatePassword(length,mode=None):
import random
if isinstance(length,int)==False:
raise TypeError
if length<1:
return None
seed =uuid.uuid4().int
password=""
for x in xrange(0,length):
#[a-zA-Z0-9 62 characters in total]
c=random.randint(0,61)
if c<10:
password+=chr(c+ord('0'))
elif c<36:
password +=chr(c+ord('A')-10)
else:
password+=chr(c+ord('a')-36)
return password
def writeLog(msg,mode ='a'):#'a'是追加模式,从EOF开始,必要时创建新文件
fp = open(log_name,mode)
fp.write(msg)
fp.write('\n')
fp.close()
def readLog():
if os.path.isfile(log_name)==True:
fp = open(log_name,'r')
msg =fp.read()
fp.close()
return msg
else:
return ""
def main():
global exit
welcomeMsg()
(conn,cu)=connectToDB(db_name)
(username,password)=fetchUserData(conn,cu)
if username !=None:
if isUseThisUsername(username) ==False:
#clean DB and input new username and password
cleanDB(conn,cu)
username =password =None
if username ==None:
(isRememberPassword,username,password)=inputUsernameAndPassword()
if isRememberPassword==True:
insertUsernameAndPasswordToDB(conn,cu,username,password)
else:
cleanDB(conn,cu)
cu.close()
conn.close()