Java JNetPcap Library Packet Capture
pcap File
테스트 환경 : Win7 64bit, Eclipse, Java 1.8
이클립스와
자바는 설치되어있다고 가정합니다.
JNetPcap 라이브러리 다운로드
저는 x84_64
압축을 풀면
jnetpcap.jar, jnetpcap.dll
두 개의 파일이 보입니다.
1. jnetpcap.jar 은 Eclipse 프로젝트에서 참조 할 수 있도록
Package Explorer > Properties > Java Build Path > Libraries > Add JARs 나 Add External JARs 로 추가하여 줍니다.
다운받은 경로보다는
해당 프로젝트 Eclipse Workspace 내에 두는것을 추천합니다.
한글 경로 때문에 안되는 경우도 있습니다,
2. jnetpcap.dll
C:\Windows\System32 경로에 jnetpcap.dll 파일을 복사하여 줍니다.
이제 설정은 끝났습니다.
테스트 코드는 다음과 같습니다.
import java.io.File; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import org.jnetpcap.ByteBufferHandler; import org.jnetpcap.Pcap; import org.jnetpcap.PcapDumper; import org.jnetpcap.PcapHeader; import org.jnetpcap.PcapIf; public class Test1 { public static void main(String[] args) { // pcap.loop(x, dumpHandler, dumper); x 개 패킷을 // String ofile = "tmp-capture-file.cap"; tmp-capture-file.cap 파일로 저장 Listalldevs = new ArrayList (); // Will be filled with // NICs StringBuilder errbuf = new StringBuilder(); // For any error msgs /*************************************************************************** * 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\n", errbuf.toString()); return; } PcapIf device = alldevs.get(0); // We know we have atleast 1 device /*************************************************************************** * Second we open up the selected device **************************************************************************/ int snaplen = 64 * 1024; // Capture all packets, no trucation int flags = Pcap.MODE_PROMISCUOUS; // capture all packets int timeout = 10 * 1000; // 10 seconds in millis Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf); if (pcap == null) { System.err.printf("Error while opening device for capture: %s\n", errbuf.toString()); return; } /*************************************************************************** * Third we create a PcapDumper and associate it with the pcap capture ***************************************************************************/ String ofile = "tmp-capture-file.cap"; PcapDumper dumper = pcap.dumpOpen(ofile); // output file /*************************************************************************** * Fouth we create a packet handler which receives packets and tells the * dumper to write those packets to its output file **************************************************************************/ ByteBufferHandler dumpHandler = new ByteBufferHandler () { public void nextPacket(PcapHeader arg0, ByteBuffer arg1, PcapDumper arg2) { // TODO Auto-generated method stub dumper.dump(arg0, arg1); } }; /*************************************************************************** * Fifth we enter the loop and tell it to capture 10 packets. We pass in * the dumper created in step 3 **************************************************************************/ pcap.loop(10, dumpHandler, dumper); File file = new File(ofile); System.out.printf("%s file has %d bytes in it!\n", ofile, file.length()); /*************************************************************************** * Last thing to do is close the dumper and pcap handles **************************************************************************/ dumper.close(); // Won't be able to delete without explicit close pcap.close(); if (file.exists()) { // file.delete(); // Cleanup } } }
'Programming > JNetPcap' 카테고리의 다른 글
Java JNetPcap Library Packet Analytics (0) | 2016.09.20 |
---|