简体   繁体   English

使用 pcap4J 解密 HTTPS 数据包

[英]Decrypting HTTPS packets using pcap4J

In Java I'm using pcap4J to capture network traffic of another application running on my computer.Java我使用pcap4J来捕获在我的计算机上运行的另一个应用程序的网络流量。 The code I'm using to do this is the following:我用来执行此操作的代码如下:

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);
    }
}

Unfortunately, the traffic is encrypted and therefore useless to analyze.不幸的是,流量是加密的,因此无法分析。 Another application called Fiddler is however able to decrypt the traffic just fine without any special configuration or private key of the server.然而,另一个名为Fiddler应用程序能够很好地解密流量,而无需服务器的任何特殊配置或私钥。 Fiddler can display the JSON structures being exchanged which I'm interested in. How can I do the same thing in Java code in order to work with the captured JSON objects? Fiddler 可以显示我感兴趣的正在交换的JSON结构。我怎样才能在Java代码中做同样的事情来处理捕获的JSON对象? (This question is about the decryption part, not the parsing afterwards) (这个问题是关于解密部分,不是后面的解析)

As by the commenters on this question:正如对这个问题的评论者所说:

By definition you can not decrypt any TLS traffic (so that includes HTTPS ) if you do not control either side or are able to have either side give you the negotiated master key and client random used.根据定义,如果您不控制任何一方或能够让任何一方为您提供协商的主密钥和使用的客户端随机数,则您无法解密任何TLS流量(因此包括HTTPS )。 Just trying to decrypt any random TLS traffic will not be possible.仅尝试解密任何随机TLS流量是不可能的。 Fiddler does it by being a man-in-the-middle, not by decrypting traffic sent directly between two other computers. Fiddler是通过作为中间人而不是通过解密直接在其他两台计算机之间发送的流量来实现的。 While Fiddler does not need special configuration the client needs a special configuration, ie it needs to trust the certificate authority used by Fiddler to dynamically create certificates.虽然Fiddler不需要特殊配置,但客户端需要特殊配置,即它需要信任Fiddler使用的证书颁发机构来动态创建证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM