File: genericsource.cc

package info (click to toggle)
kismet 1.4.2-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 504 kB
  • ctags: 435
  • sloc: cpp: 2,741; makefile: 126; sh: 29
file content (245 lines) | stat: -rw-r--r-- 5,999 bytes parent folder | download
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
#include <stdio.h>
#include <sys/ioctl.h>
#include <errno.h>

#include "config.h"
#include "genericsource.h"

#ifdef HAVE_LINUX_WIRELESS

int GenericSource::OpenSource(const char *dev) {
    snprintf(type, 64, "generic ssid source");

    strncpy(interface, dev, 64);

    sock = socket(AF_INET, SOCK_DGRAM, 0);

    // Set the essid to null to begin with
    if (SetGenericEssid(NULL) < 0)
        return -1;

    return 1;
}

int GenericSource::CloseSource() {
    return 1;
}

int GenericSource::FetchPacket(pkthdr *in_header, u_char *in_data) {
    // We don't have to sleep here, we just have to see if the kernel has
    // any ssid's to give us -- kismet main loop takes care of sleeping.

    int ret;

    if ((ret = GetGenericInfo()) < 0)
        return -1;

    if (ret == 0)
        return 0;

    /*
     fprintf(stderr, "Got ssid: %s\n", essid);
    fprintf(stderr, "Got bssid mac: %02X%02X%02X%02X%02X%02X\n",
                 mac[0], mac[1], mac[2],
                 mac[3], mac[4], mac[5]);
                 */

    Generic2Common(in_header, in_data);

    // Reset our SSID to null
    if (SetGenericEssid(NULL) < 0)
        return -1;

    return in_header->len;
}

int GenericSource::Generic2Common(pkthdr *in_header, u_char *in_data) {
    // I hate Wavelan so very very much.  Because all it can give us is
    // essid's, we have to build a whole 802.11 data packet and fake it
    // so that the rest of Kismet can dissect them correctly.

    memset(in_header, 0, sizeof(pkthdr));
    memset(in_data, 0, MAX_PACKET_LEN);


    // We'll write the packet data, then we'll write the header once we
    // know how big we are.
    int packetlen = 0;

    frame_control fc;

    memset(&fc, 0, sizeof(frame_control));

    // Fill it in.  A lot of this we could leave blank and let the zero
    // take care of it, but tis is easier to keep track of.
    fc.version = 0;
    fc.type = 0;
    fc.subtype = 8;
    fc.from_ds = 0;
    fc.to_ds = 0;
    fc.more_fragments = 0;
    fc.power_management = 0;
    fc.more_data = 0;
    fc.wep = 0;
    fc.order = 0;

    // Write the frame control, and offset
    memcpy(in_data, &fc, sizeof(frame_control));
    packetlen += sizeof(frame_control);

    uint8_t buf[128];
    memset(buf, 0, 128);

    // We just want the zeroed stuff for duration, 2 bytes
    memcpy(in_data + packetlen, buf, 2);
    packetlen += 2;

    // Dest - Broadcast MAC
    buf[0] = 0xff; buf[1] = 0xff; buf[2] = 0xff;
    buf[3] = 0xff; buf[4] = 0xff; buf[5] = 0xff;

    memcpy(in_data + packetlen, buf, 6);
    packetlen += 6;

    // Source - Our MAC
    memcpy(in_data + packetlen, mac, 6);
    packetlen += 6;

    // BSSID - Our MAC
    memcpy(in_data + packetlen, mac, 6);
    packetlen += 6;

    // We don't care about fragment or sequence number
    memset(buf, 0, 128);
    memcpy(in_data + packetlen, buf, 2);
    packetlen += 2;

    // OK, we've written our entire 802.11 header, time for the management data
    // that means something

    // We don't care about timestamping it
    memcpy(in_data + packetlen, buf, 8);
    packetlen += 8;

    // We don't care about the beacon interval
    memcpy(in_data + packetlen, buf, 2);
    packetlen += 2;

    uint8_t cap = 0;
    // Everything is an AP... We don't know anything else, unfortunately.
    cap |= (1 << 7);
    memcpy(in_data + packetlen, &cap, 1);
    packetlen += 1;
    // Likewise, we don't care about CFP
    memcpy(in_data + packetlen, buf, 1);
    packetlen += 1;

    // Tagged parameters.  Annoying to read, annoying to write, but we only
    // need one (for now).  Buf is still nulled so we can write that.
    // Tag 0
    memcpy(in_data + packetlen, buf, 1);
    packetlen += 1;
    // Length... We'll reuse our capabilities field
    cap = strlen(essid);
    memcpy(in_data + packetlen, &cap, 1);
    packetlen += 1;
    // Data
    memcpy(in_data + packetlen, essid, cap);
    packetlen += cap;




    // Now fill in our common header
    in_header->caplen = packetlen;
    in_header->len = packetlen;
    gettimeofday(&in_header->ts, 0);

    return 1;
}

int GenericSource::SetGenericEssid(const char *essid_in) {
    // Liberated from gtkskan

    struct iwreq wrq;

    char buf[IW_ESSID_MAX_SIZE + 1];

    memset(&wrq, 0, sizeof(wrq));
    memset(buf, 0, sizeof(buf));

    strcpy(wrq.ifr_name, interface);

    if(essid_in == NULL) {
        wrq.u.essid.flags = 0;
    } else {
        wrq.u.essid.flags = 1;
        strcpy(buf, essid_in);
  }

  wrq.u.essid.pointer = (caddr_t) buf;
  wrq.u.essid.length = strlen(buf) + 1;

  if (ioctl(sock, SIOCSIWESSID, &wrq) < 0) {
      snprintf(errstr, 1024, "Generic source SIOCSIWESSID failed: %s", strerror(errno));
      return -1;
  }

  return 1;
}

int GenericSource::GetGenericInfo() {
    struct iwreq wrq;

    char buf[IW_ESSID_MAX_SIZE + 1];

    memset(buf, 0, sizeof(buf));
    memset(&wrq, 0, sizeof(wrq));

    // Fill in the request input block
    strcpy(wrq.ifr_name, interface);
    wrq.u.essid.pointer = (caddr_t) buf;
    wrq.u.essid.length = 0;
    wrq.u.essid.flags = 0;


    if (ioctl(sock, SIOCGIWESSID, &wrq) < 0) {
        snprintf(errstr, 1024, "Generic source SIOCGIWESSID failed: %s", strerror(errno));
        return -1;
    }

    if (buf[0] == '\0')
        return 0;

    // Copy the SSID into our local records
    strncpy(essid, buf, IW_ESSID_MAX_SIZE);

    // Reset the buffer
    memset(buf, 0, sizeof(buf));

    // Re-issue a new ioctl to fetch the MAC of the AP we found
    if (ioctl(sock, SIOCGIWAP, &wrq) < 0) {
        snprintf(errstr, 1024, "Generic source SIOCGIWAP failed: %s", strerror(errno));
        return -1;
    }

    // Copy the MAC into our local records
    memcpy(mac, wrq.u.ap_addr.sa_data, MAC_LEN);

    /*
    // Get the network mode
    if (ioctl(sock, SIOCGIWMODE, &wrq) < 0) {
        snprintf(errstr, 1024, "Generic source SIOCGIWMODE failed: %s", strerror(errno));
        return -1;
    }

    mode = wrq.u.mode;

    */

    return 1;

}

#endif