Skip to content

Commit 83160b7

Browse files
committed
Land rapid7#7173, Add post module to compress (zip) a file or directory
2 parents 03e14ec + 89c3b6f commit 83160b7

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

data/post/zip/zip.vbs

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

modules/post/multi/manage/zip.rb

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
include Msf::Post::Windows::Priv
12+
13+
def initialize(info={})
14+
super(update_info(info,
15+
'Name' => 'Multi Manage File Compressor',
16+
'Description' => %q{
17+
This module zips a file or a directory. On Linux, it uses the zip command.
18+
On Windows, it will try to use remote target's 7Zip if found. If not, it falls
19+
back to its own VBScript.
20+
},
21+
'License' => MSF_LICENSE,
22+
'Author' => [ 'sinn3r' ],
23+
'Platform' => [ 'win', 'linux' ],
24+
'SessionTypes' => [ 'meterpreter', 'shell' ]
25+
))
26+
27+
register_options(
28+
[
29+
OptString.new('DESTINATION', [true, 'The destination path']),
30+
OptString.new('SOURCE', [true, 'The directory or file to compress'])
31+
], self.class)
32+
end
33+
34+
def get_program_file_path
35+
get_env('ProgramFiles')
36+
end
37+
38+
def has_7zip?
39+
file?("#{get_program_file_path}\\7-Zip\\7z.exe")
40+
end
41+
42+
def vbs(dest, src)
43+
vbs_file = File.read(File.join(Msf::Config.data_directory, "post", "zip", "zip.vbs"))
44+
vbs_file << "WindowsZip \"#{src}\",\"#{dest}\""
45+
vbs_file
46+
end
47+
48+
def find_pid_by_user(username)
49+
computer_name = get_env('COMPUTERNAME')
50+
print_status("Searching for PID for #{computer_name}\\\\#{username}")
51+
session.sys.process.processes.each do |p|
52+
if p['user'] == "#{computer_name}\\#{username}"
53+
return p['pid']
54+
end
55+
end
56+
57+
nil
58+
end
59+
60+
def steal_token
61+
current_user = get_env('USERNAME')
62+
pid = find_pid_by_user(current_user)
63+
64+
unless pid
65+
fail_with(Failure::Unknown, "Unable to find a PID for #{current_user} to execute .vbs")
66+
end
67+
68+
print_status("Stealing token from PID #{pid} for #{current_user}")
69+
begin
70+
session.sys.config.steal_token(pid)
71+
rescue Rex::Post::Meterpreter::RequestError => e
72+
# It could raise an exception even when the token is successfully stolen,
73+
# so we will just log the exception and move on.
74+
elog("#{e.class} #{e.message}\n#{e.backtrace * "\n"}")
75+
end
76+
77+
@token_stolen = true
78+
end
79+
80+
def upload_exec_vbs_zip
81+
if is_system?
82+
unless session
83+
print_error('Unable to decompress with VBS technique without Meterpreter')
84+
return
85+
end
86+
87+
steal_token
88+
end
89+
90+
script = vbs(datastore['DESTINATION'], datastore['SOURCE'])
91+
tmp_path = "#{get_env('TEMP')}\\zip.vbs"
92+
print_status("VBS file uploaded to #{tmp_path}")
93+
write_file(tmp_path, script)
94+
cmd_exec("wscript.exe #{tmp_path}")
95+
end
96+
97+
def do_7zip
98+
program_file_path = get_program_file_path
99+
output = cmd_exec("#{program_file_path}\\7-Zip\\7z.exe a -tzip \"#{datastore['DESTINATION']}\" \"#{datastore['SOURCE']}\"")
100+
vprint_line(output)
101+
end
102+
103+
def do_zip
104+
output = cmd_exec("zip -D -q -r #{datastore['DESTINATION']} #{datastore['SOURCE']}")
105+
vprint_line(output)
106+
end
107+
108+
def windows_zip
109+
if has_7zip?
110+
print_status("Compressing #{datastore['DESTINATION']} via 7zip")
111+
do_7zip
112+
else
113+
print_status("Compressing #{datastore['DESTINATION']} via VBS")
114+
upload_exec_vbs_zip
115+
end
116+
end
117+
118+
def linux_zip
119+
print_status("Compressing #{datastore['DESTINATION']} via zip")
120+
do_zip
121+
end
122+
123+
def cleanup
124+
if @token_stolen && session
125+
session.sys.config.revert_to_self
126+
print_status('Token restored.')
127+
end
128+
129+
super
130+
end
131+
132+
def run
133+
@token_stolen = false
134+
135+
os = get_target_os
136+
case os
137+
when Msf::Module::Platform::Windows.realname.downcase
138+
windows_zip
139+
else
140+
linux_zip
141+
end
142+
end
143+
144+
end
145+

0 commit comments

Comments
 (0)