Skip to content

Commit 3f06ea9

Browse files
authored
Merge pull request Pennyw0rth#542 from termanix/fix-dump-files-deleting
Fix lsass Dump Files Deleting Process When Dump Fail
2 parents 6a6fa94 + f87535e commit 3f06ea9

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

nxc/modules/handlekatz.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def options(self, context, module_options):
5050

5151
def on_admin_login(self, context, connection):
5252
handlekatz_loc = self.handlekatz_path + self.handlekatz
53-
53+
5454
if self.useembeded:
5555
try:
5656
with open(handlekatz_loc, "wb") as handlekatz:
@@ -78,6 +78,7 @@ def on_admin_login(self, context, connection):
7878

7979
if not p or p == "None":
8080
context.log.fail("Failed to execute command to get LSASS PID")
81+
self.delete_handlekatz_binary(connection, context)
8182
return
8283
# we get a CSV string back from `tasklist`, so we grab the PID from it
8384
pid = p.split(",")[1][1:-1]
@@ -96,12 +97,15 @@ def on_admin_login(self, context, connection):
9697
context.log.fail("Process lsass.exe error un dump, try with verbose")
9798
dump = False
9899

99-
if dump:
100+
if not dump:
101+
self.delete_handlekatz_binary(connection, context)
102+
return
103+
else:
100104
regex = r"([A-Za-z0-9-]*\.log)"
101105
matches = re.search(regex, str(p), re.MULTILINE)
102106
if not matches:
103107
context.log.display("Error getting the lsass.dmp file name")
104-
sys.exit(1)
108+
return
105109

106110
machine_name = matches.group()
107111
context.log.display(f"Copy {machine_name} to host")
@@ -113,12 +117,7 @@ def on_admin_login(self, context, connection):
113117
except Exception as e:
114118
context.log.fail(f"Error while get file: {e}")
115119

116-
try:
117-
connection.conn.deleteFile(self.share, self.tmp_share + self.handlekatz)
118-
context.log.success(f"Deleted handlekatz file on the {self.share} share")
119-
except Exception as e:
120-
context.log.fail(f"[OPSEC] Error deleting handlekatz file on share {self.share}: {e}")
121-
120+
self.delete_handlekatz_binary()
122121
try:
123122
connection.conn.deleteFile(self.share, self.tmp_share + machine_name)
124123
context.log.success(f"Deleted lsass.dmp file on the {self.share} share")
@@ -182,3 +181,10 @@ def on_admin_login(self, context, connection):
182181
add_user_bh(credz_bh, None, context.log, connection.config)
183182
except Exception as e:
184183
context.log.fail(f"Error opening dump file: {e}")
184+
185+
def delete_handlekatz_binary(self, connection, context):
186+
try:
187+
connection.conn.deleteFile(self.share, self.tmp_share + self.handlekatz)
188+
context.log.success(f"Deleted handlekatz file on the {self.share} share")
189+
except Exception as e:
190+
context.log.fail(f"[OPSEC] Error deleting handlekatz file on share {self.share}: {e}")

nxc/modules/nanodump.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ def on_admin_login(self, context, connection):
149149
self.context.log.fail("Process lsass.exe error on dump, try with verbose")
150150
dump = False
151151

152-
if dump:
152+
if not dump:
153+
self.delete_nanodump_binary()
154+
return
155+
else:
153156
self.context.log.display(f"Copying {nano_log_name} to host")
154157
filename = os.path.join(self.dir_result, f"{self.connection.hostname}_{self.connection.os_arch}_{self.connection.domain}.log")
155158
if self.context.protocol == "smb":

nxc/modules/procdump.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import base64
66
import re
7-
import sys
87
import pypykatz
98
from nxc.helpers.bloodhound import add_user_bh
109
from nxc.paths import TMP_PATH
@@ -79,15 +78,18 @@ def on_admin_login(self, context, connection):
7978
else:
8079
context.log.fail("Process lsass.exe error un dump, try with verbose")
8180

82-
if dump:
81+
if not dump:
82+
self.delete_procdump_binary(connection, context)
83+
return
84+
else:
8385
regex = r"([A-Za-z0-9-]*.dmp)"
8486
matches = re.search(regex, str(p), re.MULTILINE)
8587
machine_name = ""
8688
if matches:
8789
machine_name = matches.group()
8890
else:
8991
context.log.display("Error getting the lsass.dmp file name")
90-
sys.exit(1)
92+
return
9193

9294
context.log.display(f"Copy {machine_name} to host")
9395

@@ -98,11 +100,7 @@ def on_admin_login(self, context, connection):
98100
except Exception as e:
99101
context.log.fail(f"Error while get file: {e}")
100102

101-
try:
102-
connection.conn.deleteFile(self.share, self.tmp_share + self.procdump)
103-
context.log.success(f"Deleted procdump file on the {self.share} share")
104-
except Exception as e:
105-
context.log.fail(f"Error deleting procdump file on share {self.share}: {e}")
103+
self.delete_procdump_binary(connection, context)
106104

107105
try:
108106
connection.conn.deleteFile(self.share, self.tmp_share + machine_name)
@@ -152,3 +150,10 @@ def on_admin_login(self, context, connection):
152150
add_user_bh(credz_bh, None, context.log, connection.config)
153151
except Exception as e:
154152
context.log.fail("Error openning dump file", str(e))
153+
154+
def delete_procdump_binary(self, connection, context):
155+
try:
156+
connection.conn.deleteFile(self.share, self.tmp_share + self.procdump)
157+
context.log.success(f"Deleted procdump file on the {self.share} share")
158+
except Exception as e:
159+
context.log.fail(f"Error deleting procdump file on share {self.share}: {e}")

0 commit comments

Comments
 (0)