#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> int main(int argc, char **argv)
{
char *dev;
char *net;
char *mask;
int ret;
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 netp;
bpf_u_int32 maskp;
struct in_addr addr; dev = pcap_lookupdev(errbuf); if(dev == NULL)
{
printf("%s\n",errbuf);
exit();
} printf("DEV: %s\n",dev); ret = pcap_lookupnet(dev,&netp,&maskp,errbuf); if(ret == -)
{
printf("%s\n",errbuf);
exit();
} addr.s_addr = netp;
net = inet_ntoa(addr); if(net == NULL)
{
perror("inet_ntoa");
exit();
} printf("NET: %s\n",net); addr.s_addr = maskp;
mask = inet_ntoa(addr); if(mask == NULL)
{
perror("inet_ntoa");
exit();
} printf("MASK: %s\n",mask); return ;
}
然后gcc -o pcap_1 pcap_1.c -lpcap(一定要-lpcap参数)
编译ok~,执行./pcap_1,可以看到:
DEV: eth0
NET: 192.168.12.0
MASK: 255.255.255.0
好了,第一个pcap程序出炉了。。。。。 但是(当然有但是了,要不然我后面写啥),上面那个程序除了向我们展现pcap_lookupdev和pcap_lookupnet之外什么都没有干,好,我们接着来,动手编写我们的第一个抓包程序。 #include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> int main(int argc, char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr; u_char *ptr; dev = pcap_lookupdev(errbuf); if(dev == NULL)
{
printf("%s\n",errbuf);
exit();
} printf("DEV: %s\n",dev); descr = pcap_open_live(dev,BUFSIZ,,-,errbuf); if(descr == NULL)
{
printf("pcap_open_live(): %s\n",errbuf);
exit();
} packet = pcap_next(descr,&hdr); if(packet == NULL)
{
printf("Didn't grab packet\n");
exit();
} printf("Grabbed packet of length %d\n",hdr.len);
printf("Recieved at ..... %s\n",ctime((const time_t*)&hdr.ts.tv_sec));
printf("Ethernet address length is %d\n",ETHER_HDR_LEN); eptr = (struct ether_header *) packet; if (ntohs (eptr->ether_type) == ETHERTYPE_IP)
{
printf("Ethernet type hex:%x dec:%d is an IP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)
{
printf("Ethernet type hex:%x dec:%d is an ARP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else {
printf("Ethernet type %x not IP", ntohs(eptr->ether_type));
exit();
} ptr = eptr->ether_dhost;
i = ETHER_ADDR_LEN;
printf(" Destination Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>);
printf("\n"); ptr = eptr->ether_shost;
i = ETHER_ADDR_LEN;
printf(" Source Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>);
printf("\n"); return ;
} 好了,编译运行!
[root@norman libpcap]# ./pcap_2
DEV: eth0
Grabbed packet of length
Recieved at time..... Mon Mar :: Ethernet address length is
Ethernet type hex: dec: is an IP packet
Destination Address: :::d1:e8:
Source Address: :a0:cc::c2:
[root@pepe libpcap]# 可能有人等了半天都没有一个包过来,有个好办法,再开一个控制台,ping一下某个网站,比如google~~,呵呵
马上就有反应了~~ 这个程序是一个老外写的,大家看看注释应该没有问题吧~
但是大家也发现了一个问题,就是上面的程序只能捕捉一个包,要不停的捕捉包怎么办,用循环??libpcap提供了一个更好的方法:
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user);
这个函数能够不停的捕捉以太网的包,cnt就是捕捉的次数,callback是处理函数,这个处理函数怎么写,看看pcap_3.c就知道了。user参数是干什么的?不要问我,我也不知道。 #include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
packet)
{
static int count = ;
fprintf(stdout,"%d, ",count);
if(count == )
fprintf(stdout,"Come on baby sayyy you love me!!! ");
if(count == )
fprintf(stdout,"Tiiimmmeesss!! ");
fflush(stdout);
count++;
} int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr; if(argc != ){ fprintf(stdout,"Usage: %s numpackets\n",argv[]);return ;} dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{ printf("%s\n",errbuf); exit(); } descr = pcap_open_live(dev,BUFSIZ,,-,errbuf);
if(descr == NULL)
{ printf("pcap_open_live(): %s\n",errbuf); exit(); } pcap_loop(descr,atoi(argv[]),my_callback,NULL); fprintf(stdout,"\nDone processing packets... wheew!\n");
return ;
} 运行./pcap_3
, , , , Come on baby sayyy you love me!!! , , , Tiiimmmeesss!!
Done processing packets... wheew! pcap_loop确实很好用,但是如果没有包包过来,只有干等在那里,pcap_dispatch就含有一个超时的功能,下面是man里面的一段话:
pcap_dispatch() is used to collect and process packets. cnt specifies the maximum number of packets to process before returning. A cnt of - processes all the packets received in one buffer. A cnt of processes all packets until an error occurs, EOF is reached, or the read times out (when doing live reads and a non-zero read timeout is specified). callback specifies a routine to be called with three arguments: a u_char pointer which is passed in from pcap_dispatch(), a pointer to the pcap_pkthdr struct (which precede the actual network headers and data), and a u_char pointer to the packet data. The number of packets read is returned. Zero is returned when EOF is reached in a ``savefile.'' A return of - indicates an error in which case pcap_perror() or pcap_geterr() may be used to display the error text. 另外的问题是,我们可能对抓取的包包太多而很头痛,可能很多都不是我们感兴趣的包,别急,pcap_compile和pcap_setfilter能帮我们解决问题。 #include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
packet)
{
static int count = ;
fprintf(stdout,"%d, ",count);
fflush(stdout);
count++;
} int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp; if(argc != ){ fprintf(stdout,"Usage: %s \"filter program\"\n"
,argv[]);return ;} dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{ fprintf(stderr,"%s\n",errbuf); exit(); } pcap_lookupnet(dev,&netp,&maskp,errbuf); descr = pcap_open_live(dev,BUFSIZ,,-,errbuf);
if(descr == NULL)
{ printf("pcap_open_live(): %s\n",errbuf); exit(); } if(pcap_compile(descr,&fp,argv[],,netp) == -)
{ fprintf(stderr,"Error calling pcap_compile\n"); exit(); } if(pcap_setfilter(descr,&fp) == -)
{ fprintf(stderr,"Error setting filter\n"); exit(); } pcap_loop(descr,-,my_callback,NULL); return ;
} 运行./pcap_4.c "host www.google.com"
然后在另外一个控制台下面ping www.baidu.com
哈哈
没有反应吧
接着再ping www.google.com
就看到1, , , , , ,
ok
you got it!!
 #include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> int main(int argc, char **argv)
{
char *dev;
char *net;
char *mask;
int ret;
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 netp;
bpf_u_int32 maskp;
struct in_addr addr; dev = pcap_lookupdev(errbuf); if(dev == NULL)
{
printf("%s\n",errbuf);
exit();
} printf("DEV: %s\n",dev); ret = pcap_lookupnet(dev,&netp,&maskp,errbuf); if(ret == -)
{
printf("%s\n",errbuf);
exit();
} addr.s_addr = netp;
net = inet_ntoa(addr); if(net == NULL)
{
perror("inet_ntoa");
exit();
} printf("NET: %s\n",net); addr.s_addr = maskp;
mask = inet_ntoa(addr); if(mask == NULL)
{
perror("inet_ntoa");
exit();
} printf("MASK: %s\n",mask); return ;
}
然后gcc -o pcap_1 pcap_1.c -lpcap(一定要-lpcap参数)
编译ok~,执行./pcap_1,可以看到:
DEV: eth0
NET: 192.168.12.0
MASK: 255.255.255.0
好了,第一个pcap程序出炉了。。。。。 但是(当然有但是了,要不然我后面写啥),上面那个程序除了向我们展现pcap_lookupdev和pcap_lookupnet之外什么都没有干,好,我们接着来,动手编写我们的第一个抓包程序。 #include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> int main(int argc, char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr; u_char *ptr; dev = pcap_lookupdev(errbuf); if(dev == NULL)
{
printf("%s\n",errbuf);
exit();
} printf("DEV: %s\n",dev); descr = pcap_open_live(dev,BUFSIZ,1,0,errbuf); if(descr == NULL)
{
printf("pcap_open_live(): %s\n",errbuf);
exit();
} packet = pcap_next(descr,&hdr); if(packet == NULL)
{
printf("Didn't grab packet\n");
exit();
} printf("Grabbed packet of length %d\n",hdr.len);
printf("Recieved at ..... %s\n",ctime((const time_t*)&hdr.ts.tv_sec));
printf("Ethernet address length is %d\n",ETHER_HDR_LEN); eptr = (struct ether_header *) packet; if (ntohs (eptr->ether_type) == ETHERTYPE_IP)
{
printf("Ethernet type hex:%x dec:%d is an IP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)
{
printf("Ethernet type hex:%x dec:%d is an ARP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else {
printf("Ethernet type %x not IP", ntohs(eptr->ether_type));
exit();
} ptr = eptr->ether_dhost;
i = ETHER_ADDR_LEN;
printf(" Destination Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>);
printf("\n"); ptr = eptr->ether_shost;
i = ETHER_ADDR_LEN;
printf(" Source Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>);
printf("\n"); return ;
} 好了,编译运行!
[root@norman libpcap]# ./pcap_2
DEV: eth0
Grabbed packet of length
Recieved at time..... Mon Mar :: Ethernet address length is
Ethernet type hex: dec: is an IP packet
Destination Address: :::d1:e8:
Source Address: :a0:cc::c2:
[root@pepe libpcap]# 可能有人等了半天都没有一个包过来,有个好办法,再开一个控制台,ping一下某个网站,比如google~~,呵呵
马上就有反应了~~ 这个程序是一个老外写的,大家看看注释应该没有问题吧~
但是大家也发现了一个问题,就是上面的程序只能捕捉一个包,要不停的捕捉包怎么办,用循环??libpcap提供了一个更好的方法:
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user);
这个函数能够不停的捕捉以太网的包,cnt就是捕捉的次数,callback是处理函数,这个处理函数怎么写,看看pcap_3.c就知道了。user参数是干什么的?不要问我,我也不知道。 #include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
packet)
{
static int count = ;
fprintf(stdout,"%d, ",count);
if(count == )
fprintf(stdout,"Come on baby sayyy you love me!!! ");
if(count == )
fprintf(stdout,"Tiiimmmeesss!! ");
fflush(stdout);
count++;
} int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr; if(argc != ){ fprintf(stdout,"Usage: %s numpackets\n",argv[]);return ;} dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{ printf("%s\n",errbuf); exit(); } descr = pcap_open_live(dev,BUFSIZ,,-,errbuf);
if(descr == NULL)
{ printf("pcap_open_live(): %s\n",errbuf); exit(); } pcap_loop(descr,atoi(argv[]),my_callback,NULL); fprintf(stdout,"\nDone processing packets... wheew!\n");
return ;
} 运行./pcap_3
, , , , Come on baby sayyy you love me!!! , , , Tiiimmmeesss!!
Done processing packets... wheew! pcap_loop确实很好用,但是如果没有包包过来,只有干等在那里,pcap_dispatch就含有一个超时的功能,下面是man里面的一段话:
pcap_dispatch() is used to collect and process packets. cnt specifies the maximum number of packets to process before returning. A cnt of - processes all the packets received in one buffer. A cnt of processes all packets until an error occurs, EOF is reached, or the read times out (when doing live reads and a non-zero read timeout is specified). callback specifies a routine to be called with three arguments: a u_char pointer which is passed in from pcap_dispatch(), a pointer to the pcap_pkthdr struct (which precede the actual network headers and data), and a u_char pointer to the packet data. The number of packets read is returned. Zero is returned when EOF is reached in a ``savefile.'' A return of - indicates an error in which case pcap_perror() or pcap_geterr() may be used to display the error text. 另外的问题是,我们可能对抓取的包包太多而很头痛,可能很多都不是我们感兴趣的包,别急,pcap_compile和pcap_setfilter能帮我们解决问题。 #include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
packet)
{
static int count = ;
fprintf(stdout,"%d, ",count);
fflush(stdout);
count++;
} int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp; if(argc != ){ fprintf(stdout,"Usage: %s \"filter program\"\n"
,argv[]);return ;} dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{ fprintf(stderr,"%s\n",errbuf); exit(); } pcap_lookupnet(dev,&netp,&maskp,errbuf); descr = pcap_open_live(dev,BUFSIZ,,-,errbuf);
if(descr == NULL)
{ printf("pcap_open_live(): %s\n",errbuf); exit(); } if(pcap_compile(descr,&fp,argv[],,netp) == -)
{ fprintf(stderr,"Error calling pcap_compile\n"); exit(); } if(pcap_setfilter(descr,&fp) == -)
{ fprintf(stderr,"Error setting filter\n"); exit(); } pcap_loop(descr,-,my_callback,NULL); return ;
} 运行./pcap_4.c "host www.google.com"
然后在另外一个控制台下面ping www.baidu.com
哈哈
没有反应吧
接着再ping www.google.com
就看到1, , , , , ,
ok
you got it!!

