0
我在c中使用libpcap庫來解析pcap文件。我試圖提取TCP客戶端hello,然後檢查服務器名稱指示以匹配給定的服務器。我可以這樣做嗎?如果是的話,有人能告訴我如何?謝謝在libpcap中提取客戶端Hello和SNI
我在c中使用libpcap庫來解析pcap文件。我試圖提取TCP客戶端hello,然後檢查服務器名稱指示以匹配給定的服務器。我可以這樣做嗎?如果是的話,有人能告訴我如何?謝謝在libpcap中提取客戶端Hello和SNI
這是一個C++代碼片段,可以使用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;
libpcap只能讓您訪問網絡數據。從那裏你需要解析IP和TCP協議到達TLS層,然後你需要理解這個層來提取ClientHello,然後包含SNI擴展。因此,通過使用libpcap,你只能以5%的速度使問題過於寬泛。 –
我解析了TCP和IP協議,但我怎麼知道這個數據包是ClientHello? – sarah
那麼您是如何知道您正在處理IP和TCP?可能通過理解這些協議並相應地提取信息。您需要使用TLS來獲取ClientHello:請參閱[RFC 5246](https://tools.ietf.org/html/rfc5246)以瞭解TLS 1.2的規範,該規範還描述了ClientHello的格式。 –