Debian Codegraph @

enum_tools/azure_checks.py

raw symbols SCIP
1 """
2 Azure-specific checks. Part of the cloud_enum package available at
3 github.com/initstring/cloud_enum
4 """
5
6 import re
7 import requests
8 from enum_tools import utils
9 from enum_tools import azure_regions
10
11 BANNER = '''
12 ++++++++++++++++++++++++++
13 azure checks
14 ++++++++++++++++++++++++++
15 '''
16
17 # Known Azure domain names
18 BLOB_URL = 'blob.core.windows.net'
19 FILE_URL= 'file.core.windows.net'
20 QUEUE_URL = 'queue.core.windows.net'
21 TABLE_URL = 'table.core.windows.net'
22 MGMT_URL = 'scm.azurewebsites.net'
23 VAULT_URL = 'vault.azure.net'
24 WEBAPP_URL = 'azurewebsites.net'
25 DATABASE_URL = 'database.windows.net'
26
27 # Virtual machine DNS names are actually:
28 # {whatever}.{region}.cloudapp.azure.com
29 VM_URL = 'cloudapp.azure.com'
30
31
32 def print_account_response(reply):
33 """
34 Parses the HTTP reply of a brute-force attempt
35
36 This function is passed into the class object so we can view results
37 in real-time.
38 """
39 data = {'platform': 'azure', 'msg': '', 'target': '', 'access': ''}
40
41 if reply.status_code == 404 or 'The requested URI does not represent' in reply.reason:
42 pass
43 elif 'Server failed to authenticate the request' in reply.reason:
44 data['msg'] = 'Auth-Only Account'
45 data['target'] = reply.url
46 data['access'] = 'protected'
47 utils.fmt_output(data)
48 elif 'The specified account is disabled' in reply.reason:
49 data['msg'] = 'Disabled Account'
50 data['target'] = reply.url
51 data['access'] = 'disabled'
52 utils.fmt_output(data)
53 elif 'Value for one of the query' in reply.reason:
54 data['msg'] = 'HTTP-OK Account'
55 data['target'] = reply.url
56 data['access'] = 'public'
57 utils.fmt_output(data)
58 elif 'The account being accessed' in reply.reason:
59 data['msg'] = 'HTTPS-Only Account'
60 data['target'] = reply.url
61 data['access'] = 'public'
62 utils.fmt_output(data)
63 elif 'Unauthorized' in reply.reason:
64 data['msg'] = 'Unathorized Account'
65 data['target'] = reply.url
66 data['access'] = 'public'
67 utils.fmt_output(data)
68 else:
69 print(" Unknown status codes being received from " + reply.url +":\n"
70 " "+ str(reply.status_code)+" : "+ reply.reason)
71
72 def check_storage_accounts(names, threads, nameserver, nameserverfile=False):
73 """
74 Checks storage account names
75 """
76 print("[+] Checking for Azure Storage Accounts")
77
78 # Start a counter to report on elapsed time
79 start_time = utils.start_timer()
80
81 # Initialize the list of domain names to look up
82 candidates = []
83
84 # Initialize the list of valid hostnames
85 valid_names = []
86
87 # Take each mutated keyword craft a domain name to lookup.
88 # As Azure Storage Accounts can contain only letters and numbers,
89 # discard those not matching to save time on the DNS lookups.
90 regex = re.compile('[^a-zA-Z0-9]')
91 for name in names:
92 if not re.search(regex, name):
93 candidates.append(f'{name}.{BLOB_URL}')
94
95 # Azure Storage Accounts use DNS sub-domains. First, see which are valid.
96 valid_names = utils.fast_dns_lookup(candidates, nameserver,
97 nameserverfile, threads=threads)
98
99 # Send the valid names to the batch HTTP processor
100 utils.get_url_batch(valid_names, use_ssl=False,
101 callback=print_account_response,
102 threads=threads)
103
104 # Stop the timer
105 utils.stop_timer(start_time)
106
107 # de-dupe the results and return
108 return list(set(valid_names))
109
110 def check_file_accounts(names, threads, nameserver, nameserverfile=False):
111 """
112 Checks File account names
113 """
114 print("[+] Checking for Azure File Accounts")
115
116 # Start a counter to report on elapsed time
117 start_time = utils.start_timer()
118
119 # Initialize the list of domain names to look up
120 candidates = []
121
122 # Initialize the list of valid hostnames
123 valid_names = []
124
125 # Take each mutated keyword craft a domain name to lookup.
126 # As Azure Storage Accounts can contain only letters and numbers,
127 # discard those not matching to save time on the DNS lookups.
128 regex = re.compile('[^a-zA-Z0-9]')
129 for name in names:
130 if not re.search(regex, name):
131 candidates.append(f'{name}.{FILE_URL}')
132
133 # Azure Storage Accounts use DNS sub-domains. First, see which are valid.
134 valid_names = utils.fast_dns_lookup(candidates, nameserver,
135 nameserverfile, threads=threads)
136
137 # Send the valid names to the batch HTTP processor
138 utils.get_url_batch(valid_names, use_ssl=False,
139 callback=print_account_response,
140 threads=threads)
141
142 # Stop the timer
143 utils.stop_timer(start_time)
144
145 # de-dupe the results and return
146 return list(set(valid_names))
147
148 def check_queue_accounts(names, threads, nameserver, nameserverfile=False):
149 """
150 Checks Queue account names
151 """
152 print("[+] Checking for Azure Queue Accounts")
153
154 # Start a counter to report on elapsed time
155 start_time = utils.start_timer()
156
157 # Initialize the list of domain names to look up
158 candidates = []
159
160 # Initialize the list of valid hostnames
161 valid_names = []
162
163 # Take each mutated keyword craft a domain name to lookup.
164 # As Azure Storage Accounts can contain only letters and numbers,
165 # discard those not matching to save time on the DNS lookups.
166 regex = re.compile('[^a-zA-Z0-9]')
167 for name in names:
168 if not re.search(regex, name):
169 candidates.append(f'{name}.{QUEUE_URL}')
170
171 # Azure Storage Accounts use DNS sub-domains. First, see which are valid.
172 valid_names = utils.fast_dns_lookup(candidates, nameserver,
173 nameserverfile, threads=threads)
174
175 # Send the valid names to the batch HTTP processor
176 utils.get_url_batch(valid_names, use_ssl=False,
177 callback=print_account_response,
178 threads=threads)
179
180 # Stop the timer
181 utils.stop_timer(start_time)
182
183 # de-dupe the results and return
184 return list(set(valid_names))
185
186 def check_table_accounts(names, threads, nameserver, nameserverfile=False):
187 """
188 Checks Table account names
189 """
190 print("[+] Checking for Azure Table Accounts")
191
192 # Start a counter to report on elapsed time
193 start_time = utils.start_timer()
194
195 # Initialize the list of domain names to look up
196 candidates = []
197
198 # Initialize the list of valid hostnames
199 valid_names = []
200
201 # Take each mutated keyword craft a domain name to lookup.
202 # As Azure Storage Accounts can contain only letters and numbers,
203 # discard those not matching to save time on the DNS lookups.
204 regex = re.compile('[^a-zA-Z0-9]')
205 for name in names:
206 if not re.search(regex, name):
207 candidates.append(f'{name}.{TABLE_URL}')
208
209 # Azure Storage Accounts use DNS sub-domains. First, see which are valid.
210 valid_names = utils.fast_dns_lookup(candidates, nameserver,
211 nameserverfile, threads=threads)
212
213 # Send the valid names to the batch HTTP processor
214 utils.get_url_batch(valid_names, use_ssl=False,
215 callback=print_account_response,
216 threads=threads)
217
218 # Stop the timer
219 utils.stop_timer(start_time)
220
221 # de-dupe the results and return
222 return list(set(valid_names))
223
224 def check_mgmt_accounts(names, threads, nameserver, nameserverfile=False):
225 """
226 Checks App Management account names
227 """
228 print("[+] Checking for Azure App Management Accounts")
229
230 # Start a counter to report on elapsed time
231 start_time = utils.start_timer()
232
233 # Initialize the list of domain names to look up
234 candidates = []
235
236 # Initialize the list of valid hostnames
237 valid_names = []
238
239 # Take each mutated keyword craft a domain name to lookup.
240 # As Azure Storage Accounts can contain only letters and numbers,
241 # discard those not matching to save time on the DNS lookups.
242 regex = re.compile('[^a-zA-Z0-9]')
243 for name in names:
244 if not re.search(regex, name):
245 candidates.append(f'{name}.{MGMT_URL}')
246
247 # Azure Storage Accounts use DNS sub-domains. First, see which are valid.
248 valid_names = utils.fast_dns_lookup(candidates, nameserver,
249 nameserverfile, threads=threads)
250
251 # Send the valid names to the batch HTTP processor
252 utils.get_url_batch(valid_names, use_ssl=False,
253 callback=print_account_response,
254 threads=threads)
255
256 # Stop the timer
257 utils.stop_timer(start_time)
258
259 # de-dupe the results and return
260 return list(set(valid_names))
261
262 def check_vault_accounts(names, threads, nameserver, nameserverfile=False):
263 """
264 Checks Key Vault account names
265 """
266 print("[+] Checking for Azure Key Vault Accounts")
267
268 # Start a counter to report on elapsed time
269 start_time = utils.start_timer()
270
271 # Initialize the list of domain names to look up
272 candidates = []
273
274 # Initialize the list of valid hostnames
275 valid_names = []
276
277 # Take each mutated keyword craft a domain name to lookup.
278 # As Azure Storage Accounts can contain only letters and numbers,
279 # discard those not matching to save time on the DNS lookups.
280 regex = re.compile('[^a-zA-Z0-9]')
281 for name in names:
282 if not re.search(regex, name):
283 candidates.append(f'{name}.{VAULT_URL}')
284
285 # Azure Storage Accounts use DNS sub-domains. First, see which are valid.
286 valid_names = utils.fast_dns_lookup(candidates, nameserver,
287 nameserverfile, threads=threads)
288
289 # Send the valid names to the batch HTTP processor
290 utils.get_url_batch(valid_names, use_ssl=False,
291 callback=print_account_response,
292 threads=threads)
293
294 # Stop the timer
295 utils.stop_timer(start_time)
296
297 # de-dupe the results and return
298 return list(set(valid_names))
299
300
301 def print_container_response(reply):
302 """
303 Parses the HTTP reply of a brute-force attempt
304
305 This function is passed into the class object so we can view results
306 in real-time.
307 """
308 data = {'platform': 'azure', 'msg': '', 'target': '', 'access': ''}
309
310 # Stop brute forcing disabled accounts
311 if 'The specified account is disabled' in reply.reason:
312 print(" [!] Breaking out early, account disabled.")
313 return 'breakout'
314
315 # Stop brute forcing accounts without permission
316 if ('not authorized to perform this operation' in reply.reason or
317 'not have sufficient permissions' in reply.reason or
318 'Public access is not permitted' in reply.reason or
319 'Server failed to authenticate the request' in reply.reason):
320 print(" [!] Breaking out early, auth required.")
321 return 'breakout'
322
323 # Stop brute forcing unsupported accounts
324 if 'Blob API is not yet supported' in reply.reason:
325 print(" [!] Breaking out early, Hierarchical namespace account")
326 return 'breakout'
327
328 # Handle other responses
329 if reply.status_code == 404:
330 pass
331 elif reply.status_code == 200:
332 data['msg'] = 'OPEN AZURE CONTAINER'
333 data['target'] = reply.url
334 data['access'] = 'public'
335 utils.fmt_output(data)
336 utils.list_bucket_contents(reply.url)
337 elif 'One of the request inputs is out of range' in reply.reason:
338 pass
339 elif 'The request URI is invalid' in reply.reason:
340 pass
341 else:
342 print(f" Unknown status codes being received from {reply.url}:\n"
343 " {reply.status_code}: {reply.reason}")
344
345 return None
346
347
348 def brute_force_containers(storage_accounts, brute_list, threads):
349 """
350 Attempts to find public Blob Containers in valid Storage Accounts
351
352 Here is the URL format to list Azure Blog Container contents:
353 <account>.blob.core.windows.net/<container>/?restype=container&comp=list
354 """
355
356 # We have a list of valid DNS names that might not be worth scraping,
357 # such as disabled accounts or authentication required. Let's quickly
358 # weed those out.
359 print(f"[*] Checking {len(storage_accounts)} accounts for status before brute-forcing")
360 valid_accounts = []
361 for account in storage_accounts:
362 try:
363 reply = requests.get(f'https://{account}/')
364 if 'Server failed to authenticate the request' in reply.reason:
365 storage_accounts.remove(account)
366 elif 'The specified account is disabled' in reply.reason:
367 storage_accounts.remove(account)
368 else:
369 valid_accounts.append(account)
370 except requests.exceptions.ConnectionError as error_msg:
371 print(f" [!] Connection error on https://{account}:")
372 print(error_msg)
373
374 # Read the brute force file into memory
375 clean_names = utils.get_brute(brute_list, mini=3)
376
377 # Start a counter to report on elapsed time
378 start_time = utils.start_timer()
379
380 print(f"[*] Brute-forcing container names in {len(valid_accounts)} storage accounts")
381 for account in valid_accounts:
382 print(f"[*] Brute-forcing {len(clean_names)} container names in {account}")
383
384 # Initialize the list of correctly formatted urls
385 candidates = []
386
387 # Take each mutated keyword and craft a url with correct format
388 for name in clean_names:
389 candidates.append(f'{account}/{name}/?restype=container&comp=list')
390
391 # Send the valid names to the batch HTTP processor
392 utils.get_url_batch(candidates, use_ssl=True,
393 callback=print_container_response,
394 threads=threads)
395
396 # Stop the timer
397 utils.stop_timer(start_time)
398
399
400 def print_website_response(hostname):
401 """
402 This function is passed into the DNS brute force as a callback,
403 so we can get real-time results.
404 """
405 data = {'platform': 'azure', 'msg': '', 'target': '', 'access': ''}
406
407 data['msg'] = 'Registered Azure Website DNS Name'
408 data['target'] = hostname
409 data['access'] = 'public'
410 utils.fmt_output(data)
411
412
413 def check_azure_websites(names, nameserver, threads, nameserverfile=False):
414 """
415 Checks for Azure Websites (PaaS)
416 """
417 print("[+] Checking for Azure Websites")
418
419 # Start a counter to report on elapsed time
420 start_time = utils.start_timer()
421
422 # Initialize the list of domain names to look up
423 candidates = [name + '.' + WEBAPP_URL for name in names]
424
425 # Azure Websites use DNS sub-domains. If it resolves, it is registered.
426 utils.fast_dns_lookup(candidates, nameserver,
427 nameserverfile,
428 callback=print_website_response,
429 threads=threads)
430
431 # Stop the timer
432 utils.stop_timer(start_time)
433
434
435 def print_database_response(hostname):
436 """
437 This function is passed into the DNS brute force as a callback,
438 so we can get real-time results.
439 """
440 data = {'platform': 'azure', 'msg': '', 'target': '', 'access': ''}
441
442 data['msg'] = 'Registered Azure Database DNS Name'
443 data['target'] = hostname
444 data['access'] = 'public'
445 utils.fmt_output(data)
446
447
448 def check_azure_databases(names, nameserver, threads, nameserverfile=False):
449 """
450 Checks for Azure Databases
451 """
452 print("[+] Checking for Azure Databases")
453 # Start a counter to report on elapsed time
454 start_time = utils.start_timer()
455
456 # Initialize the list of domain names to look up
457 candidates = [name + '.' + DATABASE_URL for name in names]
458
459 # Azure databases use DNS sub-domains. If it resolves, it is registered.
460 utils.fast_dns_lookup(candidates, nameserver,
461 nameserverfile,
462 callback=print_database_response,
463 threads=threads)
464
465 # Stop the timer
466 utils.stop_timer(start_time)
467
468
469 def print_vm_response(hostname):
470 """
471 This function is passed into the DNS brute force as a callback,
472 so we can get real-time results.
473 """
474 data = {'platform': 'azure', 'msg': '', 'target': '', 'access': ''}
475
476 data['msg'] = 'Registered Azure Virtual Machine DNS Name'
477 data['target'] = hostname
478 data['access'] = 'public'
479 utils.fmt_output(data)
480
481
482 def check_azure_vms(names, nameserver, threads, nameserverfile=False):
483 """
484 Checks for Azure Virtual Machines
485 """
486 print("[+] Checking for Azure Virtual Machines")
487
488 # Start a counter to report on elapsed time
489 start_time = utils.start_timer()
490
491 # Pull the regions from a config file
492 regions = azure_regions.REGIONS
493
494 print(f"[*] Testing across {len(regions)} regions defined in the config file")
495
496 for region in regions:
497
498 # Initialize the list of domain names to look up
499 candidates = [name + '.' + region + '.' + VM_URL for name in names]
500
501 # Azure VMs use DNS sub-domains. If it resolves, it is registered.
502 utils.fast_dns_lookup(candidates, nameserver,
503 nameserverfile,
504 callback=print_vm_response,
505 threads=threads)
506
507 # Stop the timer
508 utils.stop_timer(start_time)
509
510
511 def run_all(names, args):
512 """
513 Function is called by main program
514 """
515 print(BANNER)
516
517 valid_accounts = check_storage_accounts(names, args.threads,
518 args.nameserver, args.nameserverfile)
519 if valid_accounts and not args.quickscan:
520 brute_force_containers(valid_accounts, args.brute, args.threads)
521
522 check_file_accounts(names, args.threads, args.nameserver, args.nameserverfile)
523 check_queue_accounts(names, args.threads, args.nameserver, args.nameserverfile)
524 check_table_accounts(names, args.threads, args.nameserver, args.nameserverfile)
525 check_mgmt_accounts(names, args.threads, args.nameserver, args.nameserverfile)
526 check_vault_accounts(names, args.threads, args.nameserver, args.nameserverfile)
527
528 check_azure_websites(names, args.nameserver, args.threads, args.nameserverfile)
529 check_azure_databases(names, args.nameserver, args.threads, args.nameserverfile)
530 check_azure_vms(names, args.nameserver, args.threads, args.nameserverfile)