forked from stateafl/stateafl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaflnet-replay.c
178 lines (137 loc) · 5.37 KB
/
aflnet-replay.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
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include "alloc-inl.h"
#include "aflnet.h"
#define server_wait_usecs 10000
unsigned int* (*extract_response_codes)(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref) = NULL;
/* Expected arguments:
1. Path to the test case (e.g., crash-triggering input)
2. Application protocol (e.g., RTSP, FTP)
3. Server's network port
Optional:
4. First response timeout (ms), default 1
5. Follow-up responses timeout (us), default 1000
*/
int main(int argc, char* argv[])
{
FILE *fp;
int portno, n;
struct sockaddr_in serv_addr;
char* buf = NULL, *response_buf = NULL;
int response_buf_size = 0;
unsigned int size, i, state_count, packet_count = 0;
unsigned int *state_sequence;
unsigned int socket_timeout = 1000;
unsigned int poll_timeout = 1;
if (argc < 4) {
PFATAL("Usage: ./aflnet-replay packet_file protocol port [first_resp_timeout(us) [follow-up_resp_timeout(ms)]]");
}
fp = fopen(argv[1],"rb");
if (!strcmp(argv[2], "RTSP")) extract_response_codes = &extract_response_codes_rtsp;
else if (!strcmp(argv[2], "FTP")) extract_response_codes = &extract_response_codes_ftp;
else if (!strcmp(argv[2], "DNS")) extract_response_codes = &extract_response_codes_dns;
else if (!strcmp(argv[2], "DTLS12")) extract_response_codes = &extract_response_codes_dtls12;
else if (!strcmp(argv[2], "DICOM")) extract_response_codes = &extract_response_codes_dicom;
else if (!strcmp(argv[2], "SMTP")) extract_response_codes = &extract_response_codes_smtp;
else if (!strcmp(argv[2], "SSH")) extract_response_codes = &extract_response_codes_ssh;
else if (!strcmp(argv[2], "TLS")) extract_response_codes = &extract_response_codes_tls;
else if (!strcmp(argv[2], "SIP")) extract_response_codes = &extract_response_codes_sip;
else if (!strcmp(argv[2], "HTTP")) extract_response_codes = &extract_response_codes_http;
else if (!strcmp(argv[2], "IPP")) extract_response_codes = &extract_response_codes_ipp;
else {fprintf(stderr, "[AFLNet-replay] Protocol %s has not been supported yet!\n", argv[2]); exit(1);}
portno = atoi(argv[3]);
if (argc > 4) {
poll_timeout = atoi(argv[4]);
if (argc > 5) {
socket_timeout = atoi(argv[5]);
}
}
//Wait for the server to initialize
usleep(server_wait_usecs);
if (response_buf) {
ck_free(response_buf);
response_buf = NULL;
response_buf_size = 0;
}
int sockfd;
if ((!strcmp(argv[2], "DTLS12")) || (!strcmp(argv[2], "SIP"))) {
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
} else {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
if (sockfd < 0) {
PFATAL("Cannot create a socket");
}
//Set timeout for socket data sending/receiving -- otherwise it causes a big delay
//if the server is still alive after processing all the requests
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = socket_timeout;
setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
#ifdef LOCAL_PORT
struct sockaddr_in local_serv_addr;
int optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
local_serv_addr.sin_family = AF_INET;
local_serv_addr.sin_addr.s_addr = INADDR_ANY;
local_serv_addr.sin_port = htons(LOCAL_PORT);
local_serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(sockfd, (struct sockaddr*) &local_serv_addr, sizeof(struct sockaddr_in))) {
FATAL("Unable to bind socket on local source port");
}
#endif
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
//If it cannot connect to the server under test
//try it again as the server initial startup time is varied
for (n=0; n < 1000; n++) {
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) break;
usleep(1000);
}
if (n== 1000) {
close(sockfd);
return 1;
}
}
//Send requests one by one
//And save all the server responses
while(!feof(fp)) {
#ifdef SEND_DELAY
usleep(SEND_DELAY); // send delay
#endif
if (buf) {ck_free(buf); buf = NULL;}
if (fread(&size, sizeof(unsigned int), 1, fp) > 0) {
packet_count++;
fprintf(stderr,"\nSize of the current packet %d is %d\n", packet_count, size);
buf = (char *)ck_alloc(size);
fread(buf, size, 1, fp);
if (net_recv(sockfd, timeout, poll_timeout, &response_buf, &response_buf_size)) break;
n = net_send(sockfd, timeout, buf,size);
if (n != size) break;
if (net_recv(sockfd, timeout, poll_timeout, &response_buf, &response_buf_size)) break;
}
}
fclose(fp);
close(sockfd);
//Extract response codes
state_sequence = (*extract_response_codes)(response_buf, response_buf_size, &state_count);
fprintf(stderr,"\n--------------------------------");
fprintf(stderr,"\nResponses from server:");
for (i = 0; i < state_count; i++) {
fprintf(stderr,"%d-",state_sequence[i]);
}
fprintf(stderr,"\n++++++++++++++++++++++++++++++++\nResponses in details:\n");
for (i=0; i < response_buf_size; i++) {
fprintf(stderr,"%c",response_buf[i]);
}
fprintf(stderr,"\n--------------------------------");
//Free memory
ck_free(state_sequence);
if (buf) ck_free(buf);
ck_free(response_buf);
return 0;
}