Beispiel #1
0
    def create_attack_pkts(self,
                           src_list,
                           target_list,
                           pkt_len,
                           inter=DEFAULT_INTER,
                           cnt=DEFAULT_INTER):
        # By default ether type is ipv4

        pkt_lst = []
        for target_name in target_list:
            for src_name in src_list:

                load = os.urandom(pkt_len)
                pkt = Ether(dst=machines[target_name]['mac'],
                            src=machines[src_name]['mac'],
                            type=ETHER_TYPES['IPv4'])/\
                      IP(dst=machines[target_name]['ip'],
                         src=machines[src_name]['ip'],)/\
                      UDP(dport=machines[target_name]['port'],
                          sport=machines[src_name]['port'])/\
                      Raw(load=load)
                print(len(load))
                pkt.show2()
                pkt_lst.append(pkt)

        # return pkt_lst
        sendp(pkt_lst, iface="eth2", count=cnt, inter=inter)
Beispiel #2
0
    def create_attack_pkts(self,
                           src_list,
                           target_list,
                           pkt_len,
                           inter=DEFAULT_INTER,
                           cnt=DEFAULT_INTER):
        # By default ether type is ipv4
        # TODO: Crosscheck if ether type should be 0x800 or IPv4
        # TODO: Raw len vs pkt len - sorted, same.
        # TODO: IP flags,id ?
        # Hardcode for the time being
        # if cnt < len(src_list) * len(target_list):

        # send all possible combination of (src, dst), cnt times
        # cnt = cnt * (len(src_list) * len(target_list))
        pkt_lst = []
        for target_name in target_list:
            for src_name in src_list:

                load = os.urandom(pkt_len)
                pkt = Ether(dst=machines[target_name]['mac'],
                            src=machines[src_name]['mac'],
                            type=ETHER_TYPES['IPv4'])/\
                      IP(dst=machines[target_name]['ip'],
                         src=machines[src_name]['ip'],)/\
                      UDP(dport=machines[target_name]['port'],
                          sport=machines[src_name]['port'])/\
                      Raw(load=load)
                print(len(load))
                pkt.show2()
                pkt_lst.append(pkt)

        # return pkt_lst
        sendp(pkt_lst, iface="eth2", count=cnt, inter=inter)
Beispiel #3
0
def device_send(args):
    numeric_level = getattr(logging, args.loglevel.upper(), None)
    basicConfig(format=FORMAT, level=numeric_level, filename=args.logfile)
    addr = socket.gethostbyname(args.destination)
    count = int((args.duration - args.delay) / args.interval)
    interface = get_if(args.interface)
    logger.info('Delaying %d seconds' % args.delay)
    sleep(args.delay)

    logger.info(
        'sending %s packets at %s sec/packet on interface %r to %s for %s '
        'seconds', count, args.interval, interface, args.destination,
        (args.duration - args.delay))
    logger.info('SRC ADDR for interface %s - %s', interface,
                get_if_hwaddr(interface))
    pkt = Ether(src=get_if_hwaddr(interface), dst=args.switch_ethernet)
    logger.info('packet Ether obj - %s', pkt)

    if args.source_addr:
        src_ip = args.source_addr
    else:
        src_ip = get_if_addr(interface)

    if args.source_port:
        src_port = args.source_port
    else:
        src_port = random.randint(49152, 65535)

    if args.tcp:
        pkt = pkt / IP(dst=addr, src=src_ip) / TCP(dport=args.port,
                                                   sport=src_port) / args.msg
    else:
        pkt = pkt / IP(dst=addr, src=src_ip) / UDP(dport=args.port,
                                                   sport=src_port) / args.msg

    pkt.show2()
    sendp(pkt,
          iface=interface,
          verbose=False,
          count=count,
          inter=args.interval)
    logger.info('Done')
    return
 def on_pkt_rx (self, pkt):
     scapy_pkt = Ether(pkt['binary'])
     if not 'ICMP' in scapy_pkt:
         return None
     
     #scapy_pkt.show2()    
     ip = scapy_pkt['IP']
     
     icmp = scapy_pkt['ICMP']
     
     dt = pkt['ts'] - self.start_ts
     
     if icmp.type == 0:
         # echo reply
         return self.port.ok('Reply from {0}: bytes={1}, time={2:.2f}ms, TTL={3}'.format(ip.src, len(pkt['binary']), dt * 1000, ip.ttl))
         
     # unreachable
     elif icmp.type == 3:
         return self.port.ok('Reply from {0}: Destination host unreachable'.format(icmp.src))
     else:
         scapy_pkt.show2()
         return self.port.err('unknown ICMP reply')
