Skip to content

Commit f1c2840

Browse files
authored
Update atexec.py
Signed-off-by: Kahvi-0xFF <46513413+Kahvi-0@users.noreply.github.com>
1 parent 51ac6d6 commit f1c2840

1 file changed

Lines changed: 5 additions & 21 deletions

File tree

nxc/protocols/smb/atexec.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import random
3-
import re
43
from textwrap import dedent
54
from impacket.dcerpc.v5 import tsch, transport
65
from impacket.dcerpc.v5.dtypes import NULL
@@ -112,15 +111,8 @@ def gen_xml(self, command):
112111
random.shuffle(idleSettings)
113112
randomized_idleSettings = "\n".join(idleSettings)
114113

115-
match = re.match(r'^(.+?\\[^\\ ]+)\s+(.*)', command)
116-
if match:
117-
cmd_path = match.group(1)
118-
cmd_args = match.group(2)
119-
else:
120-
self.logger.display(f"Full Path not detected, defaulting to CMD")
121-
self.__retOutput = True
122-
cmd_path = f"C:\Windows\System32\cmd.exe"
123-
cmd_args = f"/c {command}"
114+
cmd_path = "C:\Windows\System32\cmd"
115+
cmd_args = f"/c {command}"
124116

125117
xml = f"""<?xml version="1.0" encoding="UTF-16"?>
126118
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
@@ -153,10 +145,7 @@ def gen_xml(self, command):
153145
self.__output_filename = os.path.join(file_location, gen_random_string(8))
154146
else:
155147
self.__output_filename = os.path.join(file_location, self.output_filename)
156-
157-
cmd_output = f"&gt; {self.__output_filename} 2&gt;&amp;1"
158-
argument_xml = f" <Arguments>{cmd_args} {cmd_output}</Arguments>"
159-
148+
argument_xml = f" <Arguments>{cmd_args} &gt; {self.__output_filename} 2&gt;&amp;1</Arguments>"
160149

161150
elif self.__retOutput is False:
162151
argument_xml = f" <Arguments>{cmd_args}</Arguments>"
@@ -209,10 +198,8 @@ def execute_handler(self, command):
209198

210199
self.logger.info(f"Deleting task \\{self.task_name}")
211200
tsch.hSchRpcDelete(dce, f"\\{self.task_name}")
212-
213201
if self.__retOutput:
214202
smbConnection = self.__rpctransport.get_smb_connection()
215-
216203
tries = 1
217204
# Give the command a bit of time to execute before we try to read the output, 0.4 seconds was good in testing
218205
sleep(0.4)
@@ -226,8 +213,8 @@ def execute_handler(self, command):
226213
self.logger.fail("ATEXEC: Could not retrieve output file, it may have been detected by AV. Please increase the number of tries with the option '--get-output-tries'. If it is still failing, try the 'wmi' protocol or another exec method")
227214
break
228215
if "STATUS_BAD_NETWORK_NAME" in str(e):
229-
self.logger.fail(f"ATEXEC: Getting the output file failed - target has blocked access to the share: {self.__share} (but the command may have executed!)")
230-
break
216+
self.logger.fail(f"ATEXEC: Getting the output file failed - target has blocked access to the share: {self.__share} (but the command may have executed!)")
217+
break
231218
elif "STATUS_VIRUS_INFECTED" in str(e):
232219
self.logger.fail("Command did not run because a virus was detected")
233220
break
@@ -245,13 +232,10 @@ def execute_handler(self, command):
245232
self.logger.debug(f"Exception when trying to read output file: {e!s}. {self.__tries - tries} tries left, retrying...")
246233
tries += 1
247234
sleep(1)
248-
249235
try:
250236
self.logger.debug(f"Deleting file {self.__share}\\{self.__output_filename}")
251237
smbConnection.deleteFile(self.__share, self.__output_filename)
252238
except Exception:
253239
pass
254240

255-
else:
256-
self.logger.display("No output file was saved to be retrived")
257241
dce.disconnect()

0 commit comments

Comments
 (0)