Skip to content

Commit fde3de7

Browse files
authored
Merge pull request Pennyw0rth#455 from dev-fortress/aws-credentials
New module: AWS Credentials Finder
2 parents 31acd22 + 09bdf0b commit fde3de7

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

nxc/modules/aws-credentials.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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())

nxc/protocols/winrm.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,10 @@ def hash_login(self, domain, username, ntlm_hash):
238238
self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(self.nthash)} {e!s}")
239239
return False
240240

241-
def execute(self, payload=None, get_output=True, shell_type="cmd"):
241+
def execute(self, payload=None, get_output=False, shell_type="cmd"):
242242
if not payload:
243243
payload = self.args.execute
244244

245-
if self.args.no_output:
246-
get_output = False
247-
248245
try:
249246
result = self.conn.execute_cmd(payload, encoding=self.args.codec) if shell_type == "cmd" else self.conn.execute_ps(payload)
250247
except Exception as e:
@@ -260,13 +257,16 @@ def execute(self, payload=None, get_output=True, shell_type="cmd"):
260257
else:
261258
self.logger.fail(f"Execute command failed, error: {e!s}")
262259
else:
260+
if get_output:
261+
return result[0]
263262
self.logger.success(f"Executed command (shell type: {shell_type})")
264-
buf = StringIO(result[0]).readlines() if get_output else ""
265-
for line in buf:
266-
self.logger.highlight(line.strip())
263+
if not self.args.no_output:
264+
for line in StringIO(result[0]).readlines():
265+
self.logger.highlight(line.strip())
267266

268-
def ps_execute(self):
269-
self.execute(payload=self.args.ps_execute, get_output=True, shell_type="powershell")
267+
def ps_execute(self, payload=None, get_output=False):
268+
command = payload if payload else self.args.ps_execute
269+
self.execute(payload=command, get_output=get_output, shell_type="powershell")
270270

271271
# Dos attack prevent:
272272
# if someboby executed "reg save HKLM\sam C:\windows\temp\sam" before, but didn't remove "C:\windows\temp\sam" file,

0 commit comments

Comments
 (0)