-
Notifications
You must be signed in to change notification settings - Fork 0
/
azrestarn.py
executable file
·411 lines (381 loc) · 17.1 KB
/
azrestarn.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
#!/usr/bin/python3
import requests
import argparse
import json
import sys
import os
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
parser = argparse.ArgumentParser(description='azrestarn.py')
parser.add_argument("--proxy", required=False, action='store_true')
parser.add_argument("--login", required=False, action='store_true')
parser.add_argument("--refresh", required=False, action='store_true')
parser.add_argument("--bitlocker", required=False, action='store_true')
parser.add_argument("--computername", required=False, type=str)
parser.add_argument("--domain", required=False, type=str)
parser.add_argument("--checkbestprac", required=False, action='store_true')
parser.add_argument("--me", required=False,action='store_true')
parser.add_argument("--objectid", required=False, type=str)
parser.add_argument("--owneddevices", required=False, action='store_true')
parser.add_argument("--dynamicgroups", required=False, action='store_true')
parser.add_argument("--invite", required=False, action='store_true')
parser.add_argument("--email", required=False, type=str)
parser.add_argument("--dispname", required=False, type=str)
parser.add_argument("--inviteurl", required=False, type=str)
parser.add_argument("--invitedusers", required=False, action='store_true')
parser.add_argument("--invitedelete", required=False, action='store_true')
parser.add_argument("--inviteid", required=False, type=str)
parser.add_argument("--getgrouproles", required=False, type=str)
parser.add_argument("--getuser", required=False, type=str)
parser.add_argument("--getgroup", required=False, action='store_true')
parser.add_argument("--approle", required=False, action='store_true')
parser.add_argument("--memberof", required=False, action='store_true')
parser.add_argument("--groupsettings", required=False, action='store_true')
parser.add_argument("--getmemberobjects", required=False, type=str)
args = parser.parse_args()
proxy = args.proxy
login = args.login
refresh = args.refresh
bitlocker = args.bitlocker
computername = args.computername
domain = args.domain
checkbestprac = args.checkbestprac
me = args.me
objectid = args.objectid
owneddevices = args.owneddevices
dynamicgroups = args.dynamicgroups
invite = args.invite
email = args.email
dispname = args.dispname
inviteurl = args.inviteurl
invitedusers = args.invitedusers
invitedelete = args.invitedelete
inviteid = args.inviteid
getgrouproles = args.getgrouproles
getuser = args.getuser
getgroup = args.getgroup
approle = args.approle
memberof = args.memberof
groupsettings = args.groupsettings
getmemberobjects = args.getmemberobjects
if proxy:
proxies = {
"http" : "http://127.0.0.1:8080",
"https" : "http://127.0.0.1:8080"
}
else:
proxies = None
def _mslogin_devicecode():
scope = "https://graph.microsoft.com"
url = "https://login.microsoftonline.com/common/oauth2/devicecode?api-version=1.0"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "close"
}
data = {
"client_id": "1950a258-227b-4e31-a9cf-717495945fc2",
"resource": scope
}
response = requests.post(url, headers=headers, data=data, proxies=proxies, verify=False)
jsonResponse = response.json()
device_code = jsonResponse["device_code"]
print("[+] Fetching AzureAD PRT token. Will be valid for 14d", "open https://microsoft.com/devicelogin and use code:",jsonResponse["user_code"],"\n")
input("[+] Press any key when authentication is complete...\n")
url_code = "https://login.microsoftonline.com/Common/oauth2/token?api-version=1.0"
headers_code = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "close"
}
data_code = {
"client_id": "1950a258-227b-4e31-a9cf-717495945fc2",
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"code": jsonResponse["device_code"]
}
response_code = requests.post(url_code, headers=headers_code, data=data_code, proxies=proxies, verify=False)
jsonResponse_code = response_code.json()
if not os.path.exists('.azrestarn_auth.json'):
template = {'prt': '', 'https://graph.windows.net/.default': '', 'https://graph.microsoft.com/.default': ''}
with open(".azrestarn_auth.json", "w") as outfile:
json.dump(template, outfile)
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
tokens = {
"prt": jsonResponse_code["refresh_token"]
}
with open(".azrestarn_auth.json", "w") as outfile:
dic.update(tokens)
json.dump(dic, outfile)
def _mslogin_refresh(domain,scope):
if domain == None:
print("[+] Please specify --domain parameter. Ex ecorp.com or ecorp.onmicrosoft.com")
sys.exit(1)
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
prt = dic["prt"]
url = "https://login.microsoftonline.com/{}/oauth2/v2.0/token".format(domain)
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "close"
}
data = {
"client_id": "1950a258-227b-4e31-a9cf-717495945fc2",
"grant_type": "refresh_token",
"refresh_token": prt,
"scope": scope
}
response = requests.post(url, headers=headers, data=data, proxies=proxies, verify=False)
jsonResponse = response.json()
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
try:
tokens = {
scope: jsonResponse["access_token"]
}
except:
print("[zError:\n", jsonResponse["error_description"])
sys.exit(1)
with open(".azrestarn_auth.json", "w") as outfile:
dic.update(tokens)
json.dump(dic, outfile)
def _find_bitlocker_key(domain,computername):
if computername == None:
print("[+] Please specify --computername parameter. Ex LAPTOP001 or LAPTOP (aka startswith)")
sys.exit(1)
if domain == None:
print("[+] Please specify --domain parameter. Ex ecorp.com or ecorp.onmicrosoft.com")
sys.exit(1)
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.windows.net/.default"]
url = "https://graph.windows.net/{}/devices?api-version=1.61-internal&$filter=startswith(displayName, '{}')".format(domain,computername)
headers= {"Authorization": "Bearer " + str(access_token)}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _check_bestprac():
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/beta/policies/authorizationPolicy/authorizationPolicy"
headers = {
"ConsistencyLevel": "eventual",
"Authorization": "Bearer " + str(access_token)
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _checkMe():
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/beta/me"
headers= {
"Authorization": "Bearer " + str(access_token),
"ConsistencyLevel": "eventual"
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _checkUserGroups(meObjectId):
if meObjectId == None:
print("[+] Please specify --objectid parameter. Find it with --me.")
sys.exit(1)
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/users/{}/memberOf".format(meObjectId)
headers = {
"Authorization": "Bearer " + str(access_token),
"ConsistencyLevel": "eventual"
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _checkOwnedDevices():
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/me/ownedDevices"
headers = {
"Authorization": "Bearer " + str(access_token)
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _checkDynamicGroups():
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/groups?$filter=groupTypes/any(s:s eq 'DynamicMembership')&$top=999"
headers = {
"Authorization": "Bearer " + str(access_token)
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _inviteUser():
if email == None:
print("[+] Please specify --email parameter for the user you want to invite.")
sys.exit(1)
if dispname == None:
print("[+] Please specify --dispname parameter for the user you want to invite.")
sys.exit(1)
if inviteurl == None:
print("[+] Please specify --inviteurl parameter for the user you want to invite.")
sys.exit(1)
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/invitations"
headers = {
"Authorization": "Bearer " + str(access_token),
"Content-type": "application/json"
}
data = {
"invitedUserDisplayName": dispname,
"invitedUserEmailAddress": email,
"sendInvitationMessage": "true",
"invitedUserMessageInfo": {
"ccRecipients": [ {"@odata.type": "microsoft.graph.recipient"} ],
"customizedMessageBody": "Test",
"messageLanguage": "en-US"
},
"inviteRedirectUrl": inviteurl,
"inviteRedeemUrl": inviteurl,
"invitedUserType": "Guest"
}
response = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _invitedUsers():
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/users?$filter=externalUserState eq 'PendingAcceptance'"
headers = {
"Authorization": "Bearer " + str(access_token)
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _inviteDelete(inviteid):
if inviteid == None:
print("[+] Please specify --inviteid for the invite to delete. Hint: --invitedusers.")
sys.exit(1)
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/users/{}".format(inviteid)
headers = {
"Authorization": "Bearer " + str(access_token)
}
response = requests.delete(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _getGroupRoles(groupobjectid):
if groupobjectid == None:
print("[+] Please specify --groupobjectid")
sys.exit(1)
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments?$filter=principalId eq '{}'".format(groupobjectid)
headers = {
"Authorization": "Bearer " + str(access_token)
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _getuser(objectid):
if objectid == None:
print("[+] Please specify --getuser -objectid-")
sys.exit(1)
if approle:
prop = "/appRoleAssignments"
elif memberof:
prop = "/memberOf"
else: prop = ""
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/beta/users/{}{}".format(objectid,prop)
headers = {
"Authorization": "Bearer " + str(access_token),
"ConsistencyLevel": "eventual"
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _getgroup(objectid):
if objectid:
objectid = objectid
else: objectid = ""
if approle:
prop = "/appRoleAssignments"
elif memberof:
prop = "/memberOf"
else: prop = ""
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/beta/groups/{}{}?$top=999".format(objectid,prop)
headers = {
"Authorization": "Bearer " + str(access_token),
"ConsistencyLevel": "eventual"
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _getmemberobjects(objectid):
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/beta/groups/{}/getMemberObjects".format(objectid)
headers = {
"Authorization": "Bearer " + str(access_token),
"Content-type": "application/json",
"ConsistencyLevel": "eventual"
}
data = {
"securityEnabledOnly": "true"
}
response = requests.post(url=url, headers=headers, json=data, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
def _groupsettings():
if os.path.exists(".azrestarn_auth.json"):
with open('.azrestarn_auth.json','r') as f:
dic = json.load(f)
access_token = dic["https://graph.microsoft.com/.default"]
url = "https://graph.microsoft.com/v1.0/groupSettings"
headers = {
"Authorization": "Bearer " + str(access_token)
}
response = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
print(json.dumps(response.json(), indent=4))
if login:
_mslogin_devicecode()
_mslogin_refresh(domain,"https://graph.windows.net/.default")
_mslogin_refresh(domain,"https://graph.microsoft.com/.default")
if refresh:
_mslogin_refresh(domain,"https://graph.windows.net/.default")
_mslogin_refresh(domain,"https://graph.microsoft.com/.default")
if bitlocker: _find_bitlocker_key(domain,computername)
if checkbestprac: _check_bestprac()
if me: _checkMe()
if owneddevices: _checkOwnedDevices()
if dynamicgroups: _checkDynamicGroups()
if invite: _inviteUser()
if invitedusers: _invitedUsers()
if invitedelete: _inviteDelete(inviteid)
if getgrouproles: _getGroupRoles(getgrouproles)
if getuser: _getuser(getuser)
if getgroup: _getgroup(objectid)
if getmemberobjects: _getmemberobjects(getmemberobjects)
if groupsettings: _groupsettings()