Skip to content

Commit 638c101

Browse files
authored
Merge pull request Pennyw0rth#782 from Dfte/tasklist_grep
Add process filtering in --tasklist
2 parents dc239da + bfcc735 commit 638c101

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

nxc/protocols/smb.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,16 @@ def qwinsta(self):
10861086

10871087
@requires_admin
10881088
def tasklist(self):
1089+
# Formats a row to be printed on screen
1090+
def format_row(procInfo):
1091+
return template.format(
1092+
procInfo["ImageName"],
1093+
procInfo["UniqueProcessId"],
1094+
procInfo["SessionId"],
1095+
procInfo["pSid"],
1096+
f"{procInfo['WorkingSetSize'] // 1000:,} K",
1097+
)
1098+
10891099
try:
10901100
with TSTS.LegacyAPI(self.conn, self.host, self.kerberos) as legacy:
10911101
try:
@@ -1100,18 +1110,27 @@ def tasklist(self):
11001110
self.logger.success("Enumerated processes")
11011111
maxImageNameLen = max(len(i["ImageName"]) for i in res)
11021112
maxSidLen = max(len(i["pSid"]) for i in res)
1103-
template = "{: <%d} {: <8} {: <11} {: <%d} {: >12}" % (maxImageNameLen, maxSidLen) # noqa: UP031
1113+
template = f"{{: <{maxImageNameLen}}} {{: <8}} {{: <11}} {{: <{maxSidLen}}} {{: >12}}"
11041114
self.logger.highlight(template.format("Image Name", "PID", "Session#", "SID", "Mem Usage"))
11051115
self.logger.highlight(template.replace(": ", ":=").format("", "", "", "", ""))
1116+
found_task = False
1117+
1118+
# For each process on the remote host
11061119
for procInfo in res:
1107-
row = template.format(
1108-
procInfo["ImageName"],
1109-
procInfo["UniqueProcessId"],
1110-
procInfo["SessionId"],
1111-
procInfo["pSid"],
1112-
"{:,} K".format(procInfo["WorkingSetSize"] // 1000),
1113-
)
1114-
self.logger.highlight(row)
1120+
# If args.tasklist is not True then a process name was supplied
1121+
if self.args.tasklist is not True:
1122+
# So we look for it and print its information if found
1123+
if self.args.tasklist.lower() in procInfo["ImageName"].lower():
1124+
found_task = True
1125+
self.logger.highlight(format_row(procInfo))
1126+
# Else, no process was supplied, we print the entire list of remote processes
1127+
else:
1128+
self.logger.highlight(format_row(procInfo))
1129+
1130+
# If a process was suppliad to args.tasklist and it was not found, we print a fail message
1131+
if self.args.tasklist is not True and not found_task:
1132+
self.logger.fail(f"Didn't find process {self.args.tasklist}")
1133+
11151134
except SessionError:
11161135
self.logger.fail("Cannot list remote tasks, RDP is probably disabled.")
11171136

nxc/protocols/smb/proto_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def proto_args(parser, parents):
5454
mapping_enum_group.add_argument("--pass-pol", action="store_true", help="dump password policy")
5555
mapping_enum_group.add_argument("--rid-brute", nargs="?", type=int, const=4000, metavar="MAX_RID", help="Enumerate users by bruteforcing RIDs")
5656
mapping_enum_group.add_argument("--qwinsta", action="store_true", help="Enumerate RDP connections")
57-
mapping_enum_group.add_argument("--tasklist", action="store_true", help="Enumerate running processes")
57+
mapping_enum_group.add_argument("--tasklist", type=str, nargs="?", const=True, help="Enumerate running processes and filter for the specified one if specified")
5858
mapping_enum_group.add_argument("--taskkill", type=str, help="Kills a specific PID or a proces name's PID's")
5959

6060
wmi_group = smb_parser.add_argument_group("WMI", "Options for WMI Queries")

0 commit comments

Comments
 (0)