File: uniqueiv.c

package info (click to toggle)
aircrack-ng 1%3A1.6%2Bgit20210130.91820bc-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,056 kB
  • sloc: ansic: 67,045; cs: 5,392; sh: 3,773; python: 2,565; pascal: 1,074; asm: 570; makefile: 253; cpp: 46
file content (268 lines) | stat: -rw-r--r-- 6,284 bytes parent folder | download | duplicates (3)
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
/*
 *  IV uniqueness detection method.
 *
 *  Copyright (C) 2004-2008 Stanislaw Pusep:
 *
 *  This program 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.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU General Public License in all respects
 *  for all of the code used other than OpenSSL. *  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so. *  If you
 *  do not wish to do so, delete this exception statement from your
 *  version. *  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 */

/*
 *  Each IV byte is stored in corresponding "level". We have 3 levels with
 *  IV[2] as root index (level 0), IV[1] and IV[2] as level 2 and level 1
 *  indices respectively. Space required to allocate all data is at maximum
 *  2^24/8 (2 MB) and space required by filled index structures is 257 KB.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>

#include "aircrack-ng/defs.h"
#include "aircrack-ng/ce-wep/uniqueiv.h"

/* allocate root structure */

unsigned char ** uniqueiv_init(void)
{
	int i;

	/* allocate root bucket (level 0) as vector of pointers */

	unsigned char ** uiv_root
		= (unsigned char **) malloc(256 * sizeof(unsigned char *));

	if (uiv_root == NULL) return (NULL);

	/* setup initial state as empty */

	for (i = 0; i < 256; ++i) uiv_root[i] = NULL;

	return (uiv_root);
}

/* update records with new IV */

int uniqueiv_mark(unsigned char ** uiv_root, unsigned char IV[3])
{
	unsigned char ** uiv_lvl1;
	unsigned char * uiv_lvl2;
	short i;

	if (uiv_root == NULL) return (0);

	/* select bucket from level 1 */

	uiv_lvl1 = (unsigned char **) uiv_root[IV[2]];

	/* create if it doesn't exist */

	if (uiv_lvl1 == NULL)
	{
		/* allocate level 2 bucket being a vector of bits */

		uiv_lvl1 = (unsigned char **) malloc(256 * sizeof(unsigned char *));

		if (uiv_lvl1 == NULL) return (1);

		/* setup initial state as empty */

		for (i = 0; i < 256; i++) uiv_lvl1[i] = NULL;

		/* link to parent bucket */

		uiv_root[IV[2]] = (unsigned char *) uiv_lvl1;
	}

	/* select bucket from level 2 */

	uiv_lvl2 = (unsigned char *) uiv_lvl1[IV[1]];

	/* create if it doesn't exist */

	if (uiv_lvl2 == NULL)
	{
		/* allocate level 2 bucket as a vector of pointers */

		uiv_lvl2 = (unsigned char *) malloc(32 * sizeof(unsigned char));

		if (uiv_lvl2 == NULL) return (1);

		/* setup initial state as empty */

		for (i = 0; i < 32; i++) uiv_lvl2[i] = 0;

		/* link to parent bucket */

		uiv_lvl1[IV[1]] = uiv_lvl2;
	}

	/* place single bit into level 2 bucket */

	uiv_lvl2[BITWISE_OFFT(IV[0])] |= BITWISE_MASK(IV[0]);

	return (0);
}

/* check if already seen IV */

int uniqueiv_check(unsigned char ** uiv_root, unsigned char IV[3])
{
	unsigned char ** uiv_lvl1;
	unsigned char * uiv_lvl2;

	if (uiv_root == NULL) return (IV_NOTHERE);

	/* select bucket from level 1 */

	uiv_lvl1 = (unsigned char **) uiv_root[IV[2]];

	/* stop here if not even allocated */

	if (uiv_lvl1 == NULL) return (IV_NOTHERE);

	/* select bucket from level 2 */

	uiv_lvl2 = (unsigned char *) uiv_lvl1[IV[1]];

	/* stop here if not even allocated */

	if (uiv_lvl2 == NULL) return (IV_NOTHERE);

	/* check single bit from level 2 bucket */

	if ((uiv_lvl2[BITWISE_OFFT(IV[0])] & BITWISE_MASK(IV[0])) == 0)
		return (IV_NOTHERE);
	else
		return (IV_PRESENT);
}

/* unallocate everything */

void uniqueiv_wipe(unsigned char ** uiv_root)
{
	int i, j;
	unsigned char ** uiv_lvl1;
	unsigned char * uiv_lvl2;

	if (uiv_root == NULL) return;

	/* recursively wipe out allocated buckets */

	for (i = 0; i < 256; ++i)
	{
		uiv_lvl1 = (unsigned char **) uiv_root[i];

		if (uiv_lvl1 != NULL)
		{
			for (j = 0; j < 256; ++j)
			{
				uiv_lvl2 = (unsigned char *) uiv_lvl1[j];

				if (uiv_lvl2 != NULL)
				{
					free(uiv_lvl2);
					uiv_lvl2 = NULL;
				}
			}

			free(uiv_lvl1);
			uiv_lvl1 = NULL;
		}
	}

	free(uiv_root);
	uiv_root = NULL;

	return;
}

unsigned char * data_init(void)
{
	// It could eat up to (256*256*256) * 3 bytes = 48Mb :/
	unsigned char * IVs
		= (unsigned char *) calloc(256 * 256 * 256 * 3, sizeof(unsigned char));
	ALLEGE(IVs != NULL);
	return (IVs);
}

/* Checking WEP packet:
 * The 2 first bytes of 2 different data packets having the same IV (for the
 * same AP)
 * should be exactly the same due to the fact that unencrypted, they are always
 * the same:
 * AA AA
 */

int data_check(unsigned char * data_root,
			   unsigned char IV[3],
			   unsigned char data[2])
{
	int IV_position, cloaking;

	// Init vars
	cloaking = NO_CLOAKING;

	// Make sure it is allocated
	if (data_root != NULL)
	{
		// Try to find IV
		IV_position = (((IV[0] * 256) + IV[1]) * 256) + IV[2];
		IV_position *= 3;

		// Check if existing
		if (*(data_root + IV_position) == 0)
		{
			// Not existing
			*(data_root + IV_position) = 1;

			// Add it
			*(data_root + IV_position + 1) = data[0];
			*(data_root + IV_position + 2) = data[1];
		}
		else
		{
			// Good, we found it, so check it now
			if (*(data_root + IV_position + 1) != data[0]
				|| *(data_root + IV_position + 2) != data[1])
			{
				cloaking = CLOAKING;
			}
		}
	}
	// else, cannot detect since it is not started

	return (cloaking);
}

void data_wipe(unsigned char * data)
{
	if (data) free(data);
}