Utility.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. os_dist = Lea.fromValFreqsDict({"win7": 48.43, "win10": 27.99, "winxp": 6.07, "win8.1": 6.07, "macos": 5.94,
  9. "linux": 3.38, "win8": 1.35, "winvista": 0.46, "winnt": 0.31})
  10. return os_dist.random()
  11. def check_platform(platform: str):
  12. if platform not in platforms:
  13. print("\nERROR: Invalid platform: " + platform + "." +
  14. "\n Please select one of the following platforms: ", platforms)
  15. exit(1)
  16. def get_ip_range(start_ip: str, end_ip: str):
  17. start = ipaddress.ip_address(start_ip)
  18. end = ipaddress.ip_address(end_ip)
  19. ips = []
  20. if start < end:
  21. while start <= end:
  22. ips.append(start.exploded)
  23. start = start+1
  24. elif start > end:
  25. while start >= end:
  26. ips.append(start.exploded)
  27. start = start-1
  28. else:
  29. ips.append(start_ip)
  30. return ips
  31. def generate_source_port_from_platform(platform: str, previousPort=0):
  32. check_platform(platform)
  33. if platform in {"winnt", "winxp", "win2000"}:
  34. if (previousPort == 0) or (previousPort + 1 > 5000):
  35. return randint(1024, 5000)
  36. else:
  37. return previousPort + 1
  38. elif platform == "linux":
  39. return randint(32768, 61000)
  40. else:
  41. if (previousPort == 0) or (previousPort + 1 > 65535):
  42. return randint(49152, 65535)
  43. else:
  44. return previousPort + 1
  45. # FIXME: rework copy-pasted code
  46. # source: http://reliablybroken.com/b/2009/09/working-with-active-directory-filetime-values-in-python/
  47. # WORK IN PROGRESS
  48. def get_filetime_format(timestamp):
  49. EPOCH_AS_FILETIME = 116444736000000000 # January 1, 1970 as MS file time
  50. HUNDREDS_OF_NANOSECONDS = 10000000
  51. boot_datetime = datetime.fromtimestamp(timestamp)
  52. if (boot_datetime.tzinfo is None) or (boot_datetime.tzinfo.utcoffset(boot_datetime) is None):
  53. boot_datetime = boot_datetime.replace(tzinfo=boot_datetime.tzname())
  54. boot_filetime = EPOCH_AS_FILETIME + (timegm(boot_datetime.timetuple()) * HUNDREDS_OF_NANOSECONDS)
  55. return boot_filetime + (boot_datetime.microsecond * 10)
  56. def get_rnd_boot_time(timestamp, platform="winxp"):
  57. check_platform(platform)
  58. # FIXME: create probability distribution for each OS
  59. if platform is "linux":
  60. # four years
  61. timestamp -= randint(0, 126144000)
  62. if platform is "macOS":
  63. # three months
  64. timestamp -= randint(0, 7884000)
  65. else:
  66. # one month
  67. timestamp -= randint(0, 2678400)
  68. return get_filetime_format(timestamp)