How to use the scapy.layers.inet.IP function in scapy

To help you get started, we’ve selected a few scapy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github filvarga / srv6-mobile / test / test_vtr.py View on Github external
def create_packet(self, src_if, dst_if, do_dot1=True):
        packet_sizes = [64, 512, 1518, 9018]
        dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index])
        src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index])
        pkt_info = self.create_packet_info(src_if, dst_if)
        payload = self.info_to_payload(pkt_info)
        p = (Ether(dst=dst_host.mac, src=src_host.mac) /
             IP(src=src_host.ip4, dst=dst_host.ip4) /
             UDP(sport=1234, dport=1234) /
             Raw(payload))
        pkt_info.data = p.copy()
        if do_dot1 and hasattr(src_if, 'sub_if'):
            p = src_if.sub_if.add_dot1_layer(p)
        size = random.choice(packet_sizes)
        self.extend_packet(p, size)
        return p
github filvarga / srv6-mobile / test / test_ip4.py View on Github external
#
        # set the directed broadcast on pg0 first, then config IP4 addresses
        # for pg1 directed broadcast is always disabled
        self.vapi.sw_interface_set_ip_directed_broadcast(
            self.pg0.sw_if_index, 1)

        p0 = (Ether(src=self.pg1.remote_mac,
                    dst=self.pg1.local_mac) /
              IP(src="1.1.1.1",
                 dst=self.pg0._local_ip4_bcast) /
              UDP(sport=1234, dport=1234) /
              Raw(b'\xa5' * 2000))
        p1 = (Ether(src=self.pg0.remote_mac,
                    dst=self.pg0.local_mac) /
              IP(src="1.1.1.1",
                 dst=self.pg1._local_ip4_bcast) /
              UDP(sport=1234, dport=1234) /
              Raw(b'\xa5' * 2000))

        self.pg0.config_ip4()
        self.pg0.resolve_arp()
        self.pg1.config_ip4()
        self.pg1.resolve_arp()

        #
        # test packet is L2 broadcast
        #
        rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
        self.assertTrue(rx[0][Ether].dst, "ff:ff:ff:ff:ff:ff")

        self.send_and_assert_no_replies(self.pg0, p1 * NUM_PKTS,
github filvarga / srv6-mobile / test / test_ip4.py View on Github external
def modify_packet(self, src_if, packet_size, pkt):
        """Add load, set destination IP and extend packet to required packet
        size for defined interface.

        :param VppInterface src_if: Interface to create packet for.
        :param int packet_size: Required packet size.
        :param Scapy pkt: Packet to be modified.
        """
        dst_if_idx = int(packet_size / 10 % 2)
        dst_if = self.flows[src_if][dst_if_idx]
        info = self.create_packet_info(src_if, dst_if)
        payload = self.info_to_payload(info)
        p = pkt/Raw(payload)
        p[IP].dst = dst_if.remote_ip4
        info.data = p.copy()
        if isinstance(src_if, VppSubInterface):
            p = src_if.add_dot1_layer(p)
        self.extend_packet(p, packet_size)

        return p
github filvarga / srv6-mobile / test / test_ip4.py View on Github external
nh_table_id=1)])
        route_to_src = VppIpRoute(self, "1.1.1.2", 32,
                                  [VppRoutePath("0.0.0.0",
                                                0xffffffff,
                                                nh_table_id=2,
                                                is_source_lookup=1)])
        route_to_dst.add_vpp_config()
        route_to_src.add_vpp_config()

        #
        # packets to these destination are dropped, since they'll
        # hit the respective default routes in the second table
        #
        p_dst = (Ether(src=self.pg0.remote_mac,
                       dst=self.pg0.local_mac) /
                 IP(src="5.5.5.5", dst="1.1.1.1") /
                 TCP(sport=1234, dport=1234) /
                 Raw('\xa5' * 100))
        p_src = (Ether(src=self.pg0.remote_mac,
                       dst=self.pg0.local_mac) /
                 IP(src="2.2.2.2", dst="1.1.1.2") /
                 TCP(sport=1234, dport=1234) /
                 Raw('\xa5' * 100))
        pkts_dst = p_dst * 257
        pkts_src = p_src * 257

        self.send_and_assert_no_replies(self.pg0, pkts_dst,
                                        "IP in dst table")
        self.send_and_assert_no_replies(self.pg0, pkts_src,
                                        "IP in src table")

        #
