Skip to content

Commit 056ee4f

Browse files
committed
Land rapid7#3958, kill command for pyterp
2 parents 3e92892 + 766a69e commit 056ee4f

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,14 @@ class RTATTR(ctypes.Structure):
472472
ERROR_CONNECTION_ERROR = 10000
473473

474474
# Windows Constants
475-
GAA_FLAG_SKIP_ANYCAST = 0x0002
476-
GAA_FLAG_SKIP_MULTICAST = 0x0004
477-
GAA_FLAG_INCLUDE_PREFIX = 0x0010
478-
GAA_FLAG_SKIP_DNS_SERVER = 0x0080
475+
GAA_FLAG_SKIP_ANYCAST = 0x0002
476+
GAA_FLAG_SKIP_MULTICAST = 0x0004
477+
GAA_FLAG_INCLUDE_PREFIX = 0x0010
478+
GAA_FLAG_SKIP_DNS_SERVER = 0x0080
479+
PROCESS_TERMINATE = 0x0001
480+
PROCESS_VM_READ = 0x0010
481+
PROCESS_QUERY_INFORMATION = 0x0400
482+
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
479483

480484
WIN_AF_INET = 2
481485
WIN_AF_INET6 = 23
@@ -666,12 +670,11 @@ def stdapi_sys_config_sysinfo(request, response):
666670

667671
@meterpreter.register_function
668672
def stdapi_sys_process_close(request, response):
669-
proc_h_id = packet_get_tlv(request, TLV_TYPE_PROCESS_HANDLE)
673+
proc_h_id = packet_get_tlv(request, TLV_TYPE_HANDLE)
670674
if not proc_h_id:
671675
return ERROR_SUCCESS, response
672676
proc_h_id = proc_h_id['value']
673-
proc_h = meterpreter.channels[proc_h_id]
674-
proc_h.kill()
677+
del meterpreter.processes[proc_h_id]
675678
return ERROR_SUCCESS, response
676679

677680
@meterpreter.register_function
@@ -720,6 +723,23 @@ def stdapi_sys_process_getpid(request, response):
720723
response += tlv_pack(TLV_TYPE_PID, os.getpid())
721724
return ERROR_SUCCESS, response
722725

726+
@meterpreter.register_function
727+
def stdapi_sys_process_kill(request, response):
728+
for pid in packet_enum_tlvs(request, TLV_TYPE_PID):
729+
pid = pid['value']
730+
if has_windll:
731+
k32 = ctypes.windll.kernel32
732+
proc_h = k32.OpenProcess(PROCESS_TERMINATE, False, pid)
733+
if not proc_h:
734+
return ERROR_FAILURE, response
735+
if not k32.TerminateProcess(proc_h, 0):
736+
return ERROR_FAILURE, response
737+
elif hasattr(os, 'kill'):
738+
os.kill(pid, 9)
739+
else:
740+
return ERROR_FAILURE, response
741+
return ERROR_SUCCESS, response
742+
723743
def stdapi_sys_process_get_processes_via_proc(request, response):
724744
for pid in os.listdir('/proc'):
725745
pgroup = bytes()
@@ -772,9 +792,6 @@ def stdapi_sys_process_get_processes_via_ps(request, response):
772792

773793
def stdapi_sys_process_get_processes_via_windll(request, response):
774794
TH32CS_SNAPPROCESS = 2
775-
PROCESS_QUERY_INFORMATION = 0x0400
776-
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
777-
PROCESS_VM_READ = 0x10
778795
TOKEN_QUERY = 0x0008
779796
TokenUser = 1
780797
k32 = ctypes.windll.kernel32

data/meterpreter/meterpreter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ def run(self):
332332
response = self.create_response(request)
333333
self.socket.send(response)
334334
else:
335-
channels_for_removal = []
336335
# iterate over the keys because self.channels could be modified if one is closed
337336
channel_ids = list(self.channels.keys())
338337
for channel_id in channel_ids:

0 commit comments

Comments
 (0)