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.

68 lines
1.8 KiB

7 months ago
  1. #!/usr/bin/env python3
  2. import os
  3. import signal
  4. from scapy.all import *
  5. from netfilterqueue import NetfilterQueue
  6. import argparse
  7. window_size = 20
  8. def modify_window(pkt):
  9. try:
  10. ip = IP(pkt.get_payload())
  11. if ip.haslayer(TCP) and ip[TCP].flags == "SA":
  12. ip[TCP].window = window_size
  13. del ip[IP].chksum
  14. del ip[TCP].chksum
  15. pkt.set_payload(bytes(ip))
  16. elif ip.haslayer(TCP) and ip[TCP].flags == "FA":
  17. ip[TCP].window = window_size
  18. del ip[IP].chksum
  19. del ip[TCP].chksum
  20. pkt.set_payload(bytes(ip))
  21. elif ip.haslayer(TCP) and ip[TCP].flags == "PA":
  22. ip[TCP].window = window_size
  23. del ip[IP].chksum
  24. del ip[TCP].chksum
  25. pkt.set_payload(bytes(ip))
  26. elif ip.haslayer(TCP) and ip[TCP].flags == "A":
  27. ip[TCP].window = window_size
  28. del ip[IP].chksum
  29. del ip[TCP].chksum
  30. pkt.set_payload(bytes(ip))
  31. except:
  32. pass
  33. pkt.accept()
  34. def parsearg():
  35. parser = argparse.ArgumentParser(description='Description of your program')
  36. parser.add_argument('-q', '--queue', type=int, help='iptables Queue Num')
  37. parser.add_argument('-w', '--window_size', type=int, help='Tcp Window Size')
  38. args = parser.parse_args()
  39. if args.queue is None or args.window_size is None:
  40. exit(1)
  41. window_size = args.window_size
  42. return args.queue
  43. def main():
  44. queue_num = parsearg()
  45. nfqueue = NetfilterQueue()
  46. nfqueue.bind(queue_num, modify_window)
  47. try:
  48. print("Starting netfilter_queue process...")
  49. nfqueue.run()
  50. except KeyboardInterrupt:
  51. pass
  52. if __name__ == "__main__":
  53. #sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
  54. signal.signal(signal.SIGINT, lambda signal, frame: sys.exit(0))
  55. main()