PcapFile.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import hashlib
  2. import os.path
  3. import sys
  4. import ID2TLib.libpcapreader as pr
  5. class PcapFile(object):
  6. def __init__(self, pcap_file_path: str):
  7. """
  8. Creates a new PcapFile associated to the PCAP file at pcap_file_path.
  9. :param pcap_file_path: The path to the PCAP file
  10. """
  11. self.pcap_file_path = pcap_file_path
  12. def merge_attack(self, attack_pcap_path: str):
  13. """
  14. Merges the loaded PCAP with the PCAP at attack_pcap_path.
  15. :param attack_pcap_path: The path to the PCAP file to merge with the PCAP at pcap_file_path
  16. :return: The file path of the resulting PCAP file
  17. """
  18. print("Merging base PCAP with attack PCAP...", end=" ")
  19. sys.stdout.flush() # force python to print text immediately
  20. pcap = pr.pcap_processor(self.pcap_file_path)
  21. file_out_path = pcap.merge_pcaps(attack_pcap_path)
  22. print("done.")
  23. return file_out_path
  24. def get_file_hash(self):
  25. """
  26. Returns the hash for the loaded PCAP file. The hash is calculated bsaed on:
  27. - the file size in bytes
  28. - the first 224*40000 bytes of the file
  29. :return: The hash for the PCAP file as string.
  30. """
  31. # Blocksize in bytes
  32. const_blocksize = 224
  33. # Number of blocks to read at beginning of file
  34. const_max_blocks_read = 40000
  35. # Initialize required variables
  36. hasher = hashlib.sha224()
  37. blocks_read = 0
  38. # Hash calculation
  39. with open(self.pcap_file_path, 'rb') as afile:
  40. # Add filename -> makes trouble when renaming the PCAP
  41. # hasher.update(afile.name.encode('utf-8'))
  42. # Add file's last modification date -> makes trouble when copying the PCAP
  43. # hasher.update(str(time.ctime(os.path.getmtime(self.pcap_file_path))).encode('utf-8'))
  44. # Add file size
  45. hasher.update(str(os.path.getsize(self.pcap_file_path)).encode('utf-8'))
  46. # Add max. first 40000 * 224 bytes = 8,5 MB of file
  47. buf = afile.read(const_blocksize)
  48. blocks_read += 1
  49. while len(buf) > 0 and blocks_read < const_max_blocks_read:
  50. hasher.update(buf)
  51. buf = afile.read(const_blocksize)
  52. blocks_read += 1
  53. return hasher.hexdigest()
  54. def get_db_path(self, root_directory: str = os.path.join(os.path.expanduser('~'), 'ID2T_data', 'db')):
  55. """
  56. Creates a path based on a hashed directory structure. Derives a hash code by the file's hash and derives
  57. thereof the database path.
  58. Code and idea based on:
  59. http://michaelandrews.typepad.com/the_technical_times/2009/10/creating-a-hashed-directory-structure.html
  60. :param root_directory: The root directory of the hashed directory structure (optional)
  61. :return: The full path to the database file
  62. """
  63. def hashcode(input: str):
  64. """
  65. Creates a hashcode of a string, based on Java's hashcode implementation.
  66. Code based on: http://garage.pimentech.net/libcommonPython_src_python_libcommon_javastringhashcode/
  67. :param input: The string the hashcode should be calculated from
  68. :return: The hashcode as string
  69. """
  70. h = 0
  71. for c in input:
  72. h = (31 * h + ord(c)) & 0xFFFFFFFF
  73. return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
  74. file_hash = self.get_file_hash()
  75. hashcode = hashcode(file_hash)
  76. mask = 255
  77. dir_first_level = hashcode & mask
  78. dir_second_level = (hashcode >> 8) & mask
  79. return os.path.join(root_directory, str(dir_first_level), str(dir_second_level), file_hash[0:12] + ".sqlite3")