原先的时候曾经在网上看到过一些文章,关于linux下的抓包嗅探啦,网络安全工具的构建了,基本上都是libpcap和libnet配合着使用的,一个原因就是libpcap只能够cap数据包,而不能send数据包
我本来也想走这条路线来着,想着先熟悉一下libpcap和libnet,然后使用libnet编写一个pcap_sendpacket来执行原先windows平台下本来属于pcap库的发包任务,一遍查看libnet的手册,一遍抱怨,为什么winpcap能够发包而libpcap就不能发包呢?而且winpcap还是源于libpcap,是libpcap的windows实现,按道理说应该是libpcap只能比winpcap强大而不可能会比winpcap功能少啊?可现在偏偏就是发生了这种让人很不理解的现象.
正好看到下面这一段
|
Creates the libnet environment. It initializes the library and returns a libnet context. If the injection_type is LIBNET_LINK or LIBNET_LINK_ADV, the function initializes the injection primitives for the link-layer interface enabling the application programmer to build packets starting at the data-link layer (which also provides more granular control over the IP layer). If libnet uses the link-layer and the device argument is non-NULL, the function attempts to use the specified network device for packet injection. This is either a canonical string that references the device (such as "eth0" for a 100MB Ethernet card on Linux or "fxp0" for a 100MB Ethernet card on OpenBSD) or the dots and decimals representation of the device's IP address (192.168.0.1). If device is NULL, libnet attempts to find a suitable device to use. If the injection_type is LIBNET_RAW4 or LIBNET_RAW4_ADV, the function initializes the injection primitives for the IPv4 raw socket interface. The final argument, err_buf, should be a buffer of size LIBNET_ERRBUF_SIZE and holds an error message if the function fails. This function requires root privileges to execute successfully. Upon success, the function returns a valid libnet context for use in later function calls; upon failure, the function returns NULL.
|
我考虑的问题是,他的设备是用device来描述的,such as "eth0" for a 100MB Ethernet card on Linux or "fxp0" for a 100MB Ethernet card on OpenBSD,这句说得很明白,但是如果机器上有多个网卡的话,能不能列举出来然后由用户来选择呢?(我的机器是网关,一块3com 905B连外网,一个板载千兆连接内网)
原先在winpcap下很好解决,用pcap_findalldevices就可以了,然后就可以得到一个网卡链表,接下来如何选择就是数据处理的问题了.但是在libnet里我没有发现有这个功能.能不能让libpcap列举网卡列表,然后以一种libnet可以理解的方式,也就是在linux下such as "eth0"这样的方式传递给libnet,这样不就可以解决了吗>
说干就干,先看一下pcap在linux下列举网卡时显示的信息是什么样子的
下面是主要代码
{
device_t=device;
device_t=device_t->next;
printf("网络适配器列表: ");
while(device_t)
{
printf(" [%d]:%s ",i,device_t->description);
device_t=device_t->next;
i++;
}
}
保存为pcap.cpp
g++ pcap.cpp -c
成功
g++ pcap.cpp -lpcap
成功
./a.out
界面上应该显示"网络适配器列表:"的地方出现了乱码
把终端的编码改为unicode,解决
再次运行正常的显示中文了
但是他显示的列表还是很让我费解
因为没有截屏或者记录
大体说一下
一共有三个网卡
显示
[1]null
[2]slfjsldfjsldfjlsdjkf
[3]null
第二个比较长,忘了
我觉得有些不正常,我的虚拟机只有一个网卡啊
试着把printf(" [%d]:%s ",i,device_t->description);换成 printf(" [%d]:%s ",i,device_t->name);
然后再次编译运行
这次显示正常了
[1]eth0
也就是说libpcap列举的网卡列表可以为libnet所用了
问题解决,原来windows下description是比较易读的格式,linux下description不仅不易读,是完完全全的没法读,而name却在linux下工作的很好,
高兴之余,想看看libpcap跟winpcap除了不能发包之外到底有多少差距,man pcap
然后出来了一大片的帮助.无意见似乎看到有个send什么的.我想,不是说libpcap不能发包吗?怎么还有send?重来了一次,这次看准了确实是有个pcap_sendpacket!而且用法跟winpcap是一致的,高兴啊
这是libpcap给我的第二次惊喜,第一次的时候是知道了在windows下的winpcap在linux下还有个大哥,叫libpcap,那是第一次真实的感受到跨平台库给开发带来的好处,以前的时候口口声声的说过不少跨平台的话,但是从来没有如此真实的体验过使用跨平台库给自己带来的好处,现在想着自己的代码只要经过稍微的修改就可以在另外一个平台运行,,心里的冲动..溢于言表!!!
man pcap|col -b>pcap.txt
然后通过ftp传送到我外面的windows主机上,认真的看了看导出的文本帮助,确实有这个函数的!好了移植变得更加简单了.
静下心来的时候又看了看libpcap的windows实现
Under Win32, libpcap is integrated in the WinPcap packet capture system.
WinPcap provides a framework that allows libpcap to capture the packets
under Windows 95, Windows 98, Windows ME, Windows NT 4, Windows 2000
and Windows XP.
这个实在libpcap的0.9.5版中找到的,呵呵,原来如此啊
看来在pcap_sendpacket上的改进也许是由于winpcap的原因哦
自己推测的,各位不必当真,嘻嘻