- 论坛徽章:
- 0
|
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#define MAXLEN 1024
struct Etheader
{
unsigned char destaddr[6];
unsigned char sourceaddr[6];
unsigned short type;
};
struct IpHeader
{
unsigned char head;
unsigned char tos;
unsigned short total_len;
unsigned short flags;
unsigned short offset;
unsigned char ttl;
unsigned char protoc;
unsigned short checksum;
unsigned int sourceip;
unsigned int destip;
};
struct UdpHeader
{
unsigned short sourceport;
unsigned short destport;
unsigned short len;
unsigned short checksum;
};
struct pHeader
{
unsigned int sourceip;
unsigned int destip;
unsigned char zero;
unsigned char protoc;
unsigned short len;
};
struct TcpHeader
{
unsigned short sourceport;
unsigned short destport;
unsigned int seq;
unsigned int ack;
unsigned char head;
unsigned char flags;
unsigned short windows;
unsigned short checksum;
unsigned short urg;
};
#pragma pack(1)
struct arp_packet
{
unsigned short hwtype;
unsigned short protype;
unsigned char hwlen;
unsigned char prolen;
unsigned short op;
unsigned char sendhwaddr[6];
unsigned int sendipaddr;
unsigned char recvhwaddr[6];
unsigned int recvipaddr;
};
#pragma pack()
struct IcmpHeader
{
unsigned char type;
unsigned char code;
unsigned short checksum;
union
{
struct
{ unsigned short id;
unsigned short seq;
}echo;
unsigned int gateway;
struct
{
unsigned short unused;
unsigned short mtu;
}frag;
}un;
};
int pingflags=0;
int conflags=0;
int synflags=0;
int finflags=0;
int udpflags=0;
unsigned short _checksum(unsigned short *ptr,int nbytes) //校验和
{
unsigned int sum=0;
unsigned short checksum;
unsigned short oddbyte;
while(nbytes>1)
{
sum+=*ptr;
ptr++;
nbytes-=2;
}
if(nbytes==1)
{
oddbyte=0;
*((char*)&oddbyte)=*(char*)ptr;
sum+=oddbyte;
}
sum=(sum>>16)+(sum&0xffff);
sum+=(sum>>16);// safe
checksum=~sum;
return (checksum);
}
int ping(char *targetip)//ping程序
{
int ret;
char*source;
char*dest;
int pingsock;
int sendbufsize;
struct sockaddr_in pingtaraddr,fromaddr;
char *sendbuf;
char recvbuf[MAXLEN];
char c[]="192.168.64.102";
struct IpHeader *ip;
struct IpHeader *recvip;
struct IcmpHeader *icmp;
struct IcmpHeader *recvicmp;
struct timeval tstart,tend;
int flags=-1;
float timeuse=0;
pingsock=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(pingsock<0)
{
printf("pingsocket error\n" ;
exit(1);
}
int on=1;
ret=setsockopt(pingsock,0,IP_HDRINCL,&on,sizeof(on));
if(ret<0)
{
printf("setsockopt IP_HDRINCL error\n" ;
exit(1);
}
sendbufsize=sizeof(struct IpHeader)+sizeof(struct IcmpHeader);
sendbuf=(char*)malloc(sendbufsize);
memset(sendbuf,0,sizeof(sendbuf));
//pad IP header ;hostbyte to netbyte
ip=(struct IpHeader*)sendbuf;
ip->head=0;
ip->head=(ip->head|0x45);
ip->tos=0;
ip->total_len=htons(sizeof(sendbuf));
ip->flags=rand();
ip->offset=0x40;
ip->ttl=64;
ip->protoc=1;//ICMP
ip->checksum=0;
ip->sourceip=inet_addr("192.168.64.102" ;//to banery;
ip->destip=inet_addr(targetip);
//pad ICMP header
icmp=(struct IcmpHeader*)(ip+1);
icmp->type=8;//ICMP_ECHO;
icmp->code=0;
icmp->un.echo.id=htons(10086);
icmp->un.echo.seq=0;
icmp->checksum=_checksum((unsigned short *)icmp,sizeof(struct IcmpHeader));
memset(&pingtaraddr,0,sizeof(pingtaraddr));
pingtaraddr.sin_family=AF_INET;
pingtaraddr.sin_addr.s_addr=inet_aton(targetip);
// sizeof(sendbuf) is pointer size,so the NIC not identified this packet and then sendto error(invalied argument)
// ret=sendto(pingsock,sendbuf,sizeof(sendbuf),0,(struct sockaddr*)&pingtaraddr,sizeof(pingtaraddr));
ret=sendto(pingsock,sendbuf,sendbufsize,0,(struct sockaddr*)&pingtaraddr,sizeof(pingtaraddr));
if(ret<0){
printf("send to error\n" ;
fprintf(stderr,"%s\n",strerror(errno));
}
if(fcntl(pingsock,F_SETFL,O_NONBLOCK)<0)
{
printf("set fcntl error\n" ;
}
unsigned int addrlen=sizeof(struct sockaddr_in);
gettimeofday(&tstart,NULL);
do
{
ret=recvfrom(pingsock,recvbuf,1024,0,(struct sockaddr*)&fromaddr,(socklen_t *)&addrlen);
if(ret>0)
{
recvip=(struct IpHeader*)recvbuf;
recvicmp=(struct IcmpHeader*)(recvbuf+((recvip->head)&0x0f)*4);
// printf("%d\n",(char*)recvicmp-(char*)recvip);
printf("from %s\n",inet_ntoa(recvip->sourceip));
printf("to %s\n",inet_ntoa(recvip->destip));
printf("%d\n",recvip->protoc);
source=inet_ntoa(recvip->sourceip);
dest=inet_ntoa(recvip->destip);
if(strcmp(inet_ntoa(recvip->sourceip),targetip)==0)
{
if(strcmp(inet_ntoa(recvip->destip),c)==0)
{
printf("&&&&&&\n" ;
return 1;
printf("%c\n",recvicmp->code);
if(recvicmp->type=='0')
{
printf("ping su\n" ;
flags=1;
break;
}
else printf("!!!!\n" ;
}
}
}
gettimeofday(&tend,NULL);
timeuse=1000000*(tend.tv_sec-tstart.tv_sec)+(tend.tv_usec-tstart.tv_usec);
timeuse=timeuse/1000000.0;
if(timeuse<3)continue;
else
{
printf("time out\n" ;
flags=0;
break;
}
}while(1);
return 0;
}
int TCPsynscan(char *targetip,unsigned short localport,unsigned short hostport)//构造SYN包
{
struct TcpHeader *tcp;
struct pHeader *ptcp;
struct sockaddr_in scanaddr,myaddr;
char * sendbuf;
unsigned int addrlen;
int synsock,ret,sendbufsize;
char c[]="192.168.64.101";
synsock=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
if(synsock<0)
{
printf("socket error\n" ;
exit(1);
}
if(ret<0)
{
printf("bind error\n");
exit(1);
}
sendbufsize=sizeof(struct TcpHeader)+sizeof(struct pHeader);
sendbuf=(char*)malloc(sendbufsize);
ptcp=(struct pHeader*)sendbuf;
tcp=(struct TcpHeader*)(sendbuf+sizeof(struct pHeader));
//pad ptcp header
ptcp->sourceip=inet_addr("192.168.64.101");
ptcp->destip=inet_addr(targetip);
ptcp->zero=0;
ptcp->protoc=0x06;
ptcp->len=sizeof(struct TcpHeader);
//pad tcp header
memset(tcp,0,sizeof(struct TcpHeader));
tcp->sourceport=htons(localport);
tcp->destport=htons(hostport);
tcp->seq=htonl(1234567 ;
tcp->ack=0;
tcp->head=0x50;
tcp->flags=0x02;
tcp->windows=htons(65535);
tcp->checksum=0;
tcp->urg=0;
tcp->checksum=_checksum((unsigned short*)ptcp,sizeof(struct pHeader)+sizeof(struct TcpHeader));
scanaddr.sin_family=AF_INET;
scanaddr.sin_port=htons(hostport);
scanaddr.sin_addr.s_addr=inet_addr(targetip);
//sendto tcp header;
//ptcp header not send;
sendto(synsock,tcp,sizeof(struct TcpHeader),0,(struct sockaddr*)&scanaddr,sizeof(scanaddr));
if(ret<0)
{
printf("send to error\n");
fprintf(stderr,"%s\n",strerror(errno));
}
struct IpHeader *recvip;
struct TcpHeader *recvtcp;
struct sockaddr_in fromaddr;
char recvbuf[1024];
struct timeval tstart,tend;
float timeuse;
//unsigned int addrlen;
addrlen=sizeof(struct sockaddr_in);
gettimeofday(&tstart,NULL);
if(fcntl(synsock,F_SETFL,O_NONBLOCK)<0)
{
printf("set fcntl error\n");
}
do
{
ret=recvfrom(synsock,recvbuf,1024,0,(struct sockaddr*)&fromaddr,(socklen_t *)&addrlen);
if(ret>0)
{
recvip=(struct IpHeader*)recvbuf;
recvtcp=(struct TcpHeader*)(recvbuf+((recvip->head)&0x0f)*4);
printf("%d\n",(char*)recvtcp-(char*)recvip);
printf("from %s\n",inet_ntoa(recvip->sourceip));
printf("to %s\n",inet_ntoa(recvip->destip));
printf("%d\n",recvip->protoc);
if(strcmp(inet_ntoa(recvip->sourceip),targetip)==0)
{
if(strcmp(inet_ntoa(recvip->destip),c)==0)
{
if(recvtcp->flags==0x12)//SYN|ACK
{
printf("this port is opened!\n");
return 1;
}
else if(recvtcp->flags==0x14)//RST|ACK
{
printf("this port is closed!\n");
return 0;
}
else printf("iptables!\n");
}
}
else printf("######\n");
}
else printf("not recv\n");
gettimeofday(&tend,NULL);
timeuse=1000000*(tend.tv_sec-tstart.tv_sec)+(tend.tv_usec-tstart.tv_usec);
timeuse=timeuse/1000000.0;
if(timeuse<0.0002)continue;
else
{
printf("time out\n");
break;
}
}while(1);
return -1;
}
int main(int argc,char *argv[])
{
char sendbuff[MAXLEN];
char recvbuff[MAXLEN];
char targetip="";
int ret=0;
char message[]=
"-p Ping Scan\n-c TCP connect Scan\n-s TCP SYN Scan\n-f TCP FIN Scan\n-u UDP Scan\n";
int ch;
unsigned short localport,hostport;
if(argc==1)
{
printf("%s\n",message);
}
else
{
while((ch=getopt(argc,argv,"hp:c:s:f:u:"))!=-1)
{
switch(ch)
{
case 'h':printf("%s\n",message);
break;
case 'p':pingflags=1;
break;
case 'c':pingflags=1;
conflags=1;
break;
case 's':pingflags=1;
synflags=1;
localport=(unsigned short)atoi(argv[3]);
hostport=(unsigned short)atoi(argv[4]);
break;
case 'f':pingflags=1;
finflags=1;
break;
case 'u':pingflags=1;
udpflags=1;
break;
}
}
}
if(pingflags==1)
{
ret=ping(argv[2]);
}
if(synflags==1)
{
printf("syn\n");
if(ret==1)
{
ret=TCPsynscan(argv[2],localport,hostport);
}
else printf("host not exist\n");
}
return 0;
}
这是我的代码,运行时候先ping对方,对方ping同了才开始发送SYN
运行./XXX -s 192.168.64.100 2000 5555
2000为自己指定本机端口,5555对方端口
先谢谢了,弄了好久不明白
回复 3# timespace
|
|