jnetpcap简介

来源:互联网 发布:315怎么投诉淘宝卖家 编辑:程序博客网 时间:2024/06/09 22:56

jnetpcap简介

通过Fiddler可以嗅探Http协议网络数据包, 如何在Java平 台下通过第三方包来进行底层网络嗅探。

Java平台本身不支持底层网络操作,需要第三方包利用JNI封装不同系统的C库来提供Java的上层接口。常用的类库包括 JPcap,JNetPcap等,他们都是基于TcpDump/LibPcap的Java封装。其中JPcap已经一年多没更新了,而JNetPcap (jnetpcap.com) 在上周刚刚发布了1.2 RC5版本,添加了很多实用的类库,诸如高级协议分析等,本文就以JNetPcap作为例子来进行介绍。

使用JNetPcap之前必须在目标系统中安装WinPcap(Windows系统),以提供JNetPcap所需要的链接库。另外要把JNetPcap包和所属的dll文件都加入到开发目录的环境变量中。

Pcap类是JNetPcap中最为核心的类,是一个对LibPcap中方法的Java直接映射,提供了取得网卡设备列表、打开嗅探、设置过滤器等等必须的工作。

一、获得网卡列表

通过Pcap.findAllDevs(alldevs, errbuf) 这个静态方法将所有本机网卡加入到alldevs的List<PcapIf >中。然后用户可以选择一个网卡进行监听。注意基于PPPOE拨号的网络连接在实际测试中似乎并不能被嗅探到,例如笔者的铁通连接无法被嗅探,其中 的问题还有待考证。

二、打开连接

调用Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf)静态方法,返回一个Pcap对象。其中5个参数分别表示设备的系统名称(不是设备别名)、每次捕捉的数据量、捕捉方式、超时和错误信息缓冲 区。推荐的参数配置在JNetPcap的文档中有详细说明,这里不再赘述。需要注意的是超时不宜过小,否则会造成数据包捕捉不完全的问题。时间至少应该保 证一个数据包完全接收。

三、开始监听
调用pcap.loop(int cnt, JPacketHandler<T> handler, T user) 方法即可进行监听,在loop方法的参数中有两点需要关注,第一点是用户指定的数据包分析器,在之后的文章中将详细介绍;第二点是一个泛型参数,表示传输 给分析器的用户指定类型的消息。

四、数据包分析

捕捉到数据包后当然要进行分析。在这里我们使用继承JPacketHandler来实现自己的处理方法。
在JPacketHandler有 一个nextPacket(JPacket packet, T user) 方法,这是典型的通过事件机制来实现处理数据包的方法。每当Pcap嗅探到一个数据包后,他就会调用用户之前绑定的分析器中的nextPacket方法进 行处理。注意这个方法是阻塞的,也就避免了潜在的同步问题。传进的JPacket参数包含了这个数据包中的所有信息,通过不同的内置Header分析器可 以分析不同的协议。在最近的RC5版本中甚至加入了HTTP协议和图像数据的直接分析,免去了之前RC4版本中需要通过ACK信息手工拼合chunked 的数据包,然后手工分析HTTP协议文本的麻烦。限于篇幅,这里不多做介绍,具体方法可以参照API,我们只简单的调用toString()方法将这个数 据包打印在控制台中。

JNetPcap无疑是当前最强大以及最具有潜力的网络数据包捕捉类库。感谢Mark B. 的辛苦工作,让Java的JNI世界更加精彩。

 

jNetPcap是libpcap的一个Java完整封装。jNetPcap使 用与libpcap相同风格的API。libpcap是unix/linux平台下的网络数据包捕获函数库,大多数网络监控软件都以它为基础。 Libpcap可以在绝大多数类unix平台下工作。Libpcap提供了系统独立的用户级别网络数据包捕获接口,并充分考虑到应用程序的可移植性。

jNetPcap 官方网站:http://jnetpcap.com/

JAVA 网络抓包工具。网址:http://jnetpcap.com

download 一个deb包以后就开始写简单的获取 网络设备的程序,但是获取不到网络设备,正纳闷看到这个:

http://jnetpcap.com/node/269

于是问题解决,因为我用的ubuntu没有用到root权限,搞不定,所以换了root权限就搞定了。

先了解下网络分层,在每层都传输些什么包。图示如下

比如说如果我们访问一个网页,那么各层所传输的包如下图所示:

一些基本常识:

英文:

the maximum size of an IP packet is 65,535 bytes while the typical

maximum transmission unit (MTU
) for Ethernet is 1,500 bytes.

中文:

IP包最大只能传输 65535 个字节。以太网最大能传输 1500 个字节。

这是从jnetpcap官网上的例子大家可以参考参考,具体请访问,jnetpcap.com

package com.jnetpcap.kcsj.test;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.jnetpcap.Pcap;import org.jnetpcap.PcapBpfProgram;import org.jnetpcap.PcapIf;import org.jnetpcap.packet.PcapPacket;import org.jnetpcap.packet.PcapPacketHandler;import org.jnetpcap.protocol.network.*;public class JpcapTest {public static void main(String[] args) {List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with// NICsStringBuilder errbuf = new StringBuilder(); // For any error msgsPcap pcap;final Ip4 ip = new Ip4();/*************************************************************************** * First get a list of devices on this system **************************************************************************/int r = Pcap.findAllDevs(alldevs, errbuf);if (r == Pcap.NOT_OK || alldevs.isEmpty()) {System.err.printf("Can’t read list of devices, error is %s", errbuf.toString());return;}System.out.println("Network devices found:");int i = 0;for (PcapIf device : alldevs) {String description = (device.getDescription() != null) ? device.getDescription(): "No description available";System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description);}PcapIf device = alldevs.get(0); // We know we have atleast 1 deviceSystem.out.printf("\nChoosing ‘%s’ on your behalf:\n",(device.getDescription() != null) ? device.getDescription() : device.getName());/*************************************************************************** * Second we open up the selected device **************************************************************************/int snaplen = 64 * 1024; // Capture all packets, no trucationint flags = Pcap.MODE_PROMISCUOUS; // capture all packetsint timeout = 10 * 1000; // 10 seconds in millispcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);if (pcap == null) {System.err.printf("Error while opening device for capture:" + errbuf.toString());return;}/* * Compiling and appplying a filter to network interface */PcapBpfProgram filter = new PcapBpfProgram();String expression = "port 80";int optimize = 0;int netmask = 0;int m = pcap.compile(filter, expression, optimize, netmask);if (m != Pcap.OK) {System.out.println("Filter error: " + pcap.getErr());}pcap.setFilter(filter);/*************************************************************************** * Third we create a packet handler which will receive packets from the * libpcap loop. **************************************************************************/PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {public void nextPacket(PcapPacket packet, String user) {if (packet.hasHeader(ip)) {System.out.println("ip vesiont"+ip.version());System.out.println(ip.destination());}System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",new Date(packet.getCaptureHeader().timestampInMillis()), packet.getCaptureHeader().caplen(), // Length// actually // capturedpacket.getCaptureHeader().wirelen(), // Original// lengthuser // User supplied object);System.out.printf("数据包头%s\n", packet.getCaptureHeader().size());}};/*************************************************************************** * Fourth we enter the loop and tell it to capture 10 packets. The loop * method does a mapping of pcap.datalink() DLT value to JProtocol ID, * which is needed by JScanner. The scanner scans the packet buffer and * decodes the headers. The mapping is done automatically, although a * variation on the loop method exists that allows the programmer to * sepecify exactly which protocol ID to use as the data link type for * this pcap interface. **************************************************************************/pcap.loop(10, jpacketHandler, "jNetPcap rocks!");/*************************************************************************** * Last thing to do is close the pcap handle **************************************************************************/pcap.close();}}


原创粉丝点击