Lib.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import hashlib
  3. from definitions import ROOT_DIR
  4. from random import randint
  5. # TODO: generate better test pcap (1000-2000 packets)
  6. test_resource_dir = ROOT_DIR + "/../resources/test"
  7. test_pcap = ROOT_DIR + "/../resources/test/test.pcap"
  8. test_pcap_ips = ["192.168.189.143", "192.168.189.1"]
  9. """
  10. helper functions for generic_test
  11. """
  12. def get_sha256(file):
  13. """
  14. Generates a sha256 checksum from file
  15. :param file: absolute path to file
  16. :return: sha256 checksum
  17. """
  18. sha = hashlib.sha256()
  19. with open(file, 'rb') as f:
  20. while True:
  21. data = f.read(0x100000)
  22. if not data:
  23. break
  24. sha.update(data)
  25. f.close()
  26. return sha.hexdigest()
  27. def clean_up(controller):
  28. """
  29. Removes the output files from a given controller
  30. :param controller: controller which created output files
  31. """
  32. os.remove(controller.pcap_dest_path)
  33. os.remove(controller.label_manager.label_file_path)
  34. """
  35. function patches for unittests
  36. """
  37. def get_bytes(count, ignore):
  38. """
  39. unittest patch for get_rnd_bytes (ID2TLib.Utility.py)
  40. :param count: count of requested bytes
  41. :param ignore: <not used>
  42. :return: a count of As
  43. """
  44. return b'A' * count
  45. def get_x86_nop(count, side_effect_free, char_filter):
  46. """
  47. unittest patch for get_rnd_x86_nop (ID2TLib.Utility.py)
  48. :param count: count of requested nops
  49. :param side_effect_free: <not used>
  50. :param char_filter: <not used>
  51. :return: a count of \x90
  52. """
  53. return b'\x90' * count
  54. def get_win_size(pkts_num):
  55. result = []
  56. for i in range(0, pkts_num):
  57. result.append(10)
  58. return result
  59. def get_attacker_config(ip_source_list, ipAddress: str):
  60. next_port = randint(0, 2 ** 16 - 1)
  61. ttl = randint(1, 255)
  62. return next_port, ttl