-
Notifications
You must be signed in to change notification settings - Fork 46
/
libmackerel.c
300 lines (241 loc) · 5.6 KB
/
libmackerel.c
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
/*
* libmackerel - Library to manipulate MAC strings
*
* Written by Tom Nardi ([email protected]), released under the GPLv2.
* For more information, see: www.digifail.com
*/
#define MACKERELVERSION "1.1"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// For OUI lookup
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
// OUI file parameters
// Point to valid file (this is done in config.h for Bluelog)
//#define OUIFILE "oui.txt"
// Characters until vendor name starts
#define TXTOFFSET 9
// CRC parameters (default values are for CRC-32):
const int order = 32;
const unsigned long polynom = 0x4c11db7;
const int direct = 1;
const unsigned long crcinit = 0xffffffff;
const unsigned long crcxor = 0xffffffff;
const int refin = 1;
const int refout = 1;
// Global values:
unsigned long crcmask;
unsigned long crchighbit;
unsigned long crcinit_direct;
unsigned long crcinit_nondirect;
unsigned long crctab[256];
// Get things ready
int mac_init ()
{
// Make sure PRNG is seeded for MAC gen
srand(time(NULL));
// This sets up CRC encoding
int i;
unsigned long bit, crc;
crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
crchighbit = (unsigned long)1<<(order-1);
crcinit_direct = crcinit;
crc = crcinit;
for (i=0; i<order; i++)
{
bit = crc & 1;
if (bit) crc^= polynom;
crc >>= 1;
if (bit) crc|= crchighbit;
}
crcinit_nondirect = crc;
return(0);
}
// Verify string is (probably) a MAC
int mac_verify (char* test_mac)
{
int i;
// Make sure it isn't null
if (test_mac == NULL)
return(1);
// See if it's long enough
if (strlen(test_mac) != 17)
return(1);
// See if every 3rd character is a colon or dash
for (i = 2; i < 17; i += 3)
{
if ((int)test_mac[i] != 58 && (int)test_mac[i] != 45)
return(1);
}
// If we get here, it's (maybe) a MAC
return(0);
}
// Generate random MAC address
// Adapted from "SpoofTooph" by JP Dunning (.ronin)
char* mac_rand ()
{
char addr_part[3] = {0};
static char addr[18] = {0};
int i = 0;
// Fill in the middle
while ( i < 14)
{
sprintf(addr_part, "%02X", (rand() % 254));
addr[i++] = addr_part[0];
addr[i++] = addr_part[1];
addr[i++] = ':';
}
// Tack 2 more random characters to finish it
sprintf(addr_part, "%02X", (rand() % 254));
addr[i++] = addr_part[0];
addr[i++] = addr_part[1];
return(addr);
}
// Generate half of a random MAC address
char* mac_rand_half (void)
{
static char half_buffer[9] = {0};
char *full_buffer = {0};
// Get a new random MAC
full_buffer = mac_rand();
// Copy half of it to buffer
strncpy(half_buffer, full_buffer, 8);
return(half_buffer);
}
// Return MAC OUI segment
char* mac_get_oui (char* full_mac)
{
// Verify first
if (mac_verify(full_mac))
return("INVALID_MAC");
static char half_buffer[9] = {0};
// Copy half of full into buffer, return
strncpy(half_buffer, full_mac, 8);
return(half_buffer);
}
// Return MAC in hex notation
char* mac_get_hex (char* full_mac)
{
// Verify first
if (mac_verify(full_mac))
return("INVALID_MAC");
int i;
static char addr_buffer[18] = {0};
// Copy full MAC into buffer
strncpy(addr_buffer, full_mac, 17);
// Replace every 3rd character with -
for (i = 2; i < 17; i += 3)
addr_buffer[i] = 45;
return(addr_buffer);
}
// X out the device-specific part of MAC
char* mac_obfuscate (char* full_mac)
{
// Verify first
if (mac_verify(full_mac))
return("INVALID_MAC");
static char addr_buffer[18] = {0};
// Terminate to prevent duplicating previous results
memset(addr_buffer, '\0', sizeof(addr_buffer));
// Copy half of full MAC into buffer
strncpy(addr_buffer, full_mac, 9);
// Replace trimmed characters with XX
strcat(addr_buffer, "XX:XX:XX");
return(addr_buffer);
}
// Return vendor from OUI database file
// Modified from functions in oui.c from BlueZ
char* mac_get_vendor (char* full_mac)
{
char oui[9] = {0};
// Return OUI (also verify)
strncpy(oui, mac_get_oui(full_mac), 9);
struct stat st;
char *str, *map, *off, *end;
int fd;
fd = open(OUIFILE, O_RDONLY);
if (fd < 0)
return("NO_OUI_FILE");
if (fstat(fd, &st) < 0)
{
close(fd);
return("CANT_STAT_FILE");
}
str = malloc(128);
if (!str)
{
close(fd);
return("MALLOC_FAILED");
}
memset(str, 0, 128);
map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (!map || map == MAP_FAILED)
{
free(str);
close(fd);
return("MMAP_FAILED");
}
off = strstr(map, oui);
if (off)
{
off += TXTOFFSET;
end = strpbrk(off, "\r\n");
strncpy(str, off, end - off);
}
else
{
free(str);
str = "No Record";
}
munmap(map, st.st_size);
close(fd);
return str;
}
/*
* CRC functions based on CRC tester 1.3 written by Sven Reifegerste
* Available at: http://www.zorc.breitbandkatze.de/crc.html
*/
// Used internally for CRC
unsigned long reflect (unsigned long crc, int bitnum)
{
unsigned long i, j=1, crcout=0;
for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
}
// Encode MAC with CRC32
char* mac_encode (char* full_mac)
{
unsigned long i, j, c, bit;
unsigned long crc = crcinit_direct;
// Static length, could be trouble
int len = 17;
// For return formatting
static char addr[18] = {0};
// Verify first
if (mac_verify(full_mac))
return("INVALID_MAC");
for (i=0; i < len; i++)
{
c = (unsigned long)*full_mac++;
if (refin) c = reflect(c, 8);
for (j=0x80; j; j>>=1) {
bit = crc & crchighbit;
crc<<= 1;
if (c & j) bit^= crchighbit;
if (bit) crc^= polynom;
}
}
if (refout) crc=reflect(crc, order);
crc^= crcxor;
crc&= crcmask;
// Format it
sprintf(addr,"%.8X",(unsigned int)crc);
return(addr);
}