-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiado.py
568 lines (507 loc) · 22.3 KB
/
hiado.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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import requests
import json
import time
import yaml
import os
import argparse
import sys
import pyfiglet
class pipelineObject:
def __init__(self, url, repository, name):
self.name = name
self.repository = repository
self.url = url
class color:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"
# list of permissions array
full_permissions_list = [
"Contribute",
"Create branch",
"Edit policies",
"Manage permissions",
"Bypass policies when pushing",
"Bypass policies when completing pull requests",
"Contribute to pull requests",
"Create tag",
"Delete or disable repository",
"Force push (rewrite history, delete branches and tags)",
"Manage notes",
"Read",
"Remove others' locks",
"Rename repository",
]
interesting_permissions_list_wide = [
"Contribute",
"Create branch",
"Edit policies",
"Manage permissions",
"Bypass policies when pushing",
]
interesting_permissions_list = ["Contribute", "Create branch", "Manage permissions"]
burp_proxy = {"https": "https://127.0.0.1:8080"}
def get_project_id_subject_descriptor(organization, project_name, user_token):
url = f"https://dev.azure.com/{organization}/{project_name}?__rt=fps&__ver=2"
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
}
response = requests.get(url, headers=headers, timeout=10)
json_response = response.json()
content_array_data = (
json_response.get("fps", {}).get("dataProviders", {}).get("data", {})
)
return content_array_data.get("ms.vss-web.page-data", {}).get("user", {}).get(
"descriptor"
), content_array_data.get("ms.vss-tfs-web.page-data", {}).get("project", {}).get(
"id"
)
def get_repository_list(organization, project_id, user_token):
url = f"https://dev.azure.com/{organization}/{project_id}/_apis/git/Repositories"
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "application/json;api-version=5.0-preview.1;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
}
response = requests.get(url, headers=headers, timeout=10)
return response.json()
def get_repository_permissionset(organization, project_name, user_token):
url = f"https://dev.azure.com/{organization}/{project_name}/_workitems?__rt=fps&__ver=2"
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
}
try:
response = requests.get(url, headers=headers, timeout=10)
return (
response.json()
.get("fps", {})
.get("dataProviders", {})
.get("data", {})
.get("ms.vss-code-web.versioncontrol-viewmodel-data-provider", {})
.get("repositoryPermissionSet")
)
except Exception:
return None
def get_project_list(organization, user_token):
url = (
f"https://dev.azure.com/{organization}/_apis/projects?api-version=7.1-preview.4"
)
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
}
response = requests.get(url, headers=headers, timeout=10)
try:
return response.json().get("value", [])
except Exception as e:
print(f"[-] Error while retrieving the list of projects for {str(e)}")
return []
def retrieve_repository_permissions(
subject_descriptor,
project_id,
repository_id,
permissionset,
user_token,
organization,
):
url = f"https://dev.azure.com/{organization}/_apis/Contribution/HierarchyQuery"
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "application/json;api-version=5.0-preview.1;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/json",
"Origin": "https://dev.azure.com",
}
# Define the JSON payload with variables
payload_json = {
"contributionIds": ["ms.vss-admin-web.security-view-permissions-data-provider"],
"dataProviderContext": {
"properties": {
"subjectDescriptor": f"{subject_descriptor}",
"permissionSetId": f"{permissionset}",
"permissionSetToken": f"repoV2/{project_id}/{repository_id}",
"accountName": "",
}
},
}
try:
response = requests.post(url, headers=headers, data=json.dumps(payload_json))
return (
response.json()
.get("dataProviders", {})
.get("ms.vss-admin-web.security-view-permissions-data-provider", {})
.get("subjectPermissions", [])
)
except Exception:
return None
def find_text_in_file(
user_token, text, project_id, project_name, repository_name, organization
):
url = f"https://almsearch.dev.azure.com/{organization}/{project_id}/_apis/search/codesearchresults?api-version=7.1-preview.1"
headers = {
"Host": "almsearch.dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "application/json;api-version=5.0-preview.1;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/json",
}
payload_json = {
"searchText": f"{text}",
"$skip": 0,
"$top": 1,
"filters": {
"Project": [f"{project_name}"],
"Repository": [f"{repository_name}"],
"Path": ["/"],
"Branch": ["master"],
"CodeElement": ["def", "class"],
},
"$orderBy": [{"field": "filename", "sortOrder": "ASC"}],
"includeFacets": "true",
}
try:
response = requests.post(url, headers=headers, data=json.dumps(payload_json))
return response.json()
except Exception:
return None
def get_pipeline_details(url, headers):
response = requests.get(url, headers=headers, timeout=10)
return response.json()
def get_list_pipelines(user_token, project_name, organization):
url = f"https://dev.azure.com/{organization}/{project_name}/_apis/pipelines?api-version=7.1-preview.1"
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "close",
}
try:
response = requests.get(url, headers=headers, timeout=10)
pipelines = []
for pipeline in response.json().get("value", []):
pipeline_details = get_pipeline_details(pipeline.get("url"), headers)
pipelines.append(
pipelineObject(
pipeline.get("url"),
pipeline_details.get("configuration", {})
.get("repository", {})
.get("id"),
pipeline_details.get("name"),
)
)
return pipelines
except Exception:
return None
def check_trigger_value(user_token, project_name, search_result, organization):
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "close",
}
for obj in search_result.get("results", []):
path = obj.get("path")
file_extension = os.path.splitext(path)[1]
if file_extension == ".yaml" or file_extension == ".yml":
repository_id = obj.get("repository", {}).get("id")
time.sleep(1)
url = f"https://dev.azure.com/{organization}/{project_name}/_apis/git/Repositories/{repository_id}/items?path={path}&api-version=7.1-preview.1"
response = requests.get(url, headers=headers, timeout=10)
try:
yaml_content = yaml.safe_load(response.text)
except Exception as e:
print(f"[-] Error while loading the yaml file")
return "none"
try:
if isinstance(yaml_content["trigger"], list):
return yaml_content["trigger"][0]
if isinstance(yaml_content["trigger"], dict):
return str(yaml_content["trigger"])
if isinstance(yaml_content["trigger"], str):
return yaml_content["trigger"]
except Exception as e:
print(f"[-] Error while accessing the trigger key: {str(e)}")
# print(response.text)
return "none"
def check_azure_subscription_value(
user_token, project_name, search_result, organization
):
result_map = {}
azure_subscription_findings = []
headers = {
"Host": "dev.azure.com",
"Cookie": f"UserAuthentication={user_token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "close",
}
for obj in search_result.get("results", []):
path = obj.get("path")
file_extension = os.path.splitext(path)[1]
if file_extension == ".yaml" or file_extension == ".yml":
repository_id = obj.get("repository", {}).get("id")
time.sleep(1)
try:
url = f"https://dev.azure.com/{organization}/{project_name}/_apis/git/Repositories/{repository_id}/items?path={path}&api-version=7.1-preview.1"
response = requests.get(url, headers=headers, timeout=10)
except Exception:
print("[-] Error while checking the azureSubscription values")
continue
lines = response.text.split("\n")
for line in lines:
if "azureSubscription" in line:
azure_subscription_findings.append(
(line.replace("azureSubscription:", "")).replace(" ", "")
)
result_map[path] = azure_subscription_findings
if len(result_map) > 0:
return result_map
return None
def check_repo_in_pipelines(pipeline_list, repo_id):
for pipeline in pipeline_list:
if pipeline.repository == repo_id:
return pipeline.name
return "none"
def check_finding(permission_array_json, interesting_permissions_tocheck):
permissions_array = []
for obj in permission_array_json:
permissions_array.append(obj.get("displayName"))
if any(x in permissions_array for x in interesting_permissions_tocheck):
return True
return False
def validate_options(args):
if args.check_service_connection is True and args.check_trigger is False:
raise argparse.ArgumentTypeError(
"[!] check_service_connection can only be chosen when check_trigger is picked."
)
return args
def pick_permissions_list(args):
if args.interesting_permission.lower() == "interesting":
return interesting_permissions_list
elif args.interesting_permission.lower() == "wide":
return interesting_permissions_list_wide
elif args.interesting_permission.lower() == "full":
return full_permissions_list
else:
return full_permissions_list
parser = argparse.ArgumentParser(description="Azure DevOps Repository Attacking Tool")
parser.add_argument(
"-org", "--organization", type=str, required=True, help="Organization name"
)
parser.add_argument(
"-prj",
"--project",
type=str,
default="none",
help="Specify here the name of the project you want to check, all the others will be skipped",
)
parser.add_argument(
"-ct",
"--check-trigger",
action="store_true",
default=False,
help="Print only results that have a trigger condition set (default: false)",
)
parser.add_argument(
"-csc",
"--check-service-connection",
action="store_true",
default=False,
help="Print only results that have an azureSubscription set (default: false)",
)
parser.add_argument(
"-ip",
"--interesting-permission",
type=str,
required=True,
help="Choose between: interesting,wide,full",
)
parser.add_argument("-ut", "--user-token", type=str, required=True, help="User token")
args = parser.parse_args()
try:
args = validate_options(args)
except argparse.ArgumentTypeError as e:
parser.error(str(e))
interesting_permission_tocheck = pick_permissions_list(args)
organization = args.organization
user_token = args.user_token
projects_list = []
if args.project != "none":
projects_list.append(args.project)
else:
projects_list = get_project_list(organization, user_token)
if len(projects_list) == 0:
print("[-] Error while retrieving the list of projects, exiting")
sys.exit()
print(pyfiglet.figlet_format("Hiado", font="slant"))
# from now on need to loop through the projects name
for project in projects_list:
try:
project_name = project.get("name")
except AttributeError:
project_name = projects_list[0]
print(f"[+] Working on {project_name}")
subject_descriptor, project_id = get_project_id_subject_descriptor(
organization, project_name, user_token
)
permissionset = get_repository_permissionset(organization, project_name, user_token)
if permissionset is None or subject_descriptor is None or project_id is None:
print("[-] Error retrieving project information")
continue
print("[+] Found subjectDescriptor: " + subject_descriptor)
print("[+] Found projectID: " + project_id)
print("[+] Found repository permissionSet: " + permissionset)
# get list of pipelines in project
pipeline_list = get_list_pipelines(user_token, project_name, organization)
# get list of repository in project
repositories = get_repository_list(organization, project_id, user_token)
if repositories is None:
continue
for obj in repositories:
time.sleep(2)
# check if the repo is attached to any pipeline, if not does not matter
if pipeline_list is not None:
attached_pipeline = check_repo_in_pipelines(pipeline_list, obj.get("id"))
# retrieve the permission on the repository
permissions_array = retrieve_repository_permissions(
subject_descriptor,
project_id,
obj.get("id"),
permissionset,
user_token,
organization,
)
# if i was able to retrieve the permissions i move forward
if permissions_array != None:
# search all the files that contains the word trigger (filtered also by extension: yaml, yml)
# the existence of trigger takes priority over the other filters
if args.check_trigger is True:
search_result = find_text_in_file(
user_token,
"trigger",
project_id,
project_name,
obj.get("name"),
organization,
)
if search_result != None and search_result.get("count") != None:
if search_result.get("count") > 0:
trigger_value = check_trigger_value(
user_token, project_name, search_result, organization
)
search_result = find_text_in_file(
user_token,
"azureSubscription",
project_id,
project_name,
obj.get("name"),
organization,
)
azure_subscription = check_azure_subscription_value(
user_token, project_name, search_result, organization
)
# if a trigger value != none has been found i want to proceed
if trigger_value is not None and trigger_value != "none":
# if i want to check the service connection and the result is none i skip
# if csc is false i do not care anyway but it has to be blocking if csc is true and i haven't found any
if (
args.check_service_connection is True
and azure_subscription is None
):
continue
# if there are any interesting permissons consider the repo a finding and print the details
if check_finding(
permissions_array, interesting_permission_tocheck
):
print(
"\t - name: "
+ color.BOLD
+ obj.get("name")
+ color.END
)
print("\t - repository id: " + obj.get("id"))
print(
"\t - pipeline: " + "empty"
if attached_pipeline == "none"
else "\t - pipeline: " + attached_pipeline
)
print("\t - trigger condition: " + trigger_value)
print("\t - azureSubscription: ")
for file, matches in azure_subscription.items():
print(f"\t\t File: {file} - ", end="")
print("Matches: ", end="")
for index, match in enumerate(matches):
if index == len(matches) - 1:
print(match)
else:
print(match + ",", end="")
print("\t - Permissions list: ")
for perm in permissions_array:
if (
perm.get("displayName")
in interesting_permission_tocheck
):
if "Allow" in perm.get(
"permissionDisplayString"
):
print(
"\t\t"
+ perm.get("displayName")
+ ": "
+ color.GREEN
+ perm.get("permissionDisplayString")
+ color.END
)
else:
# just assess the permissions on a given repository
if check_finding(permissions_array, interesting_permission_tocheck):
print("\t - name: " + color.BOLD + obj.get("name") + color.END)
print("\t - repository id: " + obj.get("id"))
for perm in permissions_array:
if perm.get("displayName") in interesting_permission_tocheck:
if "Allow" in perm.get("permissionDisplayString"):
print(
"\t\t"
+ perm.get("displayName")
+ ": "
+ color.GREEN
+ perm.get("permissionDisplayString")
+ color.END
)