BehaviorMon.c
上传用户:xuemeng126
上传日期:2022-07-05
资源大小:454k
文件大小:84k
- //////////////////////////////////////////////////////////////////////////
- //头文件
- #include "ntddk.h"
- #include "stdio.h"
- #include "ntifs.h"
- #include "BehaviorMon.h"
- //from regmon
- #include "stdarg.h"
- #include "IoctlCmd.h"
- #include "ksecdd.h"
- #include "ntsec.h"
- #include "regsys.h"
- #include "reglib.h"
- #pragma comment(lib,"regmlib.lib")
- #pragma comment(lib,"ksecdd.lib")
- //宏定义
- #define SYSNAME "System"
- #define NT_PROCNAMELEN 16
- #define ObjectNameInformation (1)
- #define STATUS_INFO_LEN_MISMATCH 0xC0000004
- #define DELAYTIME -10
- //类型定义
- typedef unsigned long DWORD;
- //typedef void* PVOID;
- //结构体定义
- struct SYS_SERVICE_TABLE {
- void **ServiceTable;
- unsigned long CounterTable;
- unsigned long ServiceLimit;
- void **ArgumentsTable;
- };
- //保存被hook的nativeAPI函数信息
- typedef struct {
- const char * nativeAPIname;//函数名称
- ULONG Index;//在描述符表中的代号
- ULONG RealCallee;//真正的地址
- ULONG proxyfunadd;//代理函数地址
- BOOL hooked;
- }HOOKED_API_INFO;
- HOOKED_API_INFO hook_API_info[100];
- int hook_num=0;
- //交互缓冲区,驱动往里写,前台往出读,做显示
- typedef struct{
- char IsRead;
- char ProcessName[32];
- char NativeAPIName[32];
- char ProcessFullName[512];
- char Behavior[1024];
- char Result[64];
- char Time[16];
- }LOG_BEHAVIOR,* PLOG_BEHAVIOR;
- int curr_write_pointer=0;
- BOOL can_use_curr_write_pointer=TRUE;
- DWORD LOG_BEHAVIOR_NUM=10;
- PLOG_BEHAVIOR plog_behavior;
- //////////////////////////////////////////////////////////////////////////
- //常量
- const WCHAR devicename[]=L"\Device\BehaviorMon";
- const WCHAR devicelink[]=L"\DosDevices\BEHAVIORMON";
- // Is registry hooked?
- BOOLEAN Hooked = FALSE;
- //ProcessName在EPROCESS结构体中的偏移量,不同操作系统不同
- //xp:0x174 2k:0x1fc
- ULONG ProcessNameOffset;
- //事件
- KEVENT event;
- //真正NativeAPI函数的地址
- ULONG Index,RealCallee;
- //与前台的交换缓冲区
- //char*plog_behavior;
- char*tmp;
- int i=0;
- int goornot=0;
- //windows系统服务描述符表
- extern struct SYS_SERVICE_TABLE *KeServiceDescriptorTable;
- PVOID *KeServiceTablePointers;
- SERVICE_HOOK_DESCRIPTOR *HookDescriptors;
- //要监控的程序全路径
- char processname_G[1024];
- //
- // Full path name lookaside
- //
- //非分页内存
- PAGED_LOOKASIDE_LIST FullPathLookaside;
- //
- // Lenghs of rootkeys (filled in at init). This table allows us to translate
- // path names into better-known forms. Current user is treated specially since
- // its not a full match.
- //
- ROOTKEY CurrentUser[2] = {
- { "\\REGISTRY\USER\S", "HKCU", 0 },
- { "HKU\S", "HKCU", 0 }
- };
- ROOTKEY RootKey[NUMROOTKEYS] = {
- { "\\REGISTRY\USER", "HKU", 0 },
- { "\\REGISTRY\MACHINE\SYSTEM\CURRENTCONTROLSET\HARDWARE PROFILES\CURRENT",
- "HKCC", 0 },
- { "\\REGISTRY\MACHINE\SOFTWARE\CLASSES", "HKCR", 0 },
- { "\\REGISTRY\MACHINE", "HKLM", 0 }
- };
- //
- // This is a hash table for keeping names around for quick lookup.
- //
- PHASH_ENTRY HashTable[NUMHASH];
- //
- // Mutex for hash table accesses
- //
- KMUTEX HashMutex;
- //
- // Data structure for storing messages we generate
- //
- PLOG_BUF Log = NULL;
- ULONG Sequence = 0;
- LARGE_INTEGER StartTime;
- KMUTEX LogMutex;
- //
- // Maximum amount of data we will grab for buffered unread data
- //
- ULONG NumLog = 0;
- ULONG MaxLog = MAXMEM/LOGBUFSIZE;
- //
- // Global error string
- //
- CHAR errstring[256];
- //
- // A unicode string constant for the "default" value
- //
- #define DEFAULTNAMELEN (9*sizeof(WCHAR))
- WCHAR DefaultValueString[] = L"(Default)";
- UNICODE_STRING DefaultValue = {
- DEFAULTNAMELEN,
- DEFAULTNAMELEN,
- DefaultValueString
- };
- //======================================================================
- // B U F F E R R O U T I N E S
- //======================================================================
- //----------------------------------------------------------------------
- //
- // ApplyNameFilter
- //
- // If the name matches the exclusion mask, we do not log it. Else if
- // it doesn't match the inclusion mask we do not log it.
- //
- //----------------------------------------------------------------------
- BOOLEAN
- ApplyFilters(
- PCHAR fullname
- )
- {
- return TRUE;
- }
- //----------------------------------------------------------------------
- //
- // RegmonFreeLog
- //
- // Frees all the data output buffers that we have currently allocated.
- //
- //----------------------------------------------------------------------
- VOID
- RegmonFreeLog(
- VOID
- )
- {
- PLOG_BUF next;
- //
- // Just traverse the list of allocated output buffers
- //
- while( Log ) {
- next = Log->Next;
- ExFreePool( Log );
- Log = next;
- }
- }
- //----------------------------------------------------------------------
- //
- // RegmonNewLog
- //
- // Called when the current buffer has filled up. This moves us to the
- // pre-allocated buffer and then allocates another buffer.
- //
- //----------------------------------------------------------------------
- VOID
- RegmonNewLog(
- VOID
- )
- {
- PLOG_BUF prev = Log, newLog;
- //
- // If we have maxed out or haven't accessed the current Log
- // just return
- //
- if( MaxLog == NumLog ) {
- Log->Len = 0;
- return;
- }
- //
- // See if we can re-use a Log
- //
- if( !Log->Len ) {
- return;
- }
- //
- // Move to the next buffer and allocate another one
- //
- newLog = ExAllocatePool( PagedPool, sizeof(*Log) );
- if( newLog ) {
- //
- // Allocation was successful so add the buffer to the list
- // of allocated buffers and increment the buffer count.
- //
- Log = newLog;
- Log->Len = 0;
- Log->Next = prev;
- NumLog++;
- } else {
- //
- // The allocation failed - just reuse the current buffer
- //
- Log->Len = 0;
- }
- }
- //----------------------------------------------------------------------
- //
- // RegmonOldestLog
- //
- // Goes through the linked list of storage buffers and returns the
- // oldest one.
- //
- //----------------------------------------------------------------------
- PLOG_BUF
- RegmonOldestLog(
- VOID
- )
- {
- PLOG_BUF ptr = Log, prev = NULL;
- //
- // Traverse the list
- //
- while( ptr->Next ) {
- ptr = (prev = ptr)->Next;
- }
- //
- // Remove the buffer from the list
- //
- if( prev ) {
- prev->Next = NULL;
- NumLog--;
- }
- return ptr;
- }
- //----------------------------------------------------------------------
- //
- // RegmonResetLog
- //
- // When a GUI is no longer communicating with us, but we can't unload,
- // we reset the storage buffers.
- //
- //----------------------------------------------------------------------
- VOID
- RegmonResetLog(
- VOID
- )
- {
- PLOG_BUF current, next;
- MUTEX_ACQUIRE( LogMutex );
- //
- // Traverse the list of output buffers
- //
- current = Log->Next;
- while( current ) {
- //
- // Free the buffer
- //
- next = current->Next;
- ExFreePool( current );
- current = next;
- }
- //
- // Move the output pointer in the buffer that's being kept
- // the start of the buffer.
- //
- NumLog = 1;
- Log->Len = 0;
- Log->Next = NULL;
- MUTEX_RELEASE( LogMutex );
- }
- //----------------------------------------------------------------------
- //
- // LogRecord
- //
- // Add a new string to Log, if it fits.
- //
- //----------------------------------------------------------------------
- VOID
- LogRecord(
- const char * format,
- ...
- )
- {
- PENTRY Entry;
- ULONG len;
- va_list arg_ptr;
- static CHAR text[MAXPATHLEN + MAXDATALEN + MAXPROCNAMELEN + MAXERRORLEN];
- #define A (&format)
- //KdPrint(( (char *)format, A[1], A[2], A[3], A[4], A[5], A[6] ));
- //KdPrint(( "n" ));
- #undef A
- //
- // only do this if a GUI is active
- //
- //if( !GUIActive ) return;
- //
- // Lock the buffer pool
- //
- MUTEX_ACQUIRE( LogMutex );
- //
- // Sprint the string to get the length
- //
- va_start( arg_ptr, format );
- len = vsprintf( text, format, arg_ptr );
- va_end( arg_ptr );
- //
- // Only log it if it passes the filters
- //
- if( ApplyFilters( text )) {
- //
- // Get a sequence numnber
- //
- InterlockedIncrement( &Sequence );
- //
- // ULONG align for Alpha
- //
- len += 4; len &= 0xFFFFFFFC; // +1 to include null terminator and +3 to allign on longword
- //
- // See if its time to switch to extra buffer
- //
- if( Log->Len + len + sizeof(*Entry) + 1 >= LOGBUFSIZE ) {
- RegmonNewLog();
- }
- //
- // Log the sequence number so that
- // a call's result can be paired with its
- // initial data collected when it was made.
- //
- Entry = (void *)(Log->Data+Log->Len);
- Entry->seq = Sequence;
- KeQuerySystemTime( &Entry->time );
- Entry->perftime = KeQueryPerformanceCounter( NULL );
- Entry->perftime.QuadPart -= StartTime.QuadPart;
- memcpy( Entry->text, text, len );
- //
- // Log the length of the string, plus 1 for the terminating
- // NULL
- //
- Log->Len += ((ULONG) (Entry->text - (PCHAR) Entry )) + len;
- }
- //
- // Release the buffer pool
- //
- MUTEX_RELEASE( LogMutex );
- }
- VOID
- RegmonHashCleanup(
- VOID
- )
- {
- PHASH_ENTRY hashEntry, nextEntry;
- ULONG i;
- MUTEX_ACQUIRE( HashMutex );
- //
- // First free the hash table entries
- //
- for( i = 0; i < NUMHASH; i++ ) {
- hashEntry = HashTable[i];
- while( hashEntry ) {
- nextEntry = hashEntry->Next;
- ExFreePool( hashEntry->FullPathName );
- ExFreePool( hashEntry );
- hashEntry = nextEntry;
- }
- HashTable[i] = NULL;
- }
- MUTEX_RELEASE( HashMutex );
- }
- //----------------------------------------------------------------------
- //
- // Minimum
- //
- // Returns min of two numbers
- //
- //----------------------------------------------------------------------
- ULONG
- Minimum(
- ULONG Value1,
- ULONG Value2
- )
- {
- return Value1 < Value2 ? Value1 : Value2;
- }
- //////////////////////////////////////////////////////////////////////////
- //功能函数
- //----------------------------------------------------------------------
- //
- // ErrorString
- //
- // Returns the string form of an error code.
- //
- //----------------------------------------------------------------------
- PCHAR
- ErrorString(
- NTSTATUS retval
- )
- {
- //
- // Passed filter, so log it
- //
- switch( retval ) {
- case STATUS_BUFFER_TOO_SMALL:
- return "BUFTOOSMALL";
- case STATUS_SUCCESS:
- return "SUCCESS";
- case STATUS_KEY_DELETED:
- return "KEYDELETED";
- case STATUS_REGISTRY_IO_FAILED:
- return "IOFAILED";
- case STATUS_REGISTRY_CORRUPT:
- return "CORRUPT";
- case STATUS_NO_MEMORY:
- return "OUTOFMEM";
- case STATUS_ACCESS_DENIED:
- return "ACCDENIED";
- case STATUS_NO_MORE_ENTRIES:
- return "NOMORE";
- case STATUS_OBJECT_NAME_NOT_FOUND:
- return "NOTFOUND";
- case STATUS_BUFFER_OVERFLOW:
- return "BUFOVRFLOW";
- case STATUS_OBJECT_PATH_SYNTAX_BAD:
- return "SYNTAXERR";
- case STATUS_OBJECT_NAME_COLLISION:
- return "NAMECOLLISION";
- case STATUS_REPARSE:
- return "REPARSE";
- case STATUS_BAD_IMPERSONATION_LEVEL:
- return "BADIMPERSONATION";
- default:
- sprintf(errstring, "%x", retval );
- return errstring;
- }
- }
- //----------------------------------------------------------------------
- //
- // ConverToUpper
- //
- // Obvious.
- //
- //----------------------------------------------------------------------
- VOID
- ConvertToUpper(
- PCHAR Dest,
- PCHAR Source,
- ULONG Len
- )
- {
- ULONG i;
- for( i = 0; i < Len; i++ ) {
- if( Source[i] >= 'a' && Source[i] <= 'z' ) {
- Dest[i] = Source[i] - 'a' + 'A';
- } else {
- Dest[i] = Source[i];
- }
- if( Source[i] == 0 ) return;
- }
- }
- //----------------------------------------------------------------------
- //
- // GetPointer
- //
- // Translates a handle to an object pointer. In a build for .NET
- // server we simply return the passed object pointer so as to
- // avoid having to modify the code further.
- //
- //----------------------------------------------------------------------
- POBJECT
- GetPointer(
- HANDLE KeyOrHandle
- )
- {
- POBJECT pKey = NULL;
- //POBJECT_HANDLE_INFORMATION info=0;
- //
- // Ignore null handles.
- //
- if( !KeyOrHandle ) return NULL;
- //
- // Get the pointer the handle refers to.
- //
- #ifdef WNET
- pKey = KeyOrHandle;
- #else
- //
- // Make sure that we're not going to access
- // the kernel handle table from a non-system process
- //
- if( (LONG)(ULONG_PTR) KeyOrHandle < 0 &&
- ExGetPreviousMode() != KernelMode ) {
- return NULL;
- }
- // ObReferenceObjectByHandle( KeyOrHandle, KEY_READ, NULL, KernelMode, &pKey, NULL );
- // if( !pKey ) return NULL;
- if( !NT_SUCCESS( ObReferenceObjectByHandle( KeyOrHandle, 0, NULL, KernelMode, &pKey, NULL ))) {
- //KdPrint(("Error %x getting key pointern"));
- pKey = NULL;
- }
- #endif
- return pKey;
- }
- //----------------------------------------------------------------------
- //
- // ReleasePointer
- //
- // Dereferences the object.
- //
- //----------------------------------------------------------------------
- VOID
- ReleasePointer(
- POBJECT object
- )
- {
- #ifndef WNET
- if( object ) ObDereferenceObject( object );
- #endif
- }
- //----------------------------------------------------------------------
- //
- // GetProcessNameOffset
- //
- // In an effort to remain version-independent, rather than using a
- // hard-coded into the KPEB (Kernel Process Environment Block), we
- // scan the KPEB looking for the name, which should match that
- // of the GUI process
- //
- //----------------------------------------------------------------------
- //这个函数不知道为什么,我在xp上没成功,但在2k上成功了
- ULONG
- GetProcessNameOffset(
- VOID
- )
- {
- PEPROCESS curproc;
- int i;
- curproc = PsGetCurrentProcess();
- //
- // Scan for 12KB, hopping the KPEB never grows that big!
- //
- for( i = 0; i < 3*PAGE_SIZE; i++ ) {
- if( !strncmp( SYSNAME, (char *) curproc + i, strlen(SYSNAME) )) {
- return i;
- }
- }
- //
- // Name not found - oh, well
- //
- return 0;
- }
- //----------------------------------------------------------------------
- //
- // GetProcess
- //
- // Uses undocumented data structure offsets to obtain the name of the
- // currently executing process.
- // 获取当前进程的程序名和pid
- //----------------------------------------------------------------------
- char *
- GetProcess(
- char * Name
- )
- {
- PEPROCESS curproc;
- char *nameptr;
- ULONG i;
- //
- // We only try and get the name if we located the name offset
- //
- //ProcessNameOffset=0x1fc; //2k
- if( ProcessNameOffset ) {
- //
- // Get a pointer to the current process block
- //
- curproc = PsGetCurrentProcess();
- //
- // Dig into it to extract the name. Make sure to leave enough room
- // in the buffer for the appended process ID.
- //
- nameptr = (char *) curproc + ProcessNameOffset;
- strncpy( Name, nameptr, NT_PROCNAMELEN-1 );
- Name[NT_PROCNAMELEN-1] = 0;
- sprintf( Name + strlen(Name), ":%d", (ULONG) PsGetCurrentProcessId());
- } else {
- strcpy( Name, "???");
- }
- return Name;
- }
- //----------------------------------------------------------------------
- //
- // GetProcess
- //
- // Uses undocumented data structure offsets to obtain the name of the
- // currently executing process.
- // 获取当前进程的程序名
- //----------------------------------------------------------------------
- char *
- GetProcess_noid(
- char * Name
- )
- {
- PEPROCESS curproc;
- char *nameptr;
- ULONG i;
- //
- // We only try and get the name if we located the name offset
- //
- //ProcessNameOffset=0x1fc; //2k
- if( ProcessNameOffset ) {
- //
- // Get a pointer to the current process block
- //
- curproc = PsGetCurrentProcess();
- //
- // Dig into it to extract the name. Make sure to leave enough room
- // in the buffer for the appended process ID.
- //
- nameptr = (char *) curproc + ProcessNameOffset;
- strncpy( Name, nameptr, NT_PROCNAMELEN-1 );
- Name[NT_PROCNAMELEN-1] = 0;
- //sprintf( Name + strlen(Name), ":%d", (ULONG) PsGetCurrentProcessId());
- } else {
- strcpy( Name, "???");
- }
- return Name;
- }
- //----------------------------------------------------------------------
- //
- // GetCurrentProcessFileName
- //
- // 获得当前进程对应PE文件的完整路径
- // 返回PCWSTR类型
- //----------------------------------------------------------------------
- PCWSTR GetCurrentProcessFileName()
- {
- DWORD dwAddress = (DWORD)PsGetCurrentProcess();
- DWORD dwAddress1 ;
- if(KeGetCurrentIrql() != PASSIVE_LEVEL)
- return NULL;
- if(dwAddress == 0 || dwAddress == 0xFFFFFFFF)
- return NULL;
- dwAddress += 0x1B0;
- if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
- dwAddress += 0x10;
- if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
- dwAddress1=dwAddress;//20000
- dwAddress += 0x3C;
- if((dwAddress = *(DWORD*)dwAddress) == 0) return 0;
- if(dwAddress<dwAddress1)
- dwAddress=dwAddress+dwAddress1;
- //KdPrint(("Current Process Full Path Name: %wsn", (PCWSTR)dwAddress));
- return (PCWSTR)dwAddress;
- }
- //----------------------------------------------------------------------
- //
- // GetCurrentProcessFileName
- //
- // 获得当前进程的PE文件的全名
- // 返回到processfullname_c中
- //----------------------------------------------------------------------
- void GetCurrentProcessFileFullName(char * processfullname_c)
- {
- PCWSTR processfullname_pcwstr;
- PUNICODE_STRING processfullname_u;
- ANSI_STRING processfullname_a;
- processfullname_pcwstr=GetCurrentProcessFileName();
- if(processfullname_pcwstr)
- {
- processfullname_u = ExAllocatePool( PagedPool, MAXPATHLEN*sizeof(WCHAR)+2*sizeof(ULONG));
- if( !processfullname_u ) {
- //
- // Out of memory
- //
- return;
- }
- processfullname_u->MaximumLength = MAXPATHLEN*sizeof(WCHAR);
- RtlInitUnicodeString(processfullname_u,processfullname_pcwstr);
- processfullname_a.Length=0;
- if(processfullname_u->Length!=0)
- RtlUnicodeStringToAnsiString(&processfullname_a,processfullname_u,1);
- if(processfullname_a.Length!=0)
- {
- strncpy(processfullname_c,processfullname_a.Buffer,processfullname_a.Length);
- processfullname_c[processfullname_a.Length]=' ';
- }
- ExFreePool( processfullname_u );
- if(&processfullname_a)
- RtlFreeAnsiString(&processfullname_a);
- }
- }
- //----------------------------------------------------------------------
- //
- // GetKeyFullName
- //
- // Returns the full pathname of a key, if we can obtain one, else
- // returns a handle.
- //
- //----------------------------------------------------------------------
- VOID
- GetKeyFullName(
- HANDLE hKey,
- PUNICODE_STRING lpszSubKeyVal,
- PCHAR fullname //输出的fullname
- )
- {
- PHASH_ENTRY hashEntry;
- POBJECT pKey = NULL;
- CHAR tmpkey[16];
- ANSI_STRING keyname;
- PCHAR tmpname;
- PCHAR cmpname;
- PCHAR nameptr;
- PUNICODE_STRING fullUniName;
- ULONG actualLen;
- int i;
- POBJECT_NAME_INFORMATION keyNameInformation=0;
- //
- // If the fullname buffer is NULL, bail now
- //
- if( !fullname ) return;
- //
- // Allocate a temporary buffer
- //
- cmpname = ExAllocatePool( PagedPool, MAXROOTLEN );
- tmpname = ExAllocateFromPagedLookasideList( &FullPathLookaside );
- if( !tmpname || !cmpname ) {
- //
- // Not enough memory for a buffer
- //
- if( cmpname ) ExFreePool( cmpname );
- if( tmpname ) ExFreeToPagedLookasideList( &FullPathLookaside, tmpname );
- strcpy( fullname, "<INSUFFICIENT MEMORY>");
- return;
- }
- //
- // Translate the hkey into a pointer
- //
- fullname[0] = 0;
- tmpname[0] = 0;
- //
- // Is it a valid handle?
- //
- if( pKey = GetPointer( hKey )) {
- //
- // See if we find the key in the hash table
- //
- ReleasePointer( pKey );
- MUTEX_ACQUIRE( HashMutex );
- hashEntry = HashTable[ HASHOBJECT( pKey ) ];
- while( hashEntry && hashEntry->Object != pKey ) {
- hashEntry = hashEntry->Next;
- }
- if( hashEntry ) {
- strcpy( tmpname, hashEntry->FullPathName );
- MUTEX_RELEASE( HashMutex );
- } else {
- //
- // We will only get here if key was created before we loaded - ask the Configuration
- // Manager what the name of the key is.
- //
- MUTEX_RELEASE( HashMutex );
- if( pKey ) {
- fullUniName = ExAllocatePool( PagedPool, MAXPATHLEN*sizeof(WCHAR)+2*sizeof(ULONG));
- if( !fullUniName ) {
- //
- // Out of memory
- //
- strcpy( fullname, "<INSUFFICIENT MEMORY>" );
- ExFreePool( cmpname );
- ExFreeToPagedLookasideList( &FullPathLookaside, tmpname );
- return;
- }
- // NTKERNELAPI
- // NTSTATUS
- // ObQueryNameString (
- // IN PVOID Object,
- // OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
- // IN ULONG Length,
- // OUT PULONG ReturnLength
- // );
- fullUniName->MaximumLength = MAXPATHLEN*sizeof(WCHAR);
- //&keyNameInformation->Name=fullUniName;
- if( NT_SUCCESS(ObQueryNameString( pKey, fullUniName, MAXPATHLEN, &actualLen ) )) {
- //fullUniName=&keyNameInformation->Name;
- if( NT_SUCCESS( RtlUnicodeStringToAnsiString( &keyname, fullUniName, TRUE ))) {
- if( keyname.Buffer[0] ) {
- strcpy( tmpname, "\" );
- strncat( tmpname, keyname.Buffer, Minimum( keyname.Length, MAXPATHLEN -2 ));
- }
- RtlFreeAnsiString( &keyname );
- }
- }
- ExFreePool( fullUniName );
- }
- }
- }
- //
- // Append subkey and value, if they are there
- //
- try {
- if( lpszSubKeyVal ) {
- keyname.Buffer = NULL;
- if( NT_SUCCESS( RtlUnicodeStringToAnsiString( &keyname, lpszSubKeyVal, TRUE ))) {
- if( keyname.Buffer[0] ) {
- //
- // See if this is an absolute rather than relative path, which
- // can be the case on Open/Create when the Registry callback API
- // is used (.NET Server and higher)
- //
- ConvertToUpper( cmpname, keyname.Buffer, strlen("\REGISTRY")+1);
- if( !strncmp( cmpname, "\REGISTRY", strlen("\REGISTRY"))) {
- strcpy( tmpname, "\" );
- } else {
- strcat( tmpname, "\" );
- }
- strncat( tmpname, keyname.Buffer, Minimum( keyname.Length, MAXPATHLEN - 1 - strlen(tmpname) ));
- }
- RtlFreeAnsiString( &keyname );
- }
- }
- } except( EXCEPTION_EXECUTE_HANDLER ) {
- if( keyname.Buffer ) RtlFreeAnsiString( &keyname );
- strcat( tmpname, "*** Invalid Name ****" );
- }
- //
- // See if it matches current user
- //
- for( i = 0; i < 2; i++ ) {
- ConvertToUpper( cmpname, tmpname, CurrentUser[i].RootNameLen );
- if( !strncmp( cmpname, CurrentUser[i].RootName,
- CurrentUser[i].RootNameLen )) {
- // KdPrint(( " CurrentUser(%d) %s ==> %sn", i,
- // tmpname, CurrentUser[i].RootName ));
- //
- // Its current user. Process to next slash
- //
- nameptr = tmpname + CurrentUser[i].RootNameLen;
- while( *nameptr && *nameptr != '\' ) nameptr++;
- strcpy( fullname, CurrentUser[i].RootShort );
- #if 0
- cmpname = nameptr - sizeof(USER_CLASSES);
- ConvertToUpper (cmpname, cmpname, sizeof(USER_CLASSES));
- if (!strncmp( cmpname, USER_CLASSES, sizeof(USER_CLASSES))) {
- strcat (fullname, "\Software\Classes");
- }
- #endif
- strcat( fullname, nameptr );
- ExFreePool( cmpname );
- ExFreeToPagedLookasideList( &FullPathLookaside, tmpname );
- return;
- }
- }
- //
- // Now, see if we can translate a root key name
- //
- for( i = 0; i < NUMROOTKEYS; i++ ) {
- ConvertToUpper( cmpname, tmpname, RootKey[i].RootNameLen );
- if( !strncmp( cmpname, RootKey[i].RootName,
- RootKey[i].RootNameLen )) {
- nameptr = tmpname + RootKey[i].RootNameLen;
- strcpy( fullname, RootKey[i].RootShort );
- strcat( fullname, nameptr );
- ExFreePool( cmpname );
- ExFreeToPagedLookasideList( &FullPathLookaside, tmpname );
- return;
- }
- }
- //
- // No translation
- //
- strcpy( fullname, tmpname );
- ExFreeToPagedLookasideList( &FullPathLookaside, tmpname );
- ExFreePool( cmpname );
- }
- //----------------------------------------------------------------------
- //
- // GetFileFullName
- //
- // Returns the full pathname of a file, if we can obtain one, else
- // returns a handle.
- // 获取一个注册项的全名
- //----------------------------------------------------------------------
- VOID
- GetFileFullName(
- POBJECT_ATTRIBUTES ObjectAttributes,
- PCHAR fullname //输出的fullname
- )
- {
- ANSI_STRING filefullname_a;
-
- filefullname_a.Length=0;
- RtlUnicodeStringToAnsiString(&filefullname_a,ObjectAttributes->ObjectName,1);
- if(filefullname_a.Length==0)
- {
- return;
- }
- strncpy(fullname,filefullname_a.Buffer,Minimum(filefullname_a.Length,1023));
- fullname[Minimum(filefullname_a.Length,1023)]=' ';
- if(&filefullname_a)
- RtlFreeAnsiString(&filefullname_a);
- }
- //----------------------------------------------------------------------
- //
- // GetFileFullName
- //
- // Returns the full pathname of a file, if we can obtain one, else
- // returns a handle.
- // 获取一个注册项的全名
- //----------------------------------------------------------------------
- VOID
- GetFileFullNamebyFileHandle_forSection(
- HANDLE FileHandle,
- char *filefullname_c
- )
- {
- //文件相关的变量
- PFILE_OBJECT file=0;
- ANSI_STRING filefullname_a;
- POBJECT_NAME_INFORMATION fileNameInformation;
- POBJECT_HANDLE_INFORMATION info=0;
- ULONG retSize;
- ObReferenceObjectByHandle(FileHandle,0,0,KernelMode,&file,info);
- if(!file)return;
- filefullname_a.Length=0;
- RtlUnicodeStringToAnsiString(&filefullname_a,&file->FileName,1);
- if(filefullname_a.Length==0)
- {
- return;
- }
- strncpy(filefullname_c,filefullname_a.Buffer,Minimum(filefullname_a.Length,1023));
- filefullname_c[Minimum(filefullname_a.Length,1023)]=' ';
- if(&filefullname_a)
- RtlFreeAnsiString(&filefullname_a);
- }
- //----------------------------------------------------------------------
- //
- // GetFileFullNamebyFileHandle
- //
- // Returns the full pathname of a file, if we can obtain one, else
- // returns a handle.
- // 获取一个注册项的全名
- //----------------------------------------------------------------------
- VOID
- GetFileFullNamebyFileHandle_QueryNameString(
- HANDLE filehandle,
- char *filefullname_c
- )
- {
- //文件相关的变量
- PFILE_OBJECT file=0;
- ANSI_STRING filefullname_a;
- POBJECT_NAME_INFORMATION fileNameInformation;
- POBJECT_HANDLE_INFORMATION info=0;
- ULONG retSize;
- PUNICODE_STRING filefullname_u;
- //经由文件句柄得到文件名
- ObReferenceObjectByHandle(filehandle,0,0,KernelMode,&file,info);
- if(!file)return;
- // 得到的文件路径是 DeviceHarddiskVolum1....
- filefullname_u = ExAllocatePool( PagedPool, MAXPATHLEN*sizeof(WCHAR)+2*sizeof(ULONG));
- if( !filefullname_u ) {
-
- //
- // Out of memory
- //
- return;
- }
- filefullname_u->MaximumLength = MAXPATHLEN*sizeof(WCHAR);
- ObQueryNameString(file, filefullname_u, 1024, &retSize);
-
- filefullname_a.Length=0;
- RtlUnicodeStringToAnsiString(&filefullname_a,filefullname_u,1);
- if(filefullname_a.Length==0)
- {
- return;
- }
- strncpy(filefullname_c,filefullname_a.Buffer,Minimum(filefullname_a.Length,1023));
- filefullname_c[Minimum(filefullname_a.Length,1023)]=' ';
- if(&filefullname_a)
- RtlFreeAnsiString(&filefullname_a);
- ExFreePool( filefullname_u );
- }
- //----------------------------------------------------------------------
- //
- // GetFileFullNamebyFileHandle
- //
- // Returns the full pathname of a file, if we can obtain one, else
- // returns a handle.
- // 获取一个注册项的全名
- //----------------------------------------------------------------------
- VOID
- GetFileFullNamebyFileHandle(
- HANDLE filehandle,
- char *filefullname_c
- )
- {
- //文件相关的变量
- PFILE_OBJECT file=0;
- PFILE_OBJECT relatedfile=0; ANSI_STRING filefullname_a;
- PUNICODE_STRING filefullname_u;
- int relatedfilelength;
- POBJECT_NAME_INFORMATION fileNameInformation;
- POBJECT_HANDLE_INFORMATION info=0;
- ULONG retSize;
- //经由文件句柄得到文件名
- ObReferenceObjectByHandle(filehandle,0,0,KernelMode,&file,info);
- if(!file)return;
- filefullname_u = ExAllocatePool( PagedPool, MAXPATHLEN*sizeof(WCHAR)+2*sizeof(ULONG));
- if( !filefullname_u ) {
- //
- // Out of memory
- //
- return;
- }
- filefullname_u->MaximumLength = MAXPATHLEN*sizeof(WCHAR);
- //先得到盘符
- RtlVolumeDeviceToDosName(file->DeviceObject,filefullname_u);
- RtlUnicodeStringToAnsiString(&filefullname_a,filefullname_u,1);
- strncpy(filefullname_c,filefullname_a.Buffer,filefullname_a.Length);
- filefullname_c[filefullname_a.Length]=' ';
- relatedfilelength=filefullname_a.Length;
- //相对路径
- relatedfile=file->RelatedFileObject;
- RtlUnicodeStringToAnsiString(&filefullname_a,&relatedfile->FileName,1);
- strncat(filefullname_c,filefullname_a.Buffer,filefullname_a.Length);
- filefullname_c[relatedfilelength+filefullname_a.Length]=' ';
- relatedfilelength+=filefullname_a.Length;
- //文件名
- RtlUnicodeStringToAnsiString(&filefullname_a,&file->FileName,1);
- strcat(filefullname_c,"\");
- strncat(filefullname_c,filefullname_a.Buffer,filefullname_a.Length);
- filefullname_c[relatedfilelength+filefullname_a.Length+1]=' ';
- ExFreePool( filefullname_u );
- if(&filefullname_a)
- RtlFreeAnsiString(&filefullname_a);
- }
- //----------------------------------------------------------------------
- //
- // GetFullName
- //
- // Returns the full pathname of a file, if we can obtain one, else
- // returns a handle.
- // 获取一个注册项的全名
- //----------------------------------------------------------------------
- NTSTATUS GetFullName(HANDLE KeyHandle,char *fullname)
- {
- NTSTATUS ns;
- PVOID pKey=NULL,pFile=NULL;
- UNICODE_STRING fullUniName;
- ANSI_STRING akeyname;
- ULONG actualLen;
- UNICODE_STRING dosName;
- fullUniName.Buffer=NULL;
- fullUniName.Length=0;
- fullname[0]=0x00;
- ns= ObReferenceObjectByHandle( KeyHandle, 0, NULL, KernelMode, &pKey, NULL ) ;
- if( !NT_SUCCESS(ns)) return ns;
- fullUniName.Buffer = ExAllocatePool( PagedPool, MAXPATHLEN*2);//1024*2
- fullUniName.MaximumLength = MAXPATHLEN*2;
- __try
- {
- pFile=(PVOID)*(ULONG *)((char *)pKey+20);
- pFile=(PVOID)*(ULONG *)((char *)pFile);
- pFile=(PVOID)*(ULONG *)((char *)pFile+36);
- ObReferenceObjectByPointer(pFile, 0, NULL, KernelMode);
- RtlVolumeDeviceToDosName(((PFILE_OBJECT)pFile)->DeviceObject,&dosName);
- RtlCopyUnicodeString(&fullUniName, &dosName);
- RtlAppendUnicodeStringToString(&fullUniName,&((PFILE_OBJECT)pFile)->FileName);
- ObDereferenceObject(pFile);
- ObDereferenceObject(pKey );
- RtlUnicodeStringToAnsiString( &akeyname, &fullUniName, TRUE );
- if(akeyname.Length<MAXPATHLEN)
- {
- memcpy(fullname,akeyname.Buffer,akeyname.Length);
- fullname[akeyname.Length]=0x00;
- }
- else
- {
- memcpy(fullname,akeyname.Buffer,MAXPATHLEN);
- fullname[MAXPATHLEN-1]=0x00;
- }
- RtlFreeAnsiString( &akeyname );
- ExFreePool(dosName.Buffer);
- ExFreePool( fullUniName.Buffer );
- return STATUS_SUCCESS;
- }
- __except(1)
- {
- if(fullUniName.Buffer) ExFreePool( fullUniName.Buffer );
- if(pKey) ObDereferenceObject(pKey );
- return STATUS_SUCCESS;
- }
- }
- void GetRealCallee(char * hookedfunname)
- {
- int i;
- for(i=0;i<hook_num;i++)
- {
- if(strcmp(hook_API_info[i].nativeAPIname,hookedfunname)==0)
- {
- RealCallee=hook_API_info[i].RealCallee;
- break;
- }
- }
- }
- NTSTATUS
- NTAPI
- HookNtRestoreKey(
- IN HANDLE KeyHandle,
- IN HANDLE FileHandle,
- IN ULONG RestoreOption )
- {
- //函数名称
- char hookedfunname[32]="NtRestoreKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32]="???";//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push RestoreOption
- push FileHandle
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- // //经由文件句柄得到文件名
- // GetFileFullNamebyFileHandle(FileHandle,filefullname_c);
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- //--------------------------------------------------------------------------
- strcpy(behavior,"导入键"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtOpenKey(
- OUT PHANDLE pKeyHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes )
- {
- //函数名称
- char hookedfunname[32]="NtOpenKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32]="???";//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //必须放考前一点,否则不能及时得到RealCallee
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ObjectAttributes
- push DesiredAccess
- push pKeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- GetKeyFullName( ObjectAttributes->RootDirectory, ObjectAttributes->ObjectName, keyfullname_c );
- strcpy(behavior,"打开键"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateKey(
- OUT PHANDLE pKeyHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- IN ULONG TitleIndex,
- IN PUNICODE_STRING Class OPTIONAL,
- IN ULONG CreateOptions,
- OUT PULONG Disposition OPTIONAL )
- {
- //函数名称
- char hookedfunname[32]="NtCreateKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push Disposition
- push CreateOptions
- push Class
- push TitleIndex
- push ObjectAttributes
- push DesiredAccess
- push pKeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- GetKeyFullName( ObjectAttributes->RootDirectory, ObjectAttributes->ObjectName, keyfullname_c );
- strcpy(behavior,"创建键"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtQueryValueKey(
- IN HANDLE KeyHandle,
- IN PUNICODE_STRING ValueName,
- IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
- OUT PVOID KeyValueInformation,
- IN ULONG KeyValueInformationLength,
- OUT PULONG ResultLength )
- {
- //函数名称
- char hookedfunname[32]="NtQueryValueKey";
- NTSTATUS ntstatus;
-
- //key相关的变量
- char keyfullname_c[1024]="???";
- PUNICODE_STRING valueName;
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
-
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- if( !ValueName || !ValueName->Length ) valueName = &DefaultValue;
- else valueName = ValueName;
- GetKeyFullName(KeyHandle,valueName,keyfullname_c);
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ResultLength
- push KeyValueInformationLength
- push KeyValueInformation
- push KeyValueInformationClass
- push ValueName
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为---------------------------------------------------
- //经由key句柄获得key全名
- strcpy(behavior,"查询键值"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtQueryKey(
- IN HANDLE KeyHandle,
- IN KEY_INFORMATION_CLASS KeyInformationClass,
- OUT PVOID KeyInformation,
- IN ULONG KeyInformationLength,
- OUT PULONG ResultLength )
- {
- //函数名称
- char hookedfunname[32]="NtQueryKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ResultLength
- push KeyInformationLength
- push KeyInformation
- push KeyInformationClass
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为-----------------------------------------------
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- strcpy(behavior,"查询键"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtDeleteKey(
- IN HANDLE KeyHandle )
- {
- //函数名称
- char hookedfunname[32]="NtDeleteKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为-----------------------------------------------
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- strcpy(behavior,"删除键"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtDeleteValueKey(
- IN HANDLE KeyHandle,
- IN PUNICODE_STRING ValueName )
- {
- //函数名称
- char hookedfunname[32]="NtDeleteValueKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ValueName
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为-----------------------------------------------
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- strcpy(behavior,"删除键值"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtSetValueKey(
- IN HANDLE KeyHandle,
- IN PUNICODE_STRING ValueName,
- IN ULONG TitleIndex OPTIONAL,
- IN ULONG ValueType,
- IN PVOID Data,
- IN ULONG DataSize )
- {
- //函数名称
- char hookedfunname[32]="NtSetValueKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push DataSize
- push Data
- push ValueType
- push TitleIndex
- push ValueName
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为-----------------------------------------------
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- strcpy(behavior,"设置键值"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtEnumerateKey(
- IN HANDLE KeyHandle,
- IN ULONG Index,
- IN KEY_INFORMATION_CLASS KeyInformationClass,
- OUT PVOID KeyInformation,
- IN ULONG KeyInformationLength,
- OUT PULONG ResultLength )
- {
- //函数名称
- char hookedfunname[32]="NtEnumerateKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ResultLength
- push KeyInformationLength
- push KeyInformation
- push KeyInformationClass
- push Index
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为-----------------------------------------------
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- strcpy(behavior,"枚举键"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtEnumerateValueKey(
- IN HANDLE KeyHandle,
- IN ULONG Index,
- IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass ,
- OUT PVOID KeyValueInformation,
- IN ULONG KeyValueInformationLength,
- OUT PULONG ResultLength )
- {
- //函数名称
- char hookedfunname[32]="NtEnumerateValueKey";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ResultLength
- push KeyValueInformationLength
- push KeyValueInformation
- push KeyValueInformationClass
- push Index
- push KeyHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为-----------------------------------------------
- //经由key句柄获得key全名
- GetKeyFullName(KeyHandle,NULL,keyfullname_c);
- strcpy(behavior,"枚举键值"");
- strcat(behavior,keyfullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtOpenFile(
- OUT PHANDLE FileHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- OUT PIO_STATUS_BLOCK IoStatusBlock,
- IN ULONG ShareAccess,
- IN ULONG OpenOptions )
- {
- //函数名称
- char hookedfunname[32]="NtOpenFile";
- NTSTATUS ntstatus;
-
- //文件相关的变量
- char filefullname_c[1024]="???";
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push OpenOptions
- push ShareAccess
- push IoStatusBlock
- push ObjectAttributes
- push DesiredAccess
- push FileHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为---------------------------------------
- GetFileFullName(ObjectAttributes,filefullname_c);
- strcpy(behavior,"打开文件"");
- strcat(behavior,filefullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtWriteFile(
- IN HANDLE FileHandle,
- IN HANDLE Event OPTIONAL,
- IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
- IN PVOID ApcContext OPTIONAL,
- OUT PIO_STATUS_BLOCK IoStatusBlock,
- IN PVOID Buffer,
- IN ULONG BufferLength,
- IN PLARGE_INTEGER ByteOffset OPTIONAL,
- IN PULONG Key OPTIONAL )
- {
- //函数名称
- char hookedfunname[32]="NtWriteFile";
- NTSTATUS ntstatus;
- //文件相关的变量
- char filefullname_c[1024]="???";
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push Key
- push ByteOffset
- push BufferLength
- push Buffer
- push IoStatusBlock
- push ApcContext
- push ApcRoutine
- push Event
- push FileHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为------------------------------------------------
- //经由文件句柄得到文件名
- GetFileFullNamebyFileHandle_QueryNameString(FileHandle,filefullname_c);
- //--------------------------------------------------------------------------
- strcpy(behavior,"写入文件"");
- strcat(behavior,filefullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateFile(
- OUT PHANDLE FileHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- OUT PIO_STATUS_BLOCK IoStatusBlock,
- IN PLARGE_INTEGER AllocationSize OPTIONAL,
- IN ULONG FileAttributes,
- IN ULONG ShareAccess,
- IN ULONG CreateDisposition,
- IN ULONG CreateOptions,
- IN PVOID EaBuffer OPTIONAL,
- IN ULONG EaLength )
- {
- //函数名称
- char hookedfunname[32]="NtCreateFile";
- NTSTATUS ntstatus;
- //文件相关的变量
- char filefullname_c[1024]="???";
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push EaLength
- push EaBuffer
- push CreateOptions
- push CreateDisposition
- push ShareAccess
- push FileAttributes
- push AllocationSize
- push IoStatusBlock
- push ObjectAttributes
- push DesiredAccess
- push FileHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为------------------------------------------------
- //经由文件句柄得到文件名
- //??????????????????函数有问题//??????????????????
- //GetFileFullNamebyFileHandle_QueryNameString(FileHandle,filefullname_c);
- //--------------------------------------------------------------------------
- strcpy(behavior,"创建文件"");
- strcat(behavior,filefullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateProcess(
- OUT PHANDLE ProcessHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
- IN HANDLE ParentProcess,
- IN BOOLEAN InheritObjectTable,
- IN HANDLE SectionHandle OPTIONAL,
- IN HANDLE DebugPort OPTIONAL,
- IN HANDLE ExceptionPort OPTIONAL )
- {
- //函数名称
- char hookedfunname[32]="NtCreateProcess";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- WORD InheritObjectTable_W;
- //-------------------------------------------------------------------
- InheritObjectTable_W=InheritObjectTable;
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ExceptionPort
- push DebugPort
- push SectionHandle
- push InheritObjectTable_W
- push ParentProcess
- push ObjectAttributes
- push DesiredAccess
- push ProcessHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"创建进程"");
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateProcessEx(
- OUT PHANDLE ProcessHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- IN HANDLE InheritFromProcessHandle,
- IN ULONG CreateFlags,
- IN HANDLE SectionHandle OPTIONAL,
- IN HANDLE DebugObject OPTIONAL,
- IN HANDLE ExceptionPort OPTIONAL,
- IN ULONG JobMemberLevel)
- {
- //函数名称
- char hookedfunname[32]="NtCreateProcessEx";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push JobMemberLevel
- push ExceptionPort
- push DebugObject
- push SectionHandle
- push CreateFlags
- push InheritFromProcessHandle
- push ObjectAttributes
- push DesiredAccess
- push ProcessHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"创建进程"");
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateSection(
- OUT PHANDLE SectionHandle,
- IN ULONG DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
- IN PLARGE_INTEGER MaximumSize OPTIONAL,
- IN ULONG PageAttributess,
- IN ULONG SectionAttributes,
- IN HANDLE FileHandle OPTIONAL )
- {
- //函数名称
- char hookedfunname[32]="NtCreateSection";
- NTSTATUS ntstatus;
- //文件相关的变量
- char filefullname_c[1024]="???";
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push FileHandle
- push SectionAttributes
- push PageAttributess
- push MaximumSize
- push ObjectAttributes
- push DesiredAccess
- push SectionHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- //经由文件句柄得到文件名
- GetFileFullNamebyFileHandle_forSection(FileHandle,filefullname_c);
- strcpy(behavior,"创建Section为文件"");
- strcat(behavior,filefullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtOpenThread(
- OUT PHANDLE ThreadHandle,
- IN ACCESS_MASK AccessMask,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- IN PCLIENT_ID ClientId )
- {
- //函数名称
- char hookedfunname[32]="NtOpenThread";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ClientId
- push ObjectAttributes
- push AccessMask
- push ThreadHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"打开线程"");
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateThread(
- OUT PHANDLE ThreadHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
- IN HANDLE ProcessHandle,
- OUT PCLIENT_ID ClientId,
- IN PCONTEXT ThreadContext,
- IN PINITIAL_TEB InitialTeb,
- IN BOOLEAN CreateSuspended )
- {
- //函数名称
- char hookedfunname[32]="NtCreateThread";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- WORD CreateSuspended_W;
- //-------------------------------------------------------------------
- CreateSuspended_W=CreateSuspended;
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push CreateSuspended_W
- push InitialTeb
- push ThreadContext
- push ClientId
- push ProcessHandle
- push ObjectAttributes
- push DesiredAccess
- push ThreadHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"创建线程"");
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtOpenProcess(
- OUT PHANDLE ProcessHandle,
- IN ACCESS_MASK AccessMask,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- IN PCLIENT_ID ClientId )
- {
- //函数名称
- char hookedfunname[32]="NtOpenProcess";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ClientId
- push ObjectAttributes
- push AccessMask
- push ProcessHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"打开进程"");
- strcat(behavior,""");
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtOpenSection(
- OUT PHANDLE SectionHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes )
- {
- //函数名称
- char hookedfunname[32]="NtOpenSection";
- NTSTATUS ntstatus;
- //文件相关的变量
- ANSI_STRING filefullname_a;
- char filefullname_c[1024]="???";
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push ObjectAttributes
- push DesiredAccess
- push SectionHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- RtlUnicodeStringToAnsiString( &filefullname_a, ObjectAttributes->ObjectName, TRUE );
- if(filefullname_a.Length!=0)
- {
- strncpy(filefullname_c,filefullname_a.Buffer,Minimum(filefullname_a.Length,1023));
- filefullname_c[Minimum(filefullname_a.Length,1023)]=' ';
- }
- strcpy(behavior,"打开Section"");
- strcat(behavior,filefullname_c);
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtCreateSymbolicLinkObject(
- OUT PHANDLE pHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- IN PUNICODE_STRING DestinationName )
- {
- //函数名称
- char hookedfunname[32]="NtCreateSymbolicLinkObject";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push DestinationName
- push ObjectAttributes
- push DesiredAccess
- push pHandle
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"创建符号链接"");
- strcat(behavior,""");
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- NTSTATUS
- NTAPI
- HookNtSetSystemTime(
- IN PLARGE_INTEGER SystemTime,
- OUT PLARGE_INTEGER PreviousTime OPTIONAL )
- {
- //函数名称
- char hookedfunname[32]="NtSetSystemTime";
- NTSTATUS ntstatus;
- //key相关的变量
- char keyfullname_c[1024]="???";
- //行为Behavior相关变量
- CHAR behavior[1024]="???";
- //进程相关的变量
- CHAR processname[32];//进程名称
- CHAR processfullname_c[512]="???";//进程全名
- //-------------------------------------------------------------------
- //输出函数地址
- GetRealCallee(hookedfunname);
- //获得函数返回结果
- _asm{
- push PreviousTime
- push SystemTime
- call RealCallee
- mov ntstatus,eax
- }
- //获取进程的全名
- GetCurrentProcessFileFullName(processfullname_c);
-
- //监控要监控的程序
- if(strcmp(processfullname_c,processname_G)!=0)
- {
- return ntstatus;
- }
- //这里分析具体的行为?????????????????????????????????????????????????
- strcpy(behavior,"设置系统时间"");
- strcat(behavior,""");
-
- //这里要进行操作系统判断,不同的操作系统这个地址不同
- //ProcessNameOffset=0x1FC; //2k
- ProcessNameOffset=0x174; //xp
- //------------------------------------------------------------------
- LogRecord( "%st%st%st%st%s", GetProcess(processname),hookedfunname,
- processfullname_c, ErrorString( ntstatus ), behavior );
- //------------------------------------------------------------------
-
-
-
- return ntstatus;
- }
- //////////////////////////////////////////////////////////////////////////
- //驱动控制相关
- void unhook()
- {
- ULONG a,base;
- int i=0;
- for(i=0;i<hook_num;i++)
- {
- if(hook_API_info[i].hooked)
- {
- Index=hook_API_info[i].Index;
- RealCallee=hook_API_info[i].RealCallee;
- //unhook dispatch table
- a=4*Index+(ULONG)KeServiceDescriptorTable->ServiceTable;
- base=(ULONG)MmMapIoSpace(MmGetPhysicalAddress((void*)a),4,0);
- //恢复ssdt中的函数地址
- _asm
- {
- mov eax,base
- mov ebx,RealCallee
- mov dword ptr[eax],ebx
- }
- tmp=(char*)base;
- MmUnmapIoSpace(tmp,4);
- hook_API_info[i].hooked=FALSE;
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- //IRP派遣例程——IRP_MJ_DEVICE_CONTROL
- //driver->MajorFunction[IRP_MJ_DEVICE_CONTROL]=DrvDispatch;
- NTSTATUS DrvDispatch(IN PDEVICE_OBJECT device,IN PIRP Irp)
- {
- UCHAR*buff=0; ULONG a,base;
- int offsetbuff=0;
- DWORD numHookAPI=0;
- CHAR nativeAPIname[64];
- int i=0;
- int j=0;
- PIO_STATUS_BLOCK IoStatus;
- BOOLEAN retval = FALSE;
- PLOG_BUF old;
- BOOLEAN logMutexReleased;
- PIO_STACK_LOCATION irpStack;
- PVOID inputBuffer;
- PVOID outputBuffer;
- ULONG inputBufferLength;
- ULONG outputBufferLength;
- PIO_STACK_LOCATION loc;
- ULONG ioControlCode;
- irpStack = IoGetCurrentIrpStackLocation (Irp);
- inputBuffer = Irp->AssociatedIrp.SystemBuffer;
- inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
- outputBuffer = Irp->AssociatedIrp.SystemBuffer;
- outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
- loc=IoGetCurrentIrpStackLocation(Irp);//获得IRP Stack
- ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
- //使buff指向controlbuff
- buff=(UCHAR*)Irp->AssociatedIrp.SystemBuffer;
- //
- // Its a message from our GUI!
- //
- IoStatus=&Irp->IoStatus;
- IoStatus->Status = STATUS_SUCCESS; // Assume success
- IoStatus->Information = 0; // Assume nothing returned
- //
- // See if the output buffer is really a user buffer that we
- // can just dump data into.
- //
- if( IOCTL_TRANSFER_TYPE(ioControlCode) == METHOD_NEITHER ) {
- outputBuffer = Irp->UserBuffer;
- }
- switch(ioControlCode)
- {
- case IOCTL_BEHAVIORMON_HOOK://hook Native API function
- offsetbuff=0;
- //获得要Hook的NativeAPI函数的个数
- numHookAPI=(DWORD)buff[8];
- offsetbuff+=12;
- //hook
- for(i=0;i<(int)numHookAPI;i++)
- {
- //获得要Hook的NativeAPI函数的name
- strcpy(nativeAPIname,buff+offsetbuff);
- offsetbuff+=strlen(nativeAPIname);
- offsetbuff+=1;
- //获得要Hook的NativeAPI函数的Index
- memmove(&Index,buff+offsetbuff,4);//读出从前台输入的SSDT函数服务Index
- offsetbuff+=4;
- //将上面得到的信息放在对应的Hook_API_Info中
- for(j=0;j<hook_num;j++)
- {
- if(strcmp(nativeAPIname,hook_API_info[j].nativeAPIname)==0)
- {
- break;
- }
- }
- if(j==hook_num)
- {
- continue;
- }
- //获得该函数的真实地址,并开始Hook
- a=4*Index+(ULONG)KeServiceDescriptorTable->ServiceTable;
- //把正确的SSDT函数调用地址先备份到base中
- base=(ULONG)MmMapIoSpace(MmGetPhysicalAddress((void*)a),4,0);
- a=hook_API_info[j].proxyfunadd;
- _asm
- {
- mov eax,base//把正确的SSDT函数调用地址复制到eax中
- mov ebx,dword ptr[eax]//使ebx=eax
- mov RealCallee,ebx//RealCallee=eax
- mov ebx,a//使ebx指向HookRegRestoreKey()函数
- mov dword ptr[eax],ebx//把SSDT中的正确的函数调用地址改为HookRegRestoreKey()函数所在的地址!!!
- }
- //将上面得到的信息放在对应的Hook_API_Info中
- hook_API_info[j].Index=Index;
- hook_API_info[j].RealCallee=RealCallee;
- hook_API_info[j].hooked=TRUE;
- tmp=(char*)base;
- MmUnmapIoSpace(tmp,4);
- }
- //获取与用户的交互缓冲区,buff中第一个DW就是其地址
- memmove(&a,buff,4);
- plog_behavior=(PLOG_BEHAVIOR)MmMapIoSpace(MmGetPhysicalAddress((void*)a),LOG_BEHAVIOR_NUM*sizeof(LOG_BEHAVIOR),0);
- break;
- case IOCTL_BEHAVIORMON_PROC:
- //设置要监控的程序全路径
- strcpy(processname_G,buff);
- break;
- case IOCTL_BEHAVIORMON_UNHOOK:
- unhook();
- break;
- case IOCTL_BEHAVIORMON_GETSTATS:
- //
- // Probe the output buffer
- //
- try {
- ProbeForWrite( outputBuffer,
- outputBufferLength,
- sizeof( UCHAR ));
- } except( EXCEPTION_EXECUTE_HANDLER ) {
- IoStatus->Status = STATUS_INVALID_PARAMETER;
- return FALSE;
- }
- MUTEX_ACQUIRE( LogMutex );
- if( LOGBUFSIZE > outputBufferLength ) {
- //
- // Output buffer isn't big enough
- //
- MUTEX_RELEASE( LogMutex );
- IoStatus->Status = STATUS_INVALID_PARAMETER;
- return FALSE;
- } else if( Log->Len || Log->Next ) {
- //
- // Switch to a new Log
- //
- RegmonNewLog();
- //
- // Fetch the oldest to give to user
- //
- old = RegmonOldestLog();
- if( old != Log ) {
- logMutexReleased = TRUE;
- MUTEX_RELEASE( LogMutex );
- } else {
- logMutexReleased = FALSE;
- }
- //
- // Copy it
- //
- memcpy( outputBuffer, old->Data, old->Len );
- //
- // Return length of copied info
- //
- IoStatus->Information = old->Len;
- //
- // Deallocate buffer - unless its the last one
- //
- if( logMutexReleased ) {
- ExFreePool( old );
- } else {
- Log->Len = 0;
- MUTEX_RELEASE( LogMutex );
- }
- } else {
- //
- // No unread data
- //
- MUTEX_RELEASE( LogMutex );
- IoStatus->Information = 0;
- }
- break;
- case IOCTL_BEHAVIORMON_ZEROSTATS:
- //
- // Zero contents of buffer
- //
- MUTEX_ACQUIRE( LogMutex );
- while( Log->Next ) {
- //
- // Free all but the first output buffer
- //
- old = Log->Next;
- Log->Next = old->Next;
- ExFreePool( old );
- NumLog--;
- }
- //
- // Set the output pointer to the start of the output buffer
- //
- Log->Len = 0;
- //
- // Reset sequence and relative start time
- //
- Sequence = 0;
- StartTime = KeQueryPerformanceCounter( NULL );
- MUTEX_RELEASE( LogMutex );
- break;
- default:
- break;
- }
- Irp->IoStatus.Status=0;
- IoCompleteRequest(Irp,IO_NO_INCREMENT);
- return 0;
- }
- // nothing special
- NTSTATUS DrvCreateClose(IN PDEVICE_OBJECT device,IN PIRP Irp)
- {
- Irp->IoStatus.Information=0;
- Irp->IoStatus.Status=0;
- IoCompleteRequest(Irp,IO_NO_INCREMENT);
- return 0;
- }
- // nothing special -just a cleanup
- void DrvUnload(IN PDRIVER_OBJECT driver)
- {
- UNICODE_STRING devlink;
- ULONG a,base;
- int i=0;
- for(i=0;i<hook_num;i++)
- {
- if(hook_API_info[i].hooked)
- {
- Index=hook_API_info[i].Index;
- RealCallee=hook_API_info[i].RealCallee;
- //unhook dispatch table
- a=4*Index+(ULONG)KeServiceDescriptorTable->ServiceTable;
- base=(ULONG)MmMapIoSpace(MmGetPhysicalAddress((void*)a),4,0);
- //恢复ssdt中的函数地址
- _asm
- {
- mov eax,base
- mov ebx,RealCallee
- mov dword ptr[eax],ebx
- }
- tmp=(char*)base;
- MmUnmapIoSpace(tmp,4);
- hook_API_info[i].hooked=FALSE;
- }
- }
- MmUnmapIoSpace(plog_behavior,LOG_BEHAVIOR_NUM*sizeof(LOG_BEHAVIOR));
- RtlInitUnicodeString(&devlink,devicelink);
- IoDeleteSymbolicLink(&devlink);
- IoDeleteDevice(driver->DeviceObject);
- //
- // Now we can free any memory we have outstanding
- //
- RegmonHashCleanup();
- RegmonFreeLog();
- ExDeletePagedLookasideList( &FullPathLookaside );
- }
- //****************************************************************************
- //驱动程序的入口点,相当于c语言的main函数,它在驱动程序被加载进内存的时候调用
- //第一个重要的任务——就是要设定驱动程序对象的几个函数指针;
- /*
- pDriverObj->DriverUnload = DriverUnload;//卸载函数
- pDriverObj->MajorFunction[IRP_MJ_CREATE] =
- pDriverObj->MajorFunction[IRP_MJ_CLOSE] =
- pDriverObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverDispatch;//派遣函数
- */
- //第二个重要的任务——就是要创建设备对象并为其建立符号连接;
- /*
- NTKERNELAPI
- NTSTATUS
- IoCreateDevice(
- IN PDRIVER_OBJECT DriverObject,
- IN ULONG DeviceExtensionSize,
- IN PUNICODE_STRING DeviceName OPTIONAL,
- IN DEVICE_TYPE DeviceType,
- IN ULONG DeviceCharacteristics,
- IN BOOLEAN Exclusive,
- OUT PDEVICE_OBJECT *DeviceObject
- );
- //例子:
- IoCreateDevice( pDriverObj,
- 0,
- &deviceName,
- FILE_DEVICE_UNKNOWN,
- FILE_DEVICE_SECURE_OPEN,
- true,
- &pDeviceObj ); //创建设备对象
- IoCreateSymbolicLink( &linkName, &deviceName ); //建立符号连接
- */
- //DriverEntry just creates our device - nothing special here
- NTSTATUS DriverEntry(IN PDRIVER_OBJECT driver,IN PUNICODE_STRING path)
- {
- PDEVICE_OBJECT devobject=0;//设备对象
- UNICODE_STRING devlink,devname;//设备链接,设备名称
- ULONG a,b;
- RtlInitUnicodeString(&devname,devicename);
- RtlInitUnicodeString(&devlink,devicelink);
- //初始化要监控的程序全路径
- processname_G[0]='X';
- processname_G[1]='X';
- processname_G[2]='X';
- processname_G[3]=' ';
- //初始化哈希表,存放注册表全名
- MUTEX_INIT( HashMutex );
- MUTEX_INIT( LogMutex );
- //
- // Initialize a lookaside for key names
- //
- ExInitializePagedLookasideList( &FullPathLookaside, NULL, NULL,
- 0, MAXPATHLEN, 'mgeR', 256 );
- //
- // Allocate the initial output buffer
- //
- Log = ExAllocatePool( PagedPool, sizeof(*Log) );
- if( !Log ) {
- RtlInitUnicodeString(&devlink,devicelink);
- IoDeleteSymbolicLink(&devlink);
- IoDeleteDevice(driver->DeviceObject);
- return STATUS_INSUFFICIENT_RESOURCES;
- }
- Log->Len = 0;
- Log->Next = NULL;
- NumLog = 1;
- //
- // Initialize rootkey lengths
- //
- for( i = 0; i < NUMROOTKEYS; i++ ) {
- RootKey[i].RootNameLen = strlen( RootKey[i].RootName );
- }
- for( i = 0; i < 2; i++ ) {
- CurrentUser[i].RootNameLen = strlen( CurrentUser[i].RootName );
- }
- //创建设备+创建符号链接
- IoCreateDevice(driver,2048,&devname,FILE_DEVICE_UNKNOWN,0,TRUE,&devobject);
- IoCreateSymbolicLink(&devlink,&devname);
- //设定驱动程序对象的几个函数指针
- driver->MajorFunction[IRP_MJ_DEVICE_CONTROL]=DrvDispatch;//派遣函数
- driver->MajorFunction[IRP_MJ_CREATE]=DrvCreateClose;
- driver->MajorFunction[IRP_MJ_CLOSE]=DrvCreateClose;
- driver->DriverUnload=DrvUnload;//卸载函数
- ProcessNameOffset = GetProcessNameOffset();
- //初始化Hook_API_Info
- //NtRestoreKey
- hook_API_info[hook_num].nativeAPIname="NtRestoreKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtRestoreKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtOpenKey
- hook_API_info[hook_num].nativeAPIname="NtOpenKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtOpenKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtCreateKey
- hook_API_info[hook_num].nativeAPIname="NtCreateKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtQueryValueKey
- hook_API_info[hook_num].nativeAPIname="NtQueryValueKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtQueryValueKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtQueryKey
- hook_API_info[hook_num].nativeAPIname="NtQueryKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtQueryKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtDeleteKey
- hook_API_info[hook_num].nativeAPIname="NtDeleteKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtDeleteKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtDeleteValueKey
- hook_API_info[hook_num].nativeAPIname="NtDeleteValueKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtDeleteValueKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtSetValueKey
- hook_API_info[hook_num].nativeAPIname="NtSetValueKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtSetValueKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtEnumerateKey
- hook_API_info[hook_num].nativeAPIname="NtEnumerateKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtEnumerateKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtEnumerateValueKey
- hook_API_info[hook_num].nativeAPIname="NtEnumerateValueKey";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtEnumerateValueKey;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtOpenFile
- hook_API_info[hook_num].nativeAPIname="NtOpenFile";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtOpenFile;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtWriteFile
- hook_API_info[hook_num].nativeAPIname="NtWriteFile";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtWriteFile;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- // //NtCreateFile
- // hook_API_info[hook_num].nativeAPIname="NtCreateFile";
- // hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateFile;
- // hook_API_info[hook_num].hooked=FALSE;
- // hook_num++;
- //NtCreateProcess
- hook_API_info[hook_num].nativeAPIname="NtCreateProcess";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateProcess;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtCreateProcessEx
- hook_API_info[hook_num].nativeAPIname="NtCreateProcessEx";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateProcessEx;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtCreateSection
- hook_API_info[hook_num].nativeAPIname="NtCreateSection";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateSection;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtOpenThread
- hook_API_info[hook_num].nativeAPIname="NtOpenThread";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtOpenThread;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtCreateThread
- hook_API_info[hook_num].nativeAPIname="NtCreateThread";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateThread;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtOpenProcess
- hook_API_info[hook_num].nativeAPIname="NtOpenProcess";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtOpenProcess;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtOpenSection
- hook_API_info[hook_num].nativeAPIname="NtOpenSection";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtOpenSection;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtCreateSymbolicLinkObject
- hook_API_info[hook_num].nativeAPIname="NtCreateSymbolicLinkObject";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtCreateSymbolicLinkObject;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- //NtSetSystemTime
- hook_API_info[hook_num].nativeAPIname="NtSetSystemTime";
- hook_API_info[hook_num].proxyfunadd=(ULONG)&HookNtSetSystemTime;
- hook_API_info[hook_num].hooked=FALSE;
- hook_num++;
- return 0;
- }