接着是几个HOOK
NTSTATUS
NTAPI
NewZwTerminateProcess(
IN HANDLE ProcessHandle OPTIONAL,
IN NTSTATUS ExitStatus
)
{
PEPROCESS process_to_kill;
if (ObReferenceObjectByHandle(ProcessHandle,GENERIC_READ,NULL,KernelMode,
&process_to_kill,0) == STATUS_SUCCESS){
//if ( PEPROCESS2PROTECTED== process_to_kill &&
if ( (PidVerify(process_to_kill) == TRUE || ProtectIDVerify(process_to_kill) == TRUE) &&
PsGetCurrentProcess() != process_to_kill)
return STATUS_ACCESS_DENIED;
}
return Old_ZwTerminateProcess(ProcessHandle,ExitStatus);
}
NTSTATUS
NTAPI
NewZwOpenProcess(
IN PHANDLE ProcessHandle,
ACCESS_MASK MASK,
POBJECT_ATTRIBUTES attr,
PCLIENT_ID cid1
)
{
PEPROCESS EProcess;
if(cid1 != NULL)
{
DbgPrint("myZwOpneProcess Begin\n");
PsLookupProcessByProcessId( *(ULONG *)cid1, &EProcess);
//if ((MASK != 0x401 ) && (MASK != 0x400 ) && PEPROCESS2PROTECTED== EProcess &&
if ((MASK != 0x401 ) && (MASK != 0x400 ) && PidVerify(EProcess) &&
PsGetCurrentProcess() != EProcess) return STATUS_ACCESS_DENIED;
DbgPrint("myZwOpneProcess End\n");
}
return Old_ZwOpenProcess(ProcessHandle,MASK,attr,cid1);
}
void HookSSDTTable()
{
Old_ZwTerminateProcess =(PZwTerminateProcess)(SYSTEMSERVICE(ZwTerminateProcess));
g_pmdlSystemCall = MmCreateMdl(NULL, KeServiceDescriptorTable.ServiceTableBase, KeServiceDescriptorTable.NumberOfServices*4);
if(!g_pmdlSystemCall)
return;
MmBuildMdlForNonPagedPool(g_pmdlSystemCall);
g_pmdlSystemCall->MdlFlags = g_pmdlSystemCall->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
MappedSystemCallTable = MmMapLockedPages(g_pmdlSystemCall, KernelMode);
DbgPrint("Lijun_Hooked!\n");
HOOK_SYSCALL( ZwTerminateProcess, NewZwTerminateProcess, Old_ZwTerminateProcess );
HOOK_SYSCALL( ZwOpenProcess, NewZwOpenProcess, Old_ZwOpenProcess );
HOOK_SYSCALL(ZwSetValueKey,FakedZwSetValueKey,RealZwSetValueKey);
HOOK_SYSCALL(ZwDeleteKey, FakedZwDeleteKey,RealDeleteKey);
HOOK_SYSCALL(ZwDeleteValueKey,FakedZwDeleteValueKey,RealDeleteValueKey);
HOOK_SYSCALL(ZwSetInformationFile, FakedZwSetInformationFile,RealSetInformationFile);
HOOK_SYSCALL(ZwCreateFile, FakedZwCreateFile, RealZwCreateFile);
}
void UnHookSSDTTable(){
if (Old_ZwTerminateProcess){
UNHOOK_SYSCALL( ZwTerminateProcess, Old_ZwTerminateProcess, NewZwTerminateProcess );
UNHOOK_SYSCALL( ZwOpenProcess, Old_ZwOpenProcess, NewZwOpenProcess );
UNHOOK_SYSCALL(ZwSetValueKey, RealZwSetValueKey, FakedZwSetValueKey);
UNHOOK_SYSCALL(ZwDeleteKey, RealDeleteKey,FakedZwDeleteKey);
UNHOOK_SYSCALL(ZwDeleteValueKey,RealDeleteValueKey,FakedZwDeleteValueKey);
UNHOOK_SYSCALL(ZwSetInformationFile, RealSetInformationFile, FakedZwSetInformationFile);
UNHOOK_SYSCALL(ZwCreateFile, RealZwCreateFile, FakedZwCreateFile);
}
if(g_pmdlSystemCall)
{
MmUnmapLockedPages(MappedSystemCallTable, g_pmdlSystemCall);
IoFreeMdl(g_pmdlSystemCall);
}
}
BOOLEAN GetFullName(HANDLE handle,char * pch)
{
ULONG uactLength;
POBJECT_NAME_INFORMATION pustr;
ANSI_STRING astr;
PVOID pObj;
NTSTATUS ns;
ns = ObReferenceObjectByHandle( handle, 0, NULL, KernelMode, &pObj, NULL );
if (!NT_SUCCESS(ns))
{
return FALSE;
}
pustr = ExAllocatePool(NonPagedPool,1024+4);
if (pObj==NULL||pch==NULL)
return FALSE;
ns = ObQueryNameString(pObj,pustr,512,&uactLength);
if (NT_SUCCESS(ns))
{
RtlUnicodeStringToAnsiString(&astr,(PUNICODE_STRING)pustr,TRUE);
strcpy(pch,astr.Buffer);
}
ExFreePool(pustr);
RtlFreeAnsiString( &astr );
if (pObj)
{
ObDereferenceObject(pObj);
}
return TRUE;
}
//Faked的文件操作函数
NTSTATUS FakedZwSetInformationFile(IN HANDLE FileHandle,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PVOID FileInformation,
IN ULONG Length,
IN FILE_INFORMATION_CLASS FileInformationClass)
{
if ( FileHandle != NULL && (FileInformationClass == FileDispositionInformation) )
{
// get delete file name
NTSTATUS nts;
IO_STATUS_BLOCK iosb;
PWCHAR pstring = NULL;
PFILE_NAME_INFORMATION pfni = NULL;
pfni = (PFILE_NAME_INFORMATION) ExAllocatePool( PagedPool, sizeof(FILE_NAME_INFORMATION) + MAXPATHLEN);
if ( NULL != pfni )
{
nts = ZwQueryInformationFile(FileHandle, &iosb, pfni,
sizeof(FILE_NAME_INFORMATION) + MAXPATHLEN, FileNameInformation);
if ( NT_SUCCESS(nts) )
{
pstring = (PWCHAR)pfni->FileName;
//DbgPrint("ZWSetInformation FileName:%ws\n", pstring);
//if ( NULL != pstring && wcsstr(pstring , FileNameTest) != NULL || wcsstr(pstring,ProtFileDir) != NULL)
if ( NULL != pstring && FileDirVerify(pstring) && !PidVerify(PsGetCurrentProcess()))
{
ExFreePool(pfni);
pfni = NULL;
return STATUS_UNSUCCESSFUL;
}
}
ExFreePool(pfni);
pfni = NULL;
}
}
if(FileHandle != NULL && (FileInformationClass == FileRenameInformation))
{
NTSTATUS nts;
IO_STATUS_BLOCK iosb;
PWCHAR pstring = NULL;
PFILE_NAME_INFORMATION pfni = NULL;
pfni = (PFILE_NAME_INFORMATION) ExAllocatePool( PagedPool, sizeof(FILE_NAME_INFORMATION) + MAXPATHLEN);
if ( NULL != pfni )
{
nts = ZwQueryInformationFile(FileHandle, &iosb, pfni,
sizeof(FILE_NAME_INFORMATION) + MAXPATHLEN, FileNameInformation);
if ( NT_SUCCESS(nts) )
{
pstring = (PWCHAR)pfni->FileName;
//DbgPrint("ZWSetInformation RenameFile:%ws\n", pstring);
//if ( NULL != pstring && wcsstr(pstring , FileNameTest) != NULL || wcsstr(pstring,ProtFileDir) != NULL)
if ( NULL != pstring && FileDirVerify(pstring) && !PidVerify(PsGetCurrentProcess()))
{
ExFreePool(pfni);
pfni = NULL;
return STATUS_UNSUCCESSFUL;
}
}
ExFreePool(pfni);
pfni = NULL;
}
}
return RealSetInformationFile(FileHandle, IoStatusBlock, FileInformation,
Length, FileInformationClass);
}
//////////////////////////////////////////////////////////////////////////
NTSTATUS
FakedZwCreateFile(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions,
IN PVOID EaBuffer,
IN ULONG EaLength
)
{
UNICODE_STRING _unfile;
PWCHAR temp;
WCHAR tempDir[MAXPATHLEN];
int m;
_unfile.Length = 0;
_unfile.MaximumLength = MAXPATHLEN;
_unfile.Buffer = tempDir;
RtlCopyUnicodeString(&_unfile, ObjectAttributes->ObjectName);
//DbgPrint("out FileOpenDir:%ws\n", tempDir);
for (m = 0; m < curFileFilterNum; m++)
{
temp = wcschr(tempDir,L'?');
if(temp == NULL) break;
temp = temp + 3;
//if(wcsstr(temp, fileFilter[m].wchProtDirName) != NULL && !PidVerify(PsGetCurrentProcess()))
if(wchCmp(fileFilter[m].wchProtDirName, temp, wcslen(fileFilter[m].wchProtDirName)) && !PidVerify(PsGetCurrentProcess()) )
{
//DbgPrint("FileOpenDirVerify:%ws\n",temp);
return -1;
}
}
return RealZwCreateFile(FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,AllocationSize,FileAttributes,
ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength);
}