-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathafl-dff.c
More file actions
200 lines (155 loc) · 5.43 KB
/
afl-dff.c
File metadata and controls
200 lines (155 loc) · 5.43 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <glib.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "afl-dff.h"
#include "dev/networking/afldff_networking.h"
#include "dev/networking/afldff_access.h"
#include "dev/interface/afldff_ncurses.h"
#include "dev/patch/afldff_patch.h"
//startup flags
typedef struct command_args{
char * ip;
char * port;
char * path_afl_tar;
}command_args;
//command line arguments
command_args flags;
//Data structure that contains our machine ids and their data.
//
// |------| |------|
// | name |-> | name | -> NULL
// |------| |------|
// | |
// V V
// |------| NULL
// | ID |
// |------|
//
GSList * GLOBAL_JOB_MATRIX = NULL;
//mutex for accessing linked list
pthread_mutex_t ll_mutex;
GQueue * NEW_NODE_QUEUE = NULL;
//mutex for accessing queue
pthread_mutex_t q_mutex;
/********************************************************************
* Start our udp listener (starts up listener in seperate thread) *
********************************************************************/
void * start_server(void *ptr){
int sfd_server = get_udp_server(flags.ip, flags.port);
if(sfd_server == -1){
fprintf(stderr, "Failed to start server!\n");
exit(EXIT_FAILURE);
}
while(1){
packet_info * pi = get_packet_info(sfd_server);
pthread_mutex_lock(&ll_mutex);
//Look for a job node that has the hash found in the incoming packet
GSList * job_node_list_pointer = GLOBAL_JOB_MATRIX;
for(; job_node_list_pointer; job_node_list_pointer=job_node_list_pointer->next){
job_node * data = job_node_list_pointer->data;
if(memcmp( data->hash, pi->p->hash, 16)== 0){
break;
}
}
//If the hash is not found create a new job node and add it to the
//job matrix.
if(job_node_list_pointer == NULL){
job_node * data = calloc(1, sizeof(struct job_node));
memcpy(data->hash, pi->p->hash, 16);
GLOBAL_JOB_MATRIX = g_slist_append(GLOBAL_JOB_MATRIX, data);
job_node_list_pointer = g_slist_last(GLOBAL_JOB_MATRIX);
}
//Try and find matching ID in job
job_node * job = job_node_list_pointer->data;
GSList * pilp = job->packet_info_list;
for(; pilp; pilp=pilp->next){
packet_info * data = pilp->data;
if(data->p->instance_id == pi->p->instance_id){
free_packet_info(pilp->data);
pilp->data = pi;
break;
}
}
//if the id is not in our linked list append it.
if(pilp == NULL){
job->packet_info_list = g_slist_append(job->packet_info_list, pi);
pthread_mutex_lock(&q_mutex);
g_queue_push_head(NEW_NODE_QUEUE, pi);
pthread_mutex_unlock(&q_mutex);
}
pthread_mutex_unlock(&ll_mutex);
//struct sockaddr_in * sockin = (struct sockaddr_in *) &pi->src_addr;
//printf("sockaddr: %s\n", inet_ntoa(sockin->sin_addr));
}
}
//start server thread
static void start_server_thread(pthread_t * thread){
int ret = pthread_create(thread, NULL, start_server, NULL);
if(ret){
fprintf(stderr, "Could not start server thread!");
exit(EXIT_FAILURE);
}
}
static void print_help(){
puts("afldff [ options ]");
puts(" -i ip - IP address to listen on defaults to 0.0.0.0 if left blank");
puts(" -p port - Port to listen on");
puts(" -m tar - Patch afl to be network compatable");
puts(" -h - Print help screen");
}
int main(int argc, char * argv[]){
pthread_t udp_listener;
//Read our arguments from standard in and set the appropriate flags to
//use in the command_args structure
int opt;
//This should be initialized to zero but we will set it again anyway
memset(&flags, 0, sizeof(struct command_args));
while((opt = getopt(argc, argv, "m:i:p:h")) != -1){
switch(opt){
case 'i':
flags.ip = optarg;
break;
case 'p':
flags.port = optarg;
break;
case 'm':
flags.path_afl_tar = optarg;
break;
case 'h':
print_help();
exit(0);
break;
default:
print_help();
exit(EXIT_FAILURE);
}
}
if(flags.path_afl_tar != NULL){
patch_afl(flags.path_afl_tar, 1);
exit(0);
}
if( flags.port == NULL ){
print_help();
exit(EXIT_FAILURE);
}
//Create queue mutex and initialize queue data structure
NEW_NODE_QUEUE = g_queue_new();
if(pthread_mutex_init(&q_mutex, NULL)!=0){
fprintf(stderr,"Failed to create mutex\n");
exit(EXIT_FAILURE);
}
//Start up the udp listener and initialize mutex
if(pthread_mutex_init(&ll_mutex, NULL)!=0){
fprintf(stderr,"Failed to create mutex\n");
exit(EXIT_FAILURE);
}
start_server_thread(&udp_listener);
draw_afldff_interface();
}