| 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 |
|
| 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 |
|
| 28 |
|
| 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 |
|
| 79 |
start_time = utils.start_timer() |
| 80 |
|
| 81 |
|
| 82 |
candidates = [] |
| 83 |
|
| 84 |
|
| 85 |
valid_names = [] |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 96 |
valid_names = utils.fast_dns_lookup(candidates, nameserver, |
| 97 |
nameserverfile, threads=threads) |
| 98 |
|
| 99 |
|
| 100 |
utils.get_url_batch(valid_names, use_ssl=False, |
| 101 |
callback=print_account_response, |
| 102 |
threads=threads) |
| 103 |
|
| 104 |
|
| 105 |
utils.stop_timer(start_time) |
| 106 |
|
| 107 |
|
| 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 |
|
| 117 |
start_time = utils.start_timer() |
| 118 |
|
| 119 |
|
| 120 |
candidates = [] |
| 121 |
|
| 122 |
|
| 123 |
valid_names = [] |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 134 |
valid_names = utils.fast_dns_lookup(candidates, nameserver, |
| 135 |
nameserverfile, threads=threads) |
| 136 |
|
| 137 |
|
| 138 |
utils.get_url_batch(valid_names, use_ssl=False, |
| 139 |
callback=print_account_response, |
| 140 |
threads=threads) |
| 141 |
|
| 142 |
|
| 143 |
utils.stop_timer(start_time) |
| 144 |
|
| 145 |
|
| 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 |
|
| 155 |
start_time = utils.start_timer() |
| 156 |
|
| 157 |
|
| 158 |
candidates = [] |
| 159 |
|
| 160 |
|
| 161 |
valid_names = [] |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
|
| 172 |
valid_names = utils.fast_dns_lookup(candidates, nameserver, |
| 173 |
nameserverfile, threads=threads) |
| 174 |
|
| 175 |
|
| 176 |
utils.get_url_batch(valid_names, use_ssl=False, |
| 177 |
callback=print_account_response, |
| 178 |
threads=threads) |
| 179 |
|
| 180 |
|
| 181 |
utils.stop_timer(start_time) |
| 182 |
|
| 183 |
|
| 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 |
|
| 193 |
start_time = utils.start_timer() |
| 194 |
|
| 195 |
|
| 196 |
candidates = [] |
| 197 |
|
| 198 |
|
| 199 |
valid_names = [] |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 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 |
|
| 210 |
valid_names = utils.fast_dns_lookup(candidates, nameserver, |
| 211 |
nameserverfile, threads=threads) |
| 212 |
|
| 213 |
|
| 214 |
utils.get_url_batch(valid_names, use_ssl=False, |
| 215 |
callback=print_account_response, |
| 216 |
threads=threads) |
| 217 |
|
| 218 |
|
| 219 |
utils.stop_timer(start_time) |
| 220 |
|
| 221 |
|
| 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 |
|
| 231 |
start_time = utils.start_timer() |
| 232 |
|
| 233 |
|
| 234 |
candidates = [] |
| 235 |
|
| 236 |
|
| 237 |
valid_names = [] |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 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 |
|
| 248 |
valid_names = utils.fast_dns_lookup(candidates, nameserver, |
| 249 |
nameserverfile, threads=threads) |
| 250 |
|
| 251 |
|
| 252 |
utils.get_url_batch(valid_names, use_ssl=False, |
| 253 |
callback=print_account_response, |
| 254 |
threads=threads) |
| 255 |
|
| 256 |
|
| 257 |
utils.stop_timer(start_time) |
| 258 |
|
| 259 |
|
| 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 |
|
| 269 |
start_time = utils.start_timer() |
| 270 |
|
| 271 |
|
| 272 |
candidates = [] |
| 273 |
|
| 274 |
|
| 275 |
valid_names = [] |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 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 |
|
| 286 |
valid_names = utils.fast_dns_lookup(candidates, nameserver, |
| 287 |
nameserverfile, threads=threads) |
| 288 |
|
| 289 |
|
| 290 |
utils.get_url_batch(valid_names, use_ssl=False, |
| 291 |
callback=print_account_response, |
| 292 |
threads=threads) |
| 293 |
|
| 294 |
|
| 295 |
utils.stop_timer(start_time) |
| 296 |
|
| 297 |
|
| 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 |
|
| 311 |
if 'The specified account is disabled' in reply.reason: |
| 312 |
print(" [!] Breaking out early, account disabled.") |
| 313 |
return 'breakout' |
| 314 |
|
| 315 |
|
| 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 |
|
| 324 |
if 'Blob API is not yet supported' in reply.reason: |
| 325 |
print(" [!] Breaking out early, Hierarchical namespace account") |
| 326 |
return 'breakout' |
| 327 |
|
| 328 |
|
| 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 |
|
| 357 |
|
| 358 |
|
| 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 |
|
| 375 |
clean_names = utils.get_brute(brute_list, mini=3) |
| 376 |
|
| 377 |
|
| 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 |
|
| 385 |
candidates = [] |
| 386 |
|
| 387 |
|
| 388 |
for name in clean_names: |
| 389 |
candidates.append(f'{account}/{name}/?restype=container&comp=list') |
| 390 |
|
| 391 |
|
| 392 |
utils.get_url_batch(candidates, use_ssl=True, |
| 393 |
callback=print_container_response, |
| 394 |
threads=threads) |
| 395 |
|
| 396 |
|
| 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 |
|
| 420 |
start_time = utils.start_timer() |
| 421 |
|
| 422 |
|
| 423 |
candidates = [name + '.' + WEBAPP_URL for name in names] |
| 424 |
|
| 425 |
|
| 426 |
utils.fast_dns_lookup(candidates, nameserver, |
| 427 |
nameserverfile, |
| 428 |
callback=print_website_response, |
| 429 |
threads=threads) |
| 430 |
|
| 431 |
|
| 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 |
|
| 454 |
start_time = utils.start_timer() |
| 455 |
|
| 456 |
|
| 457 |
candidates = [name + '.' + DATABASE_URL for name in names] |
| 458 |
|
| 459 |
|
| 460 |
utils.fast_dns_lookup(candidates, nameserver, |
| 461 |
nameserverfile, |
| 462 |
callback=print_database_response, |
| 463 |
threads=threads) |
| 464 |
|
| 465 |
|
| 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 |
|
| 489 |
start_time = utils.start_timer() |
| 490 |
|
| 491 |
|
| 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 |
|
| 499 |
candidates = [name + '.' + region + '.' + VM_URL for name in names] |
| 500 |
|
| 501 |
|
| 502 |
utils.fast_dns_lookup(candidates, nameserver, |
| 503 |
nameserverfile, |
| 504 |
callback=print_vm_response, |
| 505 |
threads=threads) |
| 506 |
|
| 507 |
|
| 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) |