-
Notifications
You must be signed in to change notification settings - Fork 0
/
knockd.js
140 lines (120 loc) · 3.48 KB
/
knockd.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
var sys = require("sys");
var pcap = require("pcap");
var config = require("./config").config;
var util = require("util");
var exec = require('child_process').exec;
var pcap_session;
var ports = [];
var sessions = [];
var ifaces = [];
if (process.argv.length < 3) {
util.log("please provide an interface");
process.exit(1);
}
pcap_session = pcap.createSession(process.argv[2], "tcp");
pcap_session.findalldevs().forEach(function (dev) {
if (pcap_session.device_name === dev.name)
{
dev.addresses.forEach(function (address) {
ifaces.push(address.addr);
});
}
});
pcap_session.on('packet', function (raw_packet) {
var packet = pcap.decode.packet(raw_packet);
for(var i = 0; i < ifaces.length; i++)
{
if(packet.link.ip.saddr == ifaces[i])
return;
}
var tcp = packet.link.ip.tcp;
if(!tcp) return;
var dport = tcp.dport;
var ip = packet.link.ip.saddr;
//util.log("got a packet from " + ip + " on port " + dport);
// find the session
var session = null;
for(var i = 0; i < sessions.length; i++)
{
if(sessions[i].ip == ip)
session = sessions[i];
}
if(!session)
{
// find the rule starting on this port
var rule = null;
for(var key in config)
{
if(config[key].sequence[0] == dport)
rule = config[key];
}
if(rule)
{
util.log("creating session for " + ip + " expecting: " + util.inspect(rule.sequence));
var now = Math.floor((new Date().getTime()) / 1000);
session = {
ip: ip,
sequence: [dport],
seq_timeout: (now + rule.seq_timeout),
rule: rule
};
sessions.push(session);
}
else
return;
}
else
{
session.sequence.push(dport);
//util.log("found session for " + ip);
var current = session.sequence;
var required = session.rule.sequence;
var now = Math.floor((new Date().getTime()) / 1000);
if(now > session.seq_timeout)
{
removeSession(session);
return;
}
for(var i = 0; i < current.length; i++)
{
if(required[i] != current[i])
{
removeSession(session);
return;
}
}
util.log("session is still valid!");
if(session.sequence.length == session.rule.sequence.length)
{
util.log("KNOCK COMPLETE");
removeSession(session);
var cmd = session.rule.command;
cmd.replace("%IP%", ip);
util.log("execing: " + cmd);
var child = exec(cmd);
var stopcmd = session.rule.stop_command;
var cmd_timeout = session.rule.cmd_timeout;
if(stopcmd && cmd_timeout)
{
stopcmd.replace("%IP%", ip);
setTimeout(function()
{
util.log("execing: " + stopcmd);
var child = exec(stopcmd);
}, (cmd_timeout*1000));
}
}
}
});
var removeSession = function(session)
{
for(var j = 0; j < sessions.length; j++)
{
if(sessions[j].ip == session.ip)
{
util.log("removing session for " + session.ip);
sessions.splice(j, 1);
return;
}
}
}