GetImageIndex / ImageToken 等接口走 ultra_sign 签名并解 bytesData; 登录改为浏览器授权或完整 Cookie,校验通过才保存。
405 lines
11 KiB
Lua
405 lines
11 KiB
Lua
local bit = require("bit")
|
|
local band, bor, bxor = bit.band, bit.bor, bit.bxor
|
|
local lshift, rshift = bit.lshift, bit.rshift
|
|
|
|
local Crypto = {}
|
|
|
|
local ALPHA62 = "br5NH9CxpvJzmEt3OqoF7jgWMIZ2UnASfc8K6XaLYylT1eVBGh4sdQkDwRP0ui"
|
|
local B64 = {
|
|
"UalVqQk8T7+5oJxC6MzIniWDrc3XERGN/wdLyp90mf2tHuvsgeSjZbBF1KAOPhY4",
|
|
"zI3XZb8T7+5opF1KJxUaBqQk90mf6MlVCEceSjAOPhY42RGN/wdLytHuvsgniWDr",
|
|
}
|
|
|
|
local function u8(x)
|
|
return band(x, 0xff)
|
|
end
|
|
|
|
local function xtime(x)
|
|
x = u8(x)
|
|
local hi = band(x, 0x80)
|
|
local y = u8(lshift(x, 1))
|
|
if hi ~= 0 then y = bxor(y, 0x1b) end
|
|
return y
|
|
end
|
|
|
|
local function mul(a, b)
|
|
local p = 0
|
|
a, b = u8(a), u8(b)
|
|
for _ = 1, 8 do
|
|
if band(b, 1) ~= 0 then p = bxor(p, a) end
|
|
local hi = band(a, 0x80)
|
|
a = u8(lshift(a, 1))
|
|
if hi ~= 0 then a = bxor(a, 0x1b) end
|
|
b = rshift(b, 1)
|
|
end
|
|
return p
|
|
end
|
|
|
|
local function rotl8(x, n)
|
|
x = u8(x)
|
|
return u8(bor(lshift(x, n), rshift(x, 8 - n)))
|
|
end
|
|
|
|
local SBOX, INV = {}, {}
|
|
for x = 0, 255 do
|
|
local inv = 0
|
|
if x ~= 0 then
|
|
for i = 1, 255 do
|
|
if mul(x, i) == 1 then inv = i; break end
|
|
end
|
|
end
|
|
local y = bxor(inv, rotl8(inv, 1), rotl8(inv, 2), rotl8(inv, 3), rotl8(inv, 4), 0x63)
|
|
SBOX[x] = y
|
|
INV[y] = x
|
|
end
|
|
|
|
local function expand192(key)
|
|
local words = {}
|
|
for i = 0, 5 do
|
|
words[i] = {
|
|
key:byte(i * 4 + 1),
|
|
key:byte(i * 4 + 2),
|
|
key:byte(i * 4 + 3),
|
|
key:byte(i * 4 + 4),
|
|
}
|
|
end
|
|
local rcon = 0x01
|
|
for i = 6, 51 do
|
|
local prev = words[i - 1]
|
|
local t1, t2, t3, t4 = prev[1], prev[2], prev[3], prev[4]
|
|
if i % 6 == 0 then
|
|
t1, t2, t3, t4 = SBOX[t2], SBOX[t3], SBOX[t4], SBOX[t1]
|
|
t1 = bxor(t1, rcon)
|
|
rcon = xtime(rcon)
|
|
end
|
|
local old = words[i - 6]
|
|
words[i] = { bxor(old[1], t1), bxor(old[2], t2), bxor(old[3], t3), bxor(old[4], t4) }
|
|
end
|
|
return words
|
|
end
|
|
|
|
local function addRound(s, words, round)
|
|
for c = 0, 3 do
|
|
local w = words[round * 4 + c]
|
|
for r = 0, 3 do
|
|
local i = c * 4 + r
|
|
s[i] = bxor(s[i], w[r + 1])
|
|
end
|
|
end
|
|
end
|
|
|
|
local function mod4(x)
|
|
return (x % 4 + 4) % 4
|
|
end
|
|
|
|
local function invShift(s)
|
|
local t = {}
|
|
for i = 0, 15 do t[i] = s[i] end
|
|
for r = 1, 3 do
|
|
for c = 0, 3 do
|
|
s[c * 4 + r] = t[mod4(c - r) * 4 + r]
|
|
end
|
|
end
|
|
end
|
|
|
|
local function invMix(s)
|
|
for c = 0, 3 do
|
|
local o = c * 4
|
|
local a, b, d, e = s[o], s[o + 1], s[o + 2], s[o + 3]
|
|
s[o] = bxor(mul(a, 14), mul(b, 11), mul(d, 13), mul(e, 9))
|
|
s[o + 1] = bxor(mul(a, 9), mul(b, 14), mul(d, 11), mul(e, 13))
|
|
s[o + 2] = bxor(mul(a, 13), mul(b, 9), mul(d, 14), mul(e, 11))
|
|
s[o + 3] = bxor(mul(a, 11), mul(b, 13), mul(d, 9), mul(e, 14))
|
|
end
|
|
end
|
|
|
|
local function decryptBlock(block, words)
|
|
local s = {}
|
|
for i = 0, 15 do s[i] = block:byte(i + 1) end
|
|
addRound(s, words, 12)
|
|
for round = 11, 1, -1 do
|
|
invShift(s)
|
|
for i = 0, 15 do s[i] = INV[s[i]] end
|
|
addRound(s, words, round)
|
|
invMix(s)
|
|
end
|
|
invShift(s)
|
|
for i = 0, 15 do s[i] = INV[s[i]] end
|
|
addRound(s, words, 0)
|
|
local out = {}
|
|
for i = 0, 15 do out[i + 1] = string.char(s[i]) end
|
|
return table.concat(out)
|
|
end
|
|
|
|
local function b64dec(s)
|
|
local map = {}
|
|
local alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
for i = 1, #alpha do map[alpha:sub(i, i)] = i - 1 end
|
|
s = s:gsub("%s", ""):gsub("=", "")
|
|
local out, buf, n = {}, 0, 0
|
|
for i = 1, #s do
|
|
local v = map[s:sub(i, i)]
|
|
if v then
|
|
buf = buf * 64 + v
|
|
n = n + 1
|
|
if n == 4 then
|
|
out[#out + 1] = string.char(
|
|
math.floor(buf / 65536) % 256,
|
|
math.floor(buf / 256) % 256,
|
|
buf % 256)
|
|
buf, n = 0, 0
|
|
end
|
|
end
|
|
end
|
|
if n == 2 then
|
|
buf = buf * 64 * 64
|
|
out[#out + 1] = string.char(math.floor(buf / 65536) % 256)
|
|
elseif n == 3 then
|
|
buf = buf * 64
|
|
out[#out + 1] = string.char(math.floor(buf / 65536) % 256, math.floor(buf / 256) % 256)
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
|
|
function Crypto.open(bytesData, buvid3, id)
|
|
local even = {}
|
|
for i = 1, #buvid3, 2 do
|
|
even[#even + 1] = buvid3:sub(i, i)
|
|
end
|
|
local head = table.concat(even):sub(1, 18)
|
|
local tail = tostring(id)
|
|
tail = tail:sub(math.max(1, #tail - 2))
|
|
local key = (head .. "web" .. tail)
|
|
if #key < 24 then key = key .. string.rep("=", 24 - #key) end
|
|
key = key:sub(1, 24)
|
|
local words = expand192(key)
|
|
local iv = key:sub(1, 16)
|
|
local raw = b64dec(bytesData)
|
|
local prev = iv
|
|
local plain = {}
|
|
for i = 1, #raw, 16 do
|
|
local block = raw:sub(i, i + 15)
|
|
local dec = decryptBlock(block, words)
|
|
local chunk = {}
|
|
for b = 1, 16 do
|
|
chunk[b] = string.char(bxor(dec:byte(b), prev:byte(b)))
|
|
end
|
|
plain[#plain + 1] = table.concat(chunk)
|
|
prev = block
|
|
end
|
|
plain = table.concat(plain)
|
|
local pad = plain:byte(#plain)
|
|
if not pad or pad < 1 or pad > 16 then return nil end
|
|
return plain:sub(1, #plain - pad)
|
|
end
|
|
|
|
local function u32hex(w)
|
|
w = band(w, 0xffffffff)
|
|
if w < 0 then w = w + 4294967296 end
|
|
return string.format("%08x", w)
|
|
end
|
|
|
|
local function rol(x, n)
|
|
return bit.rol(band(x, 0xffffffff), n)
|
|
end
|
|
|
|
local function u32n(x)
|
|
x = band(x, 0xffffffff)
|
|
if x < 0 then x = x + 4294967296 end
|
|
return x
|
|
end
|
|
|
|
local function add32(a, b, c)
|
|
local s = u32n(a) + u32n(b)
|
|
if c then s = s + u32n(c) end
|
|
return band(s % 4294967296, 0xffffffff)
|
|
end
|
|
|
|
local function processBlock(block, a, b, c, d)
|
|
for i = 0, 15 do
|
|
local o = i * 4
|
|
local w = bor(
|
|
block[o],
|
|
lshift(block[o + 1], 8),
|
|
lshift(block[o + 2], 16),
|
|
lshift(block[o + 3], 24))
|
|
a = add32(a, bor(band(b, c), band(bit.bnot(b), d)), w)
|
|
b = add32(b, bxor(c, a, d), w)
|
|
c = add32(c, bor(band(b, a), band(bit.bnot(b), d)), w)
|
|
d = add32(d, bxor(c, a, b), w)
|
|
a, b, c, d = rol(a, 5), rol(b, 7), rol(c, 3), rol(d, 11)
|
|
end
|
|
return a, b, c, d
|
|
end
|
|
|
|
local function md5v2(data)
|
|
local len = #data
|
|
local zeros = 64 - ((len + 9) % 64)
|
|
local n = len + 1 + zeros + 8
|
|
local buf = {}
|
|
for i = 0, n - 1 do buf[i] = 0 end
|
|
for i = 1, len do buf[i - 1] = data:byte(i) end
|
|
buf[len] = 0x80
|
|
local bits = len * 8
|
|
for i = 0, 7 do
|
|
buf[n - 8 + i] = bits % 256
|
|
bits = math.floor(bits / 256)
|
|
end
|
|
local a, b, c, d = 0xabcdef01, 0x12345678, 0xdeadbeef, 0xfeedcafe
|
|
for off = 0, n - 1, 64 do
|
|
local block = {}
|
|
for i = 0, 63 do block[i] = buf[off + i] end
|
|
a, b, c, d = processBlock(block, a, b, c, d)
|
|
end
|
|
local hex = u32hex(a) .. u32hex(b) .. u32hex(c) .. u32hex(d)
|
|
local out = {}
|
|
for i = 1, #hex do
|
|
local idx = hex:byte(i) % 62
|
|
out[i] = ALPHA62:sub(idx + 1, idx + 1)
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
|
|
local function xrc4(data)
|
|
local key = { 1, 3, 7 }
|
|
local S = {}
|
|
for i = 0, 255 do S[i] = i end
|
|
local j = 0
|
|
for i = 0, 255 do
|
|
j = band(j + S[i] + key[(i % 3) + 1], 255)
|
|
S[i], S[j] = S[j], S[i]
|
|
S[i] = bxor(S[i], i)
|
|
end
|
|
local out, ii, jj = {}, 0, 0
|
|
for n = 1, #data do
|
|
ii = band(ii + 1, 255)
|
|
jj = band(jj + S[ii], 255)
|
|
S[ii], S[jj] = S[jj], S[ii]
|
|
out[n] = string.char(bxor(data:byte(n), S[band(S[ii] + S[jj] + 1, 255)]))
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
|
|
local function b64v2(data, variant)
|
|
local table64 = B64[variant + 1]
|
|
local out = {}
|
|
local n = #data
|
|
for i = 0, n - 1, 3 do
|
|
local b1 = data:byte(i + 1) or 0
|
|
local b2 = data:byte(i + 2) or 0
|
|
local b3 = data:byte(i + 3) or 0
|
|
local v = b1 * 65536 + b2 * 256 + b3
|
|
for k = 0, 3 do
|
|
local shift = 18 - 6 * k
|
|
local idx = math.floor(v / (2 ^ shift)) % 64
|
|
out[#out + 1] = table64:sub(idx + 1, idx + 1)
|
|
end
|
|
end
|
|
local rem = n % 3
|
|
if rem ~= 0 then
|
|
out[#out + 1] = string.rep("=", 3 - rem)
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
|
|
local function hexTs(ts)
|
|
local hex = {}
|
|
ts = math.floor(ts)
|
|
if ts == 0 then return "0" end
|
|
while ts > 0 do
|
|
local d = ts % 16
|
|
hex[#hex + 1] = string.sub("0123456789abcdef", d + 1, d + 1)
|
|
ts = math.floor(ts / 16)
|
|
end
|
|
for i = 1, math.floor(#hex / 2) do
|
|
hex[i], hex[#hex - i + 1] = hex[#hex - i + 1], hex[i]
|
|
end
|
|
return table.concat(hex)
|
|
end
|
|
|
|
local function binTs(ts)
|
|
ts = math.floor(ts / 128)
|
|
if ts == 0 then return "0" end
|
|
local bits = {}
|
|
while ts > 0 do
|
|
bits[#bits + 1] = (ts % 2 == 1) and "1" or "0"
|
|
ts = math.floor(ts / 2)
|
|
end
|
|
for i = 1, math.floor(#bits / 2) do
|
|
bits[i], bits[#bits - i + 1] = bits[#bits - i + 1], bits[i]
|
|
end
|
|
return table.concat(bits)
|
|
end
|
|
|
|
function Crypto.sign(query, body, ts)
|
|
local params = {}
|
|
for key, value in query:gmatch("([^&=]+)=([^&]*)") do
|
|
if key ~= "ultra_sign" then params[#params + 1] = { key, value } end
|
|
end
|
|
table.sort(params, function(a, b) return a[1] < b[1] end)
|
|
local acc = {}
|
|
for i = 1, #params do
|
|
acc[#acc + 1] = params[i][1] .. params[i][2]
|
|
end
|
|
acc = table.concat(acc)
|
|
ts = math.floor(ts)
|
|
local h1 = md5v2(acc .. hexTs(ts))
|
|
local h2 = md5v2(body .. binTs(ts))
|
|
local h3 = md5v2(b64v2(xrc4(body), ts % 2))
|
|
local t = ts % 4294967296
|
|
local function sh(n)
|
|
return math.floor(ts / (2 ^ n)) % 256
|
|
end
|
|
local function tb(n)
|
|
return math.floor(t / (2 ^ n)) % 256
|
|
end
|
|
local buf = {
|
|
1, sh(23), sh(31), sh(15), 7,
|
|
h1:byte(14), h1:byte(24), h2:byte(12), h2:byte(8), h3:byte(24), h3:byte(20),
|
|
tb(8), tb(24), tb(16),
|
|
7, 0, 5, 0, 4, 1, 5, 0, 2, 0, 1, 0, 6, 2, 0, 0,
|
|
}
|
|
local x = 0
|
|
for i = 1, 28 do x = bxor(x, buf[i]) end
|
|
buf[30] = x
|
|
local order = { 23, 8, 18, 22, 17, 29, 16, 13, 5, 4, 15, 28, 11, 7, 12, 20, 25, 26, 27, 21, 24, 19, 9, 3 }
|
|
local out = { buf[1], buf[2], 0, 0, 0 }
|
|
for i = 1, #order do out[#out + 1] = buf[order[i]] end
|
|
local function cat(bytes)
|
|
local s = {}
|
|
for i = 1, #bytes do s[i] = string.char(bytes[i]) end
|
|
return table.concat(s)
|
|
end
|
|
local head = cat(out)
|
|
local ha = md5v2(head .. string.char(h1:byte(8), h2:byte(30)))
|
|
local hb = md5v2(head .. string.char(h3:byte(32)))
|
|
local sign = { ha:sub(1, 24):byte(1, 24) }
|
|
local hb24 = { hb:sub(1, 24):byte(1, 24) }
|
|
for i = 1, 24 do sign[24 + i] = hb24[i] end
|
|
local hex = hexTs(ts)
|
|
local places = { 42, 38, 24, 30, 14, 20, 6, 48, 8, 12, 18 }
|
|
for pos = 1, #places do
|
|
local ch = hex:byte(pos)
|
|
if ch then sign[places[pos]] = ch end
|
|
end
|
|
local raw = {}
|
|
for i = 1, 48 do raw[i] = string.char(sign[i] or 0) end
|
|
return table.concat(raw)
|
|
end
|
|
|
|
function Crypto.urlencode(s)
|
|
local out = {}
|
|
for i = 1, #s do
|
|
local c = s:byte(i)
|
|
local ch = string.char(c)
|
|
if ch:match("^[%w%-_%.!~%*'%(%)]$") then
|
|
out[#out + 1] = ch
|
|
else
|
|
out[#out + 1] = string.format("%%%02X", c)
|
|
end
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
|
|
return Crypto
|