Statistics.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import os
  2. import time
  3. import ID2TLib.libpcapreader as pr
  4. import matplotlib.pyplot as plt
  5. from ID2TLib.PcapFile import PcapFile
  6. from ID2TLib.StatsDatabase import StatsDatabase
  7. class Statistics:
  8. def __init__(self, pcap_file: PcapFile):
  9. """
  10. Creates a new Statistics object.
  11. :param pcap_file: A reference to the PcapFile object
  12. """
  13. # Fields
  14. self.pcap_filepath = pcap_file.pcap_file_path
  15. self.pcap_proc = None
  16. # Create folder for statistics database if required
  17. self.path_db = pcap_file.get_db_path()
  18. path_dir = os.path.dirname(self.path_db)
  19. if not os.path.isdir(path_dir):
  20. os.makedirs(path_dir)
  21. # Class instances
  22. self.stats_db = StatsDatabase(self.path_db)
  23. def load_pcap_statistics(self, flag_write_file: bool, flag_recalculate_stats: bool, flag_print_statistics: bool):
  24. """
  25. Loads the PCAP statistics for the file specified by pcap_filepath. If the database is not existing yet, the
  26. statistics are calculated by the PCAP file processor and saved into the newly created database. Otherwise the
  27. statistics are gathered directly from the existing database.
  28. :param flag_write_file: Indicates whether the statistics should be written addiotionally into a text file (True)
  29. or not (False)
  30. :param flag_recalculate_stats: Indicates whether eventually existing statistics should be recalculated
  31. :param flag_print_statistics: Indicates whether the gathered basic statistics should be printed to the terminal
  32. """
  33. # Load pcap and get loading time
  34. time_start = time.clock()
  35. # Inform user about recalculation of statistics and its reason
  36. if flag_recalculate_stats:
  37. print("Flag -r/--recalculate found. Recalculating statistics.")
  38. # Recalculate statistics if database not exists OR param -r/--recalculate was provided
  39. if (not self.stats_db.get_db_exists()) or flag_recalculate_stats:
  40. self.pcap_proc = pr.pcap_processor(self.pcap_filepath)
  41. self.pcap_proc.collect_statistics()
  42. self.pcap_proc.write_to_database(self.path_db)
  43. outstring_datasource = "by PCAP file processor."
  44. else:
  45. outstring_datasource = "from statistics database."
  46. # Load statistics from database
  47. self.file_info = self.stats_db.get_file_info()
  48. time_end = time.clock()
  49. print("Loaded file statistics in " + str(time_end - time_start)[:4] + " sec " + outstring_datasource)
  50. # Write statistics if param -e/--export provided
  51. if flag_write_file:
  52. self.write_statistics_to_file()
  53. # Print statistics if param -s/--statistics provided
  54. if flag_print_statistics:
  55. self.print_statistics()
  56. def get_file_information(self):
  57. """
  58. Returns a list of tuples, each containing a information of the file.
  59. :return: a list of tuples, each consisting of (description, value, unit), where unit is optional.
  60. """
  61. return [("Pcap file", self.pcap_filepath),
  62. ("#Packets", self.get_packet_count(), "packets"),
  63. ("Capture length", self.get_capture_duration(), "seconds"),
  64. ("Capture start", self.get_pcap_timestamp_start()),
  65. ("Capture end", self.get_pcap_timestamp_end())]
  66. def get_general_file_statistics(self):
  67. """
  68. Returns a list of tuples, each containing a file statistic.
  69. :return: a list of tuples, each consisting of (description, value, unit).
  70. """
  71. return [("Avg. packet rate", self.file_info['avgPacketRate'], "packets/sec"),
  72. ("Avg. packet size", self.file_info['avgPacketSize'], "kbytes"),
  73. ("Avg. packets sent", self.file_info['avgPacketsSentPerHost'], "packets"),
  74. ("Avg. bandwidth in", self.file_info['avgBandwidthIn'], "kbit/s"),
  75. ("Avg. bandwidth out", self.file_info['avgBandwidthOut'], "kbit/s")]
  76. @staticmethod
  77. def write_list(desc_val_unit_list, func, line_ending="\n"):
  78. """
  79. Takes a list of tuples (statistic name, statistic value, unit) as input, generates a string of these three values
  80. and applies the function func on this string.
  81. Before generating the string, it identifies text containing a float number, casts the string to a
  82. float and rounds the value to two decimal digits.
  83. :param desc_val_unit_list: The list of tuples consisting of (description, value, unit)
  84. :param func: The function to be applied to each generated string
  85. :param line_ending: The formatting string to be applied at the end of each string
  86. """
  87. for entry in desc_val_unit_list:
  88. # Convert text containing float into float
  89. (description, value) = entry[0:2]
  90. if isinstance(value, str) and "." in value:
  91. try:
  92. value = float(value)
  93. except ValueError:
  94. pass # do nothing -> value was not a float
  95. # round float
  96. if isinstance(value, float):
  97. value = round(value, 2)
  98. # write into file
  99. if len(entry) == 3:
  100. unit = entry[2]
  101. func(description + ":\t" + str(value) + " " + unit + line_ending)
  102. else:
  103. func(description + ":\t" + str(value) + line_ending)
  104. def print_statistics(self):
  105. """
  106. Prints the basic file statistics to the terminal.
  107. """
  108. print("\nPCAP FILE INFORMATION ------------------------------")
  109. Statistics.write_list(self.get_file_information(), print, "")
  110. print("\nGENERAL FILE STATISTICS ----------------------------")
  111. Statistics.write_list(self.get_general_file_statistics(), print, "")
  112. print("\n")
  113. def write_statistics_to_file(self):
  114. """
  115. Writes the calculated basic statistics into a file.
  116. """
  117. def _write_header(title: str):
  118. """
  119. Writes the section header into the open file.
  120. :param title: The section title
  121. """
  122. target.write("====================== \n")
  123. target.write(title + " \n")
  124. target.write("====================== \n")
  125. target = open(self.pcap_filepath + ".stat", 'w')
  126. target.truncate()
  127. _write_header("PCAP file information")
  128. Statistics.write_list(self.get_file_information(), target.write)
  129. _write_header("General statistics")
  130. Statistics.write_list(self.get_general_file_statistics(), target.write)
  131. target.close()
  132. def get_capture_duration(self):
  133. """
  134. :return: The duration of the capture in seconds
  135. """
  136. return self.file_info['captureDuration']
  137. def get_pcap_timestamp_start(self):
  138. """
  139. :return: The timestamp of the first packet in the PCAP file
  140. """
  141. return self.file_info['timestampFirstPacket']
  142. def get_pcap_timestamp_end(self):
  143. """
  144. :return: The timestamp of the last packet in the PCAP file
  145. """
  146. return self.file_info['timestampLastPacket']
  147. def get_pps_sent(self, ip_address: str):
  148. """
  149. Calculates the sent packets per seconds for a given IP address.
  150. :param ip_address: The IP address whose packets per second should be calculated
  151. :return: The sent packets per seconds for the given IP address
  152. """
  153. packets_sent = self.stats_db.process_db_query("SELECT pktsSent from ip_statistics WHERE ipAddress=?", False,
  154. (ip_address,))
  155. capture_duration = float(self.get_capture_duration())
  156. return int(float(packets_sent) / capture_duration)
  157. def get_pps_received(self, ip_address: str):
  158. """
  159. Calculate the packets per second received for a given IP address.
  160. :param ip_address: The IP address used for the calculation
  161. :return: The number of packets per second received
  162. """
  163. packets_received = self.stats_db.process_db_query("SELECT pktsReceived FROM ip_statistics WHERE ipAddress=?",
  164. False,
  165. (ip_address,))
  166. capture_duration = float(self.get_capture_duration())
  167. return int(float(packets_received) / capture_duration)
  168. def get_packet_count(self):
  169. """
  170. :return: The number of packets in the loaded PCAP file
  171. """
  172. return self.file_info['packetCount']
  173. def get_most_used_ip_address(self):
  174. """
  175. :return: The IP address/addresses with the highest sum of packets sent and received
  176. """
  177. return self.process_db_query("most_used(ipAddress)")
  178. def get_ttl_distribution(self, ipAddress: str):
  179. result = self.process_db_query('SELECT ttlValue, ttlCount from ip_ttl WHERE ipAddress="' + ipAddress + '"')
  180. result_dict = {key: value for (key, value) in result}
  181. return result_dict
  182. def get_random_ip_address(self, count: int = 1):
  183. """
  184. :param count: The number of IP addreses to return
  185. :return: A randomly chosen IP address from the dataset or iff param count is greater than one, a list of randomly
  186. chosen IP addresses
  187. """
  188. if count == 1:
  189. return self.process_db_query("random(all(ipAddress))")
  190. else:
  191. ip_address_list = []
  192. for i in range(0, count):
  193. ip_address_list.append(self.process_db_query("random(all(ipAddress))"))
  194. return ip_address_list
  195. def get_mac_address(self, ipAddress: str):
  196. """
  197. :return: The MAC address used in the dataset for the given IP address.
  198. """
  199. return self.process_db_query('macAddress(ipAddress=' + ipAddress + ")")
  200. def get_mss(self, ipAddress: str):
  201. """
  202. :param ipAddress: The IP address whose used MSS should be determined
  203. :return: The TCP MSS value used by the IP address, or if the IP addresses never specified a MSS,
  204. then None is returned
  205. """
  206. mss_value = self.process_db_query('SELECT mss from tcp_mss WHERE ipAddress="' + ipAddress + '"')
  207. if isinstance(mss_value, int):
  208. return mss_value
  209. else:
  210. return None
  211. def get_statistics_database(self):
  212. """
  213. :return: A reference to the statistics database object
  214. """
  215. return self.stats_db
  216. def process_db_query(self, query_string_in: str, print_results: bool = False):
  217. """
  218. Executes a string identified previously as a query. This can be a standard SQL SELECT/INSERT query or a named
  219. query.
  220. :param query_string_in: The query to be processed
  221. :param print_results: Indicates whether the results should be printed to terminal
  222. :return: The result of the query
  223. """
  224. return self.stats_db.process_db_query(query_string_in, print_results)
  225. def is_query(self, value: str):
  226. """
  227. Checks whether the given string is a standard SQL query (SELECT, INSERT) or a named query.
  228. :param value: The string to be checked
  229. :return: True if the string is recognized as a query, otherwise False.
  230. """
  231. if not isinstance(value, str):
  232. return False
  233. else:
  234. return (any(x in value.lower().strip() for x in self.stats_db.get_all_named_query_keywords()) or
  235. any(x in value.lower().strip() for x in self.stats_db.get_all_sql_query_keywords()))
  236. def plot_statistics(self, format: str = 'png'):
  237. """
  238. Plots the statistics associated with the dataset prior attack injection.
  239. :param format: The format to be used to save the statistics diagrams.
  240. """
  241. def plot_ttl(file_ending: str):
  242. result = self.stats_db._process_user_defined_query(
  243. "SELECT ttlValue, SUM(ttlCount) FROM ip_ttl GROUP BY ttlValue")
  244. graphx, graphy = [], []
  245. for row in result:
  246. graphx.append(row[0])
  247. graphy.append(row[1])
  248. plt.autoscale(enable=True, axis='both')
  249. plt.title("TTL Distribution")
  250. plt.xlabel('TTL Value')
  251. plt.ylabel('Number of Packets')
  252. width = 0.5
  253. plt.xlim([0, max(graphx)])
  254. plt.grid(True)
  255. plt.bar(graphx, graphy, width, align='center', linewidth=2, color='red', edgecolor='red')
  256. out = self.pcap_filepath.replace('.pcap', '_plot-ttl' + file_ending)
  257. plt.savefig(out)
  258. return out
  259. out_path = plot_ttl('.' + format)
  260. print("Saved TTL distribution plot at: ", out_path)