-
Notifications
You must be signed in to change notification settings - Fork 1
/
app24_1.js
236 lines (186 loc) · 5.87 KB
/
app24_1.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
const { exec } = require("child_process");
require("dotenv").config();
const pcap = require("pcap");
const colors = require("colors");
const mqtt = require("mqtt");
var cron = require("node-cron");
const Database = require("better-sqlite3");
const {
parseSSID,
parseType,
parseFreq,
getFullDate,
parseRSSI,
parseSourceMAC,
} = require("./functions");
let db_name = getFullDate().split(" ")[0]+"_"+"Sniffer-Wific1_"+process.env.id+".db"
let nseq = 0;
const db = new Database(db_name);
const createTable =
"CREATE TABLE IF NOT EXISTS ProbeRequestFrames ('timestamp', 'NSeq','snifferId', 'SSID', 'RSSI', 'MAC_origen', 'canal','Rates','HTC_Capabilities','Vendor_Specific','Extended_rates','Extended_HTC_Capabilities','VHT_Capabilities')";
db.exec(createTable);
const insertInto = db.prepare(
"INSERT INTO ProbeRequestFrames (timestamp, NSeq, snifferId, SSID, RSSI, MAC_origen, canal, Rates, HTC_Capabilities, Vendor_Specific, Extended_rates, Extended_HTC_Capabilities, VHT_Capabilities) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
);
//=========================MQTT===========================
const options = {
clean: true, // retain session
connectTimeout: 4000, // Timeout period
// Authentication information
clientId: process.env.id + "_c1",
username: process.env.id + "_c1",
password: process.env.id + "_c1",
};
const connectUrl = "ws://212.128.44.50:8083/mqtt";
const client = mqtt.connect(connectUrl, options);
client.on("connect", function () {
console.log("Connected to MQTT URL");
});
// ====================== SNIFFER WIFI ==================================
const snifferId = "sniffer2.4Ghz_1"; //cambiar por id del sniffer en el que se ejecute
let wifidata = {};
wifidata.id = process.env.id;
var pcapSession;
let rate = 0x0
let htccap = 0x0
let vendorspecific;
let extendedrates = 0x0
let extendedhtc = 0x0
let vhtcap = 0x0
const checkProbe = (rawData) => {
//console.log(rawData)
let i = 0
let curs = rawData[i]
while (i<rawData.length){
if (rawData[i+1] == 00)
i+=2;
else{
let fr = rawData.slice(i,rawData[i+1]+i+2)
//console.log(fr," - ",i)
i+=rawData[i+1]+2
switch(fr[0]){
case 01:
rate = fr.slice(2,fr.length).toString('hex')
break;
case 45:
htccap = fr.slice(2,fr.length).toString('hex')
break;
case 50:
extendedrates = fr.slice(2,fr.length).toString('hex')
break;
case 127:
extendedhtc = fr.slice(2,fr.length).toString('hex')
break;
case 191:
vhtcap = fr.slice(2,fr.length).toString('hex')
break;
case 221:
vendorspecific += fr.slice(2,5).toString('hex')
break;
}
}
}
//console.log("rates: ",rate)
//console.log("vendor specific: ",vendorspecific)
//console.log("htc cap: ",htccap)
//console.log("ext rates: ",extendedrates)
//console.log("ext htc cap: ",extendedhtc)
//console.log("vht cap: ",vhtcap)
}
function init() {
console.log("iniciando capturas...");
// creamos sesion de pcap indicando interfaz (en modo monitor con airmon-ng) y filtros
// sustituir interfaz por la del dispositivo en el que se ejecuta la app
pcapSession = pcap.createSession(process.env.iface1, {
filter: "type mgt subtype probe-req",
});
let SSID;
let RSSI;
let MAC_origen;
let frec;
let canal;
let date;
pcapSession.on("packet", (rawPacket) => {
var length_RT = rawPacket.buf[2];
frec = parseFreq(rawPacket.buf, length_RT);
canal = (frec % 2407) / 5;
if (canal == 1) {
var tipo = parseType(rawPacket.buf, length_RT);
SSID = parseSSID(rawPacket.buf, length_RT, tipo);
RSSI = parseRSSI(rawPacket.buf, length_RT);
MAC_origen = parseSourceMAC(rawPacket.buf, length_RT);
date = getFullDate();
var disp =
`====================\n`.green +
`===PROBE RESQUEST===\n`.green +
`====================\n`.green +
`ssid: ${SSID}\n` +
`RSSI: ${RSSI} dBm\n` +
`TimeStamp: ${date}\n` +
`MAC origen: ${MAC_origen}\n` +
`Canal: ${canal}\n`;
//console.log(disp);
//console.log(rawPacket.buf)
checkProbe(rawPacket.buf.slice(42,rawPacket.buf.length))
wifidata.ssid = SSID;
wifidata.rssi = RSSI;
wifidata.timestamp = date;
wifidata.OrigMAC = MAC_origen;
wifidata.canal = canal;
wifidata.rate = rate;
wifidata.htccap = htccap;
wifidata.vendorspecific = vendorspecific;
wifidata.extendedrates = extendedrates;
wifidata.extendedhtc = extendedhtc;
wifidata.vhtcap = vhtcap;
wifidata.nseq = nseq;
nseq++;
client.publish("CRAIUPCT_WifiData", JSON.stringify(wifidata));
insertInto.run(date, nseq,snifferId, SSID, RSSI, MAC_origen, canal,rate,htccap,vendorspecific,extendedrates,extendedhtc,vhtcap);
rate = 0x0
htccap = 0x0
vendorspecific='';
extendedrates = 0x0
extendedhtc = 0x0
vhtcap = 0x0
}
});
}
console.log("esperando 5 seg a que se inicie script");
setTimeout(init, 5000);
cron.schedule("0 */5 * * * *", () => {
pcapSession.close();
console.log("esperando 5 seg a que se inicie script");
setTimeout(init, 5000);
});
let ka = wifidata
setInterval(()=>{
exec(
"cat /sys/class/thermal/thermal_zone0/temp",
function (error, stdout, stderr) {
if (error !== null) {
console.log("exec error: " + error);
} else {
ka = wifidata
ka.rssi = parseFloat(stdout / 1000);
ka.timestamp = getFullDate()
ka.OrigMAC = "01:01:01:01:01:01"
client.publish("CRAIUPCT_WifiData", JSON.stringify(ka));
insertInto.run(ka.timestamp,
ka.nseq,
ka.id,
ka.ssid,
ka.rssi,
ka.OrigMAC,
ka.canal,
ka.rate,
ka.htccap,
ka.vendorspecific,
ka.extendedrates,
ka.extendedhtc,
ka.vhtcap
);
}
}
);
}, 30000);