Crazedpsyc
July 7th, 2010, 08:33 PM
I have been trying to run the sniffer projects found in Hackin9 magazine, but Listing two (Simle sniffer) and listing three (arp sniffer) don't work
here's my typed up versions of each:
Listing 2:
/* simple sniffer */
#include <pcap.h>
#include <string.h>
#include <stdlib.h>
#define MAXBYTES2CAPTURE 2048
void processPacket(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char *packet){
int i=0, *counter = (int *)arg;
printf("Packet Count: %d\n", ++(*counter));
printf("Received Packet Size: %d\n", pkthdr->len);
printf("Payload:\n");
for (i=0; i<pkthdr->len; i++){
if ( isprint(packet[i]) )
printf("%c ", packet[i]);
else
printf(". ");
if( (i%16 == 0 && i!=0) || i==pkthdr->len-1 )
printf("\n");
}
return;
}
int main( ){
int i=0, count=0;
pcap_t *descr = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device=NULL;
memset(errbuf,0,PCAP_ERRBUF_SIZE);
device = pcap_lookupdev(errbuf);
printf("Opening device %s\n", device);
descr = pcap_open_live(device, MAXBYTES2CAPTURE, 1, 512, errbuf);
pcap_loop(descr, -1, processPacket, (u_char *)&count);
return 0;
}
listing 3:
/* Simple arp sniffer */
/* to compile: gcc arpsniffer.c -o arpsniff -lpcap */
/* run as root! */
#include <pcap.h>
#include <stdlib.h>
#include <string.h>
/* ARP header, (assuming Ethernet+IPv4) */
#define ARP_REQUEST 1 /* request */
#define ARP_REPLY 2 /* reply */
typedef struct arphdr {
u_init16_t htype;
u_init16_t ptype;
u_char hlen;
u_char plen;
u_init16_t oper;
u_char sha[6];
u_char spa[4];
u_char tha[6];
u_char tpa[4];
}arphdr_t;
#define MAXBYTES2CAPTURE 2048
int main(int argc, char *argv[]) {
int i=0;
bpf_u_int32 netaddr=0, mask=0;
struct bpf_program filter;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *descr = NULL;
struct pcap_pkthdr pkthdr;
const unsigned char *packet=NULL;
arphdr_t *arpheader = NULL;
memset(errbuf,0,PCAP_ERRBUF_SIZE);
if (argc != 2) {
printf("USAGE: arpsniffer <interface>\n");
exit(1);
}
descr = pcap_open_live(argv[1], MAXBYTES2CAPTURE, 0, 512, errbuf);
pcap_lookupnet( argv[1] , &netaddr, &mask, errbuf);
pcap_compile(descr, &filter, "arp", 1, mask);
pcap_setfilter(descr,&filter);
while(1){
packet = pcap_next(descr,&pkthdr);
arpheader = (struct arphdr *)(packet+14);
printf("\n\nRecieved Packet Size: %d bytes\n", pkthdr.len);
printf("Hardware type: %s\n", (ntohs(arpheader->htype) == 1) ? "Ethernet" : "Unknown");
printf("Protocol type: %s\n", (ntohs(arpheader->ptype) == 0x0800) ? "IPv4" : "Unknown");
printf("Operation: %s\n", (ntohs(arpheader->oper) == ARP_REQUEST) ? "ARP_REQUEST" : "ARP_REPLY");
if (ntohs(arpheader->htype) == 1 && ntohs(arpheader->ptype) == 0x0800) {
printf("Sender MAC: ");
for(i=0; i<6;i++)printf("%02X:", arpheader->sha[i]);
printf("\nSender IP: ");
for(i=0; i<4;i++)printf("%d.", arpheader->spa[i]);
printf("\nTarget MAC: ");
for(i=0; i<6;i++)printf("%02X:", arpheader->tha[i]);
printf("\nTarget IP: ");
for(i=0; i<4;i++)printf("%d.", arpheader->tpa[1]);
printf("\n");
}
}
return 0;
}
listing 2 compiles perfectly with gcc <...> -lpcap
but then when I run it, it says
Opening device (null)
Segmentation fault
and exits.
listing 3 says the following when I try to compile it:
arpsniffer.c:13: error: expected specifier-qualifier-list before ‘u_init16_t’
arpsniffer.c: In function ‘main’:
arpsniffer.c:65: error: ‘arphdr_t’ has no member named ‘htype’
arpsniffer.c:67: error: ‘arphdr_t’ has no member named ‘ptype’
arpsniffer.c:69: error: ‘arphdr_t’ has no member named ‘oper’
arpsniffer.c:71: error: ‘arphdr_t’ has no member named ‘htype’
arpsniffer.c:71: error: ‘arphdr_t’ has no member named ‘ptype’
arpsniffer.c:73: error: ‘arphdr_t’ has no member named ‘sha’
arpsniffer.c:75: error: ‘arphdr_t’ has no member named ‘spa’
arpsniffer.c:77: error: ‘arphdr_t’ has no member named ‘tha’
arpsniffer.c:79: error: ‘arphdr_t’ has no member named ‘tpa’
thanks in advance!
here's my typed up versions of each:
Listing 2:
/* simple sniffer */
#include <pcap.h>
#include <string.h>
#include <stdlib.h>
#define MAXBYTES2CAPTURE 2048
void processPacket(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char *packet){
int i=0, *counter = (int *)arg;
printf("Packet Count: %d\n", ++(*counter));
printf("Received Packet Size: %d\n", pkthdr->len);
printf("Payload:\n");
for (i=0; i<pkthdr->len; i++){
if ( isprint(packet[i]) )
printf("%c ", packet[i]);
else
printf(". ");
if( (i%16 == 0 && i!=0) || i==pkthdr->len-1 )
printf("\n");
}
return;
}
int main( ){
int i=0, count=0;
pcap_t *descr = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device=NULL;
memset(errbuf,0,PCAP_ERRBUF_SIZE);
device = pcap_lookupdev(errbuf);
printf("Opening device %s\n", device);
descr = pcap_open_live(device, MAXBYTES2CAPTURE, 1, 512, errbuf);
pcap_loop(descr, -1, processPacket, (u_char *)&count);
return 0;
}
listing 3:
/* Simple arp sniffer */
/* to compile: gcc arpsniffer.c -o arpsniff -lpcap */
/* run as root! */
#include <pcap.h>
#include <stdlib.h>
#include <string.h>
/* ARP header, (assuming Ethernet+IPv4) */
#define ARP_REQUEST 1 /* request */
#define ARP_REPLY 2 /* reply */
typedef struct arphdr {
u_init16_t htype;
u_init16_t ptype;
u_char hlen;
u_char plen;
u_init16_t oper;
u_char sha[6];
u_char spa[4];
u_char tha[6];
u_char tpa[4];
}arphdr_t;
#define MAXBYTES2CAPTURE 2048
int main(int argc, char *argv[]) {
int i=0;
bpf_u_int32 netaddr=0, mask=0;
struct bpf_program filter;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *descr = NULL;
struct pcap_pkthdr pkthdr;
const unsigned char *packet=NULL;
arphdr_t *arpheader = NULL;
memset(errbuf,0,PCAP_ERRBUF_SIZE);
if (argc != 2) {
printf("USAGE: arpsniffer <interface>\n");
exit(1);
}
descr = pcap_open_live(argv[1], MAXBYTES2CAPTURE, 0, 512, errbuf);
pcap_lookupnet( argv[1] , &netaddr, &mask, errbuf);
pcap_compile(descr, &filter, "arp", 1, mask);
pcap_setfilter(descr,&filter);
while(1){
packet = pcap_next(descr,&pkthdr);
arpheader = (struct arphdr *)(packet+14);
printf("\n\nRecieved Packet Size: %d bytes\n", pkthdr.len);
printf("Hardware type: %s\n", (ntohs(arpheader->htype) == 1) ? "Ethernet" : "Unknown");
printf("Protocol type: %s\n", (ntohs(arpheader->ptype) == 0x0800) ? "IPv4" : "Unknown");
printf("Operation: %s\n", (ntohs(arpheader->oper) == ARP_REQUEST) ? "ARP_REQUEST" : "ARP_REPLY");
if (ntohs(arpheader->htype) == 1 && ntohs(arpheader->ptype) == 0x0800) {
printf("Sender MAC: ");
for(i=0; i<6;i++)printf("%02X:", arpheader->sha[i]);
printf("\nSender IP: ");
for(i=0; i<4;i++)printf("%d.", arpheader->spa[i]);
printf("\nTarget MAC: ");
for(i=0; i<6;i++)printf("%02X:", arpheader->tha[i]);
printf("\nTarget IP: ");
for(i=0; i<4;i++)printf("%d.", arpheader->tpa[1]);
printf("\n");
}
}
return 0;
}
listing 2 compiles perfectly with gcc <...> -lpcap
but then when I run it, it says
Opening device (null)
Segmentation fault
and exits.
listing 3 says the following when I try to compile it:
arpsniffer.c:13: error: expected specifier-qualifier-list before ‘u_init16_t’
arpsniffer.c: In function ‘main’:
arpsniffer.c:65: error: ‘arphdr_t’ has no member named ‘htype’
arpsniffer.c:67: error: ‘arphdr_t’ has no member named ‘ptype’
arpsniffer.c:69: error: ‘arphdr_t’ has no member named ‘oper’
arpsniffer.c:71: error: ‘arphdr_t’ has no member named ‘htype’
arpsniffer.c:71: error: ‘arphdr_t’ has no member named ‘ptype’
arpsniffer.c:73: error: ‘arphdr_t’ has no member named ‘sha’
arpsniffer.c:75: error: ‘arphdr_t’ has no member named ‘spa’
arpsniffer.c:77: error: ‘arphdr_t’ has no member named ‘tha’
arpsniffer.c:79: error: ‘arphdr_t’ has no member named ‘tpa’
thanks in advance!