Skip to content

Commit 991f871

Browse files
authored
Merge pull request Pennyw0rth#777 from Dfte/patch_qwinsta_tasklist
Patch --qwinsta --tasklist stack trace
2 parents bbca423 + 9e8ae03 commit 991f871

1 file changed

Lines changed: 41 additions & 34 deletions

File tree

nxc/protocols/smb.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -964,15 +964,19 @@ def enumerate_sessions_info(self, sessions):
964964
sessions[SessionId]["DisconnectTime"] = sessdata["LSMSessionInfoExPtr"]["LSM_SessionInfo_Level1"]["DisconnectTime"]
965965
sessions[SessionId]["LogonTime"] = sessdata["LSMSessionInfoExPtr"]["LSM_SessionInfo_Level1"]["LogonTime"]
966966
sessions[SessionId]["LastInputTime"] = sessdata["LSMSessionInfoExPtr"]["LSM_SessionInfo_Level1"]["LastInputTime"]
967-
with TSTS.RCMPublic(self.conn, self.host, self.kerberos) as rcm:
968-
for SessionId in sessions:
969-
try:
970-
client = rcm.hRpcGetRemoteAddress(SessionId)
971-
if not client:
972-
continue
973-
sessions[SessionId]["RemoteIp"] = client["pRemoteAddress"]["ipv4"]["in_addr"]
974-
except Exception as e:
975-
self.logger.debug(f"Error getting client address for session {SessionId}: {e}")
967+
968+
try:
969+
with TSTS.RCMPublic(self.conn, self.host, self.kerberos) as rcm:
970+
for SessionId in sessions:
971+
try:
972+
client = rcm.hRpcGetRemoteAddress(SessionId)
973+
if not client:
974+
continue
975+
sessions[SessionId]["RemoteIp"] = client["pRemoteAddress"]["ipv4"]["in_addr"]
976+
except Exception as e:
977+
self.logger.debug(f"Error getting client address for session {SessionId}: {e}")
978+
except SessionError:
979+
self.logger.fail("RDP is probably not enabled, cannot list remote IPv4 addresses.")
976980

977981
@requires_admin
978982
def qwinsta(self):
@@ -1056,31 +1060,34 @@ def qwinsta(self):
10561060

10571061
@requires_admin
10581062
def tasklist(self):
1059-
with TSTS.LegacyAPI(self.conn, self.host, self.kerberos) as legacy:
1060-
try:
1061-
handle = legacy.hRpcWinStationOpenServer()
1062-
res = legacy.hRpcWinStationGetAllProcesses(handle)
1063-
except Exception as e:
1064-
# TODO: Issue https://github.com/fortra/impacket/issues/1816
1065-
self.logger.debug(f"Exception while calling hRpcWinStationGetAllProcesses: {e}")
1066-
return
1067-
if not res:
1068-
return
1069-
self.logger.success("Enumerated processes")
1070-
maxImageNameLen = max(len(i["ImageName"]) for i in res)
1071-
maxSidLen = max(len(i["pSid"]) for i in res)
1072-
template = "{: <%d} {: <8} {: <11} {: <%d} {: >12}" % (maxImageNameLen, maxSidLen) # noqa: UP031
1073-
self.logger.highlight(template.format("Image Name", "PID", "Session#", "SID", "Mem Usage"))
1074-
self.logger.highlight(template.replace(": ", ":=").format("", "", "", "", ""))
1075-
for procInfo in res:
1076-
row = template.format(
1077-
procInfo["ImageName"],
1078-
procInfo["UniqueProcessId"],
1079-
procInfo["SessionId"],
1080-
procInfo["pSid"],
1081-
"{:,} K".format(procInfo["WorkingSetSize"] // 1000),
1082-
)
1083-
self.logger.highlight(row)
1063+
try:
1064+
with TSTS.LegacyAPI(self.conn, self.host, self.kerberos) as legacy:
1065+
try:
1066+
handle = legacy.hRpcWinStationOpenServer()
1067+
res = legacy.hRpcWinStationGetAllProcesses(handle)
1068+
except Exception as e:
1069+
# TODO: Issue https://github.com/fortra/impacket/issues/1816
1070+
self.logger.debug(f"Exception while calling hRpcWinStationGetAllProcesses: {e}")
1071+
return
1072+
if not res:
1073+
return
1074+
self.logger.success("Enumerated processes")
1075+
maxImageNameLen = max(len(i["ImageName"]) for i in res)
1076+
maxSidLen = max(len(i["pSid"]) for i in res)
1077+
template = "{: <%d} {: <8} {: <11} {: <%d} {: >12}" % (maxImageNameLen, maxSidLen) # noqa: UP031
1078+
self.logger.highlight(template.format("Image Name", "PID", "Session#", "SID", "Mem Usage"))
1079+
self.logger.highlight(template.replace(": ", ":=").format("", "", "", "", ""))
1080+
for procInfo in res:
1081+
row = template.format(
1082+
procInfo["ImageName"],
1083+
procInfo["UniqueProcessId"],
1084+
procInfo["SessionId"],
1085+
procInfo["pSid"],
1086+
"{:,} K".format(procInfo["WorkingSetSize"] // 1000),
1087+
)
1088+
self.logger.highlight(row)
1089+
except SessionError:
1090+
self.logger.fail("Cannot list remote tasks, RDP is probably disabled.")
10841091

10851092
def shares(self):
10861093
temp_dir = ntpath.normpath("\\" + gen_random_string())

0 commit comments

Comments
 (0)