2022-07-18 23:59:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2023-05-02 15:17:59 +00:00
|
|
|
# Stolen from https://github.com/Wh1t3Fox/polenum
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
from impacket.dcerpc.v5.rpcrt import DCERPC_v5
|
2023-03-30 20:36:58 +00:00
|
|
|
from impacket.dcerpc.v5 import transport, samr
|
2017-04-05 15:07:00 +00:00
|
|
|
from time import strftime, gmtime
|
2023-03-30 20:36:58 +00:00
|
|
|
from cme.logger import cme_logger
|
|
|
|
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
def d2b(a):
|
|
|
|
tbin = []
|
|
|
|
while a:
|
|
|
|
tbin.append(a % 2)
|
2019-11-12 18:33:14 +00:00
|
|
|
a //= 2
|
2017-04-10 07:24:23 +00:00
|
|
|
|
|
|
|
t2bin = tbin[::-1]
|
|
|
|
if len(t2bin) != 8:
|
2019-11-11 10:06:39 +00:00
|
|
|
for x in range(6 - len(t2bin)):
|
2017-04-10 07:24:23 +00:00
|
|
|
t2bin.insert(0, 0)
|
2023-05-02 15:17:59 +00:00
|
|
|
return "".join([str(g) for g in t2bin])
|
2017-04-10 07:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def convert(low, high, lockout=False):
|
|
|
|
time = ""
|
|
|
|
tmp = 0
|
|
|
|
|
|
|
|
if low == 0 and hex(high) == "-0x80000000":
|
|
|
|
return "Not Set"
|
|
|
|
if low == 0 and high == 0:
|
|
|
|
return "None"
|
|
|
|
|
|
|
|
if not lockout:
|
2023-05-02 15:17:59 +00:00
|
|
|
if low != 0:
|
|
|
|
high = abs(high + 1)
|
2017-04-10 07:24:23 +00:00
|
|
|
else:
|
|
|
|
high = abs(high)
|
|
|
|
low = abs(low)
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
tmp = low + (high) * 16**8 # convert to 64bit int
|
|
|
|
tmp *= 1e-7 # convert to seconds
|
2017-04-10 07:24:23 +00:00
|
|
|
else:
|
|
|
|
tmp = abs(high) * (1e-7)
|
|
|
|
|
|
|
|
try:
|
|
|
|
minutes = int(strftime("%M", gmtime(tmp)))
|
|
|
|
hours = int(strftime("%H", gmtime(tmp)))
|
2023-05-02 15:17:59 +00:00
|
|
|
days = int(strftime("%j", gmtime(tmp))) - 1
|
2017-04-10 07:24:23 +00:00
|
|
|
except ValueError as e:
|
|
|
|
return "[-] Invalid TIME"
|
|
|
|
|
|
|
|
if days > 1:
|
2023-04-12 04:25:38 +00:00
|
|
|
time += f"{days} days "
|
2017-04-10 07:24:23 +00:00
|
|
|
elif days == 1:
|
2023-04-12 04:25:38 +00:00
|
|
|
time += f"{days} day "
|
2017-04-10 07:24:23 +00:00
|
|
|
if hours > 1:
|
2023-04-12 04:25:38 +00:00
|
|
|
time += f"{hours} hours "
|
2017-04-10 07:24:23 +00:00
|
|
|
elif hours == 1:
|
2023-04-12 04:25:38 +00:00
|
|
|
time += f"{hours} hour "
|
2017-04-10 07:24:23 +00:00
|
|
|
if minutes > 1:
|
2023-04-12 04:25:38 +00:00
|
|
|
time += f"{minutes} minutes "
|
2017-04-10 07:24:23 +00:00
|
|
|
elif minutes == 1:
|
2023-04-12 04:25:38 +00:00
|
|
|
time += f"{minutes} minute "
|
2017-04-10 07:24:23 +00:00
|
|
|
return time
|
|
|
|
|
2020-11-27 20:51:09 +00:00
|
|
|
|
2017-04-05 15:07:00 +00:00
|
|
|
class PassPolDump:
|
|
|
|
KNOWN_PROTOCOLS = {
|
2023-04-12 04:25:38 +00:00
|
|
|
"139/SMB": (r"ncacn_np:%s[\pipe\samr]", 139),
|
|
|
|
"445/SMB": (r"ncacn_np:%s[\pipe\samr]", 445),
|
2017-04-10 07:24:23 +00:00
|
|
|
}
|
2017-04-05 15:07:00 +00:00
|
|
|
|
|
|
|
def __init__(self, connection):
|
2023-04-14 19:15:23 +00:00
|
|
|
self.logger = cme_logger
|
2023-05-02 15:17:59 +00:00
|
|
|
self.addr = (
|
|
|
|
connection.host
|
|
|
|
if not connection.kerberos
|
|
|
|
else connection.hostname + "." + connection.domain
|
|
|
|
)
|
2017-05-05 21:10:42 +00:00
|
|
|
self.protocol = connection.args.port
|
2017-04-05 15:07:00 +00:00
|
|
|
self.username = connection.username
|
|
|
|
self.password = connection.password
|
|
|
|
self.domain = connection.domain
|
|
|
|
self.hash = connection.hash
|
2023-05-02 15:17:59 +00:00
|
|
|
self.lmhash = ""
|
|
|
|
self.nthash = ""
|
2017-04-05 15:07:00 +00:00
|
|
|
self.aesKey = None
|
2023-02-08 11:09:52 +00:00
|
|
|
self.doKerberos = connection.kerberos
|
2017-04-10 07:24:23 +00:00
|
|
|
self.protocols = PassPolDump.KNOWN_PROTOCOLS.keys()
|
|
|
|
self.pass_pol = {}
|
2017-06-23 18:15:09 +00:00
|
|
|
|
2017-04-05 15:07:00 +00:00
|
|
|
if self.hash is not None:
|
2023-05-02 15:17:59 +00:00
|
|
|
if self.hash.find(":") != -1:
|
|
|
|
self.lmhash, self.nthash = self.hash.split(":")
|
2017-06-23 18:15:09 +00:00
|
|
|
else:
|
|
|
|
self.nthash = self.hash
|
2023-05-02 15:17:59 +00:00
|
|
|
|
2017-04-05 15:07:00 +00:00
|
|
|
if self.password is None:
|
2023-05-02 15:17:59 +00:00
|
|
|
self.password = ""
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
def dump(self):
|
|
|
|
# Try all requested protocols until one works.
|
|
|
|
for protocol in self.protocols:
|
|
|
|
try:
|
|
|
|
protodef = PassPolDump.KNOWN_PROTOCOLS[protocol]
|
|
|
|
port = protodef[1]
|
|
|
|
except KeyError:
|
2023-04-12 04:25:38 +00:00
|
|
|
cme_logger.debug(f"Invalid Protocol '{protocol}'")
|
|
|
|
cme_logger.debug(f"Trying protocol {protocol}")
|
|
|
|
rpctransport = transport.SMBTransport(
|
|
|
|
self.addr,
|
|
|
|
port,
|
|
|
|
r"\samr",
|
|
|
|
self.username,
|
|
|
|
self.password,
|
|
|
|
self.domain,
|
|
|
|
self.lmhash,
|
|
|
|
self.nthash,
|
|
|
|
self.aesKey,
|
2023-05-02 15:17:59 +00:00
|
|
|
doKerberos=self.doKerberos,
|
2023-04-12 04:25:38 +00:00
|
|
|
)
|
2017-04-10 07:24:23 +00:00
|
|
|
try:
|
|
|
|
self.fetchList(rpctransport)
|
|
|
|
except Exception as e:
|
2023-04-12 04:25:38 +00:00
|
|
|
cme_logger.debug(f"Protocol failed: {e}")
|
2017-04-10 07:24:23 +00:00
|
|
|
else:
|
|
|
|
# Got a response. No need for further iterations.
|
|
|
|
self.pretty_print()
|
|
|
|
break
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
return self.pass_pol
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
def fetchList(self, rpctransport):
|
|
|
|
dce = DCERPC_v5(rpctransport)
|
2017-04-05 15:07:00 +00:00
|
|
|
dce.connect()
|
|
|
|
dce.bind(samr.MSRPC_UUID_SAMR)
|
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
# Setup Connection
|
|
|
|
resp = samr.hSamrConnect2(dce)
|
2023-04-12 04:25:38 +00:00
|
|
|
if resp["ErrorCode"] != 0:
|
|
|
|
raise Exception("Connect error")
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
resp2 = samr.hSamrEnumerateDomainsInSamServer(
|
|
|
|
dce,
|
|
|
|
serverHandle=resp["ServerHandle"],
|
|
|
|
enumerationContext=0,
|
|
|
|
preferedMaximumLength=500,
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
if resp2["ErrorCode"] != 0:
|
|
|
|
raise Exception("Connect error")
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
resp3 = samr.hSamrLookupDomainInSamServer(
|
|
|
|
dce,
|
|
|
|
serverHandle=resp["ServerHandle"],
|
|
|
|
name=resp2["Buffer"]["Buffer"][0]["Name"],
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
if resp3["ErrorCode"] != 0:
|
|
|
|
raise Exception("Connect error")
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
resp4 = samr.hSamrOpenDomain(
|
|
|
|
dce,
|
|
|
|
serverHandle=resp["ServerHandle"],
|
|
|
|
desiredAccess=samr.MAXIMUM_ALLOWED,
|
|
|
|
domainId=resp3["DomainId"],
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
if resp4["ErrorCode"] != 0:
|
|
|
|
raise Exception("Connect error")
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2023-04-12 04:25:38 +00:00
|
|
|
self.__domains = resp2["Buffer"]["Buffer"]
|
|
|
|
domainHandle = resp4["DomainHandle"]
|
2017-04-10 07:24:23 +00:00
|
|
|
# End Setup
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
re = samr.hSamrQueryInformationDomain2(
|
|
|
|
dce,
|
|
|
|
domainHandle=domainHandle,
|
|
|
|
domainInformationClass=samr.DOMAIN_INFORMATION_CLASS.DomainPasswordInformation,
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
self.__min_pass_len = re["Buffer"]["Password"]["MinPasswordLength"] or "None"
|
2023-05-02 15:17:59 +00:00
|
|
|
self.__pass_hist_len = (
|
|
|
|
re["Buffer"]["Password"]["PasswordHistoryLength"] or "None"
|
|
|
|
)
|
|
|
|
self.__max_pass_age = convert(
|
|
|
|
int(re["Buffer"]["Password"]["MaxPasswordAge"]["LowPart"]),
|
|
|
|
int(re["Buffer"]["Password"]["MaxPasswordAge"]["HighPart"]),
|
|
|
|
)
|
|
|
|
self.__min_pass_age = convert(
|
|
|
|
int(re["Buffer"]["Password"]["MinPasswordAge"]["LowPart"]),
|
|
|
|
int(re["Buffer"]["Password"]["MinPasswordAge"]["HighPart"]),
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
self.__pass_prop = d2b(re["Buffer"]["Password"]["PasswordProperties"])
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
re = samr.hSamrQueryInformationDomain2(
|
|
|
|
dce,
|
|
|
|
domainHandle=domainHandle,
|
|
|
|
domainInformationClass=samr.DOMAIN_INFORMATION_CLASS.DomainLockoutInformation,
|
|
|
|
)
|
|
|
|
self.__rst_accnt_lock_counter = convert(
|
|
|
|
0, re["Buffer"]["Lockout"]["LockoutObservationWindow"], lockout=True
|
|
|
|
)
|
|
|
|
self.__lock_accnt_dur = convert(
|
|
|
|
0, re["Buffer"]["Lockout"]["LockoutDuration"], lockout=True
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
self.__accnt_lock_thres = re["Buffer"]["Lockout"]["LockoutThreshold"] or "None"
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
re = samr.hSamrQueryInformationDomain2(
|
|
|
|
dce,
|
|
|
|
domainHandle=domainHandle,
|
|
|
|
domainInformationClass=samr.DOMAIN_INFORMATION_CLASS.DomainLogoffInformation,
|
|
|
|
)
|
|
|
|
self.__force_logoff_time = convert(
|
|
|
|
re["Buffer"]["Logoff"]["ForceLogoff"]["LowPart"],
|
|
|
|
re["Buffer"]["Logoff"]["ForceLogoff"]["HighPart"],
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
|
|
|
|
self.pass_pol = {
|
|
|
|
"min_pass_len": self.__min_pass_len,
|
|
|
|
"pass_hist_len": self.__pass_hist_len,
|
|
|
|
"max_pass_age": self.__max_pass_age,
|
|
|
|
"min_pass_age": self.__min_pass_age,
|
|
|
|
"pass_prop": self.__pass_prop,
|
|
|
|
"rst_accnt_lock_counter": self.__rst_accnt_lock_counter,
|
|
|
|
"lock_accnt_dur": self.__lock_accnt_dur,
|
|
|
|
"accnt_lock_thres": self.__accnt_lock_thres,
|
2023-05-02 15:17:59 +00:00
|
|
|
"force_logoff_time": self.__force_logoff_time,
|
2023-04-12 04:25:38 +00:00
|
|
|
}
|
2017-04-10 07:24:23 +00:00
|
|
|
|
2021-06-24 18:37:54 +00:00
|
|
|
dce.disconnect()
|
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
def pretty_print(self):
|
|
|
|
PASSCOMPLEX = {
|
2023-04-12 04:25:38 +00:00
|
|
|
5: "Domain Password Complex:",
|
|
|
|
4: "Domain Password No Anon Change:",
|
|
|
|
3: "Domain Password No Clear Change:",
|
|
|
|
2: "Domain Password Lockout Admins:",
|
|
|
|
1: "Domain Password Store Cleartext:",
|
2023-05-02 15:17:59 +00:00
|
|
|
0: "Domain Refuse Password Change:",
|
2017-04-10 07:24:23 +00:00
|
|
|
}
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2023-04-12 04:25:38 +00:00
|
|
|
cme_logger.debug("Found domain(s):")
|
2017-04-10 07:24:23 +00:00
|
|
|
for domain in self.__domains:
|
2023-04-12 04:25:38 +00:00
|
|
|
cme_logger.debug(f"{domain['Name']}")
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
self.logger.success(
|
|
|
|
f"Dumping password info for domain: {self.__domains[0]['Name']}"
|
|
|
|
)
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2023-04-12 04:25:38 +00:00
|
|
|
self.logger.highlight(f"Minimum password length: {self.__min_pass_len}")
|
|
|
|
self.logger.highlight(f"Password history length: {self.__pass_hist_len}")
|
|
|
|
self.logger.highlight(f"Maximum password age: {self.__max_pass_age}")
|
|
|
|
self.logger.highlight("")
|
2023-05-02 15:17:59 +00:00
|
|
|
self.logger.highlight(
|
|
|
|
f"Password Complexity Flags: {self.__pass_prop or 'None'}"
|
|
|
|
)
|
2017-04-05 15:07:00 +00:00
|
|
|
|
2017-04-10 07:24:23 +00:00
|
|
|
for i, a in enumerate(self.__pass_prop):
|
2023-04-12 04:25:38 +00:00
|
|
|
self.logger.highlight(f"\t{PASSCOMPLEX[i]} {str(a)}")
|
|
|
|
|
|
|
|
self.logger.highlight("")
|
|
|
|
self.logger.highlight(f"Minimum password age: {self.__min_pass_age}")
|
2023-05-02 15:17:59 +00:00
|
|
|
self.logger.highlight(
|
|
|
|
f"Reset Account Lockout Counter: {self.__rst_accnt_lock_counter}"
|
|
|
|
)
|
2023-04-12 04:25:38 +00:00
|
|
|
self.logger.highlight(f"Locked Account Duration: {self.__lock_accnt_dur}")
|
|
|
|
self.logger.highlight(f"Account Lockout Threshold: {self.__accnt_lock_thres}")
|
2023-05-02 15:17:59 +00:00
|
|
|
self.logger.highlight(f"Forced Log off Time: {self.__force_logoff_time}")
|