Beispiel #5
0
    def create_ip(self,
                  src_ip=None,
                  dst_ip=None,
                  tran_l_proto="UDP",
                  pkt_len=None,
                  src_port=None,
                  dst_port=None,
                  cnt=100,
                  inter=0.01):

        # If src_ip is named in machines, use that, and its mac addr
        # Else if src_ip written in octet format, use it and random addr
        # Else randomly select from machnes given along with mac

        #--------------------------------------
        # SRC IP, MAC, PORT
        #--------------------------------------
        if src_ip == None:
            # Select key at random
            machine_name = random.choice(list(machines.keys()))
            src = machines[machine_name]['ip']
            # Select random mac
            src_mac = self.rand_mac()

        elif src_ip in machines:
            src = machines[src_ip]['ip']
            src_mac = machines[src_ip]['mac']
            if src_port == None:
                src_port = machines[src_ip]['port']

        else:
            # Custom IP
            src = src_ip
            # Random mac
            src_mac = self.rand_mac()

        if src_port == None:
            src_port = self.random_port()
        else:
            src_port = int(src_port)

        print(src, src_mac, src_port)

        #--------------------------------------
        # DST IP, MAC, PORT
        #--------------------------------------

        if dst_ip == None:
            # Select key at random
            machine_name = random.choice(list(machines.keys()))
            dst = machines[machine_name]['ip']
            # Select random mac
            dst_mac = self.rand_mac()

        elif dst_ip in machines:
            dst = machines[dst_ip]['ip']
            dst_mac = machines[dst_ip]['mac']
            if dst_port == None:
                dst_port = machines[dst_ip]['port']

        else:
            # Custom IP
            dst = dst_ip
            # Random mac
            dst_mac = self.rand_mac()

        if dst_port == None:
            dst_port = self.random_port()
        else:
            dst_port = int(dst_port)
        print(dst, dst_mac, dst_port)

        #--------------------------------------
        # LENGTH, Protocol
        #--------------------------------------
        if pkt_len is None:
            pkt_len = random.randint(1, PKT_MAX_LEN)
        load = os.urandom(int(pkt_len))

        #--------------------------------------
        # Packet gerneration
        #--------------------------------------
        pkt = None
        if tran_l_proto == "UDP":
            pkt = Ether(dst=dst_mac,
                        src=src_mac,
                        type=ETHER_TYPES['IPv4'])/\
                  IP(dst=dst,
                     src=src,)/\
                  UDP(dport=dst_port,
                      sport=src_port)/\
                  Raw(load=load)
            #print(len(load))
            pkt.show2()

        else:
            # TCP
            pkt = Ether(dst=dst_mac,
                        src=src_mac,
                        type=ETHER_TYPES['IPv4'])/\
                  IP(dst=dst,
                     src=src,)/\
                  TCP(dport=dst_port,
                      sport=src_port)/\
                  Raw(load=load)
            #print(len(load))
            pkt.show2()
        return pkt
