-
Notifications
You must be signed in to change notification settings - Fork 4
/
clipsp-unpack.py
222 lines (164 loc) · 6.77 KB
/
clipsp-unpack.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from qiling.os.windows.const import *
from qiling.os.windows.fncc import *
from qiling.os.const import *
from qiling.os.windows.utils import *
from qiling.os.windows.thread import *
from qiling.os.windows.handle import *
from qiling.exception import *
from qiling.os.windows.api import *
from qiling.os.windows.structs import *
from qiling import *
@winsdkapi(cc=STDCALL, replace_params={"FastMutex": POINTER})
def hook_ExAcquireFastMutex(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"FastMutex": POINTER})
def hook_ExReleaseFastMutex(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"FastMutex": POINTER})
def hook_KeReleaseGuardedMutex(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={
"VirtualAddress": POINTER,
"Length": ULONG,
"SecondaryBuffer": BOOLEAN,
"ChargeQuota": BOOLEAN,
"Irp": POINTER,
})
def hook_IoAllocateMdl(ql, address, params):
objcls = {
QL_ARCH.X86 : MDL32,
QL_ARCH.X8664 : MDL64
}[ql.archtype]
mdl = objcls() # MDL64()
addr = ql.os.heap.alloc(ctypes.sizeof(objcls))
mdl.Next.value = 0
mdl.Size = params['Length']
mdl.MdlFlags = 1 # locked
mdl.Process.value = ql.eprocess_address
mdl.MappedSystemVa.value = params['VirtualAddress']
mdl.StartVa.value = params['VirtualAddress']
mdl.ByteCount = params['Length']
mdl.ByteOffset = 0
ql.mem.write(addr, bytes(mdl)[:])
return addr
@winsdkapi(cc=STDCALL, replace_params={"MemoryDescriptorList": POINTER,"AccessMode": ULONG,"Operation": ULONG})
def hook_MmProbeAndLockPages(ql, addr, params):
return None
# might need to update MDL VA member
@winsdkapi(cc=STDCALL, replace_params={
"MemoryDescriptorList": POINTER,
"VirtualAddress": POINTER,
"Size": ULONG,
"Flags": ULONG,
})
def hook_MmChangeImageProtection(ql, addr, params):
return True
@winsdkapi(cc=STDCALL, replace_params={"AddressWithinSection": POINTER})
def hook_MmLockPagableImageSection(ql, addr, params):
return params["AddressWithinSection"]
@winsdkapi(cc=STDCALL, replace_params={"ImageSectionHandle": POINTER})
def hook_MmUnlockPagableImageSection(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"MemoryDescriptorList": POINTER})
def hook_MmUnlockPages(ql, addr, params):
MemoryDescriptorList = params['MemoryDescriptorList']
if ql.archtype == QL_ARCH.X8664:
mdl_buffer = ql.mem.read(MemoryDescriptorList, ctypes.sizeof(MDL64))
mdl = MDL64.from_buffer(mdl_buffer)
mdl.Flags = 0
else:
mdl_buffer = ql.mem.read(MemoryDescriptorList, ctypes.sizeof(MDL32))
mdl = MDL32.from_buffer(mdl_buffer)
mdl.Flags = 0
ql.mem.write(addr, bytes(mdl)[:])
@winsdkapi(cc=STDCALL, replace_params={"Mdl": POINTER})
def hook_IoFreeMdl(ql, address, params):
addr = params['Mdl']
if ql.archtype == QL_ARCH.X8664:
mdl_buffer = ql.mem.read(addr, ctypes.sizeof(MDL64))
mdl = MDL64.from_buffer(mdl_buffer)
else:
mdl_buffer = ql.mem.read(addr, ctypes.sizeof(MDL32))
mdl = MDL32.from_buffer(mdl_buffer)
size = mdl.Size
va = mdl.StartVa.value
print(f"Dumping {hex(size)} bytes to section_{hex(va)}")
mem = ql.mem.read(va, size)
with open(f"section_{hex(va)}", "wb") as f:
f.write(mem)
ql.os.heap.free(addr)
return None
@winsdkapi(cc=STDCALL, replace_params={"BaseAddress": POINTER, "MemoryDescriptorList": POINTER})
def hook_MmUnmapLockedPages(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"PushLock": POINTER, "Flags": ULONG})
def hook_FltAcquirePushLockSharedEx(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"PushLock": POINTER, "Flags": ULONG})
def hook_FltAcquirePushLockExclusiveEx(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"PushLock": POINTER, "Flags": ULONG})
def hook_FltReleasePushLockEx(ql, addr, params):
return None
@winsdkapi(cc=STDCALL, replace_params={"PushLock": POINTER})
def hook_FltInitializePushLock(ql, addr, params):
return None
# read string from memory address
def readstr_wide(ql, addr):
res = ""
while True:
# read one byte at a time
c = ql.mem.read(addr, 2).decode()
if c == '\x00\x00':
break
res += c
addr += 2
return res
@winsdkapi(cc=STDCALL, replace_params={
"SourceID": POINTER,
"CustomValue": POINTER,
"DefaultPath": POINTER,
"StateLocationType": ULONG,
"TargetPath": POINTER,
"BufferLengthIn": ULONG,
"BufferLengthOut": POINTER,
})
def hook_RtlGetPersistedStateLocation(ql, address, params):
srcid = params["SourceID"]
custom = params["CustomValue"]
state_type = params["StateLocationType"]
target = params["TargetPath"]
keys = ["\Registry\Machine\System\CurrentControlSet\Control\StateSeparation\RedirectionMap\Keys",
"\Registry\Machine\System\CurrentControlSet\Control\StateSeparation\RedirectionMap\Files"]
key = keys[state_type]
print(f"key: {key}")
print(f"srcid: {readstr_wide(ql, srcid)} {readstr_wide(ql, custom)}")
ql.os.registry_manager.access(key)
return 0
def trace(ql, address, size, md):
buf = ql.mem.read(address, size)
for i in md.disasm(buf, address):
print(":: 0x%x:\t%s\t%s" %(i.address, i.mnemonic, i.op_str))
if __name__ == "__main__":
ql = Qiling(["ClipSp.sys"], "D:\\qiling\\examples\\rootfs\\x8664_windows", verbose=QL_VERBOSE.DEBUG)
md = ql.create_disassembler()
md.detail = True
ql.set_api("ExAcquireFastMutex", hook_ExAcquireFastMutex)
ql.set_api("ExReleaseFastMutex", hook_ExReleaseFastMutex)
ql.set_api("IoAllocateMdl", hook_IoAllocateMdl)
ql.set_api("MmProbeAndLockPages", hook_MmProbeAndLockPages)
ql.set_api("MmChangeImageProtection", hook_MmChangeImageProtection)
ql.set_api("MmLockPagableImageSection", hook_MmLockPagableImageSection)
ql.set_api("MmUnlockPages", hook_MmUnlockPages)
ql.set_api("IoFreeMdl", hook_IoFreeMdl)
ql.set_api("MmUnmapLockedPages", hook_MmUnmapLockedPages)
ql.set_api("KeReleaseGuardedMutex", hook_KeReleaseGuardedMutex)
ql.set_api("FltAcquirePushLockSharedEx", hook_FltAcquirePushLockSharedEx)
ql.set_api("FltReleasePushLockEx", hook_FltReleasePushLockEx)
ql.set_api("MmUnlockPagableImageSection", hook_MmUnlockPagableImageSection)
ql.set_api("FltAcquirePushLockExclusiveEx", hook_FltAcquirePushLockExclusiveEx)
ql.set_api("RtlGetPersistedStateLocation", hook_RtlGetPersistedStateLocation)
ql.set_api("FltInitializePushLock", hook_FltInitializePushLock)
ql.reg.rcx = 0
ql.reg.rdx = ql.os.heap.alloc(0x30)
ql.run(begin=0x1C00F98FC, end=0x1C00F992F)