add a simple method for adding multiple files to a Jar from the file system. may eventually bubble this up to Rex::Zip::Archive

git-svn-id: file:///home/svn/framework3/trunk@10871 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
James Lee
2010-11-02 23:10:35 +00:00
parent db4c6ae246
commit 8e44f66d31
2 changed files with 61 additions and 20 deletions
+7 -20
View File
@@ -16,8 +16,9 @@ module Msf::Payload::Java
def generate_stage
stage = ''
@stage_class_files.each do |path|
fd = File.open(File.join( Msf::Config.install_root, "data", "java", path ), "rb")
fd = File.open(File.join( Msf::Config.data_directory, "java", path ), "rb")
data = fd.read(fd.stat.size)
fd.close
stage << ([data.length].pack("N") + data)
end
stage << [0].pack("N")
@@ -33,7 +34,7 @@ module Msf::Payload::Java
end
#
# Returns a jar file as a +Rex::Zip::Archive+
# Returns a jar file as a +Rex::Zip::Jar+
#
def generate_jar
raise if not respond_to? :config
@@ -42,7 +43,8 @@ module Msf::Payload::Java
] + @class_files
jar = Rex::Zip::Jar.new
add_class_files(jar, paths)
#add_class_files(jar, paths)
jar.add_files(paths, File.join(Msf::Config.data_directory, "java"))
jar.build_manifest(:main_class => "metasploit.Payload")
jar.add_file("metasploit.dat", config)
@@ -50,7 +52,7 @@ module Msf::Payload::Java
end
def generate_war(opts={})
zip = Rex::Zip::Archive.new
zip = Rex::Zip::Jar.new
web_xml = %q{<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
@@ -79,7 +81,7 @@ module Msf::Payload::Java
zip.add_file('WEB-INF/', '')
zip.add_file('WEB-INF/web.xml', web_xml)
zip.add_file("WEB-INF/classes/", "")
add_class_files(zip, paths, "WEB-INF/classes/")
zip.add_files(paths, File.join(Msf::Config.data_directory, "java"), "WEB-INF/classes/")
zip.add_file("metasploit.dat", config)
zip.add_file("WEB-INF/metasploit.dat", config)
zip.add_file("WEB-INF/classes/metasploit.dat", config)
@@ -87,20 +89,5 @@ module Msf::Payload::Java
zip
end
protected
def add_class_files(zip, paths, base_dir="")
paths.each do |path|
1.upto(path.length - 1) do |idx|
full = base_dir + path[0,idx].join("/") + "/"
if !(zip.entries.map{|e|e.name}.include?(full))
zip.add_file(full, '')
end
end
fd = File.open(File.join( Msf::Config.install_root, "data", "java", path ), "rb")
data = fd.read(fd.stat.size)
zip.add_file(base_dir + path.join("/"), data)
fd.close
end
end
end
+54
View File
@@ -113,6 +113,60 @@ class Jar < Archive
def length
pack.length
end
#
# Add multiple files from an array
#
# +files+ should be structured like so:
# [
# [ "path", "to", "file1" ],
# [ "path", "to", "file2" ]
# ]
# and +path+ should be the location on the file system to find the files to
# add. +base_dir+ will be prepended to the path inside the jar.
#
# Example:
# <code>
# war = Rex::Zip::Jar.new
# war.add_file("WEB-INF/", '')
# war.add_file("WEB-INF/", "web.xml", web_xml)
# war.add_file("WEB-INF/classes/", '')
# files = [
# [ "servlet", "examples", "HelloWorld.class" ],
# [ "Foo.class" ],
# [ "servlet", "Bar.class" ],
# ]
# war.add_files(files, "./class_files/", "WEB-INF/classes/")
# </code>
#
# The above code would create a jar with the following structure from files
# found in ./class_files/ :
#
# +- WEB-INF/
# +- web.xml
# +- classes/
# +- Foo.class
# +- servlet/
# +- Bar.class
# +- examples/
# +- HelloWorld.class
#
def add_files(files, path, base_dir="")
files.each do |file|
# Add all of the subdirectories if they don't already exist
1.upto(file.length - 1) do |idx|
full = base_dir + file[0,idx].join("/") + "/"
if !(entries.map{|e|e.name}.include?(full))
add_file(full, '')
end
end
# Now add the actual file, grabbing data from the filesystem
fd = File.open(File.join( path, file ), "rb")
data = fd.read(fd.stat.size)
fd.close
add_file(base_dir + file.join("/"), data)
end
end
end
end