6

以下简短的 Python 脚本采用三个命令行参数:密码、输入路径和输出路径。然后它使用密码短语解密输入路径的内容,并将解密的内容放入输出路径。

from gpg import Context
import sys

pp = sys.argv[1]    # passphrase
enc = sys.argv[2]   # input file (assumed to be encrypted)
dec = sys.argv[3]   # output file

with open(enc, 'rb') as reader, open(dec, 'wb') as writer, Context() as ctx:

    try:

        ctx.decrypt(reader, sink=writer, passphrase=pp)

    except Exception as e:
        print(str(e), file=sys.stderr)

只要提供了正确的密码,此解密就可以正常工作,但显然会导致缓存此类正确的密码,因此任何后续解密尝试都会成功,而与提供的密码无关。(我在这篇文章的末尾给出了我的意思的更完整的说明,以及版本细节。)

显然,正在进行一些密码缓存,但我并不真正了解细节。

我想知道的是:如何修改 Python 脚本以禁用密码短语的缓存?请注意,我对如何在脚本之外禁用密码缓存不感兴趣!我希望脚本自动禁用密码缓存。那可能吗?


这是我上面提到的详细示例。该脚本./demo.py是我上面列出的来源。重要提示:下面给出的代码仅在我从命令行执行时才显示。如果我将它放在一个文件中并作为脚本执行(或获取它),那么所有使用错误密码的解密都会失败,无论之前是否使用正确的密码成功解密。

# Prologue: preparation

# First, define some variables

% ORIGINAL=/tmp/original.txt
% ENCRYPTED=/tmp/encrypted.gpg
% DECRYPTED=/tmp/decrypted.txt
% PASSPHRASE=yowzayowzayowza

# Next, create a cleartext original:

% echo 'Cool story, bro!' > "$ORIGINAL"

# Next, encrypt the original using /usr/bin/gpg

% rm -f "$ENCRYPTED"
% /usr/bin/gpg --batch --symmetric --cipher-algo=AES256 --compress-algo=zlib --passphrase="$PASSPHRASE" --output="$ENCRYPTED" "$ORIGINAL"

# Confirm encryption

% od -c "$ENCRYPTED"
0000000 214  \r 004  \t 003 002 304 006 020   %   q 353 335 212 361 322
0000020   U 001   w 350 335   K 347 320 260 224 227 025 275 274 033   X
0000040 020 352 002 006 254 331 374 300 221 265 021 376 254   9   $   <
0000060 233 275 361 226 340 177 330   !   c 372 017   & 300 352   $   k
0000100 252 205 244 336 222   N 027 200   | 211 371   r   Z   ] 353   6
0000120 261 177   b 336 026 023 367 220 354 210 265 002   :   r 262 037
0000140 367   L   H 262 370    
0000146


# Now, the demonstration proper.

# Initially, decryption with the wrong passphrase fails:

% rm -f "$DECRYPTED"
% python ./demo.py "certainly the wrong $PASSPHRASE" "$ENCRYPTED" "$DECRYPTED"
gpgme_op_decrypt_verify: GPGME: Decryption failed


# Decryption with the right passphrase succeeds:

% rm -f "$DECRYPTED"
% python ./demo.py "$PASSPHRASE" "$ENCRYPTED" "$DECRYPTED"
% od -c "$DECRYPTED"
0000000   C   o   o   l       s   t   o   r   y   ,       b   r   o   !
0000020  \n
0000021


# After the first successful decryption with the right
# passphrase, decryption with the wrong passphrase always
# succeeds:

% rm -f "$DECRYPTED"
% python ./demo.py "certainly the wrong $PASSPHRASE" "$ENCRYPTED" "$DECRYPTED"
% od -c "$DECRYPTED"
0000000   C   o   o   l       s   t   o   r   y   ,       b   r   o   !
0000020  \n
0000021


# Some relevant version info

% python -c 'import gpg; print((gpg.version.versionstr, gpg.version.gpgme_versionstr))'
('1.10.0', '1.8.0')

% gpg --version
gpg (GnuPG) 2.1.18
libgcrypt 1.7.6-beta
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/kj146/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

% python --version
Python 3.5.3

% uname -ar
Linux parakeet 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2 (2017-04-30) x86_64 GNU/Linux
4

4 回答 4

4

挖掘 Cgpgme库(这是您使用的 Python 库包装的内容),有:

https://www.gnupg.org/documentation/manuals/gpgme/Context-Flags.html#Context-Flags

"no-symkey-cache"
For OpenPGP disable the passphrase cache used for symmetrical en- and decryption.
This cache is based on the message specific salt value. Requires at least GnuPG
2.2.7 to have an effect.

我不确定上下文是如何与文件系统或 GPG 代理交互的,但您的第一次尝试应该是将此标志设置为 true。

于 2019-04-17T23:58:05.803 回答
1
  • 根据来源Context.decrypt可以使用“pinentry”获取密码,我认为默认情况下上下文使用它(在你的情况下是某种 gpg-agent)

  • 根据您的桌面环境,某些“代理”可能会用作 pinentry 的一部分,因此它可以“记住”密码。

  • 我认为你应该初始化上下文pinentry_mode=gpg.constants.SOME_CONSTANT(也许gpg.constants.PINENTRY_MODE_ERROR......我不确定:没有使用 gpgme 的经验,只是调查了文档和代码)模式:参见文档

  • 或者您可以停止/杀死gpg-agent// :其中之一正在执行“缓存”。kde-walletgnome-keyring

  • 或添加一行no-use-agent~/.gnupg/gpg.conf

  • 也许ctx.set_ctx_flag("no-symkey-cache", "1")初始化后调用会解决你的问题(见其他答案

于 2019-04-24T09:22:33.920 回答
0

GnuPG 文档的第 9.6 章下,有一个名为“所有命令和选项列表”的部分。

其中显示了一个--forget选项,您可以使用该选项:

“从缓存中刷新给定缓存 ID 的密码。”

“GnuPG Made Easy”参考手册中,在第7.5 章密钥管理下,有一个名为删除密钥的部分 ,其中包含一个函数的文档,该函数gpgme_op_delete_ext允许您删除公钥。

您还可以使用该标志删除私钥GPGME_DELETE_ALLOW_SECRET,根据文档:

“如果未设置,则仅删除公钥。如果设置,如果支持,也会删除密钥。”

注意:要跳过用户确认,您可以使用该GPGME_DELETE_FORCE标志。

祝你好运。

于 2019-04-24T10:06:36.243 回答
0

没有纯粹的pythonic方式来做到这一点。您可以使用的最 Pythonic 是将PYTHONDONTWRITEBYTECODE环境变量设置为1. 这是设置变量的代码:

import os
os.environ['PYTHONDONTWRITEBYTECODE'] = 1

注意:此代码还将禁用其他 python 脚本的缓存

于 2019-04-18T00:37:32.657 回答