Skip to content

Instantly share code, notes, and snippets.

@athossampayo
Forked from felipou/decrypt_dbeaver.py
Last active January 24, 2024 14:17
Show Gist options
  • Save athossampayo/c028acbfe3b9d0aa0ff230d4c9e35c83 to your computer and use it in GitHub Desktop.
Save athossampayo/c028acbfe3b9d0aa0ff230d4c9e35c83 to your computer and use it in GitHub Desktop.
DBeaver password decryption script
# https://stackoverflow.com/questions/39928401/recover-db-password-stored-in-my-dbeaver-connection
# requires pycryptodome lib (pip install pycryptodome)
import sys
import base64
import os
import json
from Crypto.Cipher import AES
default_paths = [
'~/Library/DBeaverData/workspace6/General/.dbeaver/',
'~/.local/share/DBeaverData/workspace6/General/.dbeaver/',
'~/.local/share/.DBeaverData/workspace6/General/.dbeaver/',
'~/AppData/Roaming/DBeaverData/workspace6/General/.dbeaver/',
]
cred_file = 'credentials-config.json'
conns_file = 'data-sources.json'
if len(sys.argv) < 2:
for path in default_paths:
cred_path = os.path.join(path, cred_file)
conns_path = os.path.join(path, conns_file)
filepath = os.path.expanduser(cred_path)
try:
f = open(filepath, 'rb')
f.close()
break
except Exception as e:
pass
else:
filepath = sys.argv[1]
print(filepath)
#PASSWORD_DECRYPTION_KEY = bytes([-70, -69, 74, -97, 119, 74, -72, 83, -55, 108, 45, 101, 61, -2, 84, 74])
PASSWORD_DECRYPTION_KEY = bytes([186, 187, 74, 159, 119, 74, 184, 83, 201, 108, 45, 101, 61, 254, 84, 74])
with open(filepath, 'rb') as f:
data = f.read()
decryptor = AES.new(PASSWORD_DECRYPTION_KEY, AES.MODE_CBC, data[:16])
padded_output = decryptor.decrypt(data[16:])
output = padded_output.rstrip(padded_output[-1:])
json_creds = json.loads(output)
with open(os.path.expanduser(conns_path), 'r') as f:
json_conns = json.load(f)['connections']
for k,v in json_conns.items():
# print(v['configuration'].keys())
name = v['configuration'].get('name')
host = v['configuration'].get('host')
database = v['configuration'].get('database')
url = v['configuration'].get('url')
port = v['configuration'].get('port')
json_creds[k]['#connection']['name'] = name
json_creds[k]['#connection']['host'] = host
json_creds[k]['#connection']['database'] = database
json_creds[k]['#connection']['url'] = url
json_creds[k]['#connection']['port'] = port
try:
print(json.dumps(json_creds, indent=4, sort_keys=True))
except:
print(output)
@athossampayo
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment