Import ECS deployed relay source and historical patches (2026-10-27)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""Current-user Windows DPAPI for device keys; never uses machine-wide scope."""
|
||||
import ctypes
|
||||
import os
|
||||
from ctypes import wintypes
|
||||
|
||||
|
||||
class Blob(ctypes.Structure):
|
||||
_fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))]
|
||||
|
||||
|
||||
def transform(data, decrypt=False):
|
||||
if os.name != "nt":
|
||||
raise RuntimeError("DPAPI is supported only on Windows")
|
||||
buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data)
|
||||
source = Blob(len(data), buffer)
|
||||
target = Blob()
|
||||
crypt = ctypes.WinDLL("crypt32", use_last_error=True)
|
||||
kernel = ctypes.WinDLL("kernel32", use_last_error=True)
|
||||
if decrypt:
|
||||
fn = crypt.CryptUnprotectData
|
||||
args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)]
|
||||
else:
|
||||
fn = crypt.CryptProtectData
|
||||
args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)]
|
||||
fn.restype = wintypes.BOOL
|
||||
if not fn(*args):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
try:
|
||||
return ctypes.string_at(target.pbData, target.cbData)
|
||||
finally:
|
||||
kernel.LocalFree.argtypes = [ctypes.c_void_p]
|
||||
kernel.LocalFree(target.pbData)
|
||||
|
||||
|
||||
def protect(data):
|
||||
return transform(data)
|
||||
|
||||
|
||||
def unprotect(data):
|
||||
return transform(data, decrypt=True)
|
||||
Reference in New Issue
Block a user