-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
369 lines (306 loc) · 9.63 KB
/
server.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const http = require("http");
const url = require("url");
const fs = require("fs");
const { networkInterfaces } = require("os");
const path = require("path");
const dateFormat = require("dateformat");
const https = require("https");
const querystring = require("querystring");
const uuid4 = require("uuid4");
const sqlite = require("better-sqlite3");
const { getHeadersFromArray, parseHeaders } = require("./utils");
const LOG_FILE = "/temp/cowrie/data/log/web.ndjson";
// 60s
const ATTACK_TIMEOUT = 60 * 1000;
const INTERFACE = process.env.interface || "eth0";
// declary any ip of any subset (it doesn't matter)
// delete database if it exists
try {
fs.accessSync("honeypot.db");
fs.unlinkSync("honeypot.db");
} catch (e) {}
// create new, emptry database
const db = new sqlite("honeypot.db");
const fingerprintsDb = new sqlite("/home/node/data/history.db");
// create table to store data of attacks
db.prepare(
"CREATE TABLE attacks(ip text, port integer, dest_port integer, protocol text, session text, attempts integer, datetime text)"
).run();
// transaction-based operations with db.
// They are safe even in async mode
const insertSession = db.transaction(
(ip, port, dest_port, protocol, session, attempts, datetime) => {
db.prepare(
`INSERT INTO attacks(ip, port, dest_port, protocol, session, attempts, datetime) VALUES(?, ?, ?, ?, ?, ?, ?)`
).run(ip, port, dest_port, protocol, session, attempts, datetime);
}
);
const updateAttack = db.transaction(
(session, port, dest_port, protocol, datetime) => {
db.prepare(
`UPDATE attacks SET port=?, dest_port=?, protocol=?, datetime=? WHERE session=?`
).run(port, dest_port, protocol, datetime, session);
}
);
const updateAttackAttempts = db.transaction((session, attempts) => {
db.prepare(`UPDATE attacks SET attempts=? WHERE session=?`).run(
attempts,
session
);
});
const deleteExpiredAttacks = db.transaction((datetime) => {
db.prepare(`DELETE FROM attacks WHERE datetime <= '${datetime}'`).run();
});
const nets = networkInterfaces();
const interfaces = Object.create(null); // or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// skip over non-ipv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === "IPv4" && !net.internal) {
if (!interfaces[name]) {
interfaces[name] = [];
}
interfaces[name].push(net.address);
}
}
}
const DEST_IP = interfaces[INTERFACE][0];
// Create LOG_FILE if required
try {
fs.accessSync(LOG_FILE);
} catch (err) {
const folder_path = path.dirname(LOG_FILE);
try {
fs.accessSync(folder_path);
} catch (err1) {
fs.mkdirSync(folder_path, { recursive: true });
}
fs.writeFile(LOG_FILE, "", { flag: "wx" }, function (err) {
if (err) throw err;
});
}
function getSession(req) {
// remove ipv6 prefix and remain ipv4 address
const src_ip = req.connection.remoteAddress.replace(/^.*:/, "");
let session = db.prepare(`SELECT * FROM attacks WHERE ip = ?`).get(src_ip);
if (session === undefined) {
session = uuid4();
const date = new Date().toISOString();
insertSession(
src_ip,
req.connection.remotePort,
req.connection.encrypted ? 443 : 80,
req.connection.encrypted ? "https" : "http",
session,
0,
date
);
fs.appendFile(
LOG_FILE,
JSON.stringify({
dest_ip: DEST_IP,
src_ip: src_ip,
dest_port: req.connection.encrypted ? 443 : 80,
src_port: req.connection.remotePort,
session: session,
protocol: req.connection.encrypted ? "https" : "http",
eventid: "connection",
type: "d_link_dph150s",
timestamp: date,
}) + "\n",
function (err) {
if (err) console.log("Unable to write to LOG_FILE");
}
);
session = db.prepare(`SELECT * FROM attacks WHERE ip = ?`).get(src_ip);
}
return session;
}
function prepareHeaders(headers) {
const headersToDelete = ["Cookie", "User-Agent"];
const variableHeadersToDelete = ["Host"];
for (const header of headersToDelete) {
if (headers[header]) {
delete headers[header];
}
}
for (const header of variableHeadersToDelete) {
if (headers[header]) {
delete headers[header];
}
}
return headers;
}
function handler(req, res) {
const attack = getSession(req);
const url_path = url.parse(req.url, true).pathname;
const query = url.parse(req.url, true).query;
const dest_port = req.connection.encrypted ? 443 : 80;
const protocol = req.connection.encrypted ? "https" : "http";
const src_ip = req.connection.remoteAddress.replace(/^.*:/, "");
const date = new Date().toISOString();
function logHttpRequest(form = {}) {
fs.appendFile(
LOG_FILE,
JSON.stringify({
dest_ip: DEST_IP,
src_ip: src_ip,
dest_port: dest_port,
src_port: req.connection.remotePort,
session: attack.session,
protocol: protocol,
eventid: "http_request",
url: req.url,
method: req.method,
headers: req.headers,
form: form,
type: "d_link_dph150s",
timestamp: date,
}) + "\n",
function (err) {
if (err) console.log("Unable to write to LOG_FILE");
}
);
}
updateAttack(
attack.session,
req.connection.remotePort,
dest_port,
protocol,
new Date().toISOString()
);
logHttpRequest();
res.removeHeader("Date");
const httpVersion = `HTTP/${req.httpVersion}`;
const method = req.method;
const headers = {};
for (let i = 0; i !== req.rawHeaders.length; i++) {
if (i % 2 == 0) {
headers[req.rawHeaders[i]] = req.rawHeaders[i + 1];
}
}
const preparedHeaders = prepareHeaders(headers);
let url_ = req.url;
let sqlrequest = "SELECT * FROM requests WHERE instr(data, ?) > 0";
let sqlargs = [];
if (url_.startsWith("/nmaplowercheck")) {
url_ = "/nmaplowercheck";
sqlargs.push(
JSON.stringify({ protocol: httpVersion, method, url: url_ }).slice(0, -2)
);
} else {
sqlargs.push(
JSON.stringify({ protocol: httpVersion, method, url: url_ }).slice(0, -1)
);
}
for (const headerKey of Object.keys(preparedHeaders)) {
const headerValue = preparedHeaders[headerKey];
sqlrequest = `${sqlrequest} AND instr(data, ?) > 0`;
sqlargs.push(
JSON.stringify({
[headerKey]: headerValue,
}).slice(1, -1)
);
}
// protocol, method, url - strict match
// headers - partial
// const request = fingerprintsDb
// .prepare(`SELECT * from requests WHERE instr(data, ?) > 0`)
// .get(
// JSON.stringify({
// protocol: httpVersion,
// method,
// url: url_,
// headers: preparedHeaders,
// })
// );
const request = fingerprintsDb.prepare(sqlrequest).get(...sqlargs);
if (request) {
console.log("Found: %s, %s", req.url, req.method);
const response = fingerprintsDb
.prepare(`SELECT * from responses WHERE request = ?`)
.get(request.id);
res.socket.end(response.rawData);
} else {
// answer as 404 based on nmaplowercheck (if possible)
// console.log(
// "Not found: %s, %s, %s\n%s",
// req.url,
// req.method,
// JSON.stringify(headers),
// JSON.stringify({
// protocol: httpVersion,
// method,
// url: url_,
// headers: preparedHeaders,
// })
// );
const request = fingerprintsDb
.prepare(`SELECT * from requests WHERE instr(data, ?) > 0`)
.get(
JSON.stringify({ protocol: httpVersion, method, url: url_ }).slice(
0,
-2
)
);
if (request) {
const response = fingerprintsDb
.prepare(`SELECT * from responses WHERE request = ?`)
.get(request.id);
res.socket.end(response.rawData);
}
}
// TODO:
// res.setHeader(
// "Date",
// dateFormat(new Date(), "ddd, d mmm yyyy hh:mm:ss") + " GMT"
// );
}
const http_server = http.createServer(function (req, res) {
handler(req, res);
});
http_server.on("clientError", (err, socket) => {
if (err.code === "ECONNRESET" || !socket.writable) {
return;
}
// TODO:
// socket.end(
// "HTTP/1.1 400 Bad Request\r\nConnection: close\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n"
// );
// socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
http_server.listen(80, () => {
console.log("Http server started on", 80);
});
setInterval(() => {
const now = new Date();
const now_iso = now.toISOString();
const now_minus_minute = new Date();
now_minus_minute.setTime(now.getTime() - ATTACK_TIMEOUT);
const expiredAttacks = db
.prepare(
`SELECT * from attacks WHERE datetime <= '${now_minus_minute.toISOString()}'`
)
.iterate();
for (const attack of expiredAttacks) {
const { session, ip, port, dest_port, protocol } = attack;
fs.appendFile(
LOG_FILE,
JSON.stringify({
dest_ip: DEST_IP,
src_ip: ip,
dest_port: dest_port,
src_port: port,
session: session,
protocol: protocol,
eventid: "disconnection",
type: "d_link_dph150s",
timestamp: now_iso,
}) + "\n",
function (err) {
if (err) console.log("Unable to write to LOG_FILE");
}
);
}
deleteExpiredAttacks(now_minus_minute);
}, ATTACK_TIMEOUT);