-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvapi32.py
More file actions
86 lines (71 loc) · 3.01 KB
/
advapi32.py
File metadata and controls
86 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from ctypes import FormatError
from ctypes import windll, c_void_p, byref, create_string_buffer, c_int
def assert_success(success):
if not success:
raise AssertionError(FormatError())
def CryptAcquireContext():
hprov = c_void_p()
success = windll.advapi32.CryptAcquireContextA(byref(hprov), 0, 0, 1, 0xF0000000)
assert_success(success)
return hprov
def CryptCreateHash(hProv, Algid):
hCryptHash = c_void_p()
success = windll.advapi32.CryptCreateHash(hProv, Algid, 0, 0, byref(hCryptHash))
assert_success(success)
return hCryptHash
def CryptHashData(hHash, data):
bdata = create_string_buffer(data)
dwdatalen = c_int(len(data))
success = windll.advapi32.CryptHashData(hHash, bdata, dwdatalen, 0)
assert_success(success)
def CryptDeriveKey(hProv, Algid, hBaseData):
hkey = c_void_p()
success = windll.advapi32.CryptDeriveKey(hProv, Algid, hBaseData, 0x0800000, byref(hkey))
assert_success(success)
return hkey
def CryptDecrypt(hkey, encrypted_data,Final=False):
bdata = create_string_buffer(encrypted_data)
bdatalen = c_int(len(encrypted_data))
success = windll.advapi32.CryptDecrypt(hkey, 0, Final, 0, bdata, byref(bdatalen))
assert_success(success)
return bdata.raw[:bdatalen.value]
def CryptEncrypt(hkey, plain_data):
# determine output buffer length
bdatalen_test = c_int(len(plain_data))
success = windll.advapi32.CryptEncrypt(hkey, 0, 1, 0, 0, byref(bdatalen_test), len(plain_data))
assert_success(success)
out_buf_len = bdatalen_test.value
# encrypt data
bdata = create_string_buffer(plain_data, out_buf_len)
bdatalen = c_int(len(plain_data))
success = windll.advapi32.CryptEncrypt(hkey, 0, 1, 0, bdata, byref(bdatalen), out_buf_len)
assert_success(success)
return bdata.raw[:bdatalen.value]
def format1(data):
return " ".join("%02x" % a for a in data)
def decrypt(data):
result = b""
l = len(data)
i=0
while (i+8<l):
buf = data[i:i+8]
result += CryptDecrypt(hKey,buf)
i+=8
buf = data[i:]
result += CryptDecrypt(hKey,buf,True)
return result
def encrypt(data):
data_in = b"\x05\x8f\xba\x75\x65\x34\x61\x34\x61\x62\x37\x39\x2d\x38\x66\x38\x30\x2d\x34\x38\x31\x65\x2d\x39\x39\x63\x64\x2d\x66\x33\x33\x61\x64\x36\x38\x34\x39\x64\x65\x37\x00\x64\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb4\x00\x00\x00\x96"
data_in +=data
result = b""
l = len(data_in)
for x in range(l // 8):
buf = data_in[x*8:(x+1)*8]
result += CryptDecrypt(hKey,buf)
buf = data_in[l-l%8:]
result += CryptDecrypt(hKey,buf,True)
return result
hProv = CryptAcquireContext()
hCryptHash = CryptCreateHash(hProv,0x8003)
CryptHashData(hCryptHash,b"0E2682FF-DA63-4cce-9149-766063556F8F")
hKey = CryptDeriveKey(hProv,0x6801,hCryptHash)