Beispiel #6
0
for id in id_list:
    pkt_raw = db.read_raw_by_id(id)
    print (parse_packet(Ether(pkt_raw[2])))
    pkt_raw_list.append(pkt_raw[2])
    eth_pkt = Ether(pkt_raw[2])
    eth_pkt.show()
    print("IP chk", eth_pkt[IP].chksum)
    print(eth_pkt[UDP].chksum)
    
    #CHange IP
    eth_pkt[IP].dst = "192.168.101.106"
    # Recompute chksum
    del eth_pkt[IP].chksum
    del eth_pkt[UDP].chksum
    eth_pkt.show2(dump=False)
    
    sendp(eth_pkt, iface='eth2', count = 10000, inter=0.001)#, return_packets = True)

# Write the list to pcap
# write(pkt_raw_list)


# def write(pkt):
#     wrpcap('length_pkt.pcap', pkt, append=True)  #appends packet to output file   
"""
for pkt_raw in pkt_raw_list:
    print("Reading one dump\n")
    print(pkt_raw)
    print(Ether(pkt_raw[2]).summary())
    pkt_analyzer = PacketAnalyzer(False)
Beispiel #7
0
    def test_linux_cp_tap(self):
        """Linux CP TAP"""

        #
        # Setup
        #

        arp_opts = {"who-has": 1, "is-at": 2}

        # create two pairs, wihch a bunch of hots on the phys
        hosts = [self.pg0, self.pg1]
        phys = [self.pg2, self.pg3]
        N_HOSTS = 4

        for phy in phys:
            phy.config_ip4()
            phy.generate_remote_hosts(4)
            phy.configure_ipv4_neighbors()

        pair1 = VppLcpPair(self, phys[0], hosts[0]).add_vpp_config()
        pair2 = VppLcpPair(self, phys[1], hosts[1]).add_vpp_config()

        self.logger.info(self.vapi.cli("sh lcp adj verbose"))
        self.logger.info(self.vapi.cli("sh lcp"))

        #
        # Traffic Tests
        #

        # hosts to phys
        for phy, host in zip(phys, hosts):
            for j in range(N_HOSTS):
                p = (Ether(src=phy.local_mac, dst=phy.remote_hosts[j].mac) /
                     IP(src=phy.local_ip4, dst=phy.remote_hosts[j].ip4) /
                     UDP(sport=1234, dport=1234) / Raw())

                rxs = self.send_and_expect(host, [p], phy)

                # verify packet is unchanged
                for rx in rxs:
                    self.assertEqual(p.show2(True), rx.show2(True))

                # ARPs x-connect to phy
                p = Ether(dst="ff:ff:ff:ff:ff:ff",
                          src=phy.remote_hosts[j].mac) / ARP(
                              op="who-has",
                              hwdst=phy.remote_hosts[j].mac,
                              hwsrc=phy.local_mac,
                              psrc=phy.local_ip4,
                              pdst=phy.remote_hosts[j].ip4,
                          )

                rxs = self.send_and_expect(host, [p], phy)

                # verify packet is unchanged
                for rx in rxs:
                    self.assertEqual(p.show2(True), rx.show2(True))

        # phy to host
        for phy, host in zip(phys, hosts):
            for j in range(N_HOSTS):
                p = (Ether(dst=phy.local_mac, src=phy.remote_hosts[j].mac) /
                     IP(dst=phy.local_ip4, src=phy.remote_hosts[j].ip4) /
                     UDP(sport=1234, dport=1234) / Raw())

                rxs = self.send_and_expect(phy, [p], host)

                # verify packet is unchanged
                for rx in rxs:
                    self.assertEqual(p.show2(True), rx.show2(True))

                # ARPs rx'd on the phy are sent to the host
                p = Ether(dst="ff:ff:ff:ff:ff:ff",
                          src=phy.remote_hosts[j].mac) / ARP(
                              op="is-at",
                              hwsrc=phy.remote_hosts[j].mac,
                              hwdst=phy.local_mac,
                              pdst=phy.local_ip4,
                              psrc=phy.remote_hosts[j].ip4,
                          )

                rxs = self.send_and_expect(phy, [p], host)

                # verify packet is unchanged
                for rx in rxs:
                    self.assertEqual(p.show2(True), rx.show2(True))

        # cleanup
        for phy in phys:
            phy.unconfig_ip4()