From 04ad1dbba6a3c3d28805744d38e25f4b4b0046ee Mon Sep 17 00:00:00 2001 From: John Gaunt Date: Fri, 7 Oct 2022 21:00:05 -0400 Subject: [PATCH] added no encryption option. Also added for loops for csv and json exports as some are failing --- bitwardenBackup.py | 51 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/bitwardenBackup.py b/bitwardenBackup.py index 6eee4b2..ba6b8c5 100644 --- a/bitwardenBackup.py +++ b/bitwardenBackup.py @@ -324,6 +324,12 @@ if __name__ == "__main__": dest="debug", help="Output debug/verbose info to the console for troubleshooting." ) + parser.add_option ( + "--no-encryption", + action="store_true", + dest="no_encrypt", + help="Will only zip up export and will NOT encrypt anything." + ) opts, args = parser.parse_args(sys.argv[1:]) os_detected = platform.system() @@ -454,22 +460,50 @@ if __name__ == "__main__": # export to csv and json logger.info("Exporting vault to both CSV and JSON files") logger.debug("Exporting vault to CSV") - logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--output', os.path.join(working_directory, 'Bitwarden {} Export {}.csv'.format(email, datetime_string)) , '--format', 'csv'], capture_output=True).stdout).decode()) + file_name = 'Bitwarden {} Export {}'.format(email, datetime_string) + while True: + logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--output', os.path.join(working_directory, '{}.csv'.format(file_name)) , '--format', 'csv'], capture_output=True).stdout).decode()) + file_size = os.path.getsize('{}.csv'.format(file_name)) + if (file_size > 0): + break + else: + logger.info("CSV export did not run correctly, running export again") logger.debug("Exporting vault to JSON") - logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--output', os.path.join(working_directory, 'Bitwarden {} Export {}.json'.format(email, datetime_string)), '--format', 'json'], capture_output=True).stdout).decode()) + file_name = 'Bitwarden {} Export {}'.format(email, datetime_string) + while True: + logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--output', os.path.join(working_directory, '{}.json'.format(file_name)), '--format', 'json'], capture_output=True).stdout).decode()) + file_size = os.path.getsize('{}.json'.format(file_name)) + if (file_size > 56): + break + else: + logger.info("JSON export did not run correctly, running export again") # looking for Organizations # look for organizations logger.info("Looking for Organizations") bitwarden_organizations = json.loads(((subprocess.run([bitwarden_cli_executable, 'list', 'organizations'], capture_output=True)).stdout).decode()) + logger.info("Found {} Organiztaions.".format(len(bitwarden_organizations))) for organization in bitwarden_organizations: logger.info("Exporting organization {} vault to both CSV and JSON files".format(organization['name'])) logger.debug("Exporting organization vault to CSV") - logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, 'Bitwarden Organization {} Export {}.csv'.format(organization['name'], datetime_string)) , '--format', 'csv'], capture_output=True).stdout).decode()) + file_name = 'Bitwarden Organization {} Export {}'.format(organization['name'], datetime_string) + while True: + logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, '{}.csv'.format(file_name)) , '--format', 'csv'], capture_output=True).stdout).decode()) + file_size = os.path.getsize('{}.csv'.format(file_name)) + if (file_size > 0): + break + else: + logger.info("CSV export did not run correctly, running export again") logger.debug("Exporting organization vault to JSON") - logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, 'Bitwarden Organziation {} Export {}.json'.format(organization['name'], datetime_string)), '--format', 'json'], capture_output=True).stdout).decode()) + while True: + logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, '{}.json'.format(file_name)), '--format', 'json'], capture_output=True).stdout).decode()) + file_size = os.path.getsize('{}.json'.format(file_name)) + if (file_size > 56): + break + else: + logger.info("JSON export did not run correctly, running export again") logger.info("Downlading attachments...") bitwarden_items = json.loads(((subprocess.run([bitwarden_cli_executable, 'list', 'items'], capture_output=True)).stdout).decode()) @@ -490,17 +524,20 @@ if __name__ == "__main__": zip_filename = os.path.join(exports_directory, "Bitwarden Backup {} {}".format(email, datetime_string)) shutil.make_archive(zip_filename, format="zip", root_dir=working_directory) - logger.debug((subprocess.run([gpg_executable, '--no-options', '--batch', '--passphrase', vault_password, '--symmetric', '--cipher-algo', 'AES256', '--digest-algo', 'SHA512', '--compression-algo', 'Uncompressed', '--output', zip_filename + '.zip.gpg', zip_filename + '.zip'], capture_output=True).stdout).decode()) + if not opts.no_encrypt: + logger.debug((subprocess.run([gpg_executable, '--no-options', '--batch', '--passphrase', vault_password, '--symmetric', '--cipher-algo', 'AES256', '--digest-algo', 'SHA512', '--compression-algo', 'Uncompressed', '--output', zip_filename + '.zip.gpg', zip_filename + '.zip'], capture_output=True).stdout).decode()) logger.info("Securely deleting files") if os_detected == "Windows": # sdelete.exe .\working\ -p 5 -s logger.debug((subprocess.run([sdelete_executable, '-p', '5', '-s', working_directory], capture_output=True).stdout).decode()) - logger.debug((subprocess.run([sdelete_executable, '-p', '5', zip_filename + ".zip"], capture_output=True).stdout).decode()) + if not opts.no_encrypt: + logger.debug((subprocess.run([sdelete_executable, '-p', '5', zip_filename + ".zip"], capture_output=True).stdout).decode()) elif os_detected == "Linux": # find -depth -type f -exec shred -v -n 1 -z -u {} \; logger.debug((subprocess.run(['find', working_directory, '-depth', '-type', 'f', '-exec', 'shred', '-v', '-n', '5', '-u', '/{/}', '\/', ';'], capture_output=True).stdout).decode()) - logger.debug((subprocess.run(['shred', '-v', '-u', '-n', '5', zip_filename + '.zip'], capture_output=True).stdout).decode()) + if not opts.no_encrypt: + logger.debug((subprocess.run(['shred', '-v', '-u', '-n', '5', zip_filename + '.zip'], capture_output=True).stdout).decode()) else: logger.error((bitwarden_unlock_output.stderr).decode())