// Run like this: simple_client address port // Results in argv ["./simple_client", "address", "port"] #include #include #include #include #include #include #include #include #include #include #include #define bufsize 4096 void handle_sigpipe(int signal){ printf("Sigpipe received, continuing\n"); } int main(int argc, char ** argv){ struct sigaction pipe_handler; pipe_handler.sa_handler = handle_sigpipe; sigaction(SIGPIPE, &pipe_handler, 0); if(argc < 3){ printf("Usage: %s hostname port [-c] \n", argv[0]); printf("Setting -c will send a valid but strange character followed by fuzz\n"); return 1; } char send_character = 0; if(argc == 4){ for(int i = 1; i < argc; i++){ if(!strcmp(argv[i], "-c")) send_character = 1; } } struct sockaddr_in sad; sad.sin_port = htons(atoi(argv[2])); sad.sin_family = AF_INET; // do a dns lookup struct hostent* entry = gethostbyname(argv[1]); if(!entry){ if(h_errno == HOST_NOT_FOUND){ printf("This is our own message that says the host wasn't found\n"); } herror("gethostbyname"); return 1; } struct in_addr **addr_list = (struct in_addr**)entry->h_addr_list; // -> is like (*entry).h_addr_list struct in_addr* c_addr = addr_list[0]; char* ip_string = inet_ntoa(*c_addr); sad.sin_addr = *c_addr; // copy the address we found into sad // Finally done with DNS! printf("Connecting to: %s\n", ip_string); // seed first srandom(time(0)); size_t writelen; int random_numbers[bufsize / sizeof(int)]; while(1){ int skt = socket(AF_INET, SOCK_STREAM, 0); if( connect(skt, (struct sockaddr*)&sad, sizeof(struct sockaddr_in)) ){ perror("connect"); return 1; } if(send_character){ printf("Sending a valid but strange character followed by fuzz\n"); uint8_t character_buffer[48]; for(int i = 0; i < 48; i++) character_buffer[i] = random() % 255; character_buffer[0] = 10; memset(character_buffer + 34, 0, 6); write(skt, character_buffer, 48); uint16_t description_length; memcpy(&description_length, character_buffer + 46, 2); uint8_t random_byte; for(int i = 0; i < description_length; i++){ random_byte = random() % 255; write(skt, &random_byte, 1); } random_byte = 6; write(skt, &random_byte, 1); } else printf("Sending Fuzz\n"); do { for(int i = 0; i < bufsize / sizeof(int); i++) random_numbers[i] = random(); writelen = 1 + random() % (bufsize - 1); } while (write(skt, random_numbers, writelen) == writelen); close(skt); } return 0; }