好友
阅读权限 255
听众
最后登录 1970-1-1
zzage
发表于 2009-8-31 01:26
;Inline Hook NtOpenFile 简单实现保护指定路径文件
;Code:zzage[LCG][DST]
;Bolg:http://hi.baidu.com/zzage
.386
.model flat ,stdcall
option casemap :none
include \masm32\include\w2k\ntstatus.inc
include \masm32\include\w2k\ntddk.inc
include \masm32\include\w2k\hal.inc
include \masm32\include\w2k\ntoskrnl.inc
includelib \masm32\lib\w2k\ntoskrnl.lib
includelib \masm32\lib\w2k\hal.lib
include \masm32\Macros\Strings.mac
.data
OldNtOpenFile proto :DWORD ,:DWORD ,:DWORD ,:DWORD ,:DWORD ,:DWORD
NtOpenFile_Addr dd ?
oldIrql dd ?
szName UNICODE_STRING <?>
uCmdPathName dw '\' ,'?' ,'?' ,'\' ,'C' ,':' ,'\' ,'W' ,'I' ,'N' ,'D' ,'O' ,'W' ,'S' ,'\' ,'S' ,'Y' ,'S' ,'T' ,'E' ,'M' ,'3' ,'2' ,'\' ,'c' ,'m' ,'d' ,'.' ,'e' ,'x' ,'e' ,0
.code
DriverUnload proc pDriverObject:PDRIVER_OBJECT
pushad
cli
mov eax , cr0
and eax ,0fffeffffh
mov cr0 , eax
invoke KeRaiseIrqlToDpcLevel
mov oldIrql,eax
mov ecx ,5
mov edi ,NtOpenFile_Addr
mov esi ,offset OldNtOpenFile
rep movsb
invoke KeLowerIrql,oldIrql
mov eax , cr0
or eax ,10000h
mov cr0 , eax
sti
popad
ret
DriverUnload endp
OldNtOpenFile proc FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,ShareAccess,OpenOptions
nop
nop
mov eax ,NtOpenFile_Addr
add eax ,5
jmp eax
OldNtOpenFile endp
MyNtOpenFile proc FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,ShareAccess,OpenOptions
mov eax ,ObjectAttributes
assume eax :ptr OBJECT_ATTRIBUTES
invoke RtlCompareUnicodeString ,[eax ].ObjectName,offset szName,TRUE
.if eax == 0
mov eax ,STATUS_ACCESS_DENIED
ret
.endif
assume eax :nothing
invoke OldNtOpenFile,FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,ShareAccess,OpenOptions
ret
MyNtOpenFile endp
DriverEntry proc pDriverObject:PDRIVER_OBJECT,pRegistryPath:PUNICODE_STRING
pushad
invoke RtlInitUnicodeString,offset szName,offset uCmdPathName
cli
mov eax , cr0
and eax ,0fffeffffh
mov cr0 , eax
invoke KeRaiseIrqlToDpcLevel
mov oldIrql,eax
invoke MmGetSystemRoutineAddress,$CCOUNTED_UNICODE_STRING("NtOpenFile" )
mov NtOpenFile_Addr,eax
mov ecx ,5
mov esi ,eax
mov edi ,offset OldNtOpenFile
rep movsb
mov edx ,offset MyNtOpenFile
sub edx ,eax
sub edx ,5
mov byte ptr [eax ],0e9h
xchg [eax +1],edx
invoke KeLowerIrql,oldIrql
mov eax , cr0
or eax ,10000h
mov cr0 , eax
sti
mov eax ,pDriverObject
assume eax :ptr DRIVER_OBJECT
mov [eax ].DriverUnload,offset DriverUnload
assume eax :nothing
popad
mov eax ,STATUS_SUCCESS
ret
DriverEntry endp
end DriverEntry