github viveris / librle / tests / scripts / gen_rand_ipv4_trace.py View on Github external
def send_frames(payload_len, dump_size, dump_filename):
    """ Send frames function

    send the frames.

    """
    for int_iterator in range(dump_size):
        l2_header = Ether(src="00:11:22:33:44:55", dst="55:44:33:22:11")
        l3_header = IP(src="192.168.1.1", dst="192.168.1.2", id=int_iterator)
        payload = Raw(make_payload(
            PAYLOAD_SEED,
            payload_len.rand_payload_len(),
            len(l3_header)))
        pkt = l2_header / l3_header / payload
        sendp(pkt, iface=dump_filename, verbose=False)
        if int_iterator % 100 == 0:
            print "... " + str(int_iterator) + " packets sent..."
github dark-lbp / isf / icssploit / modules / scanners / enip_scan.py View on Github external
def discover_local_device(self):
        self.sniff_mac_address = get_if_hwaddr(self.nic)
        p = threading.Thread(target=self.sniff_answer)
        p.setDaemon(True)
        p.start()
        # wait sniff start
        time.sleep(0.2)
        packet = Ether(src=self.sniff_mac_address, dst="ff:ff:ff:ff:ff:ff")/IP(src=get_if_addr(self.nic), dst="255.255.255.255")/UDP(sport=44818, dport=44818)/ENIPHeader(Command=0x0063)
        sendp(packet, iface=self.nic)
        self.sniff_finished.wait(self.timeout + 1)
github cisco-system-traffic-generator / trex-core / scripts / external_libs / scapy-2.3.1 / python2 / scapy / modules / queso.py View on Github external
def queso_sig(target, dport=80, timeout=3):
    p = queso_kdb.get_base()
    ret = []
    for flags in ["S", "SA", "F", "FA", "SF", "P", "SEC"]:
        ans, unans = sr(IP(dst=target)/TCP(dport=dport,flags=flags,seq=RandInt()),
                        timeout=timeout, verbose=0)
        if len(ans) == 0:
            rs = "- - - -"
        else:
            s,r = ans[0]
            rs = "%i" % (r.seq != 0)
            if not r.ack:
                r += " 0"
            elif r.ack-s.seq > 666:
                rs += " R" % 0
            else:
                rs += " +%i" % (r.ack-s.seq)
            rs += " %X" % r.window
            rs += " %x" % r.payload.flags
        ret.append(rs)
    return ret
github tklab-tud / ID2T / code / Lib / Generator.py View on Github external
Builds a TCP packet with the values specified by the caller.

    :param ip_src: the source IP address of the IP header
    :param ip_dst the destination IP address of the IP header
    :param mac_src: the source MAC address of the MAC header
    :param ttl: the ttl value of the packet
    :param mac_dst: the destination MAC address of the MAC header
    :param port_src: the source port of the TCP header
    :param port_dst: the destination port of the TCP header
    :param tcpflags: the TCP flags of the TCP header
    :param payload: the payload of the packet
    :return: the corresponding TCP packet
    """

    ether = Ether(src=mac_src, dst=mac_dst)
    ip = IP(src=ip_src, dst=ip_dst, ttl=ttl)
    tcp = TCP(sport=port_src, dport=port_dst, flags=tcpflags)
    packet = ether / ip / tcp / Raw(load=payload)
    return packet
github cisco-system-traffic-generator / trex-core / scripts / external_libs / scapy-2.3.1 / python3 / scapy / layers / dhcp.py View on Github external
def dhcp_request(iface=None,**kargs):
    if conf.checkIPaddr != 0:
        warning("conf.checkIPaddr is not 0, I may not be able to match the answer")
    if iface is None:
        iface = conf.iface
    hw = get_if_raw_hwaddr(iface)
    return srp1(Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0",dst="255.255.255.255")/UDP(sport=68,dport=67)
                 /BOOTP(chaddr=hw)/DHCP(options=[("message-type","discover"),"end"]),iface=iface,**kargs)
github juga0 / dhcpcanon / dhcpcanon / dhcpcanon.py View on Github external
def gen_ether_ip_discover(self):
        ether_ip = (
            Ether(src=self.client_mac, dst=BROADCAST_MAC) /
            IP(src=META_ADDR, dst=BROADCAST_ADDR)
        )
        return ether_ip