Making magic with the network stack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.4 KiB

  1. #!/usr/bin/env python3
  2. from scapy.all import *
  3. import ipaddress
  4. from threading import Thread, Event
  5. from time import sleep
  6. import os
  7. recv_count = 0
  8. server_port = 50508
  9. class Sniffer(Thread):
  10. def __init__(self, iface="ens18"):
  11. super().__init__()
  12. self.daemon = True
  13. self.recv_count = 0
  14. self.socket = None
  15. self.iface = iface
  16. self.stop_sniffer = Event()
  17. self.debug_src = ""
  18. self.debug_sport = 0
  19. self.last_ack = 0
  20. self.last_seq = 0
  21. def run(self):
  22. self.socket = conf.L2listen(
  23. type=ETH_P_ALL,
  24. iface=self.iface,
  25. filter="ip"
  26. )
  27. sniff(
  28. opened_socket=self.socket,
  29. prn=self.handle_packet,
  30. )
  31. def join(self, timeout=None):
  32. self.stop_sniffer.set()
  33. super().join(timeout)
  34. def handle_debug_packet(self, payload):
  35. some_packet = IP(bytes(payload))
  36. print("genie debug packet..")
  37. some_packet.show()
  38. def handle_packet(self, packet):
  39. #ip_layer = packet.getlayer(IP)
  40. #print("[!] New Packet: {src} -> {dst}".format(src=ip_layer.src, dst=ip_layer.dst))
  41. if TCP in packet:
  42. tcp_sport = packet[TCP].sport
  43. tcp_dport = packet[TCP].dport
  44. if tcp_dport == server_port:
  45. print("sniffed a debug packet..")
  46. #packet.show()
  47. if self.recv_count > 3:
  48. some_payload = packet[TCP].payload
  49. self.handle_debug_packet(some_payload)
  50. self.debug_src = packet[IP].src
  51. self.debug_sport = tcp_sport
  52. self.last_ack = packet[TCP].ack
  53. self.last_seq = packet[TCP].seq
  54. self.recv_count += 1
  55. # TODO: make this function be able to craft full custom packet including
  56. # source IP, sport, protocol, flags, payload, etc.
  57. def craft_send_payload(dip, dest_port):
  58. payload = ""
  59. send_pkt = IP(dst=dip, src="10.0.0.17") / TCP(dport=dest_port,sport=40404,flags="S") / "AAAAAAAA"
  60. send_bytes = bytes(send_pkt)
  61. payload = send_bytes
  62. print("debug send payload: " + str(payload))
  63. return payload
  64. def get_send_payload():
  65. payload = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  66. send_pkt = IP(dst="9.9.9.9", src="10.0.0.161") / TCP(dport=80,sport=40404,flags="S") / "AAAAAAAA"
  67. send_bytes = bytes(send_pkt)
  68. payload = send_bytes
  69. print("debug send payload: " + str(payload))
  70. return payload
  71. def send_debug_packet(sniffer, is_custom):
  72. debug_port = sniffer.debug_sport
  73. debug_ip = sniffer.debug_src
  74. send_seq = sniffer.last_ack
  75. send_ack = sniffer.last_seq
  76. if debug_port == 0 or debug_ip == '':
  77. print("There was no debug source connection to send to")
  78. return
  79. print("sending debug packet to " + str(debug_ip) + ":" + str(debug_port))
  80. send_payload = ""
  81. if is_custom:
  82. print("\n\nEnter dest ip for packet..")
  83. dip = input("Enter IP: ")
  84. print("\n\nEnter dest port for packet..")
  85. dport = input("Enter port: ")
  86. send_payload = craft_send_payload(dip, int(dport))
  87. else:
  88. send_payload = get_send_payload()
  89. packet = IP(dst=debug_ip) / TCP(dport=debug_port, sport=server_port, flags='PA', seq=send_seq, ack=send_ack) / send_payload
  90. send(packet, iface="ens18")
  91. print("sent debug packet: ")
  92. packet.show()
  93. def main():
  94. sniffer = Sniffer(iface='ens18')
  95. print('starting sniffer..')
  96. sniffer.start()
  97. time.sleep(3)
  98. done = False
  99. while not(done):
  100. print("Enter action to take..")
  101. print("1. Keep sniffing")
  102. print("2. Send test packet back")
  103. print("3. Craft custom packet to send from genie")
  104. print("4. Quit")
  105. answer = input("Enter answer: ")
  106. if answer == "1":
  107. print("sleeping for sniffer..")
  108. time.sleep(5)
  109. elif answer == "2":
  110. send_debug_packet(sniffer, False)
  111. elif answer == "3":
  112. send_debug_packet(sniffer, True)
  113. elif answer == "4":
  114. print("ending the sniffer")
  115. done = True
  116. print("user answer was: " + str(answer))
  117. print("\n")
  118. time.sleep(1)
  119. print("\n\nFinished sniffing for debug packets")
  120. if __name__ == '__main__':
  121. main()