Python 解密 Navicat导出的数据库连接,Navicat数据库连接导入DBeaver。

最近公司收到Navicat律师告知书,让停止使用Navicat,用了那么久的数据库连接工具,不得不换其他的。

最终选择了开源的DBeaver。

下载链接:https://dbeaver.io

下载开源版本:

 也可以用这个地址:

https://objects.githubusercontent.com/github-production-release-asset-2e65be/44662669/30fdb247-57eb-4183-b836-19d8d625434e?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20241219%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241219T061958Z&X-Amz-Expires=300&X-Amz-Signature=f55c4bc0a182291a6e29415848ed64ef95a4e3f039e02c8214c6c1de88627af3&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Ddbeaver-ce-24.3.0-x86_64-setup.exe&response-content-type=application%2Foctet-stream

若不能使用的话,自己进入上面的页面下载。

收费版本去这:https://dbeaver.com/download/,收费的有5个版本,都挺贵的。收费的话,还是navicat便宜。

-----------------------------------------------------------------------------------

安装完DBeaver后,把Navicat导出的connections.ncx文件直接导入DBeaver。直接访问提示连接失败,因为connections.ncx文件里的密码都是加密的。

如图:

 网上找了许久方法,有PHP的,复制代码去线上执行,反正跑不动,有python的,但也搞不定。

后来自己写了一个,大家可以直接使用:

#!/usr/bin/env python3
import sys
from Crypto.Hash import SHA1
from Crypto.Cipher import AES, Blowfish
from Crypto.Util import strxor, Padding
import xml.etree.ElementTree as ET


class Navicat11Crypto:

    def __init__(self, Key = b'3DC5CA39'):
        self._Key = SHA1.new(Key).digest()
        self._Cipher = Blowfish.new(self._Key, Blowfish.MODE_ECB)
        self._IV = self._Cipher.encrypt(b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')

    def decrypt_string(self, s: str):
        if type(s) != str:
            raise TypeError('Parameter s must be str.')
        else:
            plaintext = b''
            ciphertext = bytes.fromhex(s)
            cv = self._IV
            full_round, left_length = divmod(len(ciphertext), 8)
            for i in range(0, full_round * 8, 8):
                t = self._Cipher.decrypt(ciphertext[i:i + 8])
                t = strxor.strxor(t, cv)
                plaintext += t
                cv = strxor.strxor(cv, ciphertext[i:i + 8])
            if left_length != 0:
                cv = self._Cipher.encrypt(cv)
                plaintext += strxor.strxor(ciphertext[8 * full_round:], cv[:left_length])
            return plaintext.decode('ascii')


if __name__ == '__main__':
    pc = Navicat11Crypto()
    xml_path_x = r'C:\Users\xxxx\connections-all-20241219.ncx'
    xml_path_y = r"C:\Users\xxxxx\connections.ncx"
    tree = ET.parse(xml_path_y)
    root_element = tree.getroot()
    for child in root_element:
        print('---------------------------------')
        print('ConnectionName:', child.attrib['ConnectionName'])
        print('Host:', child.attrib['Host'])
        print('Port:', child.attrib['Port'])
        print('UserName:', child.attrib['UserName'])
        Password1 = pc.decrypt_string(child.attrib['Password'])
        print('Password:', Password1)
        print('SSH_Host:', child.attrib['SSH_Host'])
        print('SSH_Port:', child.attrib['SSH_Port'])
        print('SSH_UserName:', child.attrib['SSH_UserName'])
        Password2 = pc.decrypt_string(child.attrib['SSH_Password'])
        print('SSH_Password:', Password2)
        print('------------------------------------')

执行结果:

 得到密码后,替换密码即可:

另外,通过导入navicat的数据库连接文件,ssh默认没有加载,需要自己手动配置。

 

 

posted @ 2024-12-19 18:10  drewgg  阅读(17)  评论(0编辑  收藏  举报