Skip to content

Commit 8f7d0ea

Browse files
committed
Fix rapid7#7155 - Add post module to compress (zip) a file or directory
Fix rapid7#7155
1 parent 10653fa commit 8f7d0ea

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

data/post/zip/zip.vbs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Function WindowsZip(sFile, sZipFile)
2+
'This script is provided under the Creative Commons license located
3+
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
4+
'be used for commercial purposes with out the expressed written consent
5+
'of NateRice.com
6+
7+
Set oZipShell = CreateObject("WScript.Shell")
8+
Set oZipFSO = CreateObject("Scripting.FileSystemObject")
9+
10+
If Not oZipFSO.FileExists(sZipFile) Then
11+
NewZip(sZipFile)
12+
End If
13+
14+
Set oZipApp = CreateObject("Shell.Application")
15+
16+
sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
17+
18+
aFileName = Split(sFile, "\")
19+
sFileName = (aFileName(Ubound(aFileName)))
20+
21+
'listfiles
22+
sDupe = False
23+
For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
24+
If LCase(sFileName) = LCase(sFileNameInZip) Then
25+
sDupe = True
26+
Exit For
27+
End If
28+
Next
29+
30+
If Not sDupe Then
31+
oZipApp.NameSpace(sZipFile).Copyhere sFile
32+
33+
'Keep script waiting until Compressing is done
34+
On Error Resume Next
35+
sLoop = 0
36+
Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
37+
Wscript.Sleep(100)
38+
sLoop = sLoop + 1
39+
Loop
40+
On Error GoTo 0
41+
End If
42+
End Function
43+
44+
Sub NewZip(sNewZip)
45+
'This script is provided under the Creative Commons license located
46+
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
47+
'be used for commercial purposes with out the expressed written consent
48+
'of NateRice.com
49+
50+
Set oNewZipFSO = CreateObject("Scripting.FileSystemObject")
51+
Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)
52+
53+
oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
54+
55+
oNewZipFile.Close
56+
Set oNewZipFSO = Nothing
57+
58+
Wscript.Sleep(500)
59+
End Sub
60+

modules/post/multi/manage/zip.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
8+
class MetasploitModule < Msf::Post
9+
10+
include Msf::Post::File
11+
12+
def initialize(info={})
13+
super(update_info(info,
14+
'Name' => 'Multi Manage File Compressor',
15+
'Description' => %q{
16+
This module zips a directory or a directory. On Linux, it uses the zip command.
17+
On Windows, it will try to use remote target's 7Zip if found. If not, it falls
18+
back to its own VBScript.
19+
},
20+
'License' => MSF_LICENSE,
21+
'Author' => [ 'sinn3r' ],
22+
'Platform' => [ 'win', 'linux' ],
23+
'SessionTypes' => [ 'meterpreter', 'shell' ]
24+
))
25+
26+
register_options(
27+
[
28+
OptString.new('DESTINATION', [false, 'The destination path']),
29+
OptString.new('SOURCE', [true, 'The directory or file to compress'])
30+
], self.class)
31+
end
32+
33+
def get_program_file_path
34+
@program_file_path ||= lambda {
35+
session.sys.config.getenvs("ProgramFiles")['ProgramFiles']
36+
}.call
37+
end
38+
39+
def has_7zip?
40+
file?("#{get_program_file_path}\\7-Zip\\7z.exe")
41+
end
42+
43+
def vbs(dest, src)
44+
vbs_file = File.read(File.join(Msf::Config.data_directory, "post", "zip", "zip.vbs"))
45+
vbs_file << "WindowsZip \"#{src}\",\"#{dest}\""
46+
vbs_file
47+
end
48+
49+
def upload_exec_vbs_zip
50+
script = vbs(datastore['DESTINATION'], datastore['SOURCE'])
51+
tmp_path = "#{session.sys.config.getenvs('TEMP')['TEMP']}\\zip.vbs"
52+
print_status("VBS file uploaded to #{tmp_path}")
53+
write_file(tmp_path, script)
54+
cmd_exec("wscript.exe #{tmp_path}")
55+
end
56+
57+
def do_7zip
58+
program_file_path = get_program_file_path
59+
output = cmd_exec("#{program_file_path}\\7-Zip\\7z.exe a -tzip \"#{datastore['DESTINATION']}\" \"#{datastore['SOURCE']}\"")
60+
vprint_line(output)
61+
end
62+
63+
def do_zip
64+
output = cmd_exec("zip -D -d -q -r #{datastore['DESTINATION']} #{datastore['SOURCE']}")
65+
vprint_line(output)
66+
end
67+
68+
def windows_zip
69+
if has_7zip?
70+
print_status("Compressing #{datastore['DESTINATION']} via 7zip")
71+
do_7zip
72+
else
73+
print_status("Compressing #{datastore['DESTINATION']} via VBS")
74+
upload_exec_vbs_zip
75+
end
76+
end
77+
78+
def linux_zip
79+
print_status("Compressing #{datastore['DESTINATION']} via zip")
80+
do_zip
81+
end
82+
83+
def run
84+
os = get_target_os
85+
case os
86+
when Msf::Module::Platform::Windows.realname.downcase
87+
windows_zip
88+
else
89+
linux_zip
90+
end
91+
end
92+
93+
end
94+

0 commit comments

Comments
 (0)