Debian Codegraph @

src/ntproba/ioPattern.c

raw symbols SCIP
1 /*
2 *
3 * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory
4 * trade-off using rainbow tables.
5 *
6 * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude
7 * Hochreutiner, Andreas Huber and Etienne Dysli.
8 *
9 * Copyright (c) 2013 Philippe Oechslin, Cedric Tissieres,
10 * Bertrand Mesot, Pierre Lestringant
11 *
12 * Ophcrack is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * Ophcrack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Ophcrack; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * This program is released under the GPL with the additional exemption
27 * that compiling, linking, and/or using OpenSSL is allowed.
28 *
29 *
30 *
31 *
32 */
33 /* WARNING: this file is a NOT copy from implCPU/v6 */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <expat.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <stdint.h>
43 #include <unistd.h>
44
45 #include "ioPattern.h"
46 #include "aliasPattern.h"
47 #include "ioMarkov.h"
48 #include "patternDescriptor.h"
49
50 typedef struct xmlDataWrapper xmlDataWrapper;
51 struct xmlDataWrapper{
52 patternArray* array;
53 unsigned int currentPattern;
54 unsigned int currentCharType;
55 };
56
57 void start_element(void *data, const char *element, const char **attribute){
58 xmlDataWrapper* wrapper = (xmlDataWrapper*)data;
59 unsigned int i;
60
61 if (!strcmp(element, "patternArray")){
62 wrapper->array->size = atoi(attribute[1]);
63 wrapper->array->sizeArray = (uint64_t*)malloc(sizeof(uint64_t)*wrapper->array->size);
64 wrapper->array->patternDescriptor = (uint64_t*)malloc(sizeof(uint64_t)*wrapper->array->size);
65 wrapper->array->patternLength = (uint64_t*)malloc(sizeof(uint64_t)*wrapper->array->size);
66 wrapper->array->lut = (lookUpTable*)malloc(sizeof(lookUpTable));
67
68 memset(wrapper->array->patternDescriptor, 0, wrapper->array->size*sizeof(uint64_t));
69 memset(wrapper->array->patternLength, 0, wrapper->array->size*sizeof(uint64_t));
70
71 wrapper->currentPattern = 0;
72 return;
73 }
74 if (!strcmp(element, "pattern")){
75 wrapper->array->sizeArray[wrapper->currentPattern] = strtoull(attribute[1], NULL, 16);
76 wrapper->currentCharType = 0;
77 return;
78 }
79 if (!strcmp(element, "element")){
80 switch(attribute[3][0]){
81 case 'U' : {
82 switch(attribute[3][1]){
83 case 'N' : {
84 for (i = 0; i < (unsigned int)atoi(attribute[1]); i++){
85 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_UPPER_NOMARK<< 4*(wrapper->currentCharType + i);
86 }
87 wrapper->currentCharType += atoi(attribute[1]);
88 break;
89 }
90 case '1' : {
91 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_UPPER_MARKFIRST<< 4*wrapper->currentCharType ;
92 wrapper->array->patternLength[wrapper->currentPattern] |= (uint64_t)((atoi(attribute[1])-1) & 0x0000000f)<< 4*wrapper->currentCharType ;
93 wrapper->currentCharType ++;
94 break;
95 }
96 case '2' : {
97 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_UPPER_MARKSECOND<< 4*wrapper->currentCharType ;
98 wrapper->currentCharType ++;
99 break;
100 }
101 case 'M' : {
102 for (i = 0; i < (unsigned int)atoi(attribute[1]); i++){
103 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_UPPER_MARK<< 4*(wrapper->currentCharType + i);
104 }
105 wrapper->currentCharType += atoi(attribute[1]);
106 break;
107 }
108 default : {
109 printf("Error while parsing XML file: unknown symbol\n");
110 break;
111 }
112 }
113 break;
114 }
115 case 'L' : {
116 switch(attribute[3][1]){
117 case 'N' : {
118 for (i = 0; i < (unsigned int)atoi(attribute[1]); i++){
119 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_LOWER_NOMARK<< 4*(wrapper->currentCharType + i);
120 }
121 wrapper->currentCharType += atoi(attribute[1]);
122 break;
123 }
124 case '1' : {
125 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_LOWER_MARKFIRST<< 4*wrapper->currentCharType ;
126 wrapper->array->patternLength[wrapper->currentPattern] |= (uint64_t)((atoi(attribute[1])-1) & 0x0000000f)<< 4*wrapper->currentCharType ;
127 wrapper->currentCharType ++;
128 break;
129 }
130 case '2' : {
131 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_LOWER_MARKSECOND<< 4*wrapper->currentCharType ;
132 wrapper->currentCharType ++;
133 break;
134 }
135 case 'M' : {
136 for (i = 0; i < (unsigned int)atoi(attribute[1]); i++){
137 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_LOWER_MARK<< 4*(wrapper->currentCharType + i);
138 }
139 wrapper->currentCharType += atoi(attribute[1]);
140 break;
141 }
142 default : {
143 printf("Error while parsing XML file: unknown symbol\n");
144 break;
145 }
146 }
147 break;
148 }
149 case 'N' : {
150 for (i = 0; i < (unsigned int)atoi(attribute[1]); i++){
151 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_NUMERAL << 4*(wrapper->currentCharType + i);
152 }
153 wrapper->currentCharType += atoi(attribute[1]);
154 break;
155 }
156 case 'S' : {
157 for (i = 0; i < (unsigned int)atoi(attribute[1]); i++){
158 wrapper->array->patternDescriptor[wrapper->currentPattern] |= (uint64_t)DESC_SPECIAL << 4*(wrapper->currentCharType + i);
159 }
160 wrapper->currentCharType += atoi(attribute[1]);
161 break;
162 }
163 default : {
164 printf("Error while parsing XML file: unknown symbol\n");
165 break;
166 }
167 }
168 return;
169 }
170 }
171
172 void end_element(void *data, const char *element){
173 xmlDataWrapper* wrapper = (xmlDataWrapper*)data;
174 if (!strcmp(element, "pattern")){
175 wrapper->currentPattern ++;
176 return;
177 }
178 }
179
180 char readPatternArrayFromXMLFile(patternArray* array, char* path, char* fileName){
181 struct stat sb;
182 char* xmlBuff;
183 char buffer[256];
184 int xmlFile;
185
186 if (fileName == NULL){
187 sprintf(buffer, "%s/%s", path, DEFAULT_PATTERN_FILE_NAME);
188 }
189 else{
190 sprintf(buffer, "%s/%s", path, fileName);
191 }
192
193 xmlFile = open(buffer, O_RDONLY);
194
195 if (xmlFile == -1){
196 return 0;
197 }
198
199 if (fstat(xmlFile, &sb) == -1){
200 close(xmlFile);
201 return 0;
202 }
203
204 xmlBuff = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, xmlFile, 0);
205 if (xmlBuff == MAP_FAILED){
206 close(xmlFile);
207 return 0;
208 }
209
210 xmlDataWrapper data;
211 data.array = array;
212 data.currentPattern = 0;
213 data.currentCharType = 0;
214
215
216 XML_Parser parser = XML_ParserCreate(NULL);
217 XML_SetUserData(parser, &data);
218 XML_SetElementHandler(parser, start_element, end_element);
219
220 if(XML_Parse(parser, xmlBuff, sb.st_size, XML_TRUE) == XML_STATUS_ERROR){
221 XML_ParserFree(parser);
222 munmap(xmlBuff, sb.st_size);
223 close(xmlFile);
224 return 0;
225 }
226
227 initLookUpTable(array->lut, array);
228
229 XML_ParserFree(parser);
230 munmap(xmlBuff, sb.st_size);
231 close(xmlFile);
232
233 return 1;
234 }