-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_static.cpp
138 lines (125 loc) · 3.49 KB
/
test_static.cpp
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
/*
WinAFL - A simple test binary that exercises various behaviors
depending on inputs:
- 'test1' crashes with a normal write access violation at NULL
- 'test2' crashes with a /GS stack cookie violation
- 'test3' triggers a hang
- 'test4' triggers an exception that is caught and handled
- 'test5' triggers an OutputDebugString
- 'test6' triggers an allocation of 120MB (and a crash if the
allocation fails)
-------------------------------------------------------------
Written by Axel "0vercl0k" Souchet <[email protected]>
Copyright 2017 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include "afl-staticinstr.h"
int test(int argc, char **argv) {
char *crash = NULL;
FILE *fp = fopen(argv[1], "rb");
char c;
if (!fp) {
printf("Error opening file\n");
goto end;
}
if (fread(&c, 1, 1, fp) != 1) {
printf("Error reading file\n");
goto clean;
}
if (c != 't') {
printf("Error 1\n");
goto clean;
}
if (fread(&c, 1, 1, fp) != 1) {
printf("Error reading file\n");
goto clean;
}
if (c != 'e') {
printf("Error 2\n");
goto clean;
}
if (fread(&c, 1, 1, fp) != 1) {
printf("Error reading file\n");
goto clean;
}
if (c != 's') {
printf("Error 3\n");
goto clean;
}
if (fread(&c, 1, 1, fp) != 1) {
printf("Error reading file\n");
goto clean;
}
if (c != 't') {
printf("Error 4\n");
goto clean;
}
printf("!!!!!!!!!!OK!!!!!!!!!!\n");
if (fread(&c, 1, 1, fp) != 1) {
printf("Error reading file\n");
goto clean;
}
if (c == '1') {
crash[0] = 1;
}
else if (c == '2') {
char buffer[5] = { 0 };
strcat(buffer, argv[0]);
}
else if (c == '3') {
printf("triggering a hang\n");
Sleep(50 * 1000);
}
else if (c == '4') {
try {
throw int(1337);
}
catch (...) {
printf("Caught its ok!\n");
}
}
else if (c == '5') {
OutputDebugString(TEXT("hello!"));
}
else if (c == '6') {
printf("allocating 120MB\n");
char *buffer = (char*)malloc((1024 * 1024) * 120);
*buffer = 0;
free(buffer);
}
else {
printf("Error 5\n");
}
clean:
fclose(fp);
end:
return EXIT_SUCCESS;
}
#pragma optimize("", off)
int fuzz(int argc, char**argv) {
while(__afl_persistent_loop()) {
test(argc, argv);
}
return 1;
}
#pragma optimize("", on)
int main(int argc, char** argv)
{
if(argc < 2) {
printf("Usage: %s <input file>\n", argv[0]);
return 0;
}
return fuzz(argc, argv);
}