【问题标题】:Extracting Client Hello and SNI in libpcap在 libpcap 中提取客户端 Hello 和 SNI
【发布时间】:2017-10-14 16:09:54
【问题描述】:

我在 c 中使用 libpcap 库来解析 pcap 文件。我正在尝试提取 TCP 客户端 hello,然后检查服务器名称指示以匹配给定的服务器。我可以这样做吗?如果是的话,有人可以告诉我怎么做吗?谢谢

【问题讨论】:

  • libpcap 只能让您访问网络数据。从那里您需要解析 IP 和 TCP 协议以到达 TLS 层,然后您需要了解该层以提取 ClientHello,然后它包含 SNI 扩展。因此,仅通过使用 libpcap,您大约只完成了 5%,这使得这个问题过于宽泛。
  • 我解析了TCP和IP协议,但是我怎么知道这个包是ClientHello呢?
  • 那么你是怎么知道你在处理IP和TCP的呢?可能是通过理解这些协议并相应地提取信息。您需要使用 TLS 来获取 ClientHello:请参阅 RFC 5246 了解 TLS 1.2 的规范,该规范还描述了 ClientHello 的格式。

标签: c tcp network-programming libpcap sni


【解决方案1】:

这是一个 C++ 代码 sn-p,它可以使用 PcapPlusPlus 库来实现。此代码假定您正在从 pcap 文件中读取数据包,但从实时接口读取时也可以实现相同的目的:

// create a pcap file reader instance
pcpp::IFileReaderDevice* reader = pcpp::IFileReaderDevice::getReader("my_ssl_packets.pcap");

// open the reader for reading
reader->open();

// read the first (and only) packet from the file
pcpp::RawPacket rawPacket;
while (reader->getNextPacket(rawPacket) != NULL)
{
    // parse the packet
    pcpp::Packet sslParsedPacket(&rawPacket);

    // check if this is a SSL packet
    if (!sslParsedPacket.isPacketOfType(pcpp::SSL))
        continue;

    // check if this is a SSL handshake packet
    pcpp::SSLHandshakeLayer* sslHandshakeLayer = sslPacket->getLayerOfType<pcpp::SSLHandshakeLayer>();
    if (sslHandshakeLayer == NULL)
        continue;

    // check if this is a client-hello message
    pcpp::SSLClientHelloMessage* clientHelloMessage = sslHandshakeLayer->getHandshakeMessageOfType<pcpp::SSLClientHelloMessage>();
    if (clientHelloMessage == NULL)
        continue;

    // extract the Server Name Indication from the client-hello message
    pcpp::SSLServerNameIndicationExtension* sniExtension = clientHelloMessage->getExtensionOfType<pcpp::SSLServerNameIndicationExtension>();
    if (sniExtension != NULL)
        printf("Server Name Indication is: %s\n", sniExtension->getHostName().c_str());
}

// close the file reader
reader->close();
delete reader;

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多