背景介绍,楼主的工作是排查网吧盗号或者挖矿问题,所以需要监控到傀儡进程是谁写入的内存。
使用了论坛上的infinityhook源码来监控写入函数,却发现驱动start设置为1时,系统启动后自己设置的infinityhook失效。
经过数日排查发现AddressOfEtwpGetCycleCount的值会改变两次,一次变为无效的指针,第二次就会变成一个对的值,所以驱动启动后开启一个线程来检测这个值,代码如下
NTSTATUS ReIfhInitialize(_In_
INFINITYHOOKCALLBACK InfinityHookCallback)
{
NTSTATUS Status;
/
/
测试 CKCL 会话是否已经启动
Status
=
IfhpModifyTraceSettings(
1
);
if
(!NT_SUCCESS(Status))
{
/
/
/
/
/
/
没有启动 尝试打开
/
/
Status
=
IfhpModifyTraceSettings(
0
);
if
(!NT_SUCCESS(Status))
{
/
/
这里代表打开失败 设置就失败了
WriteErrorLogFile(
"重新初始化时尝试启动CKCL会话失败"
);
return
Status;
}
Status
=
IfhpModifyTraceSettings(
1
);
if
(!NT_SUCCESS(Status))
{
WriteErrorLogFile(
"重新初始化时尝试设置syscall失败"
);
return
Status;
}
}
/
/
/
/
We need to resolve certain unexported symbols.
if
(!IfhpResolveSymbols())
{
WriteErrorLogFile(
"重新初始化时IfhpResolveSymbols失败"
);
return
STATUS_ENTRYPOINT_NOT_FOUND;
}
/
/
We care about overwriting the GetCpuClock (
+
0x28
) pointer
in
/
/
this structure.
/
/
PVOID
*
AddressOfEtwpGetCycleCount;
if
(ISWIN7(g_oviEx)) {
/
/
WIN7系统
AddressOfEtwpGetCycleCount
=
(PVOID
*
)((uintptr_t)CkclWmiLoggerContext
+
OFFSET_WMI_LOGGER_CONTEXT_CPU_CYCLE_CLOCK_WIN7);
/
/
offset:
0x18
}
else
{
/
/
WIN10系统
AddressOfEtwpGetCycleCount
=
(PVOID
*
)((uintptr_t)CkclWmiLoggerContext
+
OFFSET_WMI_LOGGER_CONTEXT_CPU_CYCLE_CLOCK);
/
/
offset:
0x28
}
/
/
/
/
Replace this function pointer with our own. Each time syscall
/
/
is
logged by ETW, it will invoke our new timing function.
/
/
替换掉该地址的值为自己的函数 HOOK的关键步骤
_try
{
/
/
保存原来的值
static PVOID
*
OldAddressOfEtwpGetCycleCount
=
AddressOfEtwpGetCycleCount;
/
/
如果原来的值被修改 并且指针有效的情况下修改该指针即可成功,(会出现一次无效指针)
if
(AddressOfEtwpGetCycleCount !
=
OldAddressOfEtwpGetCycleCount && MmIsAddressValid(AddressOfEtwpGetCycleCount))
{
*
AddressOfEtwpGetCycleCount
=
IfhpInternalGetCpuClock;
PsTerminateSystemThread(
0
);
}
}
_except(EXCEPTION_EXECUTE_HANDLER)
{
WriteErrorLogFile(
"重新初始化时AddressOfEtwpGetCycleCount是无效的指针"
);
return
STATUS_UNSUCCESSFUL;
}
return
STATUS_SUCCESS;
}
NTSTATUS ReIfhInitialize(_In_
INFINITYHOOKCALLBACK InfinityHookCallback)
{
NTSTATUS Status;
/
/
测试 CKCL 会话是否已经启动
Status
=
IfhpModifyTraceSettings(
1
);
if
(!NT_SUCCESS(Status))
{
/
/
/
/
/
/
没有启动 尝试打开
/
/
Status
=
IfhpModifyTraceSettings(
0
);
if
(!NT_SUCCESS(Status))
{
/
/
这里代表打开失败 设置就失败了
WriteErrorLogFile(
"重新初始化时尝试启动CKCL会话失败"
);
return
Status;
}
Status
=
IfhpModifyTraceSettings(
1
);
if
(!NT_SUCCESS(Status))
{
WriteErrorLogFile(
"重新初始化时尝试设置syscall失败"
);
return
Status;
}
}
/
/
/
/
We need to resolve certain unexported symbols.
if
(!IfhpResolveSymbols())
{
WriteErrorLogFile(
"重新初始化时IfhpResolveSymbols失败"
);
return
STATUS_ENTRYPOINT_NOT_FOUND;
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)