Skip to content

Commit 887b773

Browse files
committed
[Exec-command] Add logging / delay functions in semi-interactive shell mode
Signed-off-by: XiaoliChan <[email protected]>
1 parent d2cf353 commit 887b773

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

lib/modules/exec_command.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,19 @@ def clear(self, ClassName_StoreOutput=None):
163163
class_Method.remove_Class(ClassName=ClassName_StoreOutput, return_iWbemServices=False)
164164

165165
class EXEC_COMMAND_SHELL(cmd.Cmd):
166-
def __init__(self, iWbemLevel1Login, dcom, codec):
166+
def __init__(self, iWbemLevel1Login, dcom, codec, addr):
167167
cmd.Cmd.__init__(self)
168-
self.codec = codec
169168
self.dcom = dcom
169+
self.codec = codec
170+
self.hostname = addr
171+
self.save_Path = 'save/'+self.hostname
172+
self.save_fileName = str(int(time.time())) + ".txt"
173+
self.logging = False
174+
self.interval = 5
170175
self.cwd = 'C:\Windows\System32'
171176
self.prompt = "%s>" %self.cwd
172177
self.intro = '[!] Launching semi-interactive shell - Careful what you execute'
178+
173179
self.iWbemLevel1Login = iWbemLevel1Login
174180
self.executer = executeVBS_Toolkit(self.iWbemLevel1Login)
175181
self.ClassName_StoreOutput = "Win32_OSRecoveryConfigurationDataBackup"
@@ -179,16 +185,48 @@ def __init__(self, iWbemLevel1Login, dcom, codec):
179185
self.iWbemServices_Reuse_cimv2 = class_Method.check_ClassStatus(self.ClassName_StoreOutput, return_iWbemServices=True)
180186
self.iWbemServices_Reuse_subscription = None
181187

188+
def do_help(self, line):
189+
print("""
190+
delay {seconds} - set interval time in command execution (default is 5 seconds).
191+
logging - logging everythings.
192+
exit - exit.
193+
""")
194+
195+
def do_logging(self, line):
196+
print("[+] Start logging.")
197+
print("[+] Save command result to: {}/{}".format(self.save_Path, self.save_fileName))
198+
self.logging = True
199+
200+
def do_delay(self, seconds):
201+
print("[+] Set interval time to: %s" %str(seconds))
202+
self.interval = int(seconds)
203+
182204
def do_exit(self, line):
183205
self.dcom.disconnect()
184206
sys.exit(1)
185207

186-
def process_Result(self, result):
208+
def interval_Timer(self, seconds):
209+
for i in range(seconds,0,-1):
210+
print(f"[+] Waiting {i}s for next step.", end="\r", flush=True)
211+
time.sleep(1)
212+
print("\r\n[+] Results: \r\n")
213+
214+
def save_ToFile(self, content):
215+
if os.path.exists(self.save_Path) == False:
216+
os.makedirs(self.save_Path, exist_ok=True)
217+
218+
with open("{}/{}".format(self.save_Path, self.save_fileName), 'a+') as f: f.write(content)
219+
220+
def process_Result(self, result, command):
187221
tmp_list = re.split(r'\[COMMAND\]|\[PATH\]',result)
188222
self.cwd = tmp_list[2].strip('\r\n').lstrip()
189223
cmd_Result = tmp_list[1].strip('\r\n').lstrip()
190224
self.prompt = "%s>" %self.cwd
191225
print(cmd_Result + "\r\n")
226+
227+
if self.logging == True:
228+
content = "{} {}\r\n\r\n{}\r\n\r\n".format(self.prompt, command, cmd_Result)
229+
self.save_ToFile(content)
192230

193231
def default(self, line):
194232
FileName = str(uuid.uuid4()) + ".log"
@@ -207,15 +245,12 @@ def default(self, line):
207245
tag, self.iWbemServices_Reuse_subscription = self.executer.ExecuteVBS(vbs_content=vbs, returnTag=True, BlockVerbose=True, iWbemServices=self.iWbemServices_Reuse_subscription ,return_iWbemServices=True)
208246

209247
# Wait 5 seconds for next step.
210-
for i in range(5,0,-1):
211-
print(f"[+] Waiting {i}s for next step.", end="\r", flush=True)
212-
time.sleep(1)
213-
print("\r\n[+] Results: \r\n")
214-
248+
self.interval_Timer(self.interval)
249+
215250
self.executer.remove_Event(tag, BlockVerbose=True, iWbemServices=self.iWbemServices_Reuse_subscription)
216251

217252
command_ResultObject, resp = self.iWbemServices_Reuse_cimv2.GetObject('{}.CreationClassName="{}"'.format(self.ClassName_StoreOutput, CMD_instanceID))
218253
record = dict(command_ResultObject.getProperties())
219254
result = base64.b64decode(record['DebugOptions']['value']).decode(self.codec, errors='replace')
220-
self.process_Result(result)
255+
self.process_Result(result, line)
221256

0 commit comments

Comments
 (0)