File: finitestate.cc

package info (click to toggle)
kismet 2008-05-R1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,232 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (334 lines) | stat: -rw-r--r-- 11,599 bytes parent folder | download | duplicates (2)
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
/*
    This file is part of Kismet

    Kismet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kismet is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#define __STDC_FORMAT_MACROS
#include "config.h"

#include "finitestate.h"
#include "packetracker.h"
#include "util.h"

ProbeNoJoinAutomata::ProbeNoJoinAutomata(Packetracker *in_ptracker, 
										 Alertracker *in_atracker,
                                         alert_time_unit in_unit, int in_rate, 
										 alert_time_unit in_bunit, int in_burstrate) {
    atracker = in_atracker;
    ptracker = in_ptracker;
    alertid = atracker->RegisterAlert("PROBENOJOIN", in_unit, in_rate, 
									  in_bunit, in_burstrate);
}

ProbeNoJoinAutomata::~ProbeNoJoinAutomata() {
    for (map<mac_addr, _fsa_element *>::iterator iter = bssid_map.begin();
         iter != bssid_map.end(); ++iter) {
        delete iter->second;
    }
}

int ProbeNoJoinAutomata::ProcessPacket(const packet_info *in_info) {
    _fsa_element *elem;
    map<mac_addr, _fsa_element *>::iterator iter;

    if (in_info->type == packet_management && in_info->subtype == packet_sub_probe_req) {
        // For probe reqs we look at the source MAC and see what we have.  We'll let someone
        // probe as much as they want, they'd just better answer if a network starts talking to
        // them.  Just make a tracking record that they've been probing
        if ((iter = bssid_map.find(in_info->source_mac)) == bssid_map.end()) {
            elem = new _fsa_element;
            bssid_map[in_info->source_mac] = elem;
            return 1;
        } else {
            return 1;
        }
    } else if (in_info->type == packet_management && in_info->subtype == packet_sub_probe_resp) {
        // Responses create an element if none exists for the destination, and we set anyone getting a
        // response to state 1
        if ((iter = bssid_map.find(in_info->dest_mac)) == bssid_map.end()) {
            elem = new _fsa_element;
            bssid_map[in_info->dest_mac] = elem;
        } else {
            elem = iter->second;
        }

        if (elem->state <= 1) {
            elem->state = 1;
            elem->counter++;

            // Trigger on threshold
            if (elem->counter > 25) {
                char atext[STATUS_MAX];
                snprintf(atext, STATUS_MAX, "Suspicious client %s - probing networks but never participating.",
                         iter->first.Mac2String().c_str());
                atracker->RaiseAlert(alertid, 0, iter->first, 0, 0, in_info->channel, atext);
            }

        }

        return 1;
    } else if (in_info->type == packet_data) {
        // If they look like a netstumbler packet, we don't let them go
        if (in_info->proto.type == proto_netstumbler || in_info->proto.type == proto_lucenttest ||
            in_info->proto.type == proto_wellenreiter)
            return 1;

        // If the source is our person, they're exonerated - they're doing normal traffic
        if ((iter = bssid_map.find(in_info->source_mac)) == bssid_map.end()) {
            elem = new _fsa_element;
            bssid_map[in_info->source_mac] = elem;
        } else {
            elem = iter->second;
        }

        elem->state = 2;

        return 1;
    }


    return 0;
}

DisassocTrafficAutomata::DisassocTrafficAutomata(Packetracker *in_ptracker, 
												 Alertracker *in_atracker,
												 alert_time_unit in_unit, int in_rate,
												 alert_time_unit in_bunit, 
												 int in_burstrate) {
    atracker = in_atracker;
    ptracker = in_ptracker;
    alertid = atracker->RegisterAlert("DISASSOCTRAFFIC", in_unit, in_rate, 
									  in_bunit, in_burstrate);
}

DisassocTrafficAutomata::~DisassocTrafficAutomata() {

}

int DisassocTrafficAutomata::ProcessPacket(const packet_info *in_info) {
    _fsa_element *elem;
    map<mac_addr, _fsa_element *>::iterator iter;
    char atext[STATUS_MAX];

    if (in_info->type == packet_management && in_info->subtype == packet_sub_disassociation) {
        iter = source_map.find(in_info->source_mac);

        if (iter == source_map.end()) {
            elem = new _fsa_element;
            source_map[in_info->source_mac] = elem;
            elem->counter = 0;
        } else {
            elem = iter->second;
        }

        elem->state = 0;
        gettimeofday(&elem->last_time, NULL);
    } else if (in_info->type == packet_management && in_info->subtype == packet_sub_deauthentication) {
        iter = source_map.find(in_info->source_mac);

        if (iter == source_map.end()) {
            elem = new _fsa_element;
            source_map[in_info->source_mac] = elem;
            elem->counter = 0;
        } else {
            elem = iter->second;
        }

        elem->state = 1;
        gettimeofday(&elem->last_time, NULL);
    } else if (in_info->type == packet_data) {
        iter = source_map.find(in_info->source_mac);

        if (iter == source_map.end())
            return 0;

        elem = iter->second;

        struct timeval tv;
        gettimeofday(&tv, NULL);

        // Raise an alert if someone is exchanging data w/in 10 seconds of disassociating or deauthenticating
        if (tv.tv_sec - elem->last_time.tv_sec < 10) {
            elem->counter++;

            snprintf(atext, STATUS_MAX, "Suspicious traffic on %s.  Data traffic within 10 seconds of disassociate.",
                     in_info->source_mac.Mac2String().c_str());
            atracker->RaiseAlert(alertid, in_info->bssid_mac, in_info->source_mac, 
                                 0, 0, in_info->channel, atext);

            return 1;
        } else {
            delete iter->second;
            source_map.erase(iter);
        }

    }

    return 0;
}

BssTimestampAutomata::BssTimestampAutomata(Packetracker *in_ptracker, 
										   Alertracker *in_atracker,
										   alert_time_unit in_unit, int in_rate, 
										   alert_time_unit in_bunit, 
										   int in_burstrate) {
    atracker = in_atracker;
    ptracker = in_ptracker;
    alertid = atracker->RegisterAlert("BSSTIMESTAMP", in_unit, in_rate, 
									  in_bunit, in_burstrate);
}

BssTimestampAutomata::~BssTimestampAutomata() {
    for (macmap<BssTimestampAutomata::_bs_fsa_element *>::iterator iter = bss_map.begin();
         iter != bss_map.end(); ++iter) {
        delete iter->second;
    }
}

int BssTimestampAutomata::ProcessPacket(const packet_info *in_info) {
    _bs_fsa_element *elem;
    char atext[1024];

    // Don't track BSS timestamp for non-beacon frames or for adhoc networks
    if (in_info->type != packet_management || 
		in_info->subtype != packet_sub_beacon || 
		in_info->distrib == adhoc_distribution)
        return 0;

    macmap<BssTimestampAutomata::_bs_fsa_element *>::iterator iter = 
		bss_map.find(in_info->bssid_mac);
    if (iter == bss_map.end()) {
        elem = new _bs_fsa_element;
        elem->bss_timestamp = in_info->timestamp;
        bss_map.insert(in_info->bssid_mac, elem);
        return 0;
    } else {
        elem = *(iter->second);
    }

    if (in_info->timestamp < elem->bss_timestamp) {
        if (elem->counter > 0) {
            // Generate an alert, we're getting a bunch of invalid timestamps

            snprintf(atext, STATUS_MAX, "Out-of-sequence BSS timestamp on %s "
                     "- got %"PRIx64", expected %"PRIx64" - this could indicate AP spoofing",
                     in_info->bssid_mac.Mac2String().c_str(), in_info->timestamp,
                     elem->bss_timestamp);
            atracker->RaiseAlert(alertid, in_info->bssid_mac, 0, 0, 0, 
								 in_info->channel, atext);

            // Reset so we don't keep thrashing here
            elem->counter = 0;
            elem->bss_timestamp = in_info->timestamp;

            return 1;
        } else {
            // Increase our invalid stock
            elem->counter += 10;
        }
    } else if (elem->counter > 0) {
        elem->counter--;
    }

    elem->bss_timestamp = in_info->timestamp;

    return 0;
}

WepRebroadcastAutomata::WepRebroadcastAutomata(Packetracker *in_ptracker, 
											   Alertracker *in_atracker,
                                               alert_time_unit in_unit, int in_rate, 
											   alert_time_unit in_bunit,
											   int in_burstrate) {
    atracker = in_atracker;
    ptracker = in_ptracker;
    alertid = atracker->RegisterAlert("WEPREBROADCAST", in_unit, in_rate, 
									  in_bunit, in_burstrate);
}

WepRebroadcastAutomata::~WepRebroadcastAutomata() {
}

int WepRebroadcastAutomata::ProcessPacket(const packet_info *in_info) {
    return 0;
}

#if 0
SequenceSpoofAutomata::SequenceSpoofAutomata(Packetracker *in_ptracker, Alertracker *in_atracker,
                                             alert_time_unit in_unit, int in_rate, int in_burstrate) {
    atracker = in_atracker;
    ptracker = in_ptracker;
    alertid = atracker->RegisterAlert("SEQUENCESPOOF", in_unit, in_rate, in_burstrate);
}

SequenceSpoofAutomata::~SequenceSpoofAutomata() {
}


int SequenceSpoofAutomata::ProcessPacket(const packet_info *in_info) {
    // Only sequence-track beacons (for now)
    int ret = 0;
    char atext[STATUS_MAX];

    if (in_info->type != packet_management || in_info->subtype != packet_sub_beacon)
        return 0;

    // See if we know about this network
    wireless_network *net = ptracker->MatchNetwork(in_info);

    if (net != NULL) {
        // If we found a network for this packet, see if it's got a sequence mismatch.
        // remember we modulo the sequence by 4096, so we won't worry about a sequence drop
        // if the network used to be near the wraparound
        if (net->last_sequence < 4000 && net->last_sequence != 0 &&
            (in_info->sequence_number < net->last_sequence)) {
            snprintf(atext, STATUS_MAX, "Suspicious sequence change - %s %d to %d.  Possible spoof attempt.",
                     net->bssid.Mac2String().c_str(), net->last_sequence, in_info->sequence_number);
            atracker->RaiseAlert(alertid, atext);
        }

    }

    /*
    // Try to match the mac addr to an existing network
    map<mac_adder, _fsa_element *>::iterator iter;


    // If we have more than 2 suspicious MAC changes in the records, raise an alert.
    if (count > 2) {
        char atext[STATUS_MAX];
        snprintf(atext, STATUS_MAX, "Suspicious sequence order - %s looks like %s (%d to %d).  Possible FakeAP.",
                 in_info->source_mac.Mac2String().c_str(), seq->source_mac.Mac2String().c_str(),
                 in_info->sequence_number, seq->seq_num);
        atracker->RaiseAlert(alertid, atext);
        fprintf(stderr, "**FORCED** %s\n", atext);
        ret = 1;
    }

    // Put it on the stack
    seq = new _seq_elem;
    seq->seq_num = in_info->sequence_number;
    seq->source_mac = in_info->source_mac;
    seq_stack.push_back(seq);
    if (seq_stack.size() > 150) {
        delete seq_stack[0];
        seq_stack.erase(seq_stack.begin());
    }
    */

    return ret;
}
#endif