|
本帖最后由 Tesla.Angela 于 2010-8-11 19:55 编辑
年初无聊写着玩的,大家看看吧。保护的是ANTIIFEO.EXE不被劫持。
这个写得并不好,是别人建立了键值之后我才根据KeyHandle判断是否为劫持我的项,如果是,则用ZwDeleteKey删除。
其实可以根据NtCreateKey的ObjectAttributes参数判断,不过我试过一次蓝屏了,所以害怕了,就改用KeyHandle来判断。
绕过的方法见#5。
主要代码:
PVOID GetPointer( HANDLE handle )
{
PVOID pKey;
if(!handle)
return NULL;
if(ObReferenceObjectByHandle( handle, 0, NULL, KernelMode, &pKey, NULL ) != STATUS_SUCCESS )
pKey = NULL;
return pKey;
}
ULONG GetFunctionAddr( IN PCWSTR FunctionName)
{
UNICODE_STRING UniCodeFunctionName;
RtlInitUnicodeString( &UniCodeFunctionName, FunctionName );
return (ULONG)MmGetSystemRoutineAddress( &UniCodeFunctionName );
}
ULONG GetNtCreateKeyAddr()
{
ULONG address=0;
address=0x80624792; //本机硬编码
KdPrint(("[NtCreateKey] address %x\n",address));
return address;
}
//原函数
_declspec (naked) NTSTATUS OriginalNtCreateKey
(
OUT PHANDLE KeyHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN ULONG TitleIndex,
IN PUNICODE_STRING Class OPTIONAL,
IN ULONG CreateOptions,
OUT PULONG Disposition OPTIONAL
)
{
_asm
{
//前五个字节
push 0C4h
mov eax,g_NtCreateKey
add eax,5
jmp eax
}
}
//处理函数
NTSTATUS DetourMyNtCreateKey
(
OUT PHANDLE KeyHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN ULONG TitleIndex,
IN PUNICODE_STRING Class OPTIONAL,
IN ULONG CreateOptions,
OUT PULONG Disposition OPTIONAL
)
{
PVOID pKey;
UNICODE_STRING *pUniName;
ULONG actualLen;
ANSI_STRING keyname;
NTSTATUS status;
UNICODE_STRING uStrValueName;
PCWSTR ValueName;
//execute the real function
status = OriginalNtCreateKey(KeyHandle,DesiredAccess,ObjectAttributes,TitleIndex,Class,CreateOptions,Disposition);
if (NT_SUCCESS(status))
{
pKey = GetPointer(*KeyHandle);
if(pKey!=NULL)
{
//分配内存
pUniName = ExAllocatePool(NonPagedPool, 1024*2);
pUniName->MaximumLength = 512*2;
//将pUniName里的内容清空
memset(pUniName,0,pUniName->MaximumLength);
//得到注册表项的路径
if(NT_SUCCESS(ObQueryNameString(pKey, pUniName, 512*2, &actualLen)))
{
RtlUnicodeStringToAnsiString(&keyname, pUniName, TRUE);
keyname.Buffer=_strupr(keyname.Buffer);
//判断是不是IFEO项
if (strcmp(keyname.Buffer,"\\REGISTRY\\MACHINE\\SOFTWARE\\MICROSOFT\\WINDOWS NT\\CURRENTVERSION\\IMAGE FILE EXECUTION OPTIONS\\ANTIIFEO.EXE") == 0)
{
DbgPrint("[DetourMyNtCreateKey] Some one want to hijeak me!");
//释放内存
RtlFreeAnsiString(&keyname);
if(pUniName)
ExFreePool(pUniName);
//删除键值
ZwDeleteKey(*KeyHandle);
//关闭句柄
ZwClose(*KeyHandle);
//返回成功^_^
return STATUS_SUCCESS;
}
}
}
}
return status;
} |
|