-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathifuzz.c
183 lines (157 loc) · 4.01 KB
/
ifuzz.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
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/wait.h>
#include "ifuzz.h"
pid_t child;
int
main (int argc, char *argv[])
{
if (argc < 3)
{
usage ();
exit (0);
}
check_dumpdir_existance ();
/* could also call do_fuzz, begin_fuzz is just a wrapper to
** pass files to do_fuzz so you don't have to specify them
** manually
*/
if (argc == 3)
begin_fuzz (argv[2], atoi (argv[1]), 0, 0);
else
begin_fuzz (argv[2], atoi (argv[1]), argc, argv);
remove_file ();
return 0;
}
int
begin_fuzz (const char *path, int fuzztype, int argc, char **argv)
{
DIR *dirp;
struct dirent *dp;
char *buff = NULL;
struct stat statbuf;
int ix = 0;
if ((dirp = opendir (path)) == NULL)
{
fprintf (stderr, "Error opening specified directory [%s]\n", path);
perror ("opendir");
exit (-1);
}
while ((dp = readdir (dirp)))
{
buff = realloc (buff, p_strlen (path) + 1 + strlen (dp->d_name) + 1);
sprintf (buff, "%s/%s", path, dp->d_name);
if (stat (buff, &statbuf))
{
perror ("stat");
exit (-1);
}
if (!(strcmp (dp->d_name, "ifuzz")) ||
!(strcmp (dp->d_name, ".")) ||
!(strcmp (dp->d_name, "..")) ||
!(statbuf.st_mode & S_IEXEC) || !(S_ISREG (statbuf.st_mode)))
continue;
printf ("Executable: %s\n", dp->d_name);
ix++;
do_fuzz (buff, dp->d_name, fuzztype, argc, argv);
}
free (buff);
closedir (dirp);
printf ("Fuzzed %d files in directory %s\n", ix, path);
return 0;
}
/*
fullpath is full binary path for exec
filename is argv[0]
*/
void
do_fuzz (char *fullpath, char *filename, int fuzztype, int argc, char **argv)
{
static struct getopt_args getopt_args;
static struct argv_args argv_args;
static struct singleoption_args singleoption_args;
switch (fuzztype)
{
case FUZZTYPE_ARGV0:
printf ("Doing argv[0] fuzz\n");
if (!argv_args.initialized) /* uninitialized */
parse_argv (argc, argv, &argv_args);
fuzzmethod_argvzero (fullpath, &argv_args);
break;
case FUZZTYPE_ARGV1:
printf ("Doing argv[1] fuzz\n");
if (!argv_args.initialized) /* uninitialized */
parse_argv (argc, argv, &argv_args);
fuzzmethod_argvone (fullpath, &argv_args);
break;
case FUZZTYPE_SINGLE:
printf ("Doing unintelligent singleoption fuzz\n");
parse_singleoption(argc,argv,&singleoption_args);
fuzzmethod_singleoption (fullpath,&singleoption_args);
break;
case FUZZTYPE_GETOPT:
if (!getopt_args.optstring) /* uninitialized */
parse_getopt3 (argc, argv, &getopt_args);
if (!getopt_args.optstring)
{
fuzzmethod_getopt3_usage ();
}
printf ("Doing getopt optstring fuzz [%s]\n", getopt_args.optstring);
fuzzmethod_getopt3 (fullpath, &getopt_args);
break;
default:
printf ("Fuzz type not implemented yet\n");
break;
}
return;
}
/*
** to handle those programs that either expect
** user interaction or ones that just lock up.
*/
void
handle_alarm (int signum)
{
printf ("Killing %d\n", child);
kill (child, SIGTERM);
return;
}
void
usage ()
{
printf
("Usage: ifuzz <fuzztype> <binary directory> [fuzz specific options]\n");
printf ("Fuzztypes: \t0 - argv[0] fuzzing\n\t\t");
printf ("1 - argv[1] fuzzing\n\t\t");
printf ("2 - incremental single option fuzzing\n\t\t");
printf ("3 - incremental multiple option fuzzing\n");
printf ("\tifuzz 3 directory/ <-o optstring> [-e extra-args] [-f first_arg] [-l last_arg] [-s]\n");
printf("\tifuzz 1 directory/ [-s]\n");
printf("\tifuzz 0 directory/ [-s]\n");
}
char *
asciitime ()
{
time_t t;
time (&t);
return ctime (&t);
}
void
check_dumpdir_existance ()
{
DIR *dir;
if (!(dir = opendir (CODE_DUMP_PATH)))
{
perror ("Fatal error opening code dump directory");
fprintf (stderr, "create/check permissions on %s\n", CODE_DUMP_PATH);
exit (-1);
}
closedir (dir);
return;
}