-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
228 lines (222 loc) · 7.64 KB
/
app.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @author [soumya]
* @email [soumyaprasad.rana@gmail.com]
* @create date 2022-11-26 18:08:48
* @modify date 2022-11-26 18:08:48
* @desc Entrypoint for the application
*/
const express = require("express");
var pino = require("pino");
var http = require("http");
var SSHClient = require("ssh2").Client;
var utf8 = require("utf8");
const logger = pino(
{
level: process.env.LOG_LEVEL || "info",
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
},
pino.destination("./server.log")
);
var app = express();
const port = 3000;
app.get("/", (req, res) => {
res.redirect("/SSHClient");
});
app.use(express.static(__dirname + "/static"));
app.get("/SSHClient", (req, res) => {
res.sendFile("index.html", { root: __dirname + "/static" });
});
logger.info("Creating http server for socket.io");
var server = http.createServer(app);
logger.info("Registering Socket.io");
//socket.io instantiation
const io = require("socket.io")(server, { origins: "*:*" });
server.listen(port, () => logger.info("App listening on port " + port));
//Socket Connection
io.on("connection", function(socket) {
try {
logger.info("SERVER SOCKET CONNECTION CREATED :", socket.id);
var data = socket.handshake.query;
logger.info(data);
if (data.type == "webshell") {
logger.info("Connection type : webshell");
var ssh = new SSHClient();
ssh
.on("ready", function() {
socket.emit("data", "\r\n*** SSH CONNECTION ESTABLISHED ***\r\n\n");
logger.debug("SSH CONNECTION ESTABLISHED for socket" + socket.id);
connected = true;
ssh.shell(function(err, stream) {
if (err) {
logger.debug(
"SSH SHELL ERROR: " + err.message + " for socket" + socket.id
);
logger.debug(err);
return socket.emit(
"data",
"\r\n*** SSH SHELL ERROR: " + err.message + " ***\r\n"
);
}
socket.on("data", function(data) {
logger.debug("Socket ID::" + socket.id + " on data ::");
logger.debug(data);
stream.write(data);
});
stream
.on("data", function(d) {
logger.debug(
"Socket ID::" + socket.id + " ssh stream on data ::"
);
logger.debug(utf8.decode(d.toString("binary")));
socket.emit("data", utf8.decode(d.toString("binary")));
})
.on("close", function() {
logger.debug(
"Socket ID::" +
socket.id +
" ssh stream on close() :: Going to call ssh.end()"
);
ssh.end();
});
});
})
.on("close", function() {
logger.debug(
"Socket ID::" +
socket.id +
" ssh on close() :: SSH CONNECTION CLOSED"
);
socket.emit("data", "\r\n*** SSH CONNECTION CLOSED ***\r\n");
})
.on("error", function(err) {
logger.debug(err);
logger.debug(
"Socket ID::" +
socket.id +
" ssh on error ::SSH CONNECTION ERROR: " +
err.message
);
socket.emit(
"data",
"\r\n*** SSH CONNECTION ERROR: " + err.message + " ***\r\n"
);
})
.connect({
host: data.hostname,
port: data.port, // Generally 22 but some server have diffrent port for security Reson
username: data.username, // user name
password: data.password, // Set password or use PrivateKey
// privateKey: require("fs").readFileSync("PATH OF KEY ") // <---- Uncomment this if you want to use privateKey ( Example : AWS )
});
} else if (data.type == "exec") {
logger.info("Connection type: exec");
if (
typeof data.command == "undefined" ||
data.command == null ||
data.command == "null"
) {
logger.info(
"SSH CONNECTION ERROR: Command not found for connection type execute!"
);
return socket.emit(
"data",
"\r\n*** SSH CONNECTION ERROR: Command not found for connection type execute! ***\r\n"
);
}
var ssh = new SSHClient();
ssh
.on("ready", function() {
socket.emit("data", "\r\n*** SSH CONNECTION ESTABLISHED ***\r\n");
logger.debug("SSH CONNECTION ESTABLISHED for socket" + socket.id);
connected = true;
ssh.exec(data.command, function(err, stream) {
if (err) {
logger.debug(
"SSH SHELL ERROR: " + err.message + " for socket" + socket.id
);
logger.debug(err);
return socket.emit(
"data",
"\r\n*** SSH SHELL ERROR: " + err.message + " ***\r\n"
);
}
socket.on("data", function(data) {
logger.debug("Socket ID::" + socket.id + "on data::");
logger.debug(data);
stream.write(data);
});
stream
.on("data", function(d) {
logger.debug(
"Socket ID::" + socket.id + " ssh stream on data ::"
);
logger.debug(utf8.decode(d.toString("binary")));
socket.emit("data", utf8.decode(d.toString("binary")));
})
.stderr.on("data", (d) => {
logger.debug(
"Socket ID::" + socket.id + " ssh error stream on data ::"
);
logger.debug(utf8.decode(d.toString("binary")));
socket.emit("data", utf8.decode(d.toString("binary")));
})
.on("error", function(d) {
logger.debug("Socket ID::" + socket.id + " ssh on eror ::");
logger.debug(utf8.decode(d.toString("binary")));
socket.emit("data", utf8.decode(d.toString("binary")));
})
.on("close", function() {
logger.debug(
"Socket ID::" +
socket.id +
" ssh stream on close() :: Going to call ssh.end()"
);
ssh.end();
});
});
})
.on("close", function() {
logger.debug(
"Socket ID::" +
socket.id +
" ssh on close() :: COMMAND EXECUTED CONNECTION CLOSED"
);
socket.emit(
"data",
"\r\n*** COMMAND EXECUTED CONNECTION CLOSED ***\r\n"
);
})
.on("error", function(err) {
logger.debug(err);
logger.debug(
"Socket ID::" +
socket.id +
" ssh on error ::SSH CONNECTION ERROR: " +
err.message
);
if (err.message.includes("read ECONNRESET")) {
socket.emit("data", "\r\n*** Stream closed ***\r\n");
} else {
socket.emit(
"data",
"\r\n*** SSH CONNECTION ERROR: " + err.message + " ***\r\n"
);
}
})
.connect({
host: data.hostname,
port: data.port, // Generally 22 but some server have diffrent port for security Reson
username: data.username, // user name
password: data.password, // Set password or use PrivateKey
// privateKey: require("fs").readFileSync("PATH OF KEY ") // <---- Uncomment this if you want to use privateKey ( Example : AWS )
});
}
} catch (e) {
logger.info(e);
}
});