Skip to content

Instantly share code, notes, and snippets.

@Dil4rd
Created February 3, 2021 20:21
Show Gist options
  • Save Dil4rd/d539aceee488e058eb93f566686b164d to your computer and use it in GitHub Desktop.
Save Dil4rd/d539aceee488e058eb93f566686b164d to your computer and use it in GitHub Desktop.
Synology NAS DS220j: findhost password encode/decode
import numpy as np
mtrx64 = np.matrix([
[0.0, 2.0, 2.0, -3.0, 0.0, 1.0, 3.0, -1.0],
[1.0, 1.0, -2.0, 3.0, -1.0, 0.0, 0.0, 5.0],
[-2.0, 1.0, 1.0, -1.0, 3.0, 0.0, -1.0, -2.0],
[-1.0, 0.0, 0.0, 0.0, 2.0, -3.0, -4.0, 1.0],
[0.0, -2.0, 1.0, 2.0, -2.0, 1.0, -2.0, -1.0],
[-1.0, 2.0, 0.0, 2.0, -2.0, -2.0, 1.0, 0.0],
[2.0, -4.0, 3.0, -2.0, 1.0, 5.0, 3.0, 1.0],
[1.0, 0.0, -5.0, 0.0, -1.0, -2.0, -1.0, -3.0]
])
mtri64 = mtrx64.I
mapd2c = b'UPX-BkYa4Fyi2DjcLef6WmOA8pZrshQ+uv7Vwx3G9oHb1EIJKzMg5NqRSCtTld0n'
mapc2d = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1f, 0x0,
0x3, 0x0, 0x0, 0x3e, 0x2c, 0xc, 0x26, 0x8, 0x34, 0x13, 0x22, 0x18, 0x28,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x17, 0x4, 0x39, 0xd, 0x2d, 0x9, 0x27,
0x2a, 0x2e, 0x2f, 0x30, 0x10, 0x32, 0x35, 0x16, 0x1, 0x1e, 0x37, 0x38, 0x3b,
0x0, 0x23, 0x14, 0x2, 0x6, 0x1a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x2b, 0xf,
0x3d, 0x11, 0x12, 0x33, 0x1d, 0xb, 0xe, 0x5, 0x3c, 0x15, 0x3f, 0x29, 0x19,
0x36, 0x1b, 0x1c, 0x3a, 0x20, 0x21, 0x24, 0x25, 0xa, 0x31, 0x0, 0x0, 0x0, 0x0, 0x0]
def matrix_encode(password):
# prepare password matrix
pwd_aligned_len = (len(password) + 7) & 0x7FF8
pwd_mtrx = np.zeros( (pwd_aligned_len, 8) )
for i in range(len(password)):
pwd_mtrx[i//8][i%8] = password[i]
# multiply password matrix and const matrix
out_mtrx = pwd_mtrx.dot(mtrx64)
#
out = []
out_len = 2*(pwd_aligned_len)
for i in range(out_len):
el = int(out_mtrx[i//8,i%8])
out.append(mapd2c[el & 0x3f])
out.append(mapd2c[(el >> 6) & 0x3f])
return bytes(out)
def matrix_decode(epassword):
if len(epassword) & (7<<1) != 0:
return None
# prepare password matrix
epwd_aligned_len = len(epassword) >> 4
epwd_mtrx = np.zeros( (epwd_aligned_len, 8) )
for i in range(len(password)//2):
val = mapc2d[epassword[i*2]]
val |= mapc2d[epassword[i*2+1]] << 6
val_flt = float(val) if val & 0x800 else float()
epwd_mtrx[i//8][i%8] = val_flt
#
pwd_mtrx = epwd_mtrx.dot(mtri64)
out = [int(pwd_mtrx[i//8,i%8] + 0.01)&0xff for i in range(len(epassword)//2)]
return bytes(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment