-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkpoint.py
99 lines (84 loc) · 2.44 KB
/
checkpoint.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
import requests
import json
from rsa import encrypt
from binascii import hexlify
# disable insecure request warnings.
# thx http://stackoverflow.com/a/28002687
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class pubkey_t:
def __init__(self, n, e):
self.n = n;
self.e = e;
# shitty _encoding_
# original def goes something like this (js):
#
# if (value.length > 2)
# {
# var newPass = "";
# for (var j=value.length-2; j>=0; j=j-2)
# {
# newPass = newPass.concat(value.substr(j,2));
# }
# value = newPass;
# }
#
# TODO: i have NOT checked odd chars or sanity
def revStrEncode(s):
if (len(s) > 2):
s = "".join([s[i:i+2] for i in range(len(s)-2, -2, -2)])
return s
def login(regno, password):
BaseURL = 'https://192.168.10.3';
PortalMainURL = BaseURL + '/connect/PortalMain';
RSASettingsURL = BaseURL + '/connect/RSASettings';
GetStateAndViewURL = BaseURL + '/connect/GetStateAndView';
LoginURL = BaseURL + '/connect/Login';
s = requests.Session()
rsa = None
snv = None
## get initial nacsid
# optional
# r = s.get(PortalMainURL, verify=False);
# assert r.status_code == 200, "status code %d" % r.status_code;
## get rsasettings
r = s.get(RSASettingsURL, verify=False);
assert r.status_code == 200, "status code %d" % r.status_code;
try:
rsa = r.json()
except ValueError:
return "RSASettings decode error"
## make sure we're at auth?
r = s.get(GetStateAndViewURL, verify=False);
try:
snv = r.json()
except ValueError:
return "StateAndView decode error"
# assert auth?
# optional
#assert snv['view'] == 'Authentication', "view [%s] != 'Authentication'" % snv['view']
## auth
# maketh pubkey from RSASettings
n = int("0x" + rsa['m'], 16);
e = int("0x" + rsa['e'], 16);
pubkey = pubkey_t(n, e);
# doeth teh _encryption_
plaintext = rsa['loginToken'] + password;
encrypted = encrypt(plaintext.encode('utf-8'), pubkey);
encryptedhex = hexlify(encrypted);
encryptedhexEncoded = revStrEncode(encryptedhex.decode('utf-8'))
payload = {
'realm': 'passwordRealm',
'username': regno,
'password': encryptedhexEncoded
};
# meh.
r = s.post(LoginURL, data=payload, verify=False);
# validate snv
r = s.get(GetStateAndViewURL, verify=False);
try:
snv = r.json()
except ValueError:
return "StateAndView decode error"
assert snv['view'] == 'Final', "view [%s] != 'Final'; auth failure" % snv['view']
return "ok"