Examples/TLSFingerprinting/main.cpp
/**
 * Extract TLS fingerprints from a pcap/pcapng file
 */
Extract TLS fingerprints from a pcap/pcapng file
Extract TLS fingerprints from a pcap / pcapng file
void doTlsFingerprintingOnPcapFile(const std::string& inputPcapFileName, std::string& outputFileName, const std::string& separator, bool chFP, bool shFP, const std::string& bpfFilter)
{
	pcpp::IFileReaderDevice* reader = pcpp::IFileReaderDevice::getReader(inputPcapFileName.c_str());
	if (!reader->open())
		EXIT_WITH_ERROR("Cannot open pcap/pcapng file");
	if (outputFileName.empty())
	{
		size_t fileNameOffset = inputPcapFileName.find_last_of("\\/") + 1;
		size_t extensionOffset = inputPcapFileName.find_last_of(".");
		std::string fileNameWithoutExtension = inputPcapFileName.substr(fileNameOffset, extensionOffset - fileNameOffset);
		outputFileName = fileNameWithoutExtension + ".txt";
	}
	std::ofstream outputFile(outputFileName.c_str());
	if (!outputFile)
	{
		EXIT_WITH_ERROR("Cannot open output file '" << outputFileName << "'");
	}
	writeHeaderToOutputFile(outputFile, separator);
	if (!bpfFilter.empty())
	{
		if (!reader->setFilter(bpfFilter))
			EXIT_WITH_ERROR("Error in setting BPF filter to the pcap file");
	}
	std::cout << "Start reading '" << inputPcapFileName << "'..." << std::endl;
	TLSFingerprintingStats stats;
	HandlePacketData data;
	data.chFP = chFP;
	data.shFP = shFP;
	data.outputFile = &outputFile;
	data.separator = separator;
	data.stats = &stats;
	pcpp::RawPacket rawPacket;
	while (reader->getNextPacket(rawPacket))
	{
		handlePacket(&rawPacket, &data);
	}
	reader->close();
	delete reader;
	printStats(stats, chFP, shFP);
	std::cout << "Output file was written to: '" << outputFileName << "'" << std::endl;
}
void doTlsFingerprintingOnPcapFile ( const std :: string & inputPcapFileName , std :: string & outputFileName , const std :: string & separator , bool chFP , bool shFP , const std :: string & bpfFilter ) { pcpp :: IFileReaderDevice * reader = pcpp :: IFileReaderDevice :: getReader ( inputPcapFileName . c_str ( ) ) ; if ( ! reader -> open ( ) ) EXIT_WITH_ERROR ( " " ) ; if ( outputFileName . empty ( ) ) { size_t fileNameOffset = inputPcapFileName . find_last_of ( " \\ " ) + 1 ; size_t extensionOffset = inputPcapFileName . find_last_of ( " " ) ; std :: string fileNameWithoutExtension = inputPcapFileName . substr ( fileNameOffset , extensionOffset - fileNameOffset ) ; outputFileName = fileNameWithoutExtension + " " ; } std :: ofstream outputFile ( outputFileName . c_str ( ) ) ; if ( ! outputFile ) { EXIT_WITH_ERROR ( " " << outputFileName << " " ) ; } writeHeaderToOutputFile ( outputFile , separator ) ; if ( ! bpfFilter . empty ( ) ) { if ( ! reader -> setFilter ( bpfFilter ) ) EXIT_WITH_ERROR ( " " ) ; } std :: cout << " " << inputPcapFileName << " " << std :: endl ; TLSFingerprintingStats stats ; HandlePacketData data ; data . chFP = chFP ; data . shFP = shFP ; data . outputFile = & outputFile ; data . separator = separator ; data . stats = & stats ; pcpp :: RawPacket rawPacket ; while ( reader -> getNextPacket ( rawPacket ) ) { handlePacket ( & rawPacket , & data ) ; } reader -> close ( ) ; delete reader ; printStats ( stats , chFP , shFP ) ; std :: cout << " " << outputFileName << " " << std :: endl ; }
Extract TLS fingerprints from a pcap/pcapng file
Extract TLS fingerprints from a pcap / pcapng file
// open input file (pcap or pcapng file) // try to open the file device // set output file name to input file name if not provided by the user // open output file // write the column headers to the output file // set BPF filter if provided by the user // iterate over all packets in the file // close the reader and free its memory