Esempio n. 1
0
def copy_file(f1, f2, nonce, client_id, payload_ses):
    (ses_key, ses_suite) = extract_ses_key(payload_ses)
    asking_client_id = extract_asking_client_id(payload_ses)

    assert (asking_client_id == client_id)

    src = ses_suite.decrypt(encode_data(f1)).decode()
    dest = ses_suite.decrypt(encode_data(f2)).decode()
    dec_nonce = ses_suite.decrypt(encode_data(nonce)).decode()
    dec_nonce = nonce_mod(dec_nonce)

    print(f'[cp] Executing cp with src: {src}, dest: {dest} for {client_id}')

    success = False

    try:
        shutil.copyfile(src, dest)
        message = 'Successfully copied'
        success = True
        # return Reply(success=True, message='Successfully copied', nonce=dec_nonce)
    except shutil.SameFileError:
        message = 'Error: Can not copy to the same file'
    except IsADirectoryError:
        message = 'Error: Copying folders not allowed'
    except PermissionError:
        message = 'Error: Permission denied'
    except IOError:
        message = f'Error: src file ({src}) does not exist'
    except Exception:
        message = 'Error while copying file'
    message = ses_suite.encrypt(encode_data(message))
    nonce = ses_suite.encrypt(encode_data(dec_nonce))
    return Reply(success=success, message=message, nonce=nonce)
Esempio n. 2
0
def cat(file_arg, nonce, client_id, payload_ses):
    (ses_key, ses_suite) = extract_ses_key(payload_ses)
    asking_client_id = extract_asking_client_id(payload_ses)

    assert (asking_client_id == client_id)

    file_name = ses_suite.decrypt(encode_data(file_arg)).decode()
    dec_nonce = ses_suite.decrypt(encode_data(nonce)).decode()
    dec_nonce = nonce_mod(dec_nonce)

    print(f'[cat] Executing cat with file: {file_name} for {client_id}')

    nonce = ses_suite.encrypt(encode_data(dec_nonce))

    success = False

    try:
        f = open(file_name, 'r')
        text = f.read()
        f.close()
        text_enc = ses_suite.encrypt(encode_data(text))
        return Reply(success=True, data=text_enc, nonce=nonce)
    except FileNotFoundError:
        message = 'Error: File does not exist'
    except Exception:
        message = 'Error while reading file'
    message = ses_suite.encrypt(encode_data(message))
    return Reply(success=False, message=message, nonce=nonce)
Esempio n. 3
0
def get_session_key(nonce=42):
    global OFFSET_A
    global ACTIVE_PORT
    if ACTIVE_PORT == 0:
        print('No active server')
        return
    nonce = secrets.randbelow(100)

    ses = COORDINATOR.get_enc_session_key(OFFSET_A, ACTIVE_PORT, nonce)

    for_client = ses['for_a']
    port_b_recv = for_client['port_b']
    ses_key_recv = for_client['key_ab']
    nonce_recv = for_client['nonce']

    ses_key = KEY_AS_SUITE.decrypt(encode_data(ses_key_recv)).decode()
    port_b = KEY_AS_SUITE.decrypt(encode_data(port_b_recv)).decode()
    nonce_dec = KEY_AS_SUITE.decrypt(encode_data(nonce_recv)).decode()

    verify_nonce_handler(nonce, nonce_dec, True)

    LOG.log(f'Session key (Kab): {ses_key}')

    for_fs = ses['for_b']
    return (ses_key, for_fs)
Esempio n. 4
0
def test(payload_arg, payload_ses):
    print(payload_ses)
    # nonce_recv = payload_arg['nonce']
    (ses_key, ses_suite) = extract_ses_key(payload_ses)
    pay_arg_dec = ses_suite.decrypt(encode_data(payload_arg)).decode()
    print(pay_arg_dec)
    test_resp = {'pay_arg': pay_arg_dec}
    return test_resp
Esempio n. 5
0
def extract_asking_client_id(payload_ses):
    client_id_enc = payload_ses['client_id']
    client_id = KEY_BS_SUITE.decrypt(encode_data(client_id_enc)).decode()
    try:
        client_id = int(client_id)
    except ValueError:
        client_id = None
    return client_id
Esempio n. 6
0
def list_directory(nonce, client_id, payload_ses):
    (ses_key, ses_suite) = extract_ses_key(payload_ses)
    asking_client_id = extract_asking_client_id(payload_ses)

    assert (asking_client_id == client_id)

    dec_nonce = ses_suite.decrypt(encode_data(nonce)).decode()
    dec_nonce = nonce_mod(dec_nonce)

    print(f'[ls] Executing ls for {client_id}')

    nonce = ses_suite.encrypt(encode_data(dec_nonce))

    files = os.listdir(os.getcwd())
    files = serialize_list(files)
    files = ses_suite.encrypt(encode_data(files))

    return Reply(success=True, nonce=nonce, data=files)
Esempio n. 7
0
def get_enc_session_key(offset_a, port_b, nonce):
    key_ab = Fernet.generate_key()
    print(f'Generating session key for client {offset_a} and fs {port_b}')
    print(f'Kab: {key_ab}')
    key_as = CLIENT_KEYS[offset_a % 10]
    key_bs = FS_KEYS[port_b % 10]
    key_as_suite = Fernet(key_as)
    key_bs_suite = Fernet(key_bs)
    for_a = {
        'port_b': key_as_suite.encrypt(encode_data(port_b)),
        'key_ab': key_as_suite.encrypt(key_ab),
        'nonce': key_as_suite.encrypt(encode_data(nonce)),
    }
    for_b = {
        'client_id': key_bs_suite.encrypt(encode_data(offset_a)),
        'key_ab': key_bs_suite.encrypt(key_ab)
    }
    resp = {'for_a': for_a, 'for_b': for_b}
    return resp
Esempio n. 8
0
def extract_ses_key(payload_ses):
    ses_key_enc = payload_ses['key_ab']
    ses_key = KEY_BS_SUITE.decrypt(encode_data(ses_key_enc)).decode()
    print('Kab: ', ses_key)
    return (ses_key, Fernet(ses_key))
Esempio n. 9
0
def cli():
    str_inp = input('client> ')
    tokens = ' '.join(str_inp.split(' ')).split()

    if len(tokens) == 0:
        print('Empty input')
    else:
        cmd = tokens[0]
        num_tokens = len(tokens) - 1
        if cmd not in supported_commands:
            print(f'{cmd}: Command not found')
        elif num_tokens != supported_commands[cmd]:
            print(f'Syntax error: {cmd}: argument mismatch')
        else:
            try:
                if cmd == 'help':
                    help_message()

                elif cmd == 'pwd':
                    print(pwd())

                elif cmd == 'ls':
                    if is_mount_point_root():
                        update_fileservers()
                        print_list(map(get_friendly_name, FILESERVERS))
                    else:
                        (ses_key, enc_ses_key) = get_session_key()
                        ses_suite = Fernet(ses_key)
                        nonce2 = secrets.randbelow(100)
                        nonce_enc = ses_suite.encrypt(encode_data(nonce2))

                        resp = ACTIVE_FILESERVER.list_directory(nonce_enc, OFFSET_A, enc_ses_key)

                        files_enc = resp['data']
                        nonce_recv_enc = resp['nonce']
                        files = ses_suite.decrypt(encode_data(files_enc)).decode()
                        files = deserialize_list(files)
                        nonce_recv = ses_suite.decrypt(encode_data(nonce_recv_enc)).decode()

                        verify_nonce_handler(nonce2, nonce_recv)

                        print_list(files)

                elif cmd == 'cp' and ACTIVE_FILESERVER:
                    (ses_key, enc_ses_key) = get_session_key()
                    ses_suite = Fernet(ses_key)
                    file_1 = ses_suite.encrypt(encode_data(tokens[1]))
                    file_2 = ses_suite.encrypt(encode_data(tokens[2]))
                    nonce2 = secrets.randbelow(100)
                    nonce_enc = ses_suite.encrypt(encode_data(nonce2))

                    resp = ACTIVE_FILESERVER.copy_file(file_1, file_2, nonce_enc, OFFSET_A, enc_ses_key)

                    msg_enc = resp['message']
                    nonce_recv_enc = resp['nonce']
                    msg = ses_suite.decrypt(encode_data(msg_enc)).decode()
                    nonce_recv = ses_suite.decrypt(encode_data(nonce_recv_enc)).decode()

                    verify_nonce_handler(nonce2, nonce_recv)

                    print(msg)

                elif cmd == 'cat' and ACTIVE_FILESERVER:
                    (ses_key, enc_ses_key) = get_session_key()
                    ses_suite = Fernet(ses_key)
                    file_arg = ses_suite.encrypt(encode_data(tokens[1]))
                    nonce2 = secrets.randbelow(100)
                    nonce_enc = ses_suite.encrypt(encode_data(nonce2))

                    resp = ACTIVE_FILESERVER.cat(file_arg, nonce_enc, OFFSET_A, enc_ses_key)
                    nonce_recv_enc = resp['nonce']
                    nonce_recv = ses_suite.decrypt(encode_data(nonce_recv_enc)).decode()

                    verify_nonce_handler(nonce2, nonce_recv)

                    if resp['success'] is True:
                        dec_data = resp['data']
                        dec_data = ses_suite.decrypt(encode_data(dec_data)).decode()
                        print(dec_data)
                    else:
                        dec_msg = resp['message']
                        dec_msg = ses_suite.decrypt(encode_data(dec_msg)).decode()
                        print(dec_msg)

                elif cmd == 'cd':
                    change_active_fileserver(tokens[1])

                elif cmd == 'exit':
                    exit()

                elif cmd == 'clear':
                    clear_screen()

                elif cmd == 'test':
                    (ses_key, enc_ses_key) = get_session_key()
                    ses_suite = Fernet(ses_key)
                    payload_arg = ses_suite.encrypt(encode_data(tokens[1]))
                    recv_dict = ACTIVE_FILESERVER.test(payload_arg, enc_ses_key)
                    print(recv_dict)
            except ConnectionRefusedError:
                print(f'{cmd}: Error in fetching files')
                change_active_fileserver(ROOT)