-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthenticateDevice.py
334 lines (260 loc) · 10.7 KB
/
AuthenticateDevice.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
from base64 import decode
from grab import Grab
import json
import logging
# done
LOGR = logging.getLogger(__name__)
class AuthenticateDevice:
"""
This class is used to authenticate with the Microsoft Graph API and Outlook
Attributes
----------
url : str
url of the current url
code : str
The code given by the device code
response : str
The current response of the grab object go command
tenant_id : str
The Directory (tenant) ID that is shown on the Microsoft Azure Project Page
Methods
-------
find_value_to_key
Given a target, find the value that is associated to it in the g.doc response
decode_response
Returns the utf-8 of the g.doc.body response
connect
Goes through authentication proccess
"""
def __init__(self, url, code, credential, tenant_id) -> None:
self.url = 'https://microsoft.com/devicelogin'
self.code = code
self.response = ""
self.credential = credential
self.tenant_id = tenant_id
self.connect()
def debug_print_code(self, g):
print("status code: " + str(g.doc.code))
def debug_print_response(self, g):
body = g.doc.body
body_as_string = body.decode("utf-8")
print(body_as_string)
def debug_print_url(self, g):
print("URL destination: " + g.doc.url)
def g_doc_to_json(self, g):
body_as_string = (g.doc.body).decode("utf-8")
j = json.loads(body_as_string)
return j
def find_value_to_key(self, g, target):
"""
Given a target, find the value that is associated to it in the g.doc response
Parameters
----------
g : Grab Object
target : str
The name of the keyword
"""
response = (g.doc.body).decode("utf-8")
is_ctx = False
if target == 'ctx':
is_ctx = True
findNext = True
displacer = 0
index = 0
while True:
index = (response).find(target, index + displacer)
if (index == -1):
#LOGR.exception('%s does not exist in the response from %s', target, g.doc.url)
raise Exception(target + " does not exist in the response from " + g.doc.url)
value_type = (response)[index + len(target)]
if (value_type == '\"'):
if ((response)[index + len(target) + 2: index + len(target) + 7] == "value"):
target = "value"
continue
start_index = index + len(target) + 3
break
elif (value_type == '='):
if (is_ctx == True and findNext == True):
findNext = False
continue
if ((response)[index + len(target) + 1] == "\""):
start_index = index + len(target) + 2
break
else:
start_index = index + len(target) + 1
break
if (displacer != 1):
displacer = displacer + 1
end_index = (response).find('\"', start_index)
return (response)[start_index:end_index]
def decode_response(self, g):
"""
Returns the utf-8 of the g.doc.body response
Parameters
----------
g : Grab Object
"""
return (g.doc.body).decode("utf-8")
def connect(self):
"""
Goes through authentication proccess
"""
g = Grab()
g.go(self.url)
self.response = self.decode_response(g)
canary = self.find_value_to_key(g, 'canary')
sessionId = self.find_value_to_key(g, 'sessionId')
#####################################################
url = "https://login.microsoftonline.com/common/oauth2/deviceauth?code=" + self.code
g.go(url)
self.response = self.decode_response(g)
#####################################################
url = ' https://login.microsoftonline.com/common/oauth2/deviceauth'
payload = {
'otc': self.code,
'canary': canary,
'flowToken': '',
'hpgrequestid': sessionId
}
g.go(url, post=payload)
self.response = self.decode_response(g)
sFT = self.find_value_to_key(g, 'sFT') # this is the flowToken
ctx = self.find_value_to_key(g, 'ctx')
#####################################################
url = 'https://login.microsoftonline.com/common/GetCredentialType?mkt=en-US'
payload = {
'country': 'US',
'flowToken': sFT,
'username': self.credential[0]
}
payload_as_json = json.dumps(payload)
g.go(url, post=payload_as_json)
self.response = self.decode_response(g)
#####################################################
# login
url = 'https://login.microsoftonline.com/' + self.tenant_id + '/login'
header = {
'Content-Type': 'application/x-www-form-urlencoded',
'DNT': 1,
'Host': 'login.microsoftonline.com',
'Origin': 'https://login.microsoftonline.com',
'Referer': 'https://login.microsoftonline.com/common/oauth2/deviceauth',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0'
}
payload = {
'i13': '0',
'login': self.credential[0],
'loginfmt': self.credential[0],
'type': '11',
'LoginOptions': '3',
'passwd': self.credential[1],
'ps': '2',
'canary': canary,
'ctx': ctx,
'hpgrequestid': sessionId,
'flowToken': sFT,
}
g.setup(headers=header)
g.go(url, post=payload)
self.response = self.decode_response(g)
#####################################################
g2 = Grab()
scope = self.find_value_to_key(g, 'scope')
response_mode = self.find_value_to_key(g, 'response_mode')
id_token_hint = self.find_value_to_key(g, 'id_token_hint')
response_type = self.find_value_to_key(g, 'response_type')
client_id = self.find_value_to_key(g, 'client_id')
redirect_uri = self.find_value_to_key(g, 'redirect_uri')
claims = self.find_value_to_key(g, 'claims')
client_request_id = self.find_value_to_key(g, 'client-request-id')
nonce = self.find_value_to_key(g, 'nonce')
ExternalClaimsProviderAuthorizeEndpointUri = self.find_value_to_key(g, 'ExternalClaimsProviderAuthorizeEndpointUri')
state = self.find_value_to_key(g, 'state')
flowToken = self.find_value_to_key(g, 'flowtoken')
canary = self.find_value_to_key(g, 'canary')
payload = [
('scope', scope),
('response_mode', response_mode),
('id_token_hint', id_token_hint),
('response_type', response_type),
('client_id', client_id),
('redirect_uri', redirect_uri),
('claims', claims),
('client-request-id', client_request_id),
('nonce', nonce),
('ExternalClaimsProviderAuthorizeEndpointUri', ExternalClaimsProviderAuthorizeEndpointUri),
('state', state),
('flowtoken', flowToken),
('canary', canary),
]
header = {
'Host': 'login.microsoftonline.com',
'Origin': 'https://login.microsoftonline.com',
'Referer': 'https://login.microsoftonline.com/common/login',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0'
}
g2.setup(headers=header)
g2.go('https://login.microsoftonline.com/federation/redirecttoexternalprovider', multipart_post=payload)
#####################################################
g2.submit()
#####################################################
g2.submit()
#####################################################
g2.submit()
# the response has _xsrf token
xsrf = self.find_value_to_key(g2, '_xsrf')
# The sid is created through a 302 redirect
start_index = (g2.doc.url).find('sid=')
sid = (g2.doc.url)[start_index + 4:]
exit_request_referrer = g2.doc.url
#####################################################
g2.submit()
prompt_response_as_json = self.g_doc_to_json(g2)
txid = prompt_response_as_json['response']['txid']
#####################################################
url = 'https://api-cd3ecedb.duosecurity.com/frame/v4/status'
header = {
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Host': 'api-cd3ecedb.duosecurity.com',
'Origin': 'https://api-cd3ecedb.duosecurity.com',
'Referer': exit_request_referrer,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0'
}
payload = {
'txid': txid,
'sid': sid
}
g2.go(url, post=payload)
#####################################################
g2.go(url, post=payload)
#####################################################
#exit
url = 'https://api-cd3ecedb.duosecurity.com/frame/v4/oidc/exit'
payload = [
('sid', sid),
('txid', txid),
('factor', 'Duo+Push'),
#('device_key', 'device_key'), # need to find device key instead of hardcode
('_xsrf', xsrf),
('dampen_choice', 'true')
]
g2.go(url, multipart_post=payload)
#####################################################
g2.submit()
#####################################################
ctx = self.find_value_to_key(g2, 'sCtx')
hpgrequestid = self.find_value_to_key(g2, 'sessionId')
flowToken = self.find_value_to_key(g2, 'sFT')
start_index = (self.decode_response(g2)).find('canary":"')
end_index = (self.decode_response(g2)).find('\"', start_index + 10)
canary = (self.decode_response(g2))[start_index + 9: end_index]
url = 'https://login.microsoftonline.com/appverify'
payload = [
('ContinueAuth', 'true'),
('ctx', ctx),
('hpgrequestid', hpgrequestid),
('flowToken', flowToken),
('canary', canary),
]
g2.go(url, multipart_post=payload)