Go Back   BWHacks > Development > Programming

Programming General non-hacking related programming.

Reply
 
LinkBack Thread Tools

Old 02-28-2007, 02:56 AM   #1 (permalink)
SC_Modder
Loading javascript...
Senior Member
Moderator

Inquisitor
 
SC_Modder's Avatar
 
Join Date: Nov 2004
Posts: 4,515
SC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond repute
Send a message via AIM to SC_Modder
Default [C++] Code hooking class

This is used to write JMPs or CALLs to an address for hijacking game functions to your own code. You can enable or disable an individual hook, and it keeps all the hooks in a linked list so that you can quickly enable or disable all of them (useful for avoiding b.net's login hash check *hint*).

To create a hook, all you have to do is:
Code:
hook* hMyHook = new hook((void*)0xBADF00D,MyDetour, true, 0);
The first paramater specifies the location of the code the hook should be written to, the second is the function to reroute to, the 3rd is if it should use a JMP (true= JMP; false= CALL), and the last is the number of NOPs to write.

Hope this helps some of you, and you C++ gurus can go ahead and criticize my lack of code commenting. :P



hook.h
Code:
#ifndef __HOOKING_CLASS__
#define __HOOKING_CLASS__

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

class hook
{
private:
	static hook*	First;
	hook*			Next;
	void*			code;
	void*			hookfunc;
	char*			origbytes;
	bool			UseJMP;
	int				NOPpadding;
public:
	hook(void* code, void* hookfunc, bool UseJMP, int NOPpadding);
	~hook();
	static int		DeleteAllHooks();
	static int		SetAllHooks(bool state);
	void			set(bool state);
};


#endif
hook.cpp
Code:
#include "hook.h"

hook* hook::First = 0;

hook::hook(void* cd, void* hf, bool JMP, int NOP)
{
	if(!cd || !hf) 
	{
		return;
	}

	origbytes		= 0;
	Next			= 0;

	code			= cd;
	hookfunc		= hf;
	UseJMP			= JMP;
	NOPpadding		= NOP;

	if(!First)
	{
		First = this;
	}
	else
	{
		hook* h = First;
		while(h->Next) h = h->Next;
		h->Next = this;
	}
	set(1);
}

hook::~hook()
{
	if(origbytes)
	{
		delete [] origbytes;
	}
	if( First == this)
	{
		First = First->Next;
	}
	else
	{
		hook* h = First;
		while(h->Next != this) h = h->Next;
		h->Next = this->Next;
	}
}

int hook::DeleteAllHooks()
{
	if(!First) return 0;
	hook*	 h = First;
	hook* temp = 0;

	int x = 0;

	while(h -> Next)
	{
		temp = h;
		h = h->Next;
		delete temp;
		x++;
	}
	delete h;
	return x+1;
}

int hook::SetAllHooks(bool state)
{
	if(!First) return 0;
	hook*    h = First;
	hook* temp = 0;

	int x = 0;
	while( h-> Next )
	{
		temp = h;
		h = h->Next;
		temp->set(state);
		x++;
	}
	h->set(state);
	return x+1;
}
		

void hook::set(bool state)
{
	HANDLE Thread = GetCurrentThread();
	DWORD oldPriority = GetThreadPriority(Thread);
	SetThreadPriority(Thread, THREAD_PRIORITY_TIME_CRITICAL);

	DWORD OldProtect = 0;
	VirtualProtect(code, 5+NOPpadding, PAGE_READWRITE, &OldProtect);

	if(!state)
	{
		if(origbytes)
		{
			memcpy(code, origbytes, 5+NOPpadding);
			delete [] origbytes;
			origbytes = 0;
		}
	}
	else
	{
		if(origbytes)
		{
			delete [] origbytes;
		}
		DWORD size = 5+NOPpadding+1;
		origbytes = new char [size];
		memcpy(origbytes, code, 5+NOPpadding);

		char Data[5] = {0};
		Data[0] = 0xE8 + UseJMP;
		*(DWORD*)(Data+1) = (DWORD)hookfunc - (DWORD)code - 5;
		memcpy(code, Data, 5);

		memset((void*)((DWORD)code+5), 0x90, NOPpadding);
	}
	VirtualProtect(code, 5+NOPpadding, OldProtect, &OldProtect);
	
	SetThreadPriority(Thread, oldPriority);

}
__________________
AaronOpfer.com - My music
SC_Modder 15 0FF11|\|3   Reply With Quote
Advertisement
 
Advertisement
Advertisement Sponsored links


Old 02-28-2007, 03:39 AM   #2 (permalink)
Perma
rol 3905h, 8
Senior Member
Administrator

Saint
 
Perma's Avatar
 
Join Date: Jul 2004
Location: Canada
Posts: 5,384
Perma has disabled reputation
Default

Nice work SCM.
Perma 15 0FF11|\|3   Reply With Quote

Old 07-19-2007, 11:29 PM   #3 (permalink)
Dyndrilliac

Blessed
 
Dyndrilliac's Avatar
 
Join Date: Jun 2005
Location: Jacksonville, FL, USA
Posts: 2,506
Dyndrilliac has a brilliant futureDyndrilliac has a brilliant futureDyndrilliac has a brilliant futureDyndrilliac has a brilliant futureDyndrilliac has a brilliant futureDyndrilliac has a brilliant futureDyndrilliac has a brilliant future
Send a message via MSN to Dyndrilliac
Default

Very nice lazy mans linked list. I like the flag for enabling and disabling, but I'm afraid I would obsess myself into adding specific enabling of hooks. I prefer to keep each hook as it's own object, instead of using an internal management technique (like a self altering linked list), and simply store them in a generic container. This allows you much finer control in addition to usually being much more efficient, as you would be hard pressed to find structures that run faster than the available standards, unless you wrote them yourself (assuming your competent) from scratch or obtained written from scratch code from a competent programmer.
Dyndrilliac 15 0FF11|\|3   Reply With Quote

Old 08-14-2008, 07:17 AM   #4 (permalink)
Mixter

Deviant
 
Mixter's Avatar
 
Join Date: Jul 2008
Location: here
Posts: 51
Mixter is on a distinguished road
Default

Nice job man, may wanna take a serious look to this code. ;o

(I hope you eat better than your offset :] 0 x BAD FOOD)

