Browse Source

Merge branch 'beautify_SKIP_CI' of stefan.schmidt/ID2T-toolkit into master

Carlos Garcia 6 years ago
parent
commit
cd077b4b1c
6 changed files with 47 additions and 26 deletions
  1. 4 0
      .gitignore
  2. 4 2
      build.sh
  3. 3 2
      code/Core/Controller.py
  4. 15 0
      code/Core/Statistics.py
  5. 2 3
      code/Test/ID2TAttackTest.py
  6. 19 19
      code_boost/src/cxx/pcap_processor.cpp

+ 4 - 0
.gitignore

@@ -34,3 +34,7 @@ resources/test/ID2T_results
 id2t
 run_tests
 test_efficiency
+
+# memory_profiler
+*.png
+*.dat

+ 4 - 2
build.sh

@@ -88,6 +88,7 @@ ID2T_DIR=\$(readlink -f \$0)
 SCRIPT_PATH=\${ID2T_DIR%/*}
 TEST_DIR=\${SCRIPT_PATH}/resources/test/
 TEST_PCAP=\${TEST_DIR}reference_1998.pcap
+PLOT_DIR=\${TEST_DIR}/plot/
 cd \${SCRIPT_PATH}/code
 error=0
 # Execute tests
@@ -95,6 +96,7 @@ set +e
 python3 -m unittest Test/efficiency_testing.py
 error=\$?
 cd \$SCRIPT_PATH
+mkdir \$PLOT_DIR
 smbloris="SMBLorisAttack attackers.count=4 packets.per-second=8.0"
 smbscan1="SMBScanAttack ip.src=192.168.178.1 ip.dst=192.168.178.10-192.168.179.253"
 smbscan2="SMBScanAttack ip.src=192.168.178.1 ip.dst=192.168.178.10-192.168.178.109 hosting.ip=192.168.178.10-192.168.178.109"
@@ -109,8 +111,8 @@ ms17="MS17Scan ip.src=192.168.178.1"
 eb="EternalBlue"
 for i in "\$smbloris" "\$smbscan1" "\$smbscan2" "\$ftp" "\$porto" "\$portc" "\$sqli" "\$joomla" "\$sality" "\$ddos" "\$ms17" "\$eb"; do
     mprof run ./id2t -i \${TEST_PCAP} -a \${i}
-    mprof plot -t "\${i}" -o "\${TEST_DIR}\${i}.png"
-    mv mprofile_* "\${TEST_DIR}\${i}.dat"
+    mprof plot -t "\${i}" -o "\${PLOT_DIR}\${i}.png"
+    mv mprofile_* "\${PLOT_DIR}\${i}.dat"
 done
 echo "\nPlotted images can be found in \"\${TEST_DIR}\"."
 echo "By executing \"mprof plot <file>.dat\" you can get a more detailed look."

+ 3 - 2
code/Core/Controller.py

@@ -12,7 +12,7 @@ import Core.StatsDatabase as StatsDB
 
 
 class Controller:
-    def __init__(self, pcap_file_path: str, do_extra_tests: bool, non_verbose: bool):
+    def __init__(self, pcap_file_path: str, do_extra_tests: bool, non_verbose: bool=True):
         """
         Creates a new Controller, acting as a central coordinator for the whole application.
 
@@ -123,7 +123,7 @@ class Controller:
         print('\nOutput files created: \n', self.pcap_dest_path, '\n', self.label_manager.label_file_path)
 
         # print summary statistics
-        if not self.non_verbose:
+        if not self.non_verbose and len(attacks_config) is not 1:
             self.statistics.stats_summary_post_attack(self.added_packets)
 
     def process_db_queries(self, query, print_results=False):
@@ -332,6 +332,7 @@ class Controller:
         """
         Plots the statistics to a file by using the given customization parameters.
         """
+        print("Statistical plots are being generated", end="", flush=True)
         if params is not None and params[0] is not None:
             # FIXME: cleanup
             params_dict = dict([z.split("=") for z in params])

+ 15 - 0
code/Core/Statistics.py

@@ -1178,21 +1178,36 @@ class Statistics:
                 return out
 
         ttl_out_path = plot_ttl('.' + file_format)
+        print(".", end="", flush=True)
         mss_out_path = plot_mss('.' + file_format)
+        print(".", end="", flush=True)
         win_out_path = plot_win('.' + file_format)
+        print(".", end="", flush=True)
         protocol_out_path = plot_protocol('.' + file_format)
+        print(".", end="", flush=True)
         plot_interval_pktCount = plot_interval_pkt_count('.' + file_format)
+        print(".", end="", flush=True)
         if entropy:
             plot_interval_ip_src_ent = plot_interval_ip_src_ent('.' + file_format)
+            print(".", end="", flush=True)
             plot_interval_ip_dst_ent = plot_interval_ip_dst_ent('.' + file_format)
+            print(".", end="", flush=True)
             plot_interval_ip_src_cum_ent = plot_interval_ip_src_cum_ent('.' + file_format)
+            print(".", end="", flush=True)
             plot_interval_ip_dst_cum_ent = plot_interval_ip_dst_cum_ent('.' + file_format)
+            print(".", end="", flush=True)
         plot_interval_new_ip = plot_interval_new_ip('.' + file_format)
+        print(".", end="", flush=True)
         plot_interval_new_port = plot_interval_new_port('.' + file_format)
+        print(".", end="", flush=True)
         plot_interval_new_ttl = plot_interval_new_ttl('.' + file_format)
+        print(".", end="", flush=True)
         plot_interval_new_tos = plot_interval_new_tos('.' + file_format)
+        print(".", end="", flush=True)
         plot_interval_new_win_size = plot_interval_new_win_size('.' + file_format)
+        print(".", end="", flush=True)
         plot_interval_new_mss = plot_interval_new_mss('.' + file_format)
+        print(" done.")
 
         # Time consuming plot
         # port_out_path = plot_port('.' + format)

+ 2 - 3
code/Test/ID2TAttackTest.py

@@ -50,9 +50,8 @@ class ID2TAttackTest(unittest.TestCase):
             Lib.rename_test_result_files(controller, caller_function, attack_sub_dir, test_sub_dir)
 
     def temporal_efficiency_test(self, attack_args, time_limit=15, factor=1, seed=None, cleanup=True,
-                                 pcap=Lib.test_pcap,
-                                 flag_write_file=False, flag_recalculate_stats=False, flag_print_statistics=False,
-                                 attack_sub_dir=True, test_sub_dir=True):
+                                 pcap=Lib.test_pcap, flag_write_file=False, flag_recalculate_stats=False,
+                                 flag_print_statistics=False, attack_sub_dir=True, test_sub_dir=True):
         """
         Runs the attack with given aruments and monitors time efficiency.
 

+ 19 - 19
code_boost/src/cxx/pcap_processor.cpp

@@ -97,8 +97,8 @@ std::string pcap_processor::merge_pcaps(const std::string pcap_path) {
             }
             iterator_base++;
         }
-    }    
-    
+    }
+
     // This may happen if the base PCAP is smaller than the attack PCAP
     // In this case append the remaining packets of the attack PCAP
     for (; iterator_attack != sniffer_attack.end(); iterator_attack++) {
@@ -121,8 +121,8 @@ void pcap_processor::collect_statistics() {
         std::cout << "Loading pcap..." << std::endl;
         FileSniffer sniffer(filePath);
         FileSniffer snifferOverview(filePath);
-        
-        SnifferIterator i = sniffer.begin();                
+
+        SnifferIterator i = sniffer.begin();
         std::chrono::microseconds currentPktTimestamp;
 
         // Save timestamp of first packet
@@ -135,10 +135,10 @@ void pcap_processor::collect_statistics() {
         std::chrono::microseconds firstTimestamp = stats.getTimestampFirstPacket();
 
         // An empty loop to know the capture duration, then choose a suitable time interval
-        SnifferIterator lastpkt; 
+        SnifferIterator lastpkt;
         for (SnifferIterator j = snifferOverview.begin(); j != snifferOverview.end(); ++j, ++totalPackets) {lastpkt = j;}
 
-        std::chrono::microseconds lastTimestamp = lastpkt->timestamp();                  
+        std::chrono::microseconds lastTimestamp = lastpkt->timestamp();
         std::chrono::microseconds captureDuration = lastTimestamp - firstTimestamp;
         if(captureDuration.count()<=0){
             std::cout<<"ERROR: PCAP file is empty!"<<"\n";
@@ -179,9 +179,12 @@ void pcap_processor::collect_statistics() {
         }
 
         std::cout << "\n";
-
         // Save timestamp of last packet into statistics
         stats.setTimestampLastPacket(currentPktTimestamp);
+
+        if(hasUnrecognized) {
+            std::cout << "Unrecognized PDUs detected: Check 'unrecognized_pdus' table!" << std::endl;
+        }
     }
 }
 
@@ -209,7 +212,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
     const PDU::PDUType pdu_l3_type = pdu_l3->pdu_type();
     std::string ipAddressSender;
     std::string ipAddressReceiver;
-    
+
     // PDU is IPv4
     if (pdu_l3_type == PDU::PDUType::IP) {
         const IP &ipLayer = (const IP &) *pdu_l3;
@@ -232,7 +235,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
         // Assign IP Address to MAC Address
         stats.assignMacAddress(ipAddressSender, macAddressSender);
         stats.assignMacAddress(ipAddressReceiver, macAddressReceiver);
-        
+
     } // PDU is IPv6
     else if (pdu_l3_type == PDU::PDUType::IPv6) {
         const IPv6 &ipLayer = (const IPv6 &) *pdu_l3;
@@ -255,10 +258,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
 
     } //PDU is unrecognized
     else {
-        if(!hasUnrecognized) {
-            std::cerr << "Unrecognized PDUs detected: Check 'unrecognized_pdus' table!" << std::endl;
-            hasUnrecognized = true;
-        }
+        hasUnrecognized = true;
 
         EthernetII eth = (const EthernetII &) *pdu_l2;
         Tins::Timestamp ts = pkt.timestamp();
@@ -271,8 +271,8 @@ void pcap_processor::process_packets(const Packet &pkt) {
     const PDU *pdu_l4 = pdu_l3->inner_pdu();
     if (pdu_l4 != 0) {
         // Protocol distribution - layer 4
-        PDU::PDUType p = pdu_l4->pdu_type();  
-        
+        PDU::PDUType p = pdu_l4->pdu_type();
+
         // Check for IPv4: payload
         if (pdu_l3_type == PDU::PDUType::IP) {
             stats.checkPayload(pdu_l4);
@@ -280,7 +280,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
 
         if (p == PDU::PDUType::TCP) {
             TCP tcpPkt = (const TCP &) *pdu_l4;
-            
+
           // Check TCP checksum
           if (pdu_l3_type == PDU::PDUType::IP) {
             stats.checkTCPChecksum(ipAddressSender, ipAddressReceiver, tcpPkt);
@@ -296,7 +296,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
             int win = tcpPkt.window();
             stats.incrementWinCount(ipAddressSender, win);
 
-            try {                                                                
+            try {
                 int val = tcpPkt.mss();
 
                 // MSS distribution
@@ -306,7 +306,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
             }
             stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), "TCP");
             stats.increasePortByteCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), sizeCurrentPacket, "TCP");
-            
+
           // UDP Packet
         } else if (p == PDU::PDUType::UDP) {
             const UDP udpPkt = (const UDP &) *pdu_l4;
@@ -314,7 +314,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
             stats.increaseProtocolByteCount(ipAddressSender, "UDP", sizeCurrentPacket);
             stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), "UDP");
             stats.increasePortByteCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), sizeCurrentPacket, "UDP");
-          
+
         } else if (p == PDU::PDUType::ICMP) {
             stats.incrementProtocolCount(ipAddressSender, "ICMP");
             stats.increaseProtocolByteCount(ipAddressSender, "ICMP", sizeCurrentPacket);