123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "pcap_processor.h"
- using namespace Tins;
- pcap_processor::pcap_processor(std::string path) : filePath(path) {
- }
- long double pcap_processor::get_timestamp_mu_sec(const int after_packet_number) {
- if (file_exists(filePath)) {
- FileSniffer sniffer(filePath);
- int current_packet = 1;
- for (SnifferIterator i = sniffer.begin(); i != sniffer.end(); i++) {
- if (after_packet_number == current_packet) {
- const Timestamp &ts = i->timestamp();
- return (long double) ((ts.seconds() * 1000000) + ts.microseconds() + 1);
- }
- current_packet++;
- }
- }
- return -1.0;
- }
- std::string pcap_processor::merge_pcaps(const std::string pcap_path) {
-
-
- time_t curr_time = time(0);
- char buff[1024];
- struct tm *now = localtime(&curr_time);
- strftime(buff, sizeof(buff), "%Y%m%d-%H%M%S", now);
- std::string tstmp(buff);
-
- std::string new_filepath = filePath;
- const std::string &newExt = "_" + tstmp + ".pcap";
- std::string::size_type h = new_filepath.rfind('.', new_filepath.length());
- if (h != std::string::npos) {
- new_filepath.replace(h, newExt.length(), newExt);
- } else {
- new_filepath.append(newExt);
- }
- FileSniffer sniffer_base(filePath);
- SnifferIterator iterator_base = sniffer_base.begin();
- FileSniffer sniffer_attack(pcap_path);
- SnifferIterator iterator_attack = sniffer_attack.begin();
- PacketWriter writer(new_filepath, PacketWriter::ETH2);
- bool all_attack_pkts_processed = false;
-
- for (; iterator_base != sniffer_base.end();) {
- auto tstmp_base = (iterator_base->timestamp().seconds()) + (iterator_base->timestamp().microseconds()*1e-6);
- auto tstmp_attack = (iterator_attack->timestamp().seconds()) + (iterator_attack->timestamp().microseconds()*1e-6);
- if (!all_attack_pkts_processed && tstmp_attack <= tstmp_base) {
- try {
- writer.write(*iterator_attack);
- } catch (serialization_error) {
- std::cout << std::setprecision(15) << "Could not serialize attack packet with timestamp " << tstmp_attack << std::endl;
- }
- iterator_attack++;
- if (iterator_attack == sniffer_attack.end())
- all_attack_pkts_processed = true;
- } else {
- try {
- writer.write(*iterator_base);
- } catch (serialization_error) {
- std::cout << "Could not serialize base packet with timestamp " << std::setprecision(15) << tstmp_attack << std::endl;
- }
- iterator_base++;
- }
- }
-
-
- for (; iterator_attack != sniffer_attack.end(); iterator_attack++) {
- try {
- writer.write(*iterator_attack);
- } catch (serialization_error) {
- auto tstmp_attack = (iterator_attack->timestamp().seconds()) + (iterator_attack->timestamp().microseconds()*1e-6);
- std::cout << "Could not serialize attack packet with timestamp " << std::setprecision(15) << tstmp_attack << std::endl;
- }
- }
- return new_filepath;
- }
- void pcap_processor::collect_statistics() {
-
- if (file_exists(filePath)) {
- std::cout << "Loading pcap..." << std::endl;
- FileSniffer sniffer(filePath);
- SnifferIterator i = sniffer.begin();
- Tins::Timestamp lastProcessedPacket;
-
- stats.setTimestampFirstPacket(i->timestamp());
-
-
- int counter=0;
- int timeIntervalNum = 1;
- std::chrono::duration<int, std::micro> timeInterval(10000000);
- std::chrono::microseconds intervalStartTimestamp = stats.getTimestampFirstPacket();
- std::chrono::microseconds firstTimestamp = stats.getTimestampFirstPacket();
- int pktsInterval = 1000;
- int previousPacketCount = 0;
-
-
- for (; i != sniffer.end(); i++) {
-
-
- if(counter%pktsInterval==0){
- stats.addIPEntropy(filePath);
- }
-
-
- std::chrono::microseconds lastPktTimestamp = i->timestamp();
-
-
-
- std::chrono::microseconds currentCaptureDuration = lastPktTimestamp - firstTimestamp;
- std::chrono::microseconds barrier = timeIntervalNum*timeInterval;
- if(currentCaptureDuration>barrier){
-
-
- stats.addIntervalStat(timeInterval, intervalStartTimestamp, previousPacketCount);
- stats.calculateLastIntervalIPsEntropy(filePath, intervalStartTimestamp);
- stats.calculateLastIntervalPacketRate(timeInterval, intervalStartTimestamp);
-
-
- timeIntervalNum++;
- intervalStartTimestamp = lastPktTimestamp;
- previousPacketCount = stats.getPacketCount();
- }
-
- stats.incrementPacketCount();
- this->process_packets(*i);
- lastProcessedPacket = i->timestamp();
- counter++;
- }
-
- stats.setTimestampLastPacket(lastProcessedPacket);
- }
- }
- void pcap_processor::process_packets(const Packet &pkt) {
-
- std::string macAddressSender = "";
- std::string macAddressReceiver = "";
- const PDU *pdu_l2 = pkt.pdu();
- uint32_t sizeCurrentPacket = pdu_l2->size();
- if (pdu_l2->pdu_type() == PDU::ETHERNET_II) {
- EthernetII eth = (const EthernetII &) *pdu_l2;
- macAddressSender = eth.src_addr().to_string();
- macAddressReceiver = eth.dst_addr().to_string();
- sizeCurrentPacket = eth.size();
- }
- stats.addPacketSize(sizeCurrentPacket);
-
- const PDU *pdu_l3 = pkt.pdu()->inner_pdu();
- const PDU::PDUType pdu_l3_type = pdu_l3->pdu_type();
- std::string ipAddressSender;
- std::string ipAddressReceiver;
-
- if (pdu_l3_type == PDU::PDUType::IP) {
- const IP &ipLayer = (const IP &) *pdu_l3;
- ipAddressSender = ipLayer.src_addr().to_string();
- ipAddressReceiver = ipLayer.dst_addr().to_string();
-
- stats.addIpStat_packetSent(filePath, ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket, pkt.timestamp());
-
- stats.incrementTTLcount(ipAddressSender, ipLayer.ttl());
-
- stats.incrementProtocolCount(ipAddressSender, "IPv4");
-
- stats.assignMacAddress(ipAddressSender, macAddressSender);
- stats.assignMacAddress(ipAddressReceiver, macAddressReceiver);
- }
- else if (pdu_l3_type == PDU::PDUType::IPv6) {
- const IPv6 &ipLayer = (const IPv6 &) *pdu_l3;
- ipAddressSender = ipLayer.src_addr().to_string();
- ipAddressReceiver = ipLayer.dst_addr().to_string();
-
- stats.addIpStat_packetSent(filePath, ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket, pkt.timestamp());
-
- stats.incrementTTLcount(ipAddressSender, ipLayer.hop_limit());
-
- stats.incrementProtocolCount(ipAddressSender, "IPv6");
-
- stats.assignMacAddress(ipAddressSender, macAddressSender);
- stats.assignMacAddress(ipAddressReceiver, macAddressReceiver);
- } else {
- std::cout << "Unknown PDU Type on L3: " << pdu_l3_type << std::endl;
- }
-
- const PDU *pdu_l4 = pdu_l3->inner_pdu();
- if (pdu_l4 != 0) {
-
- PDU::PDUType p = pdu_l4->pdu_type();
- if (p == PDU::PDUType::TCP) {
- TCP tcpPkt = (const TCP &) *pdu_l4;
- stats.incrementProtocolCount(ipAddressSender, "TCP");
-
-
-
- stats.addFlowStat(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), pkt.timestamp());
-
-
-
- if(tcpPkt.get_flag(TCP::SYN)) {
- int win = tcpPkt.window();
- stats.incrementWinCount(ipAddressSender, win);
- }
-
- try {
- int val = tcpPkt.mss();
- stats.addMSS(ipAddressSender, val);
-
-
-
- stats.incrementMSScount(ipAddressSender, val);
- } catch (Tins::option_not_found) {
-
- }
- stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport());
- } else if (p == PDU::PDUType::UDP) {
- const UDP udpPkt = (const UDP &) *pdu_l4;
- stats.incrementProtocolCount(ipAddressSender, "UDP");
- stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport());
- } else if (p == PDU::PDUType::ICMP) {
- stats.incrementProtocolCount(ipAddressSender, "ICMP");
- } else if (p == PDU::PDUType::ICMPv6) {
- stats.incrementProtocolCount(ipAddressSender, "ICMPv6");
- }
- }
- }
- void pcap_processor::write_to_database(std::string database_path) {
- stats.writeToDatabase(database_path);
- }
- bool inline pcap_processor::file_exists(const std::string &filePath) {
- struct stat buffer;
- return stat(filePath.c_str(), &buffer) == 0;
- }
- #include <boost/python.hpp>
- using namespace boost::python;
- BOOST_PYTHON_MODULE (libpcapreader) {
- class_<pcap_processor>("pcap_processor", init<std::string>())
- .def("merge_pcaps", &pcap_processor::merge_pcaps)
- .def("collect_statistics", &pcap_processor::collect_statistics)
- .def("get_timestamp_mu_sec", &pcap_processor::get_timestamp_mu_sec)
- .def("write_to_database", &pcap_processor::write_to_database);
- }
|