-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
80 lines (67 loc) · 1.93 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
(function() {
'use strict';
module.exports = function(key) {
require('terminal-colors');
var DEBUG = false;
var raw = require('raw-socket'),
crypto = require('./encryption.js'),
socket = raw.createSocket({
protocol: raw.Protocol.ICMP
});
var message = '',
count = 0,
salt,
iv,
derivedKey,
offset,
type,
length,
hex;
socket.on('message', listen);
function listen(buffer, source) {
// convert buffer to hex string, ignore IP header (20 bytes)
buffer = buffer.toString('hex', 20, buffer.length);
/**
* types
* 0x00 = ICMP Echo reply,
* 0x08 = ICMP Echo request
**/
type = parseInt(buffer.substring(0, 2), 16);
length = parseInt(buffer.substring(16, 18), 16);
// get message
offset = 18;
hex = buffer.substring(offset, offset + length);
if(DEBUG) {
console.log('\nping #' + count);
console.log('buffer:', buffer);
console.log('type:', type === 0 ? 'ICMP Echo reply' : 'ICMP Echo request');
console.log('message length:', length);
console.log('message content:', hex);
}
// ignore ICMP Echo replies
if(type === 0)
return;
// encountered end ping
if((hex.match(/f/g) || []).length === length) {
if(message) {
process.stdout.write(source.green + ' ');
process.stdout.write('[' + (count + 1) + ']: ');
process.stdout.write(message.bold + '\n');
}
count = 0;
message = '';
return;
}
// first ping
if(count === 0) {
salt = crypto.hex2bytes(hex.substring(0, 30));
iv = crypto.hex2bytes(hex.substring(30, 62));
derivedKey = crypto.key(key, salt);
count++;
return;
}
message += crypto.decrypt(hex, derivedKey, iv);
count++;
}
};
}());