Hi all,
I wrote a very simple TCP server and want to fuzz it with AFL using approach described here: https://www.fastly.com/blog/how-fuzz-server-american-fuzzy-lop
What's wrong with the code below? What am I doing wrong? How can I correct it?
It hangs with:
SC_AFL_CMIN=1 /home/user/afl-2.01b/afl-cmin -i testcases/ -o testcases-cmin -- ./simple
corpus minimization tool for afl-fuzz by <lca...@google.com>
[*] Testing the target binary...
Also doesn't run/fuzz with:
ALF_PERSISTENT=1 /home/user/afl-2.01b/afl-fuzz -i testcases-cmin/ -o findings ./simple
afl-fuzz 2.01b by <lca...@google.com>
[+] You have 4 CPU cores and 2 runnable tasks (utilization: 50%).
[+] Try parallel jobs - see docs/parallel_fuzzing.txt.
[*] Checking core_pattern...
[*] Checking CPU scaling governor...
[*] Setting up output directories...
[+] Output directory exists but deemed OK to reuse.
[*] Deleting old session data...
[+] Output dir cleanup successful.
[*] Scanning 'testcases-cmin/'...
[+] No auto-generated dictionary tokens to reuse.
[*] Creating hard links for all input files...
[*] Validating target binary...
[*] Attempting dry run with 'id:000000,orig:in.txt'...
[*] Spinning up the fork server...
[+] All right - fork server is up.
[-] The program took more than 1000 ms to process one of the initial test cases.
This is bad news; raising the limit with the -t option is possible, but
will probably make the fuzzing process extremely slow.
If this test case is just a fluke, the other option is to just avoid it
altogether, and find one that is less of a CPU hog.
[-] PROGRAM ABORT : Test case 'id:000000,orig:in.txt' results in a hang
Location : perform_dry_run(), afl-fuzz.c:2599
Thanks,
-----------
simple.c
-----------
/*
C socket server example
*/
#include <stdio.h>
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#include <stdlib.h>
#include <signal.h>
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
// set SO_REUSEADDR on a socket to true (1):
int optval = 1;
setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 5000 , 0)) > 0 )
{
#ifdef SC_AFL_PERSISTENT_SHIM
size_t insize;
char buf[5120];
if (getenv("SC_AFL_STDIN") || getenv("SC_AFL_CMIN") || getenv("AFL_PERSISTENT")) {
memset(buf, 0, 5120);
insize = read(0, buf, 5120);
//Send the message back to client
write(client_sock , buf , strlen(buf));
}
#endif
write(client_sock , client_message , strlen(client_message));
if(read_size>10)
break;
#ifdef SC_AFL_PERSISTENT_SHIM /* For AFL persistent mode fuzzing shim */
/* Signal AFL to fuzz input and continue execution */
if (getenv("AFL_PERSISTENT")) {
raise(SIGSTOP);
} else if (getenv("SC_AFL_CMIN")) {
exit(0);
}
#endif
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
close(socket_desc);
shutdown(socket_desc,0);
return 0;
}
I suspect that Preeny-style approaches (i.e., LD_PRELOAD to simulate
network I/O from a file and to alter the behavior of fork() and the
likes) may offer a more complete solution. Unfortunately, Preeny isn't
particularly AFL-friendly as-is :-(
On Feb 23, 2016, at 1:54 PM, Yan <zar...@gmail.com> wrote:Ah yes, the poll() loop from that first issue is what I'm tackling right now. I made a decision early on in preeny's development to try to provide replacement sockets that are as realistic as possible, which forced me into using socketpairs and synchronizing them with background threads. This is a huge pain, and causes some unfortunate issues and race conditions.After I track this down, I'm thinking of writing a desock-lite module for preeny that overrides recv(), send(), and friends. But that one will have to override almost all socket functions (setsockopt, etc), which is why I was reluctant to go that route. All that being said, I think it'll end up being a lot less of a headache to use in the end. Just need to find some time to do it...
- YanOn Tue, Feb 23, 2016 at 11:15 AM, Michal Zalewski <lca...@gmail.com> wrote:> I'm the author of preeny (I know, hard to believe! ;-) ), and a colleague of mine forwarded this thread to me.
Yello :-)
Maybe "not friendly" is too strong of a word, but we had a bunch of
people running into hard-to-diagnose problems with Preeny, e.g.:
https://groups.google.com/forum/#!topic/afl-users/covb8S_bRNw
https://groups.google.com/forum/#!msg/afl-users/VRWt0Fs99rA/Qjd-b7cUBAAJ
Others have gotten better results, albeit still needed to make some changes:
https://lolware.net/2015/04/28/nginx-fuzzing.html
I've never tried to fully diagnose them, but looking at strace output
and the likes, I suspect the issues they bump into were on Preeny
side.
/mz
--
You received this message because you are subscribed to the Google Groups "afl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to afl-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "afl-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/afl-users/fpZIcdNBMNM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to afl-users+...@googlegroups.com.