Example #1
0
def PCAPread(file_name, start, count):
    reader = PcapReader(file_name)
    if start > 0:
        reader.read_all(start)
    if count > 0:
        return reader.read_all(count)
    else:
        return reader.read_all(-1)
Example #2
0
def read(file_name, start, count):
    '''
    read packets from pcap according to the start packet number and total count 
    '''
    reader = PcapReader(file_name)
    if start > 0:
        reader.read_all(start)
    if count > 0:
        return reader.read_all(count)
    else:
        return reader.read_all(-1)
Example #3
0
    def read_pcap(self):

        file_name = input(
            "Enter the pcap file name like 2019_11_02_16_55_22.pcap:"
        )  #输入pcap文件名
        file_name = "sniff_data/" + file_name  #组合文件路径
        reader = PcapReader(file_name)  #用scapy打开pcap文件
        packets = reader.read_all(-1)  #读取所有储存的数据包
        for i in packets:  #循环数据包列表
            i.show()  #打印数据包
Example #4
0
def read_pcap():
    if chinses_mode:
        print("请输入pcap文件名:")
    else:
        file_name = input(
            "Enter the pcap file name like 2019_11_02_16_55_22.pcap:"
        )  #输入pcap文件名
    file_name = "sniff_data/" + file_name  #组合文件路径

    try:
        reader = PcapReader(file_name)  #用scapy打开pcap文件
    except FileNotFoundError:
        if chinses_mode:
            print("找不到文件")
        else:
            print("Can nod find the file")
        return

    packets = reader.read_all(-1)  #读取所有储存的数据包
    for i in packets:  #循环数据包列表
        i.show()  #打印数据包
def main():
    pcap = PcapReader('/mnt/Exec/code/research/ping-all-of-1.pcap')
    """
    Network 40576 -> Id [2]
        [ControllerRegion] ConnId [2] -> 59760 On controller
        [ReplicaRegion] ConnId [2] -> 36316 On controller
    
    Network 40578 -> Id [3]
        [ControllerRegion] ConnId [3] -> 59764 On controller
        [ReplicaRegion] ConnId [3] -> 36320 On controller
    """

    con_ids = {
        40576: 2,  # SRC
        59760: 2,  # DST
        36316: 2,  # DST
        #
        40578: 3,
        59764: 3,
        36320: 3
    }

    ports = {
        6833: "Proxy",
        6834: "Main Controller",
        6835: "Replicated Controller"
    }
    custom_ports = ports.keys()

    ips = {
        "192.168.1.244": "Main Controller",
        "192.168.1.245": "Replicated Controller",
        "192.168.1.248": "Proxy",
        "192.168.1.136": "Proxy",
        "192.168.1.241": "Network",
    }

    packet_list = filter_control_packets(pcap.read_all())

    def get_id(sprt, dprt):
        if sprt in con_ids:
            return con_ids[sprt]
        if dprt in con_ids:
            return con_ids[dprt]

        return False

    packets = []
    for packet in packet_list:
        sport = packet['TCP'].sport
        dport = packet['TCP'].dport

        con_id = get_id(sport, dport)
        if not con_id:
            print("Skip", repr(packet))
            continue  # Invalid first test socket that pox opens

        p = ConversationPacket(packet, ips, con_id, custom_ports)
        packets.append(p)
        # src = ips[packet['IP'].src]
        # dst = ips[packet['IP'].dst]

        # of_packet = OpenFlow(packet, packet.load, custom_ports)
        # print("[{}] [{}]".format(p.time, p.packet_type), p.src, "-->", p.dst, "[{}]".format(p.con_id))

    print("Found", len(packet_list), "Packets [ACKs are ignored]")
    con_2 = [x for x in packets if x.con_id == 2]
    con_3 = [x for x in packets if x.con_id == 3]
    #
    # sorted(con_2, key=lambda x: x.time)
    # packets.sort(key=lambda x: x.con_id)
    # for p in packets:
    #     print("[{}] [{}]".format(p.time, "_"), p.src, "-->", p.dst, "[{}]".format(p.con_id))
    for i in range(int(len(con_2))):
        p = con_2[i]
        print("[{}] [{}]".format(p.time, p.packet_type), p.src, "-->", p.dst,
              "[{}]".format(p.con_id))