使用pcap库进行编程

我们在确定想进行网络嗅探,也就是抓取网络当中的数据包的时候!可以有很多方式来实现这个功能
  • BPF(Berkeley Packet Filter)
  • DLPI(Data Link Provider Interface)
  • SOCKET_PACKET type sockets(Linux only)
  • using pcap library
这里主要介绍使用pcap库来实现该功能!
首先得安装pcap库,具体安装包的可以到www.tcpdump.org上查看,或者点击 这里,下载libpcap库代码,注意:在不同的库版本下的代码在移植上可能会有问题!
在开始嗅探之前,我们应该对于嗅探有一个从全局角度的把握!
  • We begin by determining which interface we want to sniff on!也就是我们得确定我们嗅探的接口
  • Initialize pcap.This is where we actually tell pcap what device we are sniffing no.初始化pcap,也就是告诉pcap我们选择那个接口进行嗅探!
  • we must set a rule what we want to sniff specific traffic(eg.only TCP/IP packets,only packets going to port 23 etc..),'complie' it ,and apply it.Then we tell pcap to apply it to whichever session we wish for it to filter.设置嗅探规则(过滤器)并应用!顾名思义:告知pcap库,我们需要嗅探什么样的数据:是TCP/IP数据、是仅仅通过23端口的数据等等..
  • we tell pcap to enter it's primary execution loop.In this state ,pcap waits data packets or grabs packets! 进入pcap的主循环!等待数据或者是获得数据并送至相应的函数处理
  • After our sniffing needs are statisfied,we close our session and are complete!关闭!!
Settting the device 
 
    1. #include <stdio.h>
    2.  #include <pcap.h>
    3.  
    4.  int main(int argc,char **argv)
    5.  {
    6.          char *dev;
    7.          char errbuf[PCAP_ERRBUF_SIZE];
    8.          dev = pcap_lookupdev(errbuf);
    9.          if(dev == NULL){
    10.                  fprintf(stderr,"Couldn't find default device %s:\n",errbuf);
    11.                   return 1;
    12.          }
    13.          return 0;
    14. }
    15. 编译:
    16. [#15#caopeng@laptop ~/pcap]$gcc device.c -o device -lpcap
    17. 执行:
    18. [#19#caopeng@laptop ~/pcap]$sudo ./device
    19. Device :eth0
我们可以通过char *pcap_lookupdev(char *errbuf);来获取当前系统的网络设备!在此有了一个比较感性的认知!原来是使用pcap库函数可以这样方便的获得当前的网络设备!下面从一段代码来详细的说明!详细的代码请点击 这里下载!!下面从一些片段来说明!
开始的函数pcap_lookupdev在上面已经介绍,在此不再赘述!
int pcap_lookupnet(const char *device,bpf_u_int32 *netp,bpf_u_int32 *maskp,char *errbuf);
将网络设备dev关联相应的network number 和mask。
  1. /*Find the properties for the device*/
  2.     if(pcap_lookupnet(dev,&net,&mask,errbuf) == -1){
  3.         fprintf(stderr,"Couldn't get netmask for device :%s :%s\n",dev,errbuf);
  4.         net = 0;
  5.         mask = 0;
  6.     }
dev既就是通过pcap_lookupdev获得的返回值!,net同mask均为bpf_u_int32类型变量,errbuf用于保存异常信息!在调用了该函数之后,变将网络设备的net和mask关联起来了!
pcap_t *pacp_open_live(const char *device,int snaplen,int promisc,int to_ms,char *errbuf)
在网卡混杂模式的基础之上建立一会话!
  1. /*Open the session in promiscuous mode*/
  2.     handle = pcap_open_live(dev,BUFSIZ,1,1000,errbuf);
  3.     if(handle == NULL){
  4.         fprintf(stderr,"Couldn't open device %s : %s\n",dev,errbuf);
  5.         return 2;
  6.     }
dev略过;BUFSIZ是一常量,表示这次可以抓取的最大的数据大小(该数字最好设置为该常数,毕竟不同的系统可能对于此限制不同);1表示真,就说明网卡设置在了混杂模式!1000指定了read timeout为1000 milliseconds;errbuf还是保存一些异常信息!

int pacp_compile(pcap_t *p,struct bpf_program *fp,char *str,int optimize,bpf_u_int32 netmask)
int pcap_setfilter(pacp_t *p,struct bpf_program *fp)

  1.    /*complie and apply the filter*/
  2.     if(pcap_compile(handle,&fp,filter_exp,0,net) == -1){
  3.         fprintf(stderr,"Couldn't parse filter %s:%s\n",filter_exp,pcap_geterr(handle));
  4.         return 2;
  5.     }

  6.     if(pcap_setfilter(handle,&fp) == -1){
  7.         fprintf(stderr,"Couldn't install filter %s:%s\n",filter_exp,pcap_geterr(handle));
  8.         return 2;
  9.     }
handle:用来标识建立的会话fp是用来存储编译之后的过滤规则的;filter_exp就是过滤规则;0就是网卡不再混杂模式;net就是ip;
pcap_compile()函数就是将char *str编译成 struct bpf_program *fp。(pcap_compile() is used to compile the string str into a filter program)
pcap_setfileter()函数就是指定一个过滤规则.(pcap_setfileter() is used to specify a filter program);

从开头的步骤可以看出,下面的一步就是要获取数据了,
  1. /*Grab a pcaket*/
  2.     pcaket = pcap_next(handle,&hander);
hander:这个有点类似于linux当中链表的实现!仅仅是给出了节点的指针域,至于如何得到“数据”的地址,在稍候会作介绍,这个是pcap给我们抓取的数据信息的头;

最后一步当然是关闭打开的device

下面给出一部分英文解释,详情请访问: http://code.google.com/p/sniffermy/wiki/pcap

For the sake of simplicity, we'll say that the address this pointer is set to is the value X. Well, if our three structures are just sitting in line, the first of them (sniff_ethernet) being located in memory at the address X, then we can easily find the address of the structure after it; that address is X plus the length of the Ethernet header, which is 14, or SIZE_ETHERNET.

Similarly if we have the address of that header, the address of the structure after it is the address of that header plus the length of that header. The IP header, unlike the Ethernet header, does not have a fixed length; its length is given, as a count of 4-byte words, by the header length field of the IP header. As it's a count of 4-byte words, it must be multiplied by 4 to give the size in bytes. The minimum length of that header is 20 bytes.

The TCP header also has a variable length; its length is given, as a number of 4-byte words, by the "data offset" field of the TCP header, and its minimum length is also 20 bytes.

So let's make a chart:

VariableLocation (in bytes)
sniff_ethernetX
sniff_ipX + SIZE_ETHERNET
sniff_tcpX + SIZE_ETHERNET + {IP header length}
payloadX + SIZE_ETHERNET + {IP header length} + {TCP header length}


转载请著名出处!
<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(502) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值