Utility.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from random import randint
  2. from datetime import datetime, timedelta, tzinfo
  3. from calendar import timegm
  4. from lea import Lea
  5. from scapy.layers.netbios import *
  6. platforms = {"win7", "win10", "winxp", "win8.1", "macos", "linux", "win8", "winvista", "winnt", "win2000"}
  7. def get_rnd_os():
  8. """
  9. Chooses random platform over an operating system probability distribution
  10. :return: random platform as string
  11. """
  12. os_dist = Lea.fromValFreqsDict({"win7": 48.43, "win10": 27.99, "winxp": 6.07, "win8.1": 6.07, "macos": 5.94,
  13. "linux": 3.38, "win8": 1.35, "winvista": 0.46, "winnt": 0.31})
  14. return os_dist.random()
  15. def check_platform(platform: str):
  16. """
  17. Checks if the given platform is currently supported
  18. if not exits with error
  19. :param platform: the platform, which should be validated
  20. """
  21. if platform not in platforms:
  22. print("\nERROR: Invalid platform: " + platform + "." +
  23. "\n Please select one of the following platforms: ", platforms)
  24. exit(1)
  25. def get_ip_range(start_ip: str, end_ip: str):
  26. """
  27. Generates a list of IPs of a given range. If the start_ip is greater than the end_ip, the reverse range is generated
  28. :param start_ip: the start_ip of the desired IP-range
  29. :param end_ip: the end_ip of the desired IP-range
  30. :return: a list of all IPs in the desired IP-range, including start-/end_ip
  31. """
  32. start = ipaddress.ip_address(start_ip)
  33. end = ipaddress.ip_address(end_ip)
  34. ips = []
  35. if start < end:
  36. while start <= end:
  37. ips.append(start.exploded)
  38. start = start+1
  39. elif start > end:
  40. while start >= end:
  41. ips.append(start.exploded)
  42. start = start-1
  43. else:
  44. ips.append(start_ip)
  45. return ips
  46. def generate_source_port_from_platform(platform: str, previousPort=0):
  47. """
  48. Generates the next source port according to the TCP-port-selection strategy of the given platform
  49. :param platform: the platform for which to generate source ports
  50. :param previousPort: the previously used/generated source port. Must be 0 if no port was generated before
  51. :return: the next source port for the given platform
  52. """
  53. check_platform(platform)
  54. if platform in {"winnt", "winxp", "win2000"}:
  55. if (previousPort == 0) or (previousPort + 1 > 5000):
  56. return randint(1024, 5000)
  57. else:
  58. return previousPort + 1
  59. elif platform == "linux":
  60. return randint(32768, 61000)
  61. else:
  62. if (previousPort == 0) or (previousPort + 1 > 65535):
  63. return randint(49152, 65535)
  64. else:
  65. return previousPort + 1
  66. def get_filetime_format(timestamp):
  67. """
  68. Converts a timestamp into MS FILETIME format
  69. :param timestamp: a timestamp in seconds
  70. :return: MS FILETIME timestamp
  71. """
  72. boot_datetime = datetime.fromtimestamp(timestamp)
  73. if boot_datetime.tzinfo is None or boot_datetime.tzinfo.utcoffset(boot_datetime) is None:
  74. boot_datetime = boot_datetime.replace(tzinfo=boot_datetime.tzname())
  75. boot_filetime = 116444736000000000 + (timegm(boot_datetime.timetuple()) * 10000000)
  76. return boot_filetime + (boot_datetime.microsecond * 10)
  77. def get_rnd_boot_time(timestamp, platform="winxp"):
  78. """
  79. Generates a random boot time based on a given timestamp and operating system
  80. :param timestamp: a timestamp in seconds
  81. :param platform: a platform as string as specified in check_platform above. default is winxp. this param is optional
  82. :return: timestamp of random boot time in seconds since EPOCH
  83. """
  84. check_platform(platform)
  85. if platform is "linux":
  86. uptime_in_days = Lea.fromValFreqsDict({3: 50, 7: 25, 14: 12.5, 31: 6.25, 92: 3.125, 183: 1.5625,
  87. 365: 0.78125, 1461: 0.390625, 2922: 0.390625})
  88. elif platform is "macos":
  89. uptime_in_days = Lea.fromValFreqsDict({7: 50, 14: 25, 31: 12.5, 92: 6.25, 183: 3.125, 365: 3.076171875,
  90. 1461: 0.048828125})
  91. else:
  92. uptime_in_days = Lea.fromValFreqsDict({3: 50, 7: 25, 14: 12.5, 31: 6.25, 92: 3.125, 183: 1.5625,
  93. 365: 0.78125, 1461: 0.78125})
  94. timestamp -= randint(0, uptime_in_days.random()*86400)
  95. return timestamp