在Java
我pcap4J
用来捕获在我的计算机上运行的另一个应用程序的网络流量。我用来执行此操作的代码如下:
import org.pcap4j.core.*;
import org.pcap4j.packet.Packet;
import org.pcap4j.util.NifSelector;
import java.io.IOException;
import static org.pcap4j.core.BpfProgram.BpfCompileMode.OPTIMIZE;
import static org.pcap4j.core.PcapNetworkInterface.PromiscuousMode.PROMISCUOUS;
public class Pcap4jLoop
{
public static void main(String[] arguments) throws Exception
{
PcapNetworkInterface networkDevice = getNetworkDevice();
try (PcapHandle handle = networkDevice.openLive(65536, PROMISCUOUS, 50))
{
String serverIP = "..."; // Filter for packets with just one server
String bpfExpression = "dst host " + serverIP + " || src host " + serverIP;
handle.setFilter(bpfExpression, OPTIMIZE);
PacketListener listener = packet -> printPacket(packet, handle);
handle.loop(Integer.MAX_VALUE, listener);
//noinspection InfiniteLoopStatement,StatementWithEmptyBody
while (true)
{
}
}
}
private static PcapNetworkInterface getNetworkDevice() throws IOException
{
NifSelector nifSelector = new NifSelector();
PcapNetworkInterface nif = nifSelector.selectNetworkInterface();
if (nif == null)
{
System.exit(1);
}
return nif;
}
private static void printPacket(Packet packet, PcapHandle pcapHandle)
{
StringBuilder sb = new StringBuilder();
sb.append("A packet captured at ")
.append(pcapHandle.getTimestampPrecision())
.append(":");
System.out.println(sb);
System.out.println(packet);
}
}
不幸的是,流量是加密的,因此无法分析。然而,另一个名为Fiddler
的应用程序能够很好地解密流量,而无需任何特殊配置或服务器的私钥。Fiddler 可以显示JSON
我感兴趣的正在交换的结构。我怎样才能在Java
代码中做同样的事情来处理捕获的JSON
对象?(这个问题是关于解密部分,而不是事后解析)