-
Notifications
You must be signed in to change notification settings - Fork 30
/
vuln_db.c
423 lines (394 loc) · 12.7 KB
/
vuln_db.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright (c) 2001-2002 Secure Software, Inc
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_EXPAT_H
# include "expat.h"
# else /* HAVE_XMLPARSE_H */
# ifdef HAVE_XMLPARSE_H
# include "xmlparse.h"
# else
# ifdef _MSC_VER
# include "expat.h"
# endif
# endif
#endif
#include "hash.h"
#include "vuln_db.h"
/* Modified by Mike Ellison February 17th, 2002 - win32 port
*
* Just #define to using native win32 functions, since VC++ at least doesn't
* seem to have the strncasecmp() function.
*/
#ifdef _MSC_VER
#define strcasecmp _strcmpi
#endif
typedef struct udata_t
{
char * fname[10]; /* Arbitrary max depth of XML doc */
int depth;
char databuf[2048];
char * bufptr;
Vuln_t * vuln;
int failed;
int failedat;
XML_Parser parser;
Hash myhash;
Hash parenthash;
} udata_t;
void InitUdata(udata_t *udata, XML_Parser parser)
{
udata->failed = 0;
udata->failedat = 0;
udata->parser = parser;
udata->depth = -1;
udata->bufptr = (char *)NULL;
udata->vuln = (Vuln_t *)NULL;
udata->myhash = (Hash)NULL;
}
void FreeVuln(Vuln_t *vuln)
{
if (vuln == (Vuln_t *)NULL)
return;
if (vuln->Name != (char *)NULL)
free(vuln->Name);
if (vuln->FSProblem != (FSProblem_t *)NULL)
free(vuln->FSProblem);
if (vuln->BOProblem != (BOProblem_t *)NULL)
free(vuln->BOProblem);
if (vuln->InputProblem != (InputProblem_t *)NULL)
free(vuln->InputProblem);
if (vuln->Info != (Info_t *)NULL)
{
FreeInfo(vuln->Info);
free(vuln->Info);
}
}
void InitVuln(Vuln_t *vuln)
{
vuln->Name = (char *)NULL;
vuln->FSProblem = (FSProblem_t *)NULL;
vuln->BOProblem = (BOProblem_t *)NULL;
vuln->RaceCheck = 0;
vuln->RaceUse = 0;
vuln->Input = 0;
vuln->Info = (Info_t *)NULL;
vuln->InputProblem = (InputProblem_t *)NULL;
}
void InitFSProblem(FSProblem_t *prob)
{
prob->Arg = 0;
prob->Severity = Default;
}
void InitBOProblem(BOProblem_t *prob)
{
prob->FormatArg = 0;
prob->SrcBufArg = 0;
prob->Severity = Default;
}
void InitInputProblem(InputProblem_t *prob)
{
prob->Arg = 0;
prob->Severity = Default;
}
void FreeInfo(Info_t *info)
{
if (info == (Info_t *)NULL)
return;
if (info->Description != (char *)NULL)
free(info->Description);
if (info->URL != (char *)NULL)
free(info->URL);
}
void InitInfo(Info_t *info)
{
info->Description = (char *)NULL;
info->URL = (char *)NULL;
info->Severity = Default;
}
static int SetFailed(udata_t *mydata)
{
mydata->failed = 1;
mydata->failedat = XML_GetCurrentLineNumber(mydata->parser);
return mydata->failedat;
}
void StartElement(void *udata, const char *name, const char **atts)
{
udata_t * mydata = (udata_t *)udata;
char * langname = (char *)NULL;
char ** tmp;
char * curname = (char *)NULL;
char * curval;
int count = 0;
unsigned int i;
if (mydata->failed)
return;
mydata->fname[mydata->depth + 1] = (char *)malloc(strlen(name) + 1);
strncpy(mydata->fname[mydata->depth + 1], name, strlen(name));
mydata->fname[mydata->depth + 1][strlen(name)] = '\0';
mydata->depth++;
mydata->bufptr = mydata->databuf;
if (!strcmp(name, "VulnDB"))
{
if (!atts || !atts[0])
langname = "default";
else
{
tmp = (char **)atts;
while (*tmp != NULL)
{
if (!(count % 2))
curname = *tmp;
else if (count % 2 == 1)
{
curval = *tmp;
if (!strcasecmp(curname, "lang"))
{
langname = (char *)malloc(strlen(curval) + 1);
for (i = 0; i < strlen(curval); i++)
langname[i] = tolower(curval[i]);
langname[strlen(curval)] = 0;
}
}
tmp++;
count++;
}
}
/* This prevented loading multiple xml files for the same language */
/* if (!HashGet(mydata->parenthash, langname)) */
if(!(mydata->myhash=HashGet(mydata->parenthash,langname)))
{
mydata->myhash = HashInit();
if (mydata->myhash != (Hash)NULL)
HashInsert(mydata->parenthash, mydata->myhash, langname);
}
}
else if (!strcmp(name, "Vulnerability"))
{
if (mydata->vuln != (Vuln_t *)NULL)
{
fprintf(stderr, "Vulnerability open tag found, but already in one at line %d\n", SetFailed(mydata));
return;
}
mydata->vuln = (Vuln_t *)malloc(sizeof(Vuln_t));
InitVuln(mydata->vuln);
}
else if (!strcmp(name, "FSProblem"))
{
if (mydata->vuln == (Vuln_t *)NULL)
{
fprintf(stderr,"In FSProblem, but no Vuln struct at line %d\n", SetFailed(mydata));
return;
}
if (mydata->vuln->FSProblem != (FSProblem_t *)NULL)
{
fprintf(stderr, "FSProblem open tag found but already in one at line %d\n", SetFailed(mydata));
return;
}
mydata->vuln->FSProblem = (FSProblem_t *)malloc(sizeof(FSProblem_t));
InitFSProblem(mydata->vuln->FSProblem);
}
else if (!strcmp(name, "BOProblem"))
{
if (mydata->vuln == (Vuln_t *)NULL)
{
fprintf(stderr, "In BOProblem, but no Vuln struct at line %d\n", SetFailed(mydata));
return;
}
if (mydata->vuln->BOProblem != (BOProblem_t *)NULL)
{
fprintf(stderr,"BOProblem open tag found, but already in one at line %d\n", SetFailed(mydata));
return;
}
mydata->vuln->BOProblem = (BOProblem_t *)malloc(sizeof(BOProblem_t));
InitBOProblem(mydata->vuln->BOProblem);
}
else if (!strcmp(name, "InputProblem"))
{
if (mydata->vuln == (Vuln_t *)NULL)
{
fprintf(stderr, "In InputProblem, but no Vuln struct at line %d\n", SetFailed(mydata));
return;
}
if (mydata->vuln->InputProblem != (InputProblem_t *)NULL)
{
fprintf(stderr,"InputProblem open tag found, but already in one at %d\n", SetFailed(mydata));
return;
}
mydata->vuln->InputProblem = (InputProblem_t *)malloc(sizeof(InputProblem_t));
InitInputProblem(mydata->vuln->InputProblem);
}
else if (!strcmp(name, "Info"))
{
if (mydata->vuln == (Vuln_t *)NULL)
{
fprintf(stderr,"In Info, but no Vuln struct at line %d\n", SetFailed(mydata));
return;
}
if (mydata->vuln->Info != (Info_t *)NULL)
{
fprintf(stderr, "Found Info open tag, but already in one at line %d\n", SetFailed(mydata));
return;
}
mydata->vuln->Info = (Info_t *)malloc(sizeof(Info_t));
InitInfo(mydata->vuln->Info);
}
else if (!strcmp(name, "Description"))
{
if (mydata->vuln == (Vuln_t *)NULL || mydata->vuln->Info == (Info_t *)NULL)
fprintf(stderr, "Found Description tag, in wrong place, must be inside <Info>\n");
}
}
int FrameIsName(udata_t *udata, const char *name)
{
return strcmp(udata->fname[udata->depth], name);
}
Severity_t ConvertSeverity(const char *buf)
{
if (!strcasecmp(buf, "high"))
return High;
if (!strcasecmp(buf, "medium"))
return Medium;
if (!strcasecmp(buf, "low"))
return Low;
return Default;
}
void EndElement(void *udata, const char *name)
{
udata_t * mydata = (udata_t *)udata;
if (mydata->failed)
return;
free(mydata->fname[mydata->depth]);
mydata->depth--;
if (!strcmp(name, "Vulnerability"))
{
if (mydata->vuln == (Vuln_t *)NULL)
{
fprintf(stderr, "At end of Vulnerability section, but no vuln data?!\n");
return;
}
else
{
if (!HashInsert(mydata->myhash, (void *)mydata->vuln, mydata->vuln->Name))
{
FreeVuln(mydata->vuln);
free(mydata->vuln);
}
mydata->vuln = (Vuln_t *)NULL;
}
}
else if (!strcmp(name, "Name"))
{
mydata->vuln->Name = (char *)malloc(strlen(mydata->databuf) + 1);
strncpy(mydata->vuln->Name, mydata->databuf, strlen(mydata->databuf));
mydata->vuln->Name[strlen(mydata->databuf)] = '\0';
}
else if (!strcmp(name, "FormatArg"))
mydata->vuln->BOProblem->FormatArg = atoi(mydata->databuf);
else if (!strcmp(name, "SrcBufArg"))
mydata->vuln->BOProblem->SrcBufArg = atoi(mydata->databuf);
else if (!strcmp(name, "Scan"))
mydata->vuln->BOProblem->Scan = 1;
else if (!strcmp(name, "Description"))
{
mydata->vuln->Info->Description = (char *)malloc(strlen(mydata->databuf) + 1);
strncpy(mydata->vuln->Info->Description, mydata->databuf, strlen(mydata->databuf));
mydata->vuln->Info->Description[strlen(mydata->databuf)] = '\0';
}
else if (!strcmp(name, "URL"))
{
mydata->vuln->Info->URL = (char *)malloc(strlen(mydata->databuf) + 1);
strncpy(mydata->vuln->Info->URL, mydata->databuf, strlen(mydata->databuf));
mydata->vuln->Info->URL[strlen(mydata->databuf)] = '\0';
}
else if (!strcmp(name, "RaceCheck"))
mydata->vuln->RaceCheck = atoi(mydata->databuf);
else if (!strcmp(name, "RaceUse"))
mydata->vuln->RaceUse = atoi(mydata->databuf);
else if (!strcmp(name, "Input"))
mydata->vuln->Input = 1;
else if (!strcmp(name, "Arg"))
{
if (!FrameIsName(mydata, "FSProblem"))
mydata->vuln->FSProblem->Arg = atoi(mydata->databuf);
else if (!FrameIsName(mydata, "InputProblem"))
mydata->vuln->InputProblem->Arg = atoi(mydata->databuf);
}
else if (!strcmp(name, "Severity"))
{
if (!FrameIsName(mydata, "FSProblem"))
mydata->vuln->FSProblem->Severity = ConvertSeverity(mydata->databuf);
else if(!FrameIsName(mydata, "BOProblem"))
mydata->vuln->BOProblem->Severity = ConvertSeverity(mydata->databuf);
else if(!FrameIsName(mydata, "InputProblem"))
mydata->vuln->InputProblem->Severity = ConvertSeverity(mydata->databuf);
else if(!FrameIsName(mydata, "Info"))
mydata->vuln->Info->Severity = ConvertSeverity(mydata->databuf);
}
mydata->bufptr = mydata->databuf;
}
void CharData(void *udata, const XML_Char *str, int len)
{
udata_t * mydata = (udata_t *)udata;
if (mydata->failed)
return;
if (mydata->bufptr + len > mydata->databuf + sizeof(mydata->databuf))
{
fprintf(stderr, "Possible data overflow, ignoring it.\n");
return;
}
strncpy(mydata->bufptr, str, len);
mydata->bufptr += len;
*mydata->bufptr = '\0';
}
Hash ParseVulnDb(char *buf, Hash *usehash)
{
XML_Parser parser;
udata_t mydata;
parser = XML_ParserCreate(NULL);
if (parser == (XML_Parser)NULL)
{
fprintf(stderr, "Failed to create XML Parser\n");
return (Hash)NULL;
}
InitUdata(&mydata, parser);
XML_SetUserData(parser, &mydata);
XML_SetElementHandler(parser, StartElement, EndElement);
XML_SetCharacterDataHandler(parser, CharData);
if (*usehash == (Hash)NULL)
*usehash = HashInit();
mydata.parenthash = *usehash;
if (*usehash == NULL)
{
fprintf(stderr, "Failed to create Hash table\n");
return (Hash)NULL;
}
if (!XML_Parse(parser, buf, strlen(buf), 1))
{
fprintf(stderr, "%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
return (Hash)NULL;
}
if (mydata.failed)
return (Hash)NULL;
return *usehash;
}