Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Hooking kernel functions

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Thu Jan 10, 2013 9:03 pm    Post subject: Hooking kernel functions Reply with quote

I have a few questions in regards to drivers as well as hooking kernel functions. I've personally created my own driver so I know that KeStackAttachProcess is a crucial function to any driver that accesses memory be it for anti-hacking purposes or for hacking purposes.

What i've noticed however is these functions don't have their own libraries (understandable) but then if they don't have their own libraries, where does the call to the function end up at? What application handles the call to the function?

I'm attempting to set a hook on the function in order to prevent specific drivers from attaching to specific processes. I figured that if 64-bit machines reject unsigned drivers from loading, and if they are loaded, from running correctly then I must be able to set a hook on KeStackAttachProcess without receiving much of an error from the game such as "Can't communicate with the driver".

But theres a big difference from setting a hook in usermode and kernelmode.

I don't know where to set the hook and I also don't have to the ability to read the inline assembly of the kernel function like I can read it in usermode in order for me to create an appropriate hook. I have a variety of tools for usermode functions such as Cheat Engine and ollydbg but I don't have those luxories when I want to view kernelmode memory so I was wondering if there is any softwares similiar to OllyDbg which allows to be view kernel memory.

If anybody can clear up my questions that would be awesome.
Back to top
View user's profile Send private message MSN Messenger
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Fri Jan 11, 2013 3:28 am    Post subject: Reply with quote

This is something you could have easily found by searching.
Many kernel debuggers exist, such as WingDbg, which is a very powerful tool, and Syser.

Moreover, I did not understand your original question: "What application handles the call to the function? ". I suggest you read a good book, like Windows Internals.
Back to top
View user's profile Send private message MSN Messenger
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25827
Location: The netherlands

PostPosted: Fri Jan 11, 2013 4:07 am    Post subject: Reply with quote

Cheat engine can view kernel memory as well, just enable kernelmode readprocessmemory in settings. And for debugging use dbvm when in 64bit

Anyhow, you can't hook KeAttachProcess in 64-bit as windows does an integrity check now and then

Also, mov cr3 is a very useful instruction that works without that api

You can call obRegisterCallback to register a signed function to block everything from getting a handle to your process and threads (not windows i think), but KeAttachProcess will keep working

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Fri Jan 11, 2013 5:22 pm    Post subject: Reply with quote

Thanks for your detailed response.

My goal is to hook KeStackAttachProcess on 32 bit computers so I shouldn't have to worry about an integrity check should I?

Anyhow, I dived right into the subject and currently i'm attempting to hook KeStackAttachProcess after using MmGetSystemRoutine to locate its address.

This is my following hooking code, short but breif:

Code:

// Setup Hook
VOID Hook ()
{
   __asm
   {
      pushfd
      pushad
   }

   DbgPrint ( "I'm inside KeStackAttachProcess" );

   __asm
   {
      popfd
      popad
      mov edi, edi
      push ebp
      mov ebp, esp
   }
}

VOID Setup ( PVOID From, PVOID To )
{
   // Setup bytes
   JmpToHook [0] = 0xE9;
   *(DWORD*) &JmpToHook [1] = (DWORD) To - ( (DWORD) From + 0x05 );

   // Set protection
   __try
   {
      ntStatus = ZwProtectVirtualMemory ( NtCurrentProcess (), From, &szToProtect, PAGE_EXECUTE_READWRITE, &oldProtection );

      // Set memory
      memcpy ( From, &JmpToHook, 5 );
   }
   __except ( 1 )
   {
      DbgPrint ( "Failed to protect/set hook" );
   }
}
//


I keep getting a BSOD that says "An attempt was made to execute non executable memory". I tried to doing research on it but there wasn't much info and the error message seems to be self-explanatory. I'm curious however to why i'm receiving that message as I don't see any potential logic errors in my code.

Also, a quick question. KeStackAttachProcess is a function in Ntoskrnl.exe. I do understand that its a stub to the the real KeStackAttachProcess. SO my question is if I set a hook on this function, would it take into effect globally? If I set the hook in kernel mode, and then opened CE and attached it into a random process and then browsed to the memory address of KeStackAttachProcess I should see my hook in place correct?
Back to top
View user's profile Send private message MSN Messenger
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25827
Location: The netherlands

PostPosted: Fri Jan 11, 2013 7:36 pm    Post subject: Reply with quote

Your hook function is not declared as naked, so it places a push ebp/mov ebp,esp at the start, causing your own push ebp/mov ebp,esp to be duplicate and thus mess up the return address, causing it to jump to a random location.(stack)

And yes, most kernelmode memory is shared by all processes, so if you change it in one process, you're changing it in all (You can bypass that by setting up your own pagetable entries for the .code section of ntoskrnl.exe though so that your own process never sees the hooked code, but that's very ugly)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Fri Jan 11, 2013 9:27 pm    Post subject: Reply with quote

Thanks for your help.

I got everything working, however there is one final problem. Everytime I load up my driver, it halts any driver including cheat engine's from loading up if it hasn't already loaded up until my driver has finished and unloaded. StartService () doesn't return until my driver has been unloaded.

I have no clue how to solve this as I am new to driver coding and the documentation on the DriverEntry function in MSDN doesn't to my knowledge cover where you should initialize your main code. Can you guide me to any links that covers this issue?

If you need to look at my driverentry code, here it is:

Code:

NTSTATUS DriverEntry ( struct _DRIVER_OBJECT *DriverObject, PUNICODE_STRING RegistryPath )
{
   // Specify the 'Unload' function
   DriverObject->DriverUnload = DriverUnload;

   // Notify that you've loaded
   DbgPrint ( "EazyDriver: Loaded!" );

   // Initialize 'uKeStackAttachProcess'
   RtlInitUnicodeString ( &uKeStackAttachProcess, cKeStackAttachProcess );

   // Obtain the address
   pKeStackAttachProcess  = MmGetSystemRoutineAddress ( &uKeStackAttachProcess );
   dwKeStackAttachProcess = (DWORD) pKeStackAttachProcess + 0x5;

   if ( !pKeStackAttachProcess )
      DbgPrint ( "EazyDriver: Failed to locate." );
   else
   {
      // Display the address
      DbgPrint ( "%x", pKeStackAttachProcess );

      ZwProtectVirtualMemory = (ZWPROTECTMEM) findUnresolved ( ZwPulseEvent );

      // Failed to locate 'ZwProtectVirtualMemory'
      if ( !ZwProtectVirtualMemory )
         DbgPrint ( "EazyDriver: Failed to locate (2)." );
      else
      {
         // Good grounds to setup the hook
         Setup ( pKeStackAttachProcess, &Hook );
      }
   }

   return STATUS_SUCCESS;
}
Back to top
View user's profile Send private message MSN Messenger
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25827
Location: The netherlands

PostPosted: Fri Jan 11, 2013 9:34 pm    Post subject: Reply with quote

Try commenting out Setup ( pKeStackAttachProcess, &Hook ); and see if it still freezes
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Fri Jan 11, 2013 9:43 pm    Post subject: Reply with quote

Its not a problem of freezing.

The problem is that each time I start up my driver, it works wonders. However, it for some reason doesn't allow any other driver to load up until it has unloaded.

I assumed that was because I had something screwed up in DriverEntry.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites