Skip to content

Commit ab579b3

Browse files
committed
Change Trigger to type RegistrationTrigger and add end boundary to prevent execution after some time if something fails, see Pennyw0rth#481
1 parent bab8acb commit ab579b3

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

nxc/modules/schtask_as.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from time import sleep
3-
from datetime import datetime
3+
from datetime import datetime, timedelta
44
from impacket.dcerpc.v5.dtypes import NULL
55
from impacket.dcerpc.v5 import tsch, transport
66
from nxc.helpers.misc import gen_random_string
@@ -92,6 +92,8 @@ def on_admin_login(self, context, connection):
9292
if "SCHED_S_TASK_HAS_NOT_RUN" in str(e):
9393
self.logger.fail("Task was not run, seems like the specified user has no active session on the target")
9494
exec_method.deleteartifact()
95+
else:
96+
self.logger.fail(f"Failed to execute command: {e}")
9597

9698
class TSCH_EXEC:
9799
def __init__(self, target, share_name, username, password, domain, user, cmd, file, task, location, doKerberos=False, aesKey=None, remoteHost=None, kdcHost=None, hashes=None, logger=None, tries=None, share=None):
@@ -163,24 +165,20 @@ def execute(self, command, output=False):
163165
def output_callback(self, data):
164166
self.__outputBuffer = data
165167

166-
def get_current_date(self):
168+
def get_end_boundary(self):
167169
# Get current date and time
168-
now = datetime.now()
170+
end_boundary = datetime.now() + timedelta(minutes=5)
169171

170172
# Format it to match the format in the XML: "YYYY-MM-DDTHH:MM:SS.ssssss"
171-
return now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
173+
return end_boundary.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
172174

173175
def gen_xml(self, command, fileless=False):
174176
xml = f"""<?xml version="1.0" encoding="UTF-16"?>
175177
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
176178
<Triggers>
177-
<CalendarTrigger>
178-
<StartBoundary>{self.get_current_date()}</StartBoundary>
179-
<Enabled>true</Enabled>
180-
<ScheduleByDay>
181-
<DaysInterval>1</DaysInterval>
182-
</ScheduleByDay>
183-
</CalendarTrigger>
179+
<RegistrationTrigger>
180+
<EndBoundary>{self.get_end_boundary()}</EndBoundary>
181+
</RegistrationTrigger>
184182
</Triggers>
185183
<Principals>
186184
<Principal id="LocalSystem">

0 commit comments

Comments
 (0)