StatsDatabase.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import os.path
  2. import re
  3. import sqlite3
  4. import sys
  5. from random import randint
  6. def dict_gen(curs: sqlite3.Cursor):
  7. """
  8. Generates a dictionary of a sqlite3.Cursor object by fetching the query's results.
  9. Taken from Python Essential Reference by David Beazley.
  10. """
  11. field_names = [d[0] for d in curs.description]
  12. while True:
  13. rows = curs.fetchmany()
  14. if not rows:
  15. return
  16. for row in rows:
  17. yield dict(zip(field_names, row))
  18. class StatsDatabase:
  19. def __init__(self, db_path: str):
  20. """
  21. Creates a new StatsDatabase.
  22. :param db_path: The path to the database file
  23. """
  24. self.existing_db = os.path.exists(db_path)
  25. self.database = sqlite3.connect(db_path)
  26. self.cursor = self.database.cursor()
  27. # If DB not existing, create a new DB scheme
  28. if self.existing_db:
  29. print('Located statistics database at: ', db_path)
  30. else:
  31. print('Statistics database not found. Creating new database at: ', db_path)
  32. def get_file_info(self):
  33. """
  34. Retrieves general file statistics from the database. This includes:
  35. - packetCount : Number of packets in the PCAP file
  36. - captureDuration : Duration of the packet capture in seconds
  37. - timestampFirstPacket : Timestamp of the first captured packet
  38. - timestampLastPacket : Timestamp of the last captured packet
  39. - avgPacketRate : Average packet rate
  40. - avgPacketSize : Average packet size
  41. - avgPacketsSentPerHost : Average number of packets sent per host
  42. - avgBandwidthIn : Average incoming bandwidth
  43. - avgBandwidthOut : Average outgoing bandwidth
  44. :return: a dictionary of keys (see above) and their respective values
  45. """
  46. return [r for r in dict_gen(
  47. self.cursor.execute('SELECT * FROM file_statistics'))][0]
  48. def get_db_exists(self):
  49. """
  50. :return: True if the database was already existent, otherwise False
  51. """
  52. # Aidmar - for testing: return false always
  53. #return self.existing_db
  54. return False
  55. @staticmethod
  56. def _get_selector_keywords():
  57. """
  58. :return: a list of selector keywords
  59. """
  60. return ['most_used', 'least_used', 'avg', 'all']
  61. @staticmethod
  62. def _get_parametrized_selector_keywords():
  63. """
  64. :return: a list of parameterizable selector keywords
  65. """
  66. return ['ipaddress', 'macaddress']
  67. @staticmethod
  68. def _get_extractor_keywords():
  69. """
  70. :return: a list of extractor keywords
  71. """
  72. return ['random', 'first', 'last']
  73. def get_all_named_query_keywords(self):
  74. """
  75. :return: a list of all named query keywords, used to identify named queries
  76. """
  77. return (
  78. self._get_selector_keywords() + self._get_parametrized_selector_keywords() + self._get_extractor_keywords())
  79. @staticmethod
  80. def get_all_sql_query_keywords():
  81. """
  82. :return: a list of all supported SQL keywords, used to identify SQL queries
  83. """
  84. return ["select", "insert"]
  85. def _process_user_defined_query(self, query_string: str, query_parameters: tuple = None):
  86. """
  87. Takes as input a SQL query query_string and optional a tuple of parameters which are marked by '?' in the query
  88. and later substituted.
  89. :param query_string: The query to execute
  90. :param query_parameters: The tuple of parameters to inject into the query
  91. :return: the results of the query
  92. """
  93. if query_parameters is not None:
  94. self.cursor.execute(query_string, query_parameters)
  95. else:
  96. self.cursor.execute(query_string)
  97. self.database.commit()
  98. return self.cursor.fetchall()
  99. def get_field_types(self, *table_names):
  100. """
  101. Creates a dictionary whose keys are the fields of the given table(s) and whose values are the appropriate field
  102. types, like TEXT for strings and REAL for float numbers.
  103. :param table_names: The name of table(s)
  104. :return: a dictionary of {field_name : field_type} for fields of all tables
  105. """
  106. dic = {}
  107. for table in table_names:
  108. self.cursor.execute("PRAGMA table_info('%s')" % table)
  109. results = self.cursor.fetchall()
  110. for field in results:
  111. dic[field[1].lower()] = field[2]
  112. return dic
  113. def named_query_parameterized(self, keyword: str, param_op_val: list):
  114. """
  115. Executes a parameterizable named query.
  116. :param keyword: The query to be executed, like ipaddress or macadress
  117. :param param_op_val: A list consisting of triples with (parameter, operator, value)
  118. :return: the results of the executed query
  119. """
  120. named_queries = {
  121. "ipaddress": "SELECT DISTINCT ip_statistics.ipAddress from ip_statistics INNER JOIN ip_mac, ip_ttl, ip_ports, ip_protocols ON ip_statistics.ipAddress=ip_mac.ipAddress AND ip_statistics.ipAddress=ip_ttl.ipAddress AND ip_statistics.ipAddress=ip_ports.ipAddress AND ip_statistics.ipAddress=ip_protocols.ipAddress WHERE ",
  122. "macaddress": "SELECT DISTINCT macAddress from ip_mac WHERE "}
  123. query = named_queries.get(keyword)
  124. field_types = self.get_field_types('ip_mac', 'ip_ttl', 'ip_ports', 'ip_protocols', 'ip_statistics', 'ip_mac')
  125. conditions = []
  126. for key, op, value in param_op_val:
  127. # this makes sure that TEXT fields are queried by strings,
  128. # e.g. ipAddress=192.168.178.1 --is-converted-to--> ipAddress='192.168.178.1'
  129. if field_types.get(key) == 'TEXT':
  130. if not str(value).startswith("'") and not str(value).startswith('"'):
  131. value = "'" + value + "'"
  132. # this replacement is required to remove ambiguity in SQL query
  133. if key == 'ipAddress':
  134. key = 'ip_mac.ipAddress'
  135. conditions.append(key + op + str(value))
  136. where_clause = " AND ".join(conditions)
  137. query += where_clause
  138. self.cursor.execute(query)
  139. return self.cursor.fetchall()
  140. def _process_named_query(self, query_param_list):
  141. """
  142. Executes a named query.
  143. :param query_param_list: A query list consisting of (keyword, params), e.g. [(most_used, ipAddress), (random,)]
  144. :return: the result of the query
  145. """
  146. # Definition of SQL queries associated to named queries
  147. named_queries = {
  148. "most_used.ipaddress": "SELECT ipAddress FROM ip_statistics WHERE (pktsSent+pktsReceived) == (SELECT MAX(pktsSent+pktsReceived) from ip_statistics) LIMIT 1",
  149. "most_used.macaddress": "SELECT * FROM (SELECT macAddress, COUNT(*) as occ from ip_mac GROUP BY macAddress ORDER BY occ DESC) WHERE occ=(SELECT COUNT(*) as occ from ip_mac GROUP BY macAddress ORDER BY occ DESC LIMIT 1)",
  150. "most_used.portnumber": "SELECT portNumber, COUNT(portNumber) as cntPort FROM ip_ports GROUP BY portNumber HAVING cntPort=(SELECT MAX(cntPort) from (SELECT portNumber, COUNT(portNumber) as cntPort FROM ip_ports GROUP BY portNumber))",
  151. "most_used.protocolname": "SELECT protocolName, COUNT(protocolCount) as countProtocols FROM ip_protocols GROUP BY protocolName HAVING countProtocols=(SELECT COUNT(protocolCount) as cnt FROM ip_protocols GROUP BY protocolName ORDER BY cnt DESC LIMIT 1)",
  152. # Aidmar
  153. #"most_used.ttlvalue": "SELECT ttlValue FROM ip_ttl WHERE ttlCount == (SELECT MAX(ttlCount) FROM ip_ttl)",
  154. "most_used.ttlvalue": "SELECT ttlValue FROM ip_ttl GROUP BY ttlValue ORDER BY SUM(ttlCount) DESC LIMIT 1",
  155. "most_used.mssvalue": "SELECT mssValue FROM tcp_mss_dist GROUP BY mssValue ORDER BY SUM(mssCount) DESC LIMIT 1",
  156. "most_used.winsize": "SELECT winSize FROM tcp_syn_win GROUP BY winSize ORDER BY SUM(winCount) DESC LIMIT 1",
  157. "most_used.ipclass": "SELECT class FROM ip_statistics GROUP BY class ORDER BY COUNT(*) DESC LIMIT 1",
  158. "least_used.ipaddress": "SELECT ipAddress FROM ip_statistics WHERE (pktsSent+pktsReceived) == (SELECT MIN(pktsSent+pktsReceived) from ip_statistics)",
  159. "least_used.macaddress": "SELECT * FROM (SELECT macAddress, COUNT(*) as occ from ip_mac GROUP BY macAddress ORDER BY occ ASC) WHERE occ=(SELECT COUNT(*) as occ from ip_mac GROUP BY macAddress ORDER BY occ ASC LIMIT 1)",
  160. "least_used.portnumber": "SELECT portNumber, COUNT(portNumber) as cntPort FROM ip_ports GROUP BY portNumber HAVING cntPort=(SELECT MIN(cntPort) from (SELECT portNumber, COUNT(portNumber) as cntPort FROM ip_ports GROUP BY portNumber))",
  161. "least_used.protocolname": "SELECT protocolName, COUNT(protocolCount) as countProtocols FROM ip_protocols GROUP BY protocolName HAVING countProtocols=(SELECT COUNT(protocolCount) as cnt FROM ip_protocols GROUP BY protocolName ORDER BY cnt ASC LIMIT 1)",
  162. "least_used.ttlvalue": "SELECT ttlValue FROM ip_ttl WHERE ttlCount == (SELECT MIN(ttlCount) FROM ip_ttl)",
  163. "avg.pktsreceived": "SELECT avg(pktsReceived) from ip_statistics",
  164. "avg.pktssent": "SELECT avg(pktsSent) from ip_statistics",
  165. "avg.kbytesreceived": "SELECT avg(kbytesReceived) from ip_statistics",
  166. "avg.kbytessent": "SELECT avg(kbytesSent) from ip_statistics",
  167. "avg.ttlvalue": "SELECT avg(ttlValue) from ip_ttl",
  168. "avg.mss": "SELECT avg(mss) from tcp_mss",
  169. "all.ipaddress": "SELECT ipAddress from ip_statistics",
  170. "all.ttlvalue": "SELECT DISTINCT ttlValue from ip_ttl",
  171. "all.mss": "SELECT DISTINCT mss from tcp_mss",
  172. "all.macaddress": "SELECT DISTINCT macAddress from ip_mac",
  173. "all.portnumber": "SELECT DISTINCT portNumber from ip_ports",
  174. "all.protocolname": "SELECT DISTINCT protocolName from ip_protocols"}
  175. # Retrieve values by selectors, if given, reduce results by extractor
  176. last_result = 0
  177. for q in query_param_list:
  178. # if selector, like avg, ttl, is given
  179. if any(e in q[0] for e in self._get_selector_keywords()):
  180. (keyword, param) = q
  181. query = named_queries.get(keyword + "." + param)
  182. self.cursor.execute(str(query))
  183. last_result = self.cursor.fetchall()
  184. # if selector is parametrized, i.e. ipAddress(mac=AA:BB:CC:DD:EE) or macAddress(ipAddress=192.168.178.1)
  185. elif any(e in q[0] for e in self._get_parametrized_selector_keywords()) and any(
  186. o in q[1] for o in ["<", "=", ">", "<=", ">="]):
  187. (keyword, param) = q
  188. # convert string 'paramName1<operator1>paramValue1,paramName2<operator2>paramValue2,...' into list of triples
  189. param_op_val = [(key, op, value) for (key, op, value) in
  190. [re.split("(<=|>=|>|<|=)", x) for x in param.split(",")]]
  191. last_result = self.named_query_parameterized(keyword, param_op_val)
  192. # if extractor, like random, first, last, is given
  193. elif any(e in q[0] for e in self._get_extractor_keywords()) and (
  194. isinstance(last_result, list) or isinstance(last_result, tuple)):
  195. extractor = q[0]
  196. if extractor == 'random':
  197. index = randint(a=0, b=len(last_result) - 1)
  198. last_result = last_result[index]
  199. elif extractor == 'first':
  200. last_result = last_result[0]
  201. elif extractor == 'last':
  202. last_result = last_result[-1]
  203. return last_result
  204. def process_db_query(self, query_string_in: str, print_results=False, sql_query_parameters: tuple = None):
  205. """
  206. Processes a database query. This can either be a standard SQL query or a named query (predefined query).
  207. :param query_string_in: The string containing the query
  208. :param print_results: Indicated whether the results should be printed to terminal (True) or not (False)
  209. :param sql_query_parameters: Parameters for the SQL query (optional)
  210. :return: the results of the query
  211. """
  212. named_query_keywords = self.get_all_named_query_keywords()
  213. # Clean query_string
  214. query_string = query_string_in.lower().lstrip()
  215. # query_string is a user-defined SQL query
  216. result = None
  217. if sql_query_parameters is not None or query_string.startswith("select") or query_string.startswith("insert"):
  218. result = self._process_user_defined_query(query_string, sql_query_parameters)
  219. # query string is a named query -> parse it and pass it to statisticsDB
  220. elif any(k in query_string for k in named_query_keywords) and all(k in query_string for k in ['(', ')']):
  221. # Clean query_string
  222. query_string = query_string.replace(" ", "")
  223. # Validity check: Brackets
  224. brackets_open, brackets_closed = query_string.count("("), query_string.count(")")
  225. if not (brackets_open == brackets_closed):
  226. sys.stderr.write("Bracketing of given query '" + query_string + "' is incorrect.")
  227. # Parse query string into [ (query_keyword1, query_params1), ... ]
  228. delimiter_start, delimiter_end = "(", ")"
  229. kplist = []
  230. current_word = ""
  231. for char in query_string: # process characters one-by-one
  232. # if char is no delimiter, add char to current_word
  233. if char != delimiter_end and char != delimiter_start:
  234. current_word += char
  235. # if a start delimiter was found and the current_word so far is a keyword, add it to kplist
  236. elif char == delimiter_start:
  237. if current_word in named_query_keywords:
  238. kplist.append((current_word,))
  239. current_word = ""
  240. else:
  241. print("ERROR: Unrecognized keyword '" + current_word + "' found. Ignoring query.")
  242. return
  243. # else if characeter is end delimiter and there were no two directly following ending delimiters,
  244. # the current_word must be the parameters of an earlier given keyword
  245. elif char == delimiter_end and len(current_word) > 0:
  246. kplist[-1] += (current_word,)
  247. current_word = ""
  248. result = self._process_named_query(kplist[::-1])
  249. else:
  250. sys.stderr.write(
  251. "Query invalid. Only named queries and SQL SELECT/INSERT allowed. Please check the query's syntax!\n")
  252. return
  253. # If result is tuple/list with single element, extract value from list
  254. requires_extraction = (isinstance(result, list) or isinstance(result, tuple)) and len(result) == 1 and \
  255. (not isinstance(result[0], tuple) or len(result[0]) == 1)
  256. while requires_extraction:
  257. if isinstance(result, list) or isinstance(result, tuple):
  258. result = result[0]
  259. else:
  260. requires_extraction = False
  261. # If tuple of tuples or list of tuples, each consisting of single element is returned,
  262. # then convert it into list of values, because the returned colum is clearly specified by the given query
  263. if (isinstance(result, tuple) or isinstance(result, list)) and all(len(val) == 1 for val in result):
  264. result = [c for c in result for c in c]
  265. # Print results if option print_results is True
  266. if print_results:
  267. if len(result) == 1 and isinstance(result, list):
  268. result = result[0]
  269. print("Query returned 1 record:\n")
  270. for i in range(0, len(result)):
  271. print(str(self.cursor.description[i][0]) + ": " + str(result[i]))
  272. else:
  273. self._print_query_results(query_string_in, result)
  274. return result
  275. def _print_query_results(self, query_string_in: str, result):
  276. """
  277. Prints the results of a query.
  278. Based on http://stackoverflow.com/a/20383011/3017719.
  279. :param query_string_in: The query the results belong to
  280. :param result: The results of the query
  281. """
  282. # Print number of results according to type of result
  283. if isinstance(result, list):
  284. print("Query returned " + str(len(result)) + " records:\n")
  285. else:
  286. print("Query returned 1 record:\n")
  287. # Print query results
  288. if query_string_in.lstrip().upper().startswith(
  289. "SELECT") and result is not None and self.cursor.description is not None:
  290. widths = []
  291. columns = []
  292. tavnit = '|'
  293. separator = '+'
  294. for cd in self.cursor.description:
  295. widths.append(len(cd) + 10)
  296. columns.append(cd[0])
  297. for w in widths:
  298. tavnit += " %-" + "%ss |" % (w,)
  299. separator += '-' * w + '--+'
  300. print(separator)
  301. print(tavnit % tuple(columns))
  302. print(separator)
  303. if isinstance(result, list):
  304. for row in result:
  305. print(tavnit % row)
  306. else:
  307. print(tavnit % result)
  308. print(separator)
  309. else:
  310. print(result)