最新文章

  1. Python性能提升小技巧
  2. 【转】LiveWriter插入高亮代码插件介绍 基于SyntaxHighighter
  3. 在命令行中通过adb shell am broadcast发送广播通知
  4. scrapy 代理
  5. 九度 题目1437:To Fill or Not to Fill
  6. 【HDOJ】1313 Round and Round We Go
  7. iOS 7用户界面过渡指南
  8. Python json序列化
  9. 问题之Spring MVC配置后,可以打开jsp页面,但打不开html页面
  10. static 静态与非静态的区别
  11. oralce 常用sql
  12. mysql 数据库 命令行的操作——对表和字段的操作
  13. android 组件使用()
  14. JQuery Mobile - html5+CSS 禁止IOS长按复制粘贴实现
  15. windows环境下为php打开ssh2扩展
  16. 开源软件架构总结之——Asterisk(DSL、组件、多线程)
  17. pta习题集5-16 朋友圈
  18. LOJ#6360. 复燃「恋之埋火」(最小圆覆盖+高斯消元)
  19. CCF 交通规划(Dijkstra+优先队列)
  20. Android蓝牙自动配对Demo,亲测好使!!!

热门文章

  1. AT 指令和常见错误码
  2. idea之jrebel热部署使用教程
  3. TCP/IP网络编程系列之二(初级)
  4. 简单的SOCKET例子
  5. (转) docker跨主机 macvlan 网络配置
  6. sqlserver并发处理,锁和事务
  7. 脱壳系列(二) - EZIP 壳
  8. 灾难恢复:RPO与RTO
  9. 使用被动混合内容的方式来跨越浏览器会阻断HTTPS上的非安全请求(HTTP)请求的安全策略抓包详解
  10. python学习整理