hook* hMyHook = new hook((void*)0xBADF00D,MyDetour, true, 0);
Mixter 15 0FF11|\|3   Reply With Quote

Old 08-14-2008, 01:30 PM   #5 (permalink)
SC_Modder
Loading javascript...
Senior Member
Moderator

Inquisitor
 
SC_Modder's Avatar
 
Join Date: Nov 2004
Posts: 4,515
SC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond repute
Send a message via AIM to SC_Modder
Default

If DEP is enabled this can cause the application to crash. To fix it replace every instance of PAGE_READWRITE with PAGE_EXECUTE_READWRITE. Only in rare cases though.
__________________
AaronOpfer.com - My music
SC_Modder 15 0FF11|\|3   Reply With Quote

Old 08-19-2008, 04:14 AM   #6 (permalink)
Mixter

Deviant
 
Mixter's Avatar
 
Join Date: Jul 2008
Location: here
Posts: 51
Mixter is on a distinguished road
Default

If you could add a couple of "CALL"/"RET" in your code, I think It could really rox :D
Musnt you write:
Quote:
class Hook {...
instead of :
Quote:
classe hook {...
?

Last edited by Mixter : 08-19-2008 at 06:26 AM.
Mixter 15 0FF11|\|3   Reply With Quote
Sponsored links
Advertisement
 
Advertisement
Advertisement

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Re-Release] DBZ Supreme RPG Code Generator TheSpectator Warcraft Gaming 24 09-18-2007 08:52 PM
Question regarding code caves arpsmack Starcraft Hacking Related 24 04-23-2007 12:09 PM
our korean hack source code HiddenReverC Reverse Engineering 5 08-18-2006 05:45 AM
Is this the Zero2.0.2 source? ShadowTassadar Programming 32 07-09-2005 11:06 PM


All times are GMT. The time now is 12:42 AM.


vBulletin style developed by Transverse Styles

Powered by vBulletin Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright © 2004-2008 BWHacksAd Management by RedTyger