Example #1
0
def read_c_result(ID, server: cServer):
    global result_ID_list

    p = pcap.PCAP(server.s_c_feed)
    p.open('r')
    for w in p:
        # here we apply our knowledge about the event/pkt's internal struct
        try:
            e = cbor2.loads(w)
        except:
            logging.critical('cbor2 loader failed - skipping logentry')
            continue
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])
        # rewrite the packet's byte arrays for pretty printing:
        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        if e[2] != None:
            e[2] = cbor2.loads(e[2])
            e[1] = pcap.base64ify(e[1])

        if isinstance(e[2], dict) and e[2]['type'] == 'result':
            if e[2]['ID'] == ID:
                logging.debug(f'from read_result  ID={e[2]["ID"]}')
                logging.debug(f"** fid={fid}, seq={seq}, ${len(w)} bytes")
                logging.debug(f"   hashref={href.hex()}")
                logging.debug(f"   content={e[2]}")
                if result_ID_list.__contains__(ID):
                    clear_await(ID)
                handle_result(e)
                return True

    p.close()
    return False
Example #2
0
def read_request():
    '''
    Since the server can also detruce - close a connection the client has to read requests
    :return:
    '''
    global next_request_ID
    p = pcap.PCAP(isp_log)
    p.open('r')
    for w in p:

        e = cbor2.loads(w)
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])

        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        if e[2] != None:
            e[2] = cbor2.loads(e[2])

        if isinstance(e[2], dict) and e[2]['type'] == 'request':
            request_ID = e[2]["ID"]

            logging.debug(f'req_id:{request_ID},next:{next_request_ID}')
            if request_ID == next_request_ID:
                logging.info(f'Handling request from server')
                next_request_ID += 1
                handle_request(e[2])

    p.close()
Example #3
0
def createInventory(fname, inventoryDict):
    log = importPCAP(fname)
    log.open('r')
    inventory = open(inventoryDict, 'w+')
    for w in log:
        e = cbor2.loads(w)
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])
        # rewrite the packet's byte arrays for pretty printing:
        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        inventory.write("%d \n" % seq)
    log.close()
Example #4
0
def createPayload(fname, inventoryint, inventoryext):
    log = importPCAP(fname)
    payload = importPCAP('payload.pcap')
    seq_payload = compareInventory(inventoryint, inventoryext)
    if seq_payload == set():
        return
    log.open('r')
    payload.open('a')
    for w in log:
        e = cbor2.loads(w)
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])
        # rewrite the packet's byte arrays for pretty printing:
        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        if seq in seq_payload:
            payload.write(w)
    payload.close()
    log.close()
Example #5
0
def createPayload(fname, inventoryint, inventoryext):
    """
    Please comment
    """
    #how do we create the payload? As a clear text file just like we assume to store them locally?
    log = importPCAP(fname)
    payload = importPCAP('payload.pcap')
    seq_payload = compareInventory(inventoryint, inventoryext)
    if seq_payload == set():
        return
    log.open('r')
    payload.open('a')
    for w in log:
        e = cbor2.loads(w)
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])
        # rewrite the packet's byte arrays for pretty printing:
        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        if seq in seq_payload:
            payload.write(w)
    payload.close()
    log.close()
Example #6
0
#!/usr/bin/python
import lib.pcap as pcap
import cbor2
import hashlib

inventory_file = open("inventory.txt")
inventory = inventory_file.read().splitlines()
log = pcap.PCAP('test.pcap')
log.open('r')
for w in log:
    e = cbor2.loads(w)
    href = hashlib.sha256(e[0]).digest()
    e[0] = cbor2.loads(e[0])
    e[0] = pcap.base64ify(e[0])
    fid = e[0][0]
    seq = e[0][1]
    e[2] = cbor2.loads(e[2])
    print(fid)
    print(seq)
    print(e[2])

    print(e[0])
    print(href.hex())
