我正在使用 jnetpcap 来分析数据包。我想获取 html 网页源代码。
但是,当我html.page()
用来获取源代码时,我会得到一些看起来像二进制代码的杂乱代码。谁能帮我?如何解决?
我正在使用 jnetpcap 来分析数据包。我想获取 html 网页源代码。
但是,当我html.page()
用来获取源代码时,我会得到一些看起来像二进制代码的杂乱代码。谁能帮我?如何解决?
不是 jnetpcap 专家,但我一直在使用这个类,它似乎工作。它实际上获得了许多 HTTP 字段,包括其有效负载。
package br.com.mvalle.ids.sniffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;
import org.jnetpcap.packet.format.FormatUtils;
import org.jnetpcap.protocol.network.Ip4;
import org.jnetpcap.protocol.tcpip.Http;
import org.jnetpcap.protocol.tcpip.Tcp;
public class Sniffer {
private List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
private StringBuilder errbuf = new StringBuilder(); // For any error msgs
private PcapIf selectedDevice;
private Pcap pcap;
private PcapPacketHandler<String> jpacketHandler;
public Sniffer(){
listDevices();
selectedDevice = selectDevice(1);
openDevice(selectedDevice);
packetHandler();
capturePackets();
}
public void listDevices(){
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);
}
}
private PcapIf selectDevice(int deviceId){
PcapIf device = alldevs.get(1); // We know we have atleast 1 device (parameter changed from 0 to 1)
System.out
.printf("\nChoosing '%s' on your behalf:\n",
(device.getDescription() != null) ? device.getDescription()
: device.getName());
return device;
}
private void openDevice (PcapIf 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.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null) {
System.err.printf("Error while opening device for capture: "
+ errbuf.toString());
return;
}
}
private void packetHandler(){
jpacketHandler = new PcapPacketHandler<String>() {
Http httpheader = new Http();
public void nextPacket(PcapPacket packet, String user) {
if(packet.hasHeader(httpheader)){
System.out.println(httpheader.toString());
if(httpheader.hasPayload()){
System.out.println("HTTP payload: (string length is "
+new String(httpheader.getPayload()).length()+")");
System.out.println(new String(httpheader.getPayload()));
System.out.println("HTTP truncated? "
+httpheader.isPayloadTruncated());
}
//System.out.println(packet.toString());
}}
};
}
private void capturePackets(){
pcap.loop(pcap.LOOP_INFINITE , jpacketHandler, "Received Packet");
pcap.close();
}
}
希望能帮助到你。