[Source] Hooking ! [Series part 0]

Posts 1624 of 24 · Page 2 of 2
i wish i had spent my time learning asm instead of c++.
This is a good tutorial, Thanks.
It's nice you guys like it . I think I'm going to write part 1/2 today, or maybe tomorrow.
i tryed to UnHook, but it says me that it's a privileged instruction... that's the source(it's a little bit modifyed). the hook part works, but the unhook not:
Code:
#include <iostream>
#include <windows.h>


void __stdcall hook(DWORD Timeout){   

	/*
	since the caller (the one with the Sleep() in his code) does not know that the function is hooked
	We have access to all the arguments that the function has. We can change and display them before passing on execution,
	or we can even prevent the function from being executed!
	*/

	printf("No rest for the wicked! not even %d seconds!\n",(int) (Timeout/1000));  // display the timeout in milliseconds

return;  // we wont execute the Sleep() function at all, No rest for the wicked!
}

DWORD *GetPointer()
{	
	DWORD* func;
_asm{
	lea eax, Sleep           // note: lea = Load Extended Address		 moving the address of the pointer to eax
	mov func, eax			 // note: mov = Move 					     now we have the address of the pointer in Func
	}

	return func;

}
DWORD* Tfunc = GetPointer();    //i think that's the Sleep pointer
DWORD* DoHook(DWORD* hook){

/*
Here *func (what func points to) still points to the address of sleep.
We however, don't want it to point there, we want it to point to our hook function.
Therefore we're going to change it, this can be done without any modifications to our virtual memory access.
Because the pointers are kept in our .data section, and we have write permission in our .data section.
*/DWORD* func = GetPointer();
	

	*func = (DWORD) hook;      // change what *func points to, remeber that func is the same pointer as used in the call dword[addfress] instruction.                
	return func;

}

void UnHook()
{	DWORD* func = GetPointer();
	
	*func = (DWORD)Tfunc;
	
}

int main(){

	DWORD* Hooked;

printf("Sleeping for 2 seconds!\n");
Sleep(2000);   // sleep is in milliseconds 1/1000th second therefore 2000 = 2 seconds
printf("Done sleeping!\n");



Hooked = DoHook((DWORD*)&hook);                       // now detour Sleep()!

printf("Sleeping for 3 seconds!\n");
Sleep(3000);
printf("Done sleeping!\n");

printf("Unhooking...\n");
UnHook();
printf("Done...Sleeping for 3sec\n");
Sleep(3000);
printf("Done");

std::cin.ignore();
	return 0;
}
Quote Originally Posted by Sixx93 View Post
i tryed to UnHook, but it says me that it's a privileged instruction... that's the source(it's a little bit modifyed). the hook part works, but the unhook not:
Code:
#include <iostream>
#include <windows.h>


void __stdcall hook(DWORD Timeout){   

	/*
	since the caller (the one with the Sleep() in his code) does not know that the function is hooked
	We have access to all the arguments that the function has. We can change and display them before passing on execution,
	or we can even prevent the function from being executed!
	*/

	printf("No rest for the wicked! not even %d seconds!\n",(int) (Timeout/1000));  // display the timeout in milliseconds

return;  // we wont execute the Sleep() function at all, No rest for the wicked!
}

DWORD *GetPointer()
{	
	DWORD* func;
_asm{
	lea eax, Sleep           // note: lea = Load Extended Address		 moving the address of the pointer to eax
	mov func, eax			 // note: mov = Move 					     now we have the address of the pointer in Func
	}

	return func;

}
DWORD* Tfunc = GetPointer();    //i think that's the Sleep pointer
DWORD* DoHook(DWORD* hook){

/*
Here *func (what func points to) still points to the address of sleep.
We however, don't want it to point there, we want it to point to our hook function.
Therefore we're going to change it, this can be done without any modifications to our virtual memory access.
Because the pointers are kept in our .data section, and we have write permission in our .data section.
*/DWORD* func = GetPointer();
	

	*func = (DWORD) hook;      // change what *func points to, remeber that func is the same pointer as used in the call dword[addfress] instruction.                
	return func;

}

void UnHook()
{	DWORD* func = GetPointer();
	
	*func = (DWORD)Tfunc;
	
}

int main(){

	DWORD* Hooked;

printf("Sleeping for 2 seconds!\n");
Sleep(2000);   // sleep is in milliseconds 1/1000th second therefore 2000 = 2 seconds
printf("Done sleeping!\n");



Hooked = DoHook((DWORD*)&hook);                       // now detour Sleep()!

printf("Sleeping for 3 seconds!\n");
Sleep(3000);
printf("Done sleeping!\n");

printf("Unhooking...\n");
UnHook();
printf("Done...Sleeping for 3sec\n");
Sleep(3000);
printf("Done");

std::cin.ignore();
	return 0;
}
You're not saving the proper value it seems, try the example above (I gave to jason) That should work for you, and using that you should be able to puzzle out how to continue.
"Part 0" - something only programmers would do :P
Quote Originally Posted by freedompeace View Post
"Part 0" - something only programmers would do :P
I can't get my head around normal notation and arrays anymore I'm never sure that when someone (eg. at a local store) names a quantity(5) of something they mean 0 to 5 or 1 to 5 or 0 to 4 of whatever :S

Programming got me confused
so to sum it up , in which situations would it set a pointer and in which situations would it not

Ie. statically linked libraries ? would this use a pointer?

dynamically linked libraries ? would this use a pointer? assuming they are linked using LoadLibrary ?

I want to know in which situations this would be useful ... And how can we be sure that the pointer name will be that of the function name (eg. Sleep ) .. that seems a bit odd to me
Posts 1624 of 24 · Page 2 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?