|
| 1 | +class NXCModule: |
| 2 | + """ |
| 3 | + Search for aws credentials files on linux and windows machines |
| 4 | +
|
| 5 | + Module by Fortress |
| 6 | + """ |
| 7 | + |
| 8 | + name = "aws-credentials" |
| 9 | + description = "Search for aws credentials files." |
| 10 | + supported_protocols = ["ssh", "smb", "winrm"] |
| 11 | + opsec_safe = True |
| 12 | + multiple_hosts = True |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self.search_path_linux = "'/home/' '/tmp/'" |
| 16 | + self.search_path_win = "'C:\\Users\\', 'C:\\ProgramData\\AWSCLI\\', 'C:\\Temp\\'" |
| 17 | + |
| 18 | + def options(self, context, module_options): |
| 19 | + r""" |
| 20 | + SEARCH_PATH_LINUX Linux location where to search for aws credentials related files |
| 21 | + Default: "'/home/' '/tmp/'" |
| 22 | +
|
| 23 | + SEARCH_PATH_WIN Windows locations where to search for aws credentials related files |
| 24 | + Default: "'C:\\Users\\', 'C:\\ProgramData\\AWSCLI\\', 'C:\\Temp\\'" |
| 25 | + """ |
| 26 | + if "SEARCH_PATH_LINUX" in module_options: |
| 27 | + self.search_path_linux = module_options["SEARCH_PATH_LINUX"] |
| 28 | + |
| 29 | + if "SEARCH_PATH_WIN" in module_options: |
| 30 | + self.search_path_win = module_options["SEARCH_PATH_WIN"] |
| 31 | + |
| 32 | + def on_login(self, context, connection): |
| 33 | + # search for aws_credentials-related files on linux systems |
| 34 | + if "ssh" in context.protocol: |
| 35 | + search_aws_creds_files_payload = f"find {self.search_path_linux} -type f -name credentials -exec grep -l 'aws_' {{}} \\; 2>&1 | grep -v 'Permission denied$'" |
| 36 | + search_aws_creds_files_cmd = f'/bin/bash -c "{search_aws_creds_files_payload}"' |
| 37 | + output = connection.execute(search_aws_creds_files_cmd) |
| 38 | + else: |
| 39 | + # search for aws_credentials-related files on windows systems |
| 40 | + # we have to exclude "Application Data" as this creates an infinite recursion, see: https://www.reddit.com/r/PowerShell/comments/17pctnv/symbolic_link_application_data_in_appdatalocal/ |
| 41 | + search_aws_creds_files_payload_win = f"Get-ChildItem -Path {self.search_path_win} -Recurse -Force -Include 'credentials' -ErrorAction SilentlyContinue | Where-Object {{ Select-String -Path $_.FullName -Pattern 'aws' -Quiet }} | Select-Object -ExpandProperty FullName" |
| 42 | + search_aws_creds_files_cmd_win = f'powershell.exe "{search_aws_creds_files_payload_win}"' |
| 43 | + output = connection.execute(search_aws_creds_files_cmd_win, True) |
| 44 | + |
| 45 | + if output: |
| 46 | + context.log.success("The following files were found:") |
| 47 | + for line in output.splitlines(): |
| 48 | + context.log.highlight(line.rstrip()) |
0 commit comments