追蹤 TDS 封包發現 SQL Server (7.0/2000) 的密碼加密極弱,網路傳輸與儲存兩端皆可逆向還原。
登入封包中密碼欄位經過簡單位運算混淆,非真正加密。反組譯碼還原:
.text:0065C880 mov cl, [edi] ; 取得加密位元組
.text:0065C882 mov dl, cl
.text:0065C884 xor cl, 5 ; cl ^= 0x05
.text:0065C887 xor dl, 0AFh ; dl ^= 0xAF
.text:0065C88A shr dl, 4 ; dl >>= 4
.text:0065C88D shl cl, 4 ; cl <<= 4
.text:0065C890 or dl, cl ; 合併
.text:0065C892 mov [edi], dl ; 寫回解密後位元組
void sqlpasswd_decrypt(const unsigned char *enc, unsigned char *dec) {
for (int i = 0; i < 128 && enc[i]; i++) {
unsigned char a1 = (enc[i] ^ 0x05) << 4;
unsigned char a2 = (enc[i] ^ 0xAF) >> 4;
dec[i] = a1 | a2;
}
dec[i] = 0;
}
結論:等同明文,嗅探器內嵌此演算法即可即時解密登入密碼。
master..syslogins 的 password 欄位結構 (SQL 7.0/2000):
2 bytes header: 0x0100 (固定)
4 bytes salt (key)
20 bytes hash1 (小寫密碼 + salt 的 SHA-1)
20 bytes hash2 (大寫密碼 + salt 的 SHA-1)
0x0100 1751857F DFDEC4FB618D8D18EBA5A27F615639F607CD46BE DFDEC4FB618D8D18EBA5A27F615639F607CD46BE
^salt^ ^hash1 (lower)^ ^hash2 (upper)^
觀察:純數字密碼導致 hash1 == hash2,洩露密碼無字母資訊。
CryptAcquireContext(&hProv, NULL, "Microsoft Base Cryptographic Provider v1.0", PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash);
CryptHashData(hHash, lower_pwd + salt, len, 0); // hash1
CryptHashData(hHash, upper_pwd + salt, len, 0); // hash2
CryptGetHashParam(hHash, HP_HASHVAL, out, &len, 0);
sys.sql_logins 密碼雜湊強度,弱密碼強制換發。