Skip to content

Commit 80b069f

Browse files
committed
Add support for spoofed zip Central Dir names at Entry level
1 parent 46e6f93 commit 80b069f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-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

0 commit comments

Comments
 (0)