-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathbasic_fuzzer.cc
More file actions
executable file
·180 lines (147 loc) · 4.22 KB
/
basic_fuzzer.cc
File metadata and controls
executable file
·180 lines (147 loc) · 4.22 KB
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
/*
* Fuzzing test code for libhttpserver using LLVM's libFuzzer
* (http://llvm.org/docs/LibFuzzer.html)
* Refer README.md for build instructions
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <sstream>
#include <httpserver.hpp>
#define HOST_IP "127.0.0.1"
unsigned int get_port_no(void);
using namespace httpserver;
unsigned int port;
webserver ws = create_webserver(get_port_no());
class fuzz_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
std::stringstream ss;
req.get_args();
req.get_headers();
req.get_footers();
req.get_cookies();
req.get_querystring();
req.get_user();
req.get_pass();
req.get_digested_user();
req.get_requestor();
req.get_requestor_port();
for (unsigned int i = 0; i < req.get_path_pieces().size(); i++)
ss << req.get_path_piece(i) << ",";
return std::shared_ptr<http_response>(new string_response(ss.str(), 200));
}
}hwr;
class args_resource: public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("ARGS: " +
req.get_arg("arg1") + "and" + req.get_arg("arg2")));
}
}agr;
unsigned int get_port_no(void) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
socklen_t len = sizeof(address);
memset(&address, 0 ,sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(HOST_IP);
address.sin_port = 0; //Use the next free port
bind(fd, (struct sockaddr*) &address, sizeof(address));
getsockname(fd, (struct sockaddr*) &address, &len);
port = ntohs(address.sin_port);
printf("Using port %d\n", port);
close(fd);
return port;
}
void quit(const char *msg) {
perror(msg);
exit(-1);
}
int connect_server(void) {
struct sockaddr_in address;
int sfd, ret;
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
quit("Failed to open socket");
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = inet_addr(HOST_IP);
retry:
ret = connect(sfd, (struct sockaddr *)&address, sizeof(address));
if (ret < 0) {
if (errno == EINTR)
goto retry;
quit("Failed to connect to server");
}
return sfd;
}
void write_request(int sfd, const uint8_t *data, size_t size) {
std::string method = "PUT ";
std::string suffix = " HTTP/1.1\r\n\r\n";
std::string str(reinterpret_cast<const char *>(data), size);
std::string fstr = method + str + suffix;
const char *msg;
int bytes, sent = 0;
size = fstr.length();
msg = fstr.c_str();
do {
bytes = write(sfd, msg + sent, size - sent);
if (bytes == 0)
break;
else if (bytes < 0) {
if (errno == EINTR)
continue;
quit("Failed to write HTTP request");
}
sent += bytes;
} while (sent < size);
}
void read_response(int sfd) {
char response[150];
int bytes;
bytes = read(sfd, response , 150);
if (bytes < 0)
return;
#if PRINT_RESPONSE
printf("%s\n", response);
#endif
}
void cleanup(void)
{
/* Stop the server */
ws.stop();
}
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
{
ws.register_resource("{arg1|[A-Z]+}/{arg2|(.*)}", &agr);
ws.register_resource(R"(.*)", &hwr);
/* Start the server */
ws.start(false);
atexit(cleanup);
return 0;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
int sfd;
if (memchr(data, '\n', size))
return 0;
if (memchr(data, '\r', size))
return 0;
/* Client -> connect to server */
sfd = connect_server();
/* HTTP request and response*/
write_request(sfd, data, size);
read_response(sfd);
/* Client -> close connection */
close(sfd);
return 0;
}