-
Notifications
You must be signed in to change notification settings - Fork 2
/
aircrack.js
202 lines (168 loc) · 5.22 KB
/
aircrack.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
/**
* Connection to airserv-ng server
*
* Class AirCrackConnection
* Emits these events:
* - connect()
* - end()
* - raw-packet(Buffer)
* - unknown-aircrack-command(cmd, Buffer)
*
* Public functions:
* - open() - opens connection
* - writeMessage(cmd, data, callback)
* - send(Buffer, callback)
* - getMac(cb) - get mac address, cb is callback with one argument, which mac address
*/
var sys = require('sys');
var net = require('net');
var eve = require('events');
var Buffer = require('buffer').Buffer;
var Pcap = require('pcap');
// Aircrack commands
var commands = {
NET_RC: 1,
NET_GET_CHAN: 2,
NET_SET_CHAN: 3,
NET_WRITE: 4,
NET_PACKET: 5,
NET_GET_MAC: 6,
NET_MAC: 7,
NET_GET_MONITOR:8,
NET_GET_RATE: 9,
NET_SET_RATE: 10,
};
function AirCrackConnection(ip, port) {
var self = this,
callbackQueue = [], // Queue for callbacks of written messages
writeQueue = []; // Queue for pending writes
self.ip = ip;
self.port = port;
self.opened = false;
self.conn = null;
// Create big buffer
var buffer = new Buffer(4096);
var bufferStart = 0, bufferEnd = 0;
for(var i = 0; i < 4096; i++) buffer[i] = 0
// Open connection
self.open = function open() {
self.conn = net.createConnection(self.port, self.ip);
// On connection
this.conn.on('connect', function() {
self.opened = true;
self.emit('connect');
// Write all messages which are in queue
//console.log("Socket is connected, writing ", writeQueue.length, "messages from queue");
while(writeQueue.length > 0) {
var item = writeQueue.shift();
writeMessage_(item[0], item[1]);
}
});
// On end of connection
this.conn.on('end', function() {
self.conn.end(); // End also my part of connection
self.opened = false;
self.emit('end');
});
// On received data
this.conn.on('data', receivedData);
};
// On received data
function receivedData(buf) {
// Put data to big buffer
buf.copy(buffer, bufferEnd, 0); bufferEnd += buf.length;
while(processBuffer()); // Process all messages in buffer
cleanBuffer();
}
// Process big buffer
function processBuffer() {
var buf = buffer.slice(bufferStart, bufferEnd); // Get actual part of bug buffer
if(buf.length < 5) return false; // Not enough data
var cmd = buf[0];
var len = buf[4] + buf[3] * 0x100 + buf[2] * 0x10000 + buf[1] * 0x1000000;
if(buf.length < len + 5) return false; // Leave it to next time (we dont have whole data part)
var data = buf.slice(5, len + 5);
bufferStart += len + 5; // Move start pointer to next data
if(cmd == commands.NET_PACKET) {
// Emit new packet event
self.emit('packet', data);
}
else {
// Execute callback
var cb = callbackQueue.shift();
if(typeof cb == 'function') cb(cmd, len, data); // Callback is set, cal it
else { // No callback
if(cmd != commands.NET_RC) self.emit('unknown-aircrack-command', cmd, data);
}
}
return true; // Processed
}
// Cleans big buffer
function cleanBuffer() {
if(bufferStart > 0) {
if(bufferStart == bufferEnd) { // It's empty, just move pointers
bufferStart = bufferEnd = 0;
}
else {
// Move to beggining via helper buffer
var b2 = new Buffer(bufferEnd - bufferStart);
buffer.copy(b2, 0, bufferStart, bufferEnd); // Copy all valid data to new buffer
b2.copy(buffer, 0, 0); // Copy back to big buffer
bufferStart = 0;
bufferEnd = b2.length;
}
}
}
// Write message to airserv connection
function writeMessage_(buf, cb) {
// Send the data
self.conn.write(buf);
callbackQueue.push(cb);
}
// Queue writing (when not yet connected)
function queueMessage_(buf, cb) {
writeQueue.push([buf, cb]);
}
// Send message to airserv-ng server (returns true if actually send, or false if queues)
self.writeMessage = function writeMessage(cmd, data, callback) {
var len = data ? data.length : 0;
var buf = new Buffer(5 + len);
// Command
buf[0] = cmd;
// Length
buf[4] = len & 0xff;
buf[3] = (len >> 8) & 0xff;
buf[2] = (len >> 16) & 0xff;
buf[1] = (len >> 24) & 0xff;
// Data
if(data instanceof Buffer) data.copy(buf, 5, 0, len);
else if(typeof data == 'string') buf.write(data, 5, 'ascii');
if(self.opened) {
writeMessage_(buf, callback);
//console.log("Writing message");
return true;
}
else {
queueMessage_(buf, callback);
//console.log("Queueing message");
return false;
}
};
// Send buffer to network
self.send = function send(buf, cb) {
return self.writeMessage(commands.NET_WRITE, buf, cb);
};
// Encode packet and all lower layers, and send it
self.sendPacket = function sendPacket(pkt, cb) {
return self.send(pkt.encodeAll(), cb);
};
// Get MAC address
self.getMac = function getMac(cb) {
self.writeMessage(commands.NET_GET_MAC, undefined, function(cmd, len, data) {
cb(Pcap.unpack.ethernet_addr(data, 0));
});
}
};
// Make it instance of EventEmitter
AirCrackConnection.prototype = new eve.EventEmitter;
exports.AirCrackConnection = AirCrackConnection;