基于Go編寫一個(gè)可視化Navicat本地密碼解析器
前提
開發(fā)小組在測(cè)試環(huán)境基于docker構(gòu)建和遷移一個(gè)MySQL8.x實(shí)例,過程中大意沒有記錄對(duì)應(yīng)的用戶密碼,然后發(fā)現(xiàn)某開發(fā)同事本地Navicat記錄了根用戶,于是搜索是否能夠反解析Navicat中的密碼掩碼(這里可以基本斷定Navicat對(duì)密碼是采用了對(duì)稱加密算法),于是發(fā)現(xiàn)了這個(gè)倉庫:
-
how-does-navicat-encrypt-password
密碼的解密算法顯然是被泄露了,那么就可以利用起來。加之筆者之前花了一點(diǎn)點(diǎn)時(shí)間入門了一下Go,于是業(yè)余花了點(diǎn)時(shí)間編寫了一個(gè)GUI工具。這個(gè)工具主要功能是:在Windows系統(tǒng)下,自動(dòng)讀取Navicat在注冊(cè)列表中寫入的所有(數(shù)據(jù)庫)服務(wù)器連接數(shù)據(jù)作為列表展示,對(duì)于每個(gè)服務(wù)器連接數(shù)據(jù)的密碼嘗試進(jìn)行解密。效果如下:
大致原理
參考how-does-navicat-encrypt-password倉庫,因?yàn)?code style="padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;">Navicat兩種版本的對(duì)稱加密算法的具體算法、秘鑰和加密向量都被泄露了,得知:
-
版本一( Low):使用Blowfish/ECB/NoPadding模式 -
版本二( High):使用AES/CBC/PKCS5Padding模式
其中AES/CBC/PKCS5Padding實(shí)現(xiàn)是比較簡(jiǎn)單的,Blowfish/ECB/NoPadding在Go的原生類庫中剛好缺少了ECB解碼器,只能仔細(xì)翻閱how-does-navicat-encrypt-password的Java版本代碼并且強(qiáng)行轉(zhuǎn)換為Go實(shí)現(xiàn):
func (l *LowVersionCipher) Decrypt(input string) (string, error) {
ciphertext, err := hex.DecodeString(input)
if err != nil {
return "", err
}
if len(ciphertext)%8 != 0 {
return "", errors.New("ciphertext length must be a multiple of 8")
}
plaintext := make([]byte, len(ciphertext))
cv := make([]byte, len(l.iv))
copy(cv, l.iv)
blocksLen := len(ciphertext) / blowfish.BlockSize
leftLen := len(ciphertext) % blowfish.BlockSize
decrypter := NewECBDecrypter(l.cipher)
for i := 0; i < blocksLen; i++ {
temp := make([]byte, blowfish.BlockSize)
copy(temp, ciphertext[i*blowfish.BlockSize:(i+1)*blowfish.BlockSize])
if err != nil {
panic(err)
}
decrypter.CryptBlocks(temp, temp)
xorBytes(temp, cv)
copy(plaintext[i*blowfish.BlockSize:(i+1)*blowfish.BlockSize], temp)
for j := 0; j < len(cv); j++ {
cv[j] ^= ciphertext[i*blowfish.BlockSize+j]
}
}
if leftLen != 0 {
decrypter.CryptBlocks(cv, cv)
temp := make([]byte, leftLen)
copy(temp, ciphertext[blocksLen*blowfish.BlockSize:])
xorBytes(temp, cv[:leftLen])
copy(plaintext[blocksLen*blowfish.BlockSize:], temp)
}
return string(plaintext), nil
}
func xorBytes(a []byte, b []byte) {
for i := 0; i < len(a); i++ {
aVal := int(a[i]) & 0xff // convert byte to integer
bVal := int(b[i]) & 0xff
a[i] = byte(aVal ^ bVal) // xor aVal and bVal and typecast to byte
}
}
接著基于golang.org/x/sys/windows/registry加載Windows系統(tǒng)注冊(cè)列表下的服務(wù)器連接數(shù)據(jù)列表,Navicat多個(gè)版本測(cè)試發(fā)現(xiàn)服務(wù)器連接數(shù)保存在注冊(cè)列表的Software\PremiumSoft\Navicat\Servers目錄下,只需要全量讀取出來并且按照每個(gè)服務(wù)器連接數(shù)據(jù)的明細(xì)k-v一步一步解析即可。這個(gè)解析過程的偽代碼如下:
const NsPath = `Software\PremiumSoft\Navicat\Servers`
nsp, _ := registry.OpenKey(registry.CURRENT_USER, NsPath, registry.READ)
subKeys, _ := nsp.ReadSubKeyNames(999)
var servers []*Server
for _, subKey := range subKeys {
serverPath := strings.Join([]string{NsPath, subKey}, `\`)
sp, _ := registry.OpenKey(registry.CURRENT_USER, serverPath, registry.READ)
// 數(shù)據(jù)庫的版本
serverVersion, _, _ := sp.GetIntegerValue("ServerVersion")
// host
host, _, _ := sp.GetStringValue("Host")
// 用戶名
username, _, _ := sp.GetStringValue("UserName")
// 密碼密文
pwd, _, _ := sp.GetStringValue("Pwd")
// 端口,一般是3306
port, _, _ := sp.GetIntegerValue("Port")
realPwd := pwd
if (len(pwd) > 0){
// 解密得到密碼明文
realPwd, _ = cipher.Decrypt(pwd)
}
servers = append(servers, &Server{...})
}
小結(jié)
「提醒」 - 這個(gè)項(xiàng)目?jī)H僅是提供參考和學(xué)習(xí),供個(gè)人本地開發(fā)時(shí)候使用,切勿用于竊取他人的數(shù)據(jù)庫密碼。項(xiàng)目倉庫:
-
navicat-watcher
順帶一提使用fyne做GUI開發(fā)效果還可以,不過目前這個(gè)庫還存在比較多BUG,性能高的同時(shí)占用的資源也比較高。
(本文完 c-1-d e-a-20230802 很久沒寫文章了)
