Created
June 2, 2021 06:36
-
-
Save kamysheblid/8af2e60ef2a64c32e1fcba4ee116e072 to your computer and use it in GitHub Desktop.
decrypt gnu pass files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Decrypt gnu pass files. | |
Simply give it the location of the directory storing all the .gpg files and it will dump the contents into a file. | |
''' | |
from sys import exit | |
from argparse import ArgumentParser | |
from pathlib import Path | |
from gnupg import GPG | |
import logging | |
parser = ArgumentParser() | |
parser.add_argument('-d','--dir', default='~/.password-store', required=False, metavar='dir', help='pass directory where all the .gpg files are stored, default is ~/.password-store' ) | |
parser.add_argument('-o','--out', default='decrypted.txt', required=False, help='File to output data. Default is decrypted.txt') | |
parser.add_argument('-n', action='store_true', help='Do not dump contents to file. Print to stdout') | |
parser.add_argument('-f', '--force', action='store_true', help='Force overwrite output file') | |
parser.add_argument('-v', action='count', default=0, help='Verbose, for more verbosity do -vv') | |
args = parser.parse_args() | |
#Setup logging | |
if args.v == 0: | |
logging.basicConfig(level=logging.ERROR) | |
elif args.v == 1: | |
logging.basicConfig(level=logging.INFO) | |
else: | |
logging.basicConfig(level=logging.DEBUG) | |
logging.info(args) | |
#Find password-store | |
logging.debug(f'Opening pass storage directory: {args.dir}') | |
store = Path(args.dir).expanduser() | |
if not store.exists(): | |
logging.error(f'{store.as_posix()} does not exist') | |
#Check if output file exists and is writable, then open it | |
output_file = Path(args.out).expanduser() | |
if output_file.exists() and not args.force and input(f'{output_file.as_posix()} exists, overwrite? [y/N]').lower() != 'y': | |
exit(0) | |
logging.debug(f'Opening {args.out}') | |
try: | |
output = output_file.open('w') | |
except e: | |
logging.error(f'Failed to open file {output_file.as_posix()}:',e) | |
gpg = GPG() | |
def unpack_folder(pathname): | |
if pathname.is_file() and pathname.name != '.gpg-id': | |
logging.debug(f'Decrypting {pathname}...') | |
decrypted = gpg.decrypt(pathname.read_bytes()) | |
text_out = '{}\n{}\n'.format( pathname.as_posix(), decrypted) | |
if not args.n: | |
logging.info(f'Decrypting {pathname.as_posix()}') | |
output.write(text_out) | |
logging.debug(f'Wrote:\n{text_out}') | |
else: | |
print(text_out) | |
elif pathname.is_dir(): | |
for file in pathname.iterdir(): | |
unpack_folder(file) | |
def main(): | |
unpack_folder(store) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment