Skip to content

Commit e874223

Browse files
committed
Land rapid7#3083, fix pymet when ctypes isn't available
2 parents b431bf3 + b87c2dc commit e874223

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ctypes
21
import fnmatch
32
import getpass
43
import os
@@ -10,7 +9,13 @@
109
import subprocess
1110
import sys
1211

13-
has_windll = hasattr(ctypes, 'windll')
12+
try:
13+
import ctypes
14+
has_ctypes = True
15+
has_windll = hasattr(ctypes, 'windll')
16+
except:
17+
has_windll = False
18+
has_ctypes = False
1419

1520
try:
1621
import pty
@@ -36,34 +41,35 @@
3641
except ImportError:
3742
has_winreg = False
3843

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),]
44+
if has_ctypes:
45+
class PROCESSENTRY32(ctypes.Structure):
46+
_fields_ = [("dwSize", ctypes.c_uint32),
47+
("cntUsage", ctypes.c_uint32),
48+
("th32ProcessID", ctypes.c_uint32),
49+
("th32DefaultHeapID", ctypes.c_void_p),
50+
("th32ModuleID", ctypes.c_uint32),
51+
("cntThreads", ctypes.c_uint32),
52+
("th32ParentProcessID", ctypes.c_uint32),
53+
("thPriClassBase", ctypes.c_int32),
54+
("dwFlags", ctypes.c_uint32),
55+
("szExeFile", (ctypes.c_char * 260))]
56+
57+
class SYSTEM_INFO(ctypes.Structure):
58+
_fields_ = [("wProcessorArchitecture", ctypes.c_uint16),
59+
("wReserved", ctypes.c_uint16),
60+
("dwPageSize", ctypes.c_uint32),
61+
("lpMinimumApplicationAddress", ctypes.c_void_p),
62+
("lpMaximumApplicationAddress", ctypes.c_void_p),
63+
("dwActiveProcessorMask", ctypes.c_uint32),
64+
("dwNumberOfProcessors", ctypes.c_uint32),
65+
("dwProcessorType", ctypes.c_uint32),
66+
("dwAllocationGranularity", ctypes.c_uint32),
67+
("wProcessorLevel", ctypes.c_uint16),
68+
("wProcessorRevision", ctypes.c_uint16),]
69+
70+
class SID_AND_ATTRIBUTES(ctypes.Structure):
71+
_fields_ = [("Sid", ctypes.c_void_p),
72+
("Attributes", ctypes.c_uint32),]
6773

6874
##
6975
# STDAPI
@@ -675,12 +681,12 @@ def stdapi_fs_ls(request, response):
675681

676682
@meterpreter.register_function
677683
def stdapi_fs_md5(request, response):
678-
if sys.version_info[0] == 2 and sys.version_info[1] < 5:
679-
import md5
680-
m = md5.new()
681-
else:
684+
try:
682685
import hashlib
683686
m = hashlib.md5()
687+
except ImportError:
688+
import md5
689+
m = md5.new()
684690
path = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
685691
m.update(open(path, 'rb').read())
686692
response += tlv_pack(TLV_TYPE_FILE_NAME, m.digest())
@@ -722,12 +728,12 @@ def stdapi_fs_separator(request, response):
722728

723729
@meterpreter.register_function
724730
def stdapi_fs_sha1(request, response):
725-
if sys.version_info[0] == 2 and sys.version_info[1] < 5:
726-
import sha1
727-
m = sha1.new()
728-
else:
731+
try:
729732
import hashlib
730733
m = hashlib.sha1()
734+
except ImportError:
735+
import sha
736+
m = sha.new()
731737
path = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
732738
m.update(open(path, 'rb').read())
733739
response += tlv_pack(TLV_TYPE_FILE_NAME, m.digest())

data/meterpreter/meterpreter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/python
22
import code
3-
import ctypes
3+
try:
4+
import ctypes
5+
except:
6+
has_windll = False
7+
else:
8+
has_windll = hasattr(ctypes, 'windll')
9+
410
import os
511
import random
612
import select
@@ -10,8 +16,6 @@
1016
import sys
1117
import threading
1218

13-
has_windll = hasattr(ctypes, 'windll')
14-
1519
#
1620
# Constants
1721
#

0 commit comments

Comments
 (0)