File: cowpatty.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 (224 lines) | stat: -rw-r--r-- 5,408 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
/*
 *  coWPAtty hash DB file helper functions
 *
 *  Copyright (C) 2018 Thomas d'Otreppe <tdotreppe@aircrack-ng.org>
 *
 *  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.
 */

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "aircrack-ng/defs.h"
#include "aircrack-ng/cowpatty/cowpatty.h"

void close_free_cowpatty_hashdb(struct cowpatty_file * cf)
{
	if (cf != NULL)
	{
		if (cf->fp)
		{
			fclose(cf->fp);
		}
		free(cf);
	}
}

struct cowpatty_file * open_cowpatty_hashdb(const char * filename,
											const char * mode)
{
	struct hashdb_head filehead;

	// Initialize structure
	struct cowpatty_file * ret
		= (struct cowpatty_file *) malloc(sizeof(struct cowpatty_file));
	ALLEGE(ret != NULL);
	memset(ret->ssid, 0, sizeof(ret->ssid));
	memset(ret->error, 0, sizeof(ret->error));
	ret->fp = NULL;

	if (filename == NULL || filename[0] == 0)
	{
		strcpy(ret->error, "No filename specified");
		return (ret);
	}

	if (mode == NULL || strncmp(mode, "r", 1) == 0)
	{
		if (strcmp(filename, "-") == 0)
		{
			ret->fp = stdin;
		}
		else
		{
			ret->fp = fopen(filename, "r");
			if (ret->fp == NULL)
			{
				snprintf(ret->error,
						 sizeof(ret->error),
						 "File <%s> cannot be opened",
						 filename);
				return (ret);
			}
		}

		// Check headers
		if (fread(&filehead, sizeof(struct hashdb_head), 1, ret->fp) != 1)
		{
			strcpy(ret->error, "Failed reading hash DB header");
			fclose(ret->fp);
			ret->fp = NULL;
			return (ret);
		}

		if (filehead.magic != GENPMKMAGIC)
		{ // Verify header magic
			strcpy(ret->error, "Header magic doesn't match");
			fclose(ret->fp);
			ret->fp = NULL;
			return (ret);
		}
		if (filehead.ssid[0] == 0)
		{
			strcpy(ret->error, "SSID is NULL");
			fclose(ret->fp);
			ret->fp = NULL;
			return (ret);
		}

		// Copy SSID
		memcpy(ret->ssid, filehead.ssid, sizeof(filehead.ssid));
		if (filehead.ssidlen > 32 || filehead.ssidlen == 0)
		{
			snprintf(ret->error,
					 sizeof(ret->error),
					 "Advertised SSID length is %u (Max length: 32)",
					 filehead.ssidlen);
			fclose(ret->fp);
			ret->fp = NULL;
		}
	}
	else
	{
		// Write not supported yet
		strcpy(ret->error, "Write and other modes not supported yet");
	}

	return (ret);
}

struct hashdb_rec * read_next_cowpatty_record(struct cowpatty_file * cf)
{
	int rc, wordlength;
	struct hashdb_rec * ret = NULL;

	if (cf == NULL || cf->error[0])
	{
		return (NULL);
	}

	if (cf->fp == NULL)
	{
		strcpy(cf->error, "File pointer is NULL");
		return (NULL);
	}

	// Allocate memory
	ret = (struct hashdb_rec *) malloc(sizeof(struct hashdb_rec));
	if (ret == NULL)
	{
		strcpy(cf->error, "Failed allocating memory for coWPAtty record");
		return (NULL);
	}

	// Read record size
	rc = fread(&(ret->rec_size), sizeof(ret->rec_size), 1, cf->fp);

	// Close and exit if failed
	if (rc != 1 && feof(cf->fp))
	{
		free(ret);
		fclose(cf->fp);
		cf->fp = NULL;
		return (NULL);
	}

	// Get passphrase length
	ret->word = NULL;
	wordlength = ret->rec_size - (sizeof(ret->pmk) + sizeof(ret->rec_size));

	if (wordlength > 0 && wordlength <= MAX_PASSPHRASE_LENGTH)
	{
		ret->word = (char *) calloc(wordlength + 1, sizeof(char));
		ALLEGE(ret->word != NULL);

		// Read passphrase
		rc += fread(ret->word, wordlength, 1, cf->fp);
		if (rc == 2)
		{
			// And the PMK
			rc += fread(&ret->pmk, sizeof(ret->pmk), 1, cf->fp);
		}
	}

	// Check if everything went well
	if (rc != 3 || ret->word == NULL || ret->word[0] == 0)
	{
		if (rc == 1)
		{
			snprintf(cf->error,
					 sizeof(cf->error),
					 "Error while reading record, failed to read passphrase "
					 "invalid word length: %i",
					 wordlength);
		}
		else if (rc == 2)
		{
			strcpy(cf->error, "Error while reading record, failed reading PMK");
		}
		else
		{
			strcpy(cf->error, "NULL or empty passphrase");
		}

		// Cleanup and close file
		fclose(cf->fp);
		free(ret->word);
		free(ret);
		ret = NULL;
		cf->fp = NULL;
	}

	return (ret);
}