IPPacketGenerator.py 1.1 KB

123456789101112131415161718192021222324
  1. from scapy.layers.inet import IP, Ether, TCP
  2. from scapy.packet import Raw
  3. def generate_ip_packet(ip_src:str="192.168.64.32", ip_dst:str="192.168.64.48", mac_src:str="56:6D:D9:BC:70:1C",
  4. mac_dst:str="F4:2B:95:B3:0E:1A", port_src:int=1337, port_dst:int=6442, tcpflags:str="S", payload:str=""):
  5. """
  6. Builds an IP packet with the values specified by the caller. If a parameter was not specified by the caller,
  7. it is set to a default value.
  8. :param ip_src: the source IP address of the IP header
  9. :param ip_dst the destination IP address of the IP header
  10. :param mac_src: the source MAC address of the MAC header
  11. :param mac_dst: the destination MAC address of the MAC header
  12. :param port_src: the source port of the TCP header
  13. :param port_dst: the destination port of the TCP header
  14. :param tcpflags: the TCP flags of the TCP header
  15. :param payload: the payload of the packet
  16. :return: the corresponding IP packet
  17. """
  18. ether = Ether(src=mac_src, dst=mac_dst)
  19. ip = IP(src=ip_src, dst=ip_dst)
  20. tcp = TCP(sport=port_src, dport=port_dst, flags=tcpflags)
  21. packet = ether / ip / tcp / Raw(load=payload)
  22. return packet