Example #7
0
def init():
    '''
    Initialises the whole client environment
    :return:
    '''
    global next_request_ID
    global highest_result_ID
    global result_ID_list

    create_feed()

    # This part is currently not working
    logging.info('Initialising from feeds...')
    p = pcap.PCAP(client_log)
    p.open('r')
    for w in p:
        # here we apply our knowledge about the event/pkt's internal struct
        e = cbor2.loads(w)
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])
        # rewrite the packet's byte arrays for pretty printing:
        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        if e[2] != None:
            e[2] = cbor2.loads(e[2])
        # print(f"** fid={fid}, seq={seq}, ${len(w)} bytes")
        # print(f"   hashref={href.hex()}")
        # print(f"   content={e[2]}")

        if isinstance(e[2], dict) and e[2]['type'] == 'request':
            logging.debug(f'from init request  ID={e[2]["ID"]}')
            await_result(e[2]['ID'])
            next_request_ID = max(int(e[2]["ID"]), next_request_ID)

    p.close()

    p = pcap.PCAP(isp_log)
    p.open('r')
    for w in p:
        # here we apply our knowledge about the event/pkt's internal struct
        e = cbor2.loads(w)
        href = hashlib.sha256(e[0]).digest()
        e[0] = cbor2.loads(e[0])
        # rewrite the packet's byte arrays for pretty printing:
        e[0] = pcap.base64ify(e[0])
        fid = e[0][0]
        seq = e[0][1]
        if e[2] != None:
            e[2] = cbor2.loads(e[2])

        if isinstance(e[2], dict) and e[2]['type'] == 'result':
            if result_ID_list.__contains__(e[2]['ID']):
                logging.debug(f'from init result  ID={e[2]["ID"]}')
                logging.debug(f"** fid={fid}, seq={seq}, ${len(w)} bytes")
                logging.debug(f"   hashref={href.hex()}")
                logging.debug(f"   content={e[2]}")

                read_result(e[2]['ID'])

    p.close()

    path = client_config['location']

    for log in os.listdir(path):

        if os.path.isfile(os.path.join(path, log)) and log.endswith(".pcap"):

            p = pcap.PCAP(f'{path}/{log}')
            p.open('r')
            for w in p:
                # here we apply our knowledge about the event/pkt's internal struct
                e = cbor2.loads(w)
                href = hashlib.sha256(e[0]).digest()
                e[0] = cbor2.loads(e[0])
                # rewrite the packet's byte arrays for pretty printing:
                e[0] = pcap.base64ify(e[0])
                fid = e[0][0]
                seq = e[0][1]
                if e[2] != None:
                    e[2] = cbor2.loads(e[2])

                if isinstance(e[2], dict) and e[2]['type'] == 'init':

                    try:
                        server = e[2]['cserver']
                        rep = e[2]['cserver']['replicator']
                        creplicator = replicator.Replicator(
                            rep['name'], rep['source'], rep['destination'])
                        cserver = cServer(server['name'], server['s_c_feed'],
                                          server['c_s_feed'],
                                          server['c_s_key'], 0, [],
                                          creplicator)
                        c_server_dict[server['name']] = cserver

                    except:
                        pass
            p.close()
    logging.info(f'Servers:{c_server_dict}')

    for s in c_server_dict.values():
        p = pcap.PCAP(s.c_s_feed)
        p.open('r')
        for w in p:
            # here we apply our knowledge about the event/pkt's internal struct
            e = cbor2.loads(w)
            href = hashlib.sha256(e[0]).digest()
            e[0] = cbor2.loads(e[0])
            # rewrite the packet's byte arrays for pretty printing:
            e[0] = pcap.base64ify(e[0])
            fid = e[0][0]
            seq = e[0][1]
            if e[2] != None:
                e[2] = cbor2.loads(e[2])

            if isinstance(e[2], dict) and e[2]['type'] == 'request':
                logging.debug(f'from init request  ID={e[2]["ID"]}')
                await_result(e[2]['ID'])
                next_request_ID = max(int(e[2]["ID"]), next_request_ID)

        p.close()

    logging.info(f'Highest ID: {next_request_ID}')
    pass