Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Tutorial] C++ DLL injection for Main Server

Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
A small guide on how to inject C++ code/assembler into the Main Server.
Thanks to Bakabug for some samples :)

Requirements
- Visual C++
- A hex editor
- Basic coding skills

Step 1 - Hex Injection
First thing to do , is unpack the MainServer.exe , or find a unpacked version.
When this is done, open the mainserver in your favorite hex editor, and search for "ADVAPI32.dll"

Now replace "ADVAPI32.dll" with "KalHooks.dll" , our new DLL.
Notice the mainserver will not function untill you provide the nessary dll in the same folder as the MainServer.exe

Step 2 - Some Basic Code
In visual studio, create a standard windows library project, and set it to create a DLL.
Now find dllmain.cpp , and replace it's content with the following code:

PHP:
#include "stdafx.h"

HMODULE libraryHandle;

_declspec(dllexport) BOOL WINAPI GetUserNameA(LPSTR input, LPDWORD buffer)
{
	typedef BOOL (WINAPI* CFunction) (LPSTR input, LPDWORD buffer);
	CFunction getUserName = (CFunction)GetProcAddress(libraryHandle, "GetUserNameA");
	return getUserName(input, buffer);
}

BOOL WINAPI DllMain(HMODULE module,DWORD action,LPVOID reserved)
{
	libraryHandle = LoadLibraryA("ADVAPI32.dll");
	
	switch(action)
	{
		case DLL_PROCESS_ATTACH:
			// Startup Functions
		break;
		case DLL_THREAD_ATTACH:
			// Shutdown Functions 
		break;
	}
	return true;
}

Step 3 - Injector Class
Now we wish to create a class handling our actions.

And here is the class, plus a example of a injected function. KalHooks::MemoryCopy and KalHooks::Intercept bluntly stolen from Bakabug.

KalHooks.cpp
PHP:
#include "stdafx.h"
#include "KalHook.h"

namespace Sword
{
	///
	///	DLL Loading.
	///
	void KalHook::Attach()
	{
	   this->DisableExperienceLoss();
	}

	///
	///	DLL Unloading.
	///
	void KalHook::Detach()
	{
	}

	///
	///	Disable experience loss when dying. 
	///
	void KalHook::DisableExperienceLoss()	  
	{
		unsigned char myCode[4] = {0xC2, 0x04, 0x00, 0x90};
		this->MemoryCopy((DWORD)0x004643A0,(DWORD)&myCode,4);
	}

	///
	///	Thread safe memory copying (address changing).
	///
	LPVOID KalHook::MemoryCopy(DWORD destination, DWORD source, int length)
	{
		DWORD oldSource      = 0;
		DWORD oldDestination = 0;
		
		VirtualProtect((LPVOID)source,length,PAGE_EXECUTE_READWRITE,&oldSource);
 		VirtualProtect((LPVOID)destination,length,PAGE_EXECUTE_READWRITE,&oldDestination);
		
		memcpy((void*)destination,(void*)source,length);
		
		VirtualProtect((LPVOID)destination,length,oldDestination,&oldDestination);
		VirtualProtect((LPVOID)source,length,oldSource,&oldSource);
		
		return (LPVOID)destination;
	};

	///
	///	Intercept a instruction into the memory.
	///
	DWORD KalHook::Intercept(int instruction, DWORD source, DWORD destination, int length)
	{
		DWORD realTarget;
		LPBYTE buffer = new BYTE[length];
		
		memset(buffer,0x90,length); 

		if(instruction != INST_NOP && length >= 5)
		{
			buffer[(length-5)] = instruction;
			DWORD dwJMP = (DWORD)destination - (source + 5 + (length-5));
			memcpy(&realTarget,(void*)(source+1),4);
			realTarget = realTarget + source + 5;
			memcpy(buffer + 1 + (length - 5),&dwJMP,4);
		}
		
		if(instruction == SHORT_JZ)
		{
			buffer[0] = instruction;
			buffer[1] = (BYTE)destination;
		}
		
		if(instruction == INST_BYTE)
		{
			buffer[0] = (BYTE)destination;
		}
		
		this->MemoryCopy(source,(DWORD)buffer,length);
		delete[] buffer;
		
		return realTarget;
	}
}

KalHooks.h
PHP:
///
/// KalOnline DLL hook handler.
///
/// By Windcape and KingIzu.
/// 
#define INST_NOP  0x90
#define INST_CALL 0xE8
#define INST_JMP  0xE9
#define INST_BYTE 0x00
#define SHORT_JZ  0x74

namespace Sword
{	
	class KalHook
	{
		public:
			void Attach();
			void Detach();
			void DisableExperienceLoss();

		private:
			LPVOID MemoryCopy(DWORD destination, DWORD source, int length);
			DWORD Intercept(int instruction, DWORD source, DWORD destination, int length);
	};
};

And now change dllmain.cpp to the following

PHP:
#include "stdafx.h"
#include "KalHook.h"

HMODULE libraryHandle;

/// 
/// Implementation of the WINBASE.H method GetUserNameA().
/// Required for proxying the ADVAPI32.dll library.
/// 
_declspec(dllexport) BOOL WINAPI GetUserNameA(LPSTR input, LPDWORD buffer)
{
	typedef BOOL (WINAPI* CFunction)(LPSTR input,LPDWORD buffer);
	CFunction getUserName = (CFunction)GetProcAddress(libraryHandle, "GetUserNameA");
	return getUserName(input, buffer);
}

/// 
/// Initialize and attach the KalHooks class to the DLL loading
/// allowing us to do inline assembler and memory editing.
/// 
BOOL WINAPI DllMain(HMODULE module,DWORD action,LPVOID reserved)
{
	libraryHandle = LoadLibraryA("ADVAPI32.dll");
	
	Sword::KalHook *hook = new Sword::KalHook();
	switch(action)
	{
		case DLL_PROCESS_ATTACH:
			hook->Attach();
		break;
		case DLL_THREAD_ATTACH:
			hook->Detach();
		break;
	}
	return true;
}

Step 4 - Finalizing
Before we compile, one more important addition is required.

Due to a error made in the 70's by the Windows C developers, DLLs are compiled by default to have a _ (underscore) before a function name.
But since the MainServer cannot handle this, we need to create a new file, to ensure the underscores are not added.

Simply, add a file named Exports.def to your project, with following content

PHP:
LIBRARY	"KalHooks"
EXPORTS
	GetUserNameA
	DllMain

Step 5 - Compiling
Compile (Make sure to compile to Release!), and copy your new KalHooks.dll into your MainServer folder.
Once this is done, run the MainServer and cross your fingers you did not do any ASM hacks that didden't work.
 
Last edited:
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

Part 2 - ASM Injects
The tricky part about doing injections is you need to use the Assembly language , ASM, to change call buildin methods of the Main Server.
I'm not going to teach you ASM, but just provide a few examples which you can call from the KalHooks::Attach()

The trick is to use a ASM address from http://forum.ragezone.com/f389/1909-mainsvrt-functions-370611/ , which links to a method, and
then call the method with the correct params. The params you can get from disassemble the mainserver using
In REC browse to the address() (as a function) and you'll get a list of arguments + type.

Feel free to add your own examples , but PLEASE use the proper CODE or PHP tags to encapsule the code.

PHP:
	///
	/// Broadcast a message to all players ingame.	
	///
	void KalHook::ShowServerMessage(LPCSTR text)
	{
		DWORD addr = 0x00450910; 
		__asm {
			push text
			push 0x004B4484
			push 0x0F
			call addr
			add esp, 0x0C 
		}
	}

PHP:
	///
	/// Kicks a user from the server using the players UserId.	
	///
	void KalHook::KickUser(DWORD userId)
	{
		__asm {
			push eax
			push ecx
			push edx
			push ebx
		}
		
		DWORD kick = 0x00452E60;
		DWORD s = 0x004B4DE8;
		DWORD x = (DWORD)0x2D;
		
		__asm {
			push 1
			push s
			push x
			push userId
			call kick
			add ESP, 0x10
		}
		
		__asm {
			pop ebx
			pop edx
			pop ecx
			pop eax
		}
	}
 
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

Actually I don't think it's packed, atleast not the most common versions.If it is, the same packaging as the client.

Oh, and it have to be a CLEAN client. Anything allready using KOSP or similiar will not work with this.
I also updated the Part 2, as I just tested REC for finding function arguments , it works very well.
 
Joined
Sep 10, 2006
Messages
1,243
Reaction score
179
Re: [Guide] C++ DLL injection for Main Server

---------------------------
MainSvrT.exe - Entrypoint not found
---------------------------
Can't find entrypoint from GetUserNameA in DLL-file KalHooks.dll.
---------------------------
OK
---------------------------
 
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

You forgot the Exports.def

Edit: Updated the dllmain.cpp source with a performance improvement for the HMODULE libraryHandle.
 
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

Did you add a "Definitions" file using the GUI, or manually added a text file you renamed to .def ?
The former is the correct way to do it.
 
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

Sure there is:

DeathArt - [Tutorial]  C++ DLL injection for Main Server - RaGEZONE Forums
 
Newbie Spellweaver
Joined
Oct 10, 2006
Messages
61
Reaction score
1
Re: [Guide] C++ DLL injection for Main Server

hello :)
wow cool it is so nice tut DeathArt
hmm i realy still dont able to understand the things of c# ^^ as you are unsing namespaces even with c++ (yes im beginner xD )

so here whati did with my little knowledge :)
this example will let you edit as you like you just need to know what codes to use :
As i renamed my hacking project [SyRoN]of pservers to [SyRoN X]Int server
so i named this little thing [PME X]

First make new project :
File >> New >> Project >> Win32 Console application
name is :WSOCK32
Next >> [DLL][Empty Project]

then start to add items

dllmain.cpp
I used proxy of WSOCK32.dll as it has just 75 exports and only used with MainSvrt.exe just use this proxy source and yes you can use other dll as you like
PHP:
#include "PME_X.h"

void Begin()
{
	Hook KalHook;//to make KalHook as Hook Class
	KalHook.LvlupPoints(6);
}

HINSTANCE hLThis = 0;
HINSTANCE hL = 0;
FARPROC p[75] = {0};

BOOL WINAPI DllMain(HINSTANCE hInst,DWORD reason,LPVOID)
	{
	if (reason == DLL_PROCESS_ATTACH)
		{
		hLThis = hInst;
		char system[MAX_PATH];
		GetSystemDirectoryA(system,sizeof(system));
		strcat_s(system,"\\WSOCK32.dll");
		hL = LoadLibraryA(system);
		if (!hL) return false;
		
		//start our things
		Begin();

		p[0] = GetProcAddress(hL,"AcceptEx");
		p[1] = GetProcAddress(hL,"EnumProtocolsA");
		p[2] = GetProcAddress(hL,"EnumProtocolsW");
		p[3] = GetProcAddress(hL,"GetAcceptExSockaddrs");
		p[4] = GetProcAddress(hL,"GetAddressByNameA");
		p[5] = GetProcAddress(hL,"GetAddressByNameW");
		p[6] = GetProcAddress(hL,"GetNameByTypeA");
		p[7] = GetProcAddress(hL,"GetNameByTypeW");
		p[8] = GetProcAddress(hL,"GetServiceA");
		p[9] = GetProcAddress(hL,"GetServiceW");
		p[10] = GetProcAddress(hL,"GetTypeByNameA");
		p[11] = GetProcAddress(hL,"GetTypeByNameW");
		p[12] = GetProcAddress(hL,"MigrateWinsockConfiguration");
		p[13] = GetProcAddress(hL,"NPLoadNameSpaces");
		p[14] = GetProcAddress(hL,"SetServiceA");
		p[15] = GetProcAddress(hL,"SetServiceW");
		p[16] = GetProcAddress(hL,"TransmitFile");
		p[17] = GetProcAddress(hL,"WEP");
		p[18] = GetProcAddress(hL,"WSAAsyncGetHostByAddr");
		p[19] = GetProcAddress(hL,"WSAAsyncGetHostByName");
		p[20] = GetProcAddress(hL,"WSAAsyncGetProtoByName");
		p[21] = GetProcAddress(hL,"WSAAsyncGetProtoByNumber");
		p[22] = GetProcAddress(hL,"WSAAsyncGetServByName");
		p[23] = GetProcAddress(hL,"WSAAsyncGetServByPort");
		p[24] = GetProcAddress(hL,"WSAAsyncSelect");
		p[25] = GetProcAddress(hL,"WSACancelAsyncRequest");
		p[26] = GetProcAddress(hL,"WSACancelBlockingCall");
		p[27] = GetProcAddress(hL,"WSACleanup");
		p[28] = GetProcAddress(hL,"WSAGetLastError");
		p[29] = GetProcAddress(hL,"WSAIsBlocking");
		p[30] = GetProcAddress(hL,"WSARecvEx");
		p[31] = GetProcAddress(hL,"WSASetBlockingHook");
		p[32] = GetProcAddress(hL,"WSASetLastError");
		p[33] = GetProcAddress(hL,"WSAStartup");
		p[34] = GetProcAddress(hL,"WSAUnhookBlockingHook");
		p[35] = GetProcAddress(hL,"WSApSetPostRoutine");
		p[36] = GetProcAddress(hL,"__WSAFDIsSet");
		p[37] = GetProcAddress(hL,"accept");
		p[38] = GetProcAddress(hL,"bind");
		p[39] = GetProcAddress(hL,"closesocket");
		p[40] = GetProcAddress(hL,"connect");
		p[41] = GetProcAddress(hL,"dn_expand");
		p[42] = GetProcAddress(hL,"gethostbyaddr");
		p[43] = GetProcAddress(hL,"gethostbyname");
		p[44] = GetProcAddress(hL,"gethostname");
		p[45] = GetProcAddress(hL,"getnetbyname");
		p[46] = GetProcAddress(hL,"getpeername");
		p[47] = GetProcAddress(hL,"getprotobyname");
		p[48] = GetProcAddress(hL,"getprotobynumber");
		p[49] = GetProcAddress(hL,"getservbyname");
		p[50] = GetProcAddress(hL,"getservbyport");
		p[51] = GetProcAddress(hL,"getsockname");
		p[52] = GetProcAddress(hL,"getsockopt");
		p[53] = GetProcAddress(hL,"htonl");
		p[54] = GetProcAddress(hL,"htons");
		p[55] = GetProcAddress(hL,"inet_addr");
		p[56] = GetProcAddress(hL,"inet_network");
		p[57] = GetProcAddress(hL,"inet_ntoa");
		p[58] = GetProcAddress(hL,"ioctlsocket");
		p[59] = GetProcAddress(hL,"listen");
		p[60] = GetProcAddress(hL,"ntohl");
		p[61] = GetProcAddress(hL,"ntohs");
		p[62] = GetProcAddress(hL,"rcmd");
		p[63] = GetProcAddress(hL,"recv");
		p[64] = GetProcAddress(hL,"recvfrom");
		p[65] = GetProcAddress(hL,"rexec");
		p[66] = GetProcAddress(hL,"rresvport");
		p[67] = GetProcAddress(hL,"s_perror");
		p[68] = GetProcAddress(hL,"select");
		p[69] = GetProcAddress(hL,"send");
		p[70] = GetProcAddress(hL,"sendto");
		p[71] = GetProcAddress(hL,"sethostname");
		p[72] = GetProcAddress(hL,"setsockopt");
		p[73] = GetProcAddress(hL,"shutdown");
		p[74] = GetProcAddress(hL,"socket");


		}
	if (reason == DLL_PROCESS_DETACH)
		{
		FreeLibrary(hL);
		}

	return 1;
	}

// AcceptEx
extern "C" __declspec(naked) void __stdcall __E__0__()
	{
	__asm
		{
		jmp p[0*4];
		}
	}

// EnumProtocolsA
extern "C" __declspec(naked) void __stdcall __E__1__()
	{
	__asm
		{
		jmp p[1*4];
		}
	}

// EnumProtocolsW
extern "C" __declspec(naked) void __stdcall __E__2__()
	{
	__asm
		{
		jmp p[2*4];
		}
	}

// GetAcceptExSockaddrs
extern "C" __declspec(naked) void __stdcall __E__3__()
	{
	__asm
		{
		jmp p[3*4];
		}
	}

// GetAddressByNameA
extern "C" __declspec(naked) void __stdcall __E__4__()
	{
	__asm
		{
		jmp p[4*4];
		}
	}

// GetAddressByNameW
extern "C" __declspec(naked) void __stdcall __E__5__()
	{
	__asm
		{
		jmp p[5*4];
		}
	}

// GetNameByTypeA
extern "C" __declspec(naked) void __stdcall __E__6__()
	{
	__asm
		{
		jmp p[6*4];
		}
	}

// GetNameByTypeW
extern "C" __declspec(naked) void __stdcall __E__7__()
	{
	__asm
		{
		jmp p[7*4];
		}
	}

// GetServiceA
extern "C" __declspec(naked) void __stdcall __E__8__()
	{
	__asm
		{
		jmp p[8*4];
		}
	}

// GetServiceW
extern "C" __declspec(naked) void __stdcall __E__9__()
	{
	__asm
		{
		jmp p[9*4];
		}
	}

// GetTypeByNameA
extern "C" __declspec(naked) void __stdcall __E__10__()
	{
	__asm
		{
		jmp p[10*4];
		}
	}

// GetTypeByNameW
extern "C" __declspec(naked) void __stdcall __E__11__()
	{
	__asm
		{
		jmp p[11*4];
		}
	}

// MigrateWinsockConfiguration
extern "C" __declspec(naked) void __stdcall __E__12__()
	{
	__asm
		{
		jmp p[12*4];
		}
	}

// NPLoadNameSpaces
extern "C" __declspec(naked) void __stdcall __E__13__()
	{
	__asm
		{
		jmp p[13*4];
		}
	}

// SetServiceA
extern "C" __declspec(naked) void __stdcall __E__14__()
	{
	__asm
		{
		jmp p[14*4];
		}
	}

// SetServiceW
extern "C" __declspec(naked) void __stdcall __E__15__()
	{
	__asm
		{
		jmp p[15*4];
		}
	}

// TransmitFile
extern "C" __declspec(naked) void __stdcall __E__16__()
	{
	__asm
		{
		jmp p[16*4];
		}
	}

// WEP
extern "C" __declspec(naked) void __stdcall __E__17__()
	{
	__asm
		{
		jmp p[17*4];
		}
	}

// WSAAsyncGetHostByAddr
extern "C" __declspec(naked) void __stdcall __E__18__()
	{
	__asm
		{
		jmp p[18*4];
		}
	}

// WSAAsyncGetHostByName
extern "C" __declspec(naked) void __stdcall __E__19__()
	{
	__asm
		{
		jmp p[19*4];
		}
	}

// WSAAsyncGetProtoByName
extern "C" __declspec(naked) void __stdcall __E__20__()
	{
	__asm
		{
		jmp p[20*4];
		}
	}

// WSAAsyncGetProtoByNumber
extern "C" __declspec(naked) void __stdcall __E__21__()
	{
	__asm
		{
		jmp p[21*4];
		}
	}

// WSAAsyncGetServByName
extern "C" __declspec(naked) void __stdcall __E__22__()
	{
	__asm
		{
		jmp p[22*4];
		}
	}

// WSAAsyncGetServByPort
extern "C" __declspec(naked) void __stdcall __E__23__()
	{
	__asm
		{
		jmp p[23*4];
		}
	}

// WSAAsyncSelect
extern "C" __declspec(naked) void __stdcall __E__24__()
	{
	__asm
		{
		jmp p[24*4];
		}
	}

// WSACancelAsyncRequest
extern "C" __declspec(naked) void __stdcall __E__25__()
	{
	__asm
		{
		jmp p[25*4];
		}
	}

// WSACancelBlockingCall
extern "C" __declspec(naked) void __stdcall __E__26__()
	{
	__asm
		{
		jmp p[26*4];
		}
	}

// WSACleanup
extern "C" __declspec(naked) void __stdcall __E__27__()
	{
	__asm
		{
		jmp p[27*4];
		}
	}

// WSAGetLastError
extern "C" __declspec(naked) void __stdcall __E__28__()
	{
	__asm
		{
		jmp p[28*4];
		}
	}

// WSAIsBlocking
extern "C" __declspec(naked) void __stdcall __E__29__()
	{
	__asm
		{
		jmp p[29*4];
		}
	}

// WSARecvEx
extern "C" __declspec(naked) void __stdcall __E__30__()
	{
	__asm
		{
		jmp p[30*4];
		}
	}

// WSASetBlockingHook
extern "C" __declspec(naked) void __stdcall __E__31__()
	{
	__asm
		{
		jmp p[31*4];
		}
	}

// WSASetLastError
extern "C" __declspec(naked) void __stdcall __E__32__()
	{
	__asm
		{
		jmp p[32*4];
		}
	}

// WSAStartup
extern "C" __declspec(naked) void __stdcall __E__33__()
	{
	__asm
		{
		jmp p[33*4];
		}
	}

// WSAUnhookBlockingHook
extern "C" __declspec(naked) void __stdcall __E__34__()
	{
	__asm
		{
		jmp p[34*4];
		}
	}

// WSApSetPostRoutine
extern "C" __declspec(naked) void __stdcall __E__35__()
	{
	__asm
		{
		jmp p[35*4];
		}
	}

// __WSAFDIsSet
extern "C" __declspec(naked) void __stdcall __E__36__()
	{
	__asm
		{
		jmp p[36*4];
		}
	}

// accept
extern "C" __declspec(naked) void __stdcall __E__37__()
	{
	__asm
		{
		jmp p[37*4];
		}
	}

// bind
extern "C" __declspec(naked) void __stdcall __E__38__()
	{
	__asm
		{
		jmp p[38*4];
		}
	}

// closesocket
extern "C" __declspec(naked) void __stdcall __E__39__()
	{
	__asm
		{
		jmp p[39*4];
		}
	}

// connect
extern "C" __declspec(naked) void __stdcall __E__40__()
	{
	__asm
		{
		jmp p[40*4];
		}
	}

// dn_expand
extern "C" __declspec(naked) void __stdcall __E__41__()
	{
	__asm
		{
		jmp p[41*4];
		}
	}

// gethostbyaddr
extern "C" __declspec(naked) void __stdcall __E__42__()
	{
	__asm
		{
		jmp p[42*4];
		}
	}

// gethostbyname
extern "C" __declspec(naked) void __stdcall __E__43__()
	{
	__asm
		{
		jmp p[43*4];
		}
	}

// gethostname
extern "C" __declspec(naked) void __stdcall __E__44__()
	{
	__asm
		{
		jmp p[44*4];
		}
	}

// getnetbyname
extern "C" __declspec(naked) void __stdcall __E__45__()
	{
	__asm
		{
		jmp p[45*4];
		}
	}

// getpeername
extern "C" __declspec(naked) void __stdcall __E__46__()
	{
	__asm
		{
		jmp p[46*4];
		}
	}

// getprotobyname
extern "C" __declspec(naked) void __stdcall __E__47__()
	{
	__asm
		{
		jmp p[47*4];
		}
	}

// getprotobynumber
extern "C" __declspec(naked) void __stdcall __E__48__()
	{
	__asm
		{
		jmp p[48*4];
		}
	}

// getservbyname
extern "C" __declspec(naked) void __stdcall __E__49__()
	{
	__asm
		{
		jmp p[49*4];
		}
	}

// getservbyport
extern "C" __declspec(naked) void __stdcall __E__50__()
	{
	__asm
		{
		jmp p[50*4];
		}
	}

// getsockname
extern "C" __declspec(naked) void __stdcall __E__51__()
	{
	__asm
		{
		jmp p[51*4];
		}
	}

// getsockopt
extern "C" __declspec(naked) void __stdcall __E__52__()
	{
	__asm
		{
		jmp p[52*4];
		}
	}

// htonl
extern "C" __declspec(naked) void __stdcall __E__53__()
	{
	__asm
		{
		jmp p[53*4];
		}
	}

// htons
extern "C" __declspec(naked) void __stdcall __E__54__()
	{
	__asm
		{
		jmp p[54*4];
		}
	}

// inet_addr
extern "C" __declspec(naked) void __stdcall __E__55__()
	{
	__asm
		{
		jmp p[55*4];
		}
	}

// inet_network
extern "C" __declspec(naked) void __stdcall __E__56__()
	{
	__asm
		{
		jmp p[56*4];
		}
	}

// inet_ntoa
extern "C" __declspec(naked) void __stdcall __E__57__()
	{
	__asm
		{
		jmp p[57*4];
		}
	}

// ioctlsocket
extern "C" __declspec(naked) void __stdcall __E__58__()
	{
	__asm
		{
		jmp p[58*4];
		}
	}

// listen
extern "C" __declspec(naked) void __stdcall __E__59__()
	{
	__asm
		{
		jmp p[59*4];
		}
	}

// ntohl
extern "C" __declspec(naked) void __stdcall __E__60__()
	{
	__asm
		{
		jmp p[60*4];
		}
	}

// ntohs
extern "C" __declspec(naked) void __stdcall __E__61__()
	{
	__asm
		{
		jmp p[61*4];
		}
	}

// rcmd
extern "C" __declspec(naked) void __stdcall __E__62__()
	{
	__asm
		{
		jmp p[62*4];
		}
	}

// recv
extern "C" __declspec(naked) void __stdcall __E__63__()
	{
	__asm
		{
		jmp p[63*4];
		}
	}

// recvfrom
extern "C" __declspec(naked) void __stdcall __E__64__()
	{
	__asm
		{
		jmp p[64*4];
		}
	}

// rexec
extern "C" __declspec(naked) void __stdcall __E__65__()
	{
	__asm
		{
		jmp p[65*4];
		}
	}

// rresvport
extern "C" __declspec(naked) void __stdcall __E__66__()
	{
	__asm
		{
		jmp p[66*4];
		}
	}

// s_perror
extern "C" __declspec(naked) void __stdcall __E__67__()
	{
	__asm
		{
		jmp p[67*4];
		}
	}

// select
extern "C" __declspec(naked) void __stdcall __E__68__()
	{
	__asm
		{
		jmp p[68*4];
		}
	}

// send
extern "C" __declspec(naked) void __stdcall __E__69__()
	{
	__asm
		{
		jmp p[69*4];
		}
	}

// sendto
extern "C" __declspec(naked) void __stdcall __E__70__()
	{
	__asm
		{
		jmp p[70*4];
		}
	}

// sethostname
extern "C" __declspec(naked) void __stdcall __E__71__()
	{
	__asm
		{
		jmp p[71*4];
		}
	}

// setsockopt
extern "C" __declspec(naked) void __stdcall __E__72__()
	{
	__asm
		{
		jmp p[72*4];
		}
	}

// shutdown
extern "C" __declspec(naked) void __stdcall __E__73__()
	{
	__asm
		{
		jmp p[73*4];
		}
	}

// socket
extern "C" __declspec(naked) void __stdcall __E__74__()
	{
	__asm
		{
		jmp p[74*4];
		}
	}

exports.def
PHP:
LIBRARY	"WSOCK32"
EXPORTS
AcceptEx=__E__0__ @1141
EnumProtocolsA=__E__1__ @1111
EnumProtocolsW=__E__2__ @1112
GetAcceptExSockaddrs=__E__3__ @1142
GetAddressByNameA=__E__4__ @1109
GetAddressByNameW=__E__5__ @1110
GetNameByTypeA=__E__6__ @1115
GetNameByTypeW=__E__7__ @1116
GetServiceA=__E__8__ @1119
GetServiceW=__E__9__ @1120
GetTypeByNameA=__E__10__ @1113
GetTypeByNameW=__E__11__ @1114
MigrateWinsockConfiguration=__E__12__ @24
NPLoadNameSpaces=__E__13__ @1130
SetServiceA=__E__14__ @1117
SetServiceW=__E__15__ @1118
TransmitFile=__E__16__ @1140
WEP=__E__17__ @500
WSAAsyncGetHostByAddr=__E__18__ @102
WSAAsyncGetHostByName=__E__19__ @103
WSAAsyncGetProtoByName=__E__20__ @105
WSAAsyncGetProtoByNumber=__E__21__ @104
WSAAsyncGetServByName=__E__22__ @107
WSAAsyncGetServByPort=__E__23__ @106
WSAAsyncSelect=__E__24__ @101
WSACancelAsyncRequest=__E__25__ @108
WSACancelBlockingCall=__E__26__ @113
WSACleanup=__E__27__ @116
WSAGetLastError=__E__28__ @111
WSAIsBlocking=__E__29__ @114
WSARecvEx=__E__30__ @1107
WSASetBlockingHook=__E__31__ @109
WSASetLastError=__E__32__ @112
WSAStartup=__E__33__ @115
WSAUnhookBlockingHook=__E__34__ @110
WSApSetPostRoutine=__E__35__ @1000
__WSAFDIsSet=__E__36__ @151
accept=__E__37__ @1
bind=__E__38__ @2
closesocket=__E__39__ @3
connect=__E__40__ @4
dn_expand=__E__41__ @1106
gethostbyaddr=__E__42__ @51
gethostbyname=__E__43__ @52
gethostname=__E__44__ @57
getnetbyname=__E__45__ @1101
getpeername=__E__46__ @5
getprotobyname=__E__47__ @53
getprotobynumber=__E__48__ @54
getservbyname=__E__49__ @55
getservbyport=__E__50__ @56
getsockname=__E__51__ @6
getsockopt=__E__52__ @7
htonl=__E__53__ @8
htons=__E__54__ @9
inet_addr=__E__55__ @10
inet_network=__E__56__ @1100
inet_ntoa=__E__57__ @11
ioctlsocket=__E__58__ @12
listen=__E__59__ @13
ntohl=__E__60__ @14
ntohs=__E__61__ @15
rcmd=__E__62__ @1102
recv=__E__63__ @16
recvfrom=__E__64__ @17
rexec=__E__65__ @1103
rresvport=__E__66__ @1104
s_perror=__E__67__ @1108
select=__E__68__ @18
send=__E__69__ @19
sendto=__E__70__ @20
sethostname=__E__71__ @1105
setsockopt=__E__72__ @21
shutdown=__E__73__ @22
socket=__E__74__ @23


PME_X.h
PHP:
// basic file operations
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <windows.h>

using namespace std;

#pragma pack(1)

#define ASM			void __declspec(naked)
#define	EXPORT		__declspec(dllexport) __cdecl
#define	THREAD		DWORD WINAPI
#define Naked __declspec( naked )
#define INST_NOP 0x90
#define INST_CALL 0xe8
#define INST_JMP 0xe9
#define INST_BYTE 0x00
#define SHORT_JZ 0x74
#define MAXWAIT 100000

////////////////////////////////////////////////////////////////////////
//////////////////////////  CPP FILES    ///////////////////////////////
//Mem.cpp
LPVOID MemcpyEx(DWORD lpDest, DWORD lpSource, int len);
DWORD Intercept(int instruction, DWORD lpSource, DWORD lpDest, int len);
void DOS();


//Hook.cpp
class Hook
{
	public:
		void LvlupPoints(BYTE);
};

Mem.cpp
PHP:
#include "PME_X.h"


LPVOID MemcpyEx(DWORD lpDest, DWORD lpSource, int len)
{
	DWORD oldSourceProt,oldDestProt=0;
	VirtualProtect((LPVOID)lpSource,len,PAGE_EXECUTE_READWRITE,&oldSourceProt);
	VirtualProtect((LPVOID)lpDest,len,PAGE_EXECUTE_READWRITE,&oldDestProt);
	memcpy((void*)lpDest,(void*)lpSource,len);
	VirtualProtect((LPVOID)lpDest,len,oldDestProt,&oldDestProt);
	VirtualProtect((LPVOID)lpSource,len,oldSourceProt,&oldSourceProt);
	return (LPVOID)lpDest;
};

DWORD Intercept(int instruction, DWORD lpSource, DWORD lpDest, int len)
{

	DWORD realtarget;
	LPBYTE buffer = new BYTE[len];

	memset(buffer,0x90,len); //Fill out with nops

	if (instruction != INST_NOP && len >= 5)
	{
		buffer[(len-5)] = instruction;
		DWORD dwJMP = (DWORD)lpDest - (lpSource + 5 + (len-5));
		memcpy(&realtarget,(void*)(lpSource+1),4);
		realtarget = realtarget+lpSource+5;
		memcpy(buffer + 1 + (len-5),&dwJMP,4);
	}
	if (instruction == SHORT_JZ)
	{
		buffer[0]=instruction;
		buffer[1]=(BYTE)lpDest;
	}
	if (instruction == INST_BYTE)
	{
		buffer[0]=(BYTE)lpDest;
	}
	MemcpyEx(lpSource, (DWORD) buffer, len);
	delete[] buffer;
	return realtarget;
}



void DOS()
{
	int hCrtIn, hCrtOut;
	FILE *conIn, *conOut;

	AllocConsole();
	hCrtIn = _open_osfhandle ((intptr_t) GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
	hCrtOut = _open_osfhandle ((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
	conIn = _fdopen( hCrtIn, "r" );
	conOut = _fdopen( hCrtOut, "w" );
	*stdin = *conIn;
	*stdout = *conOut;
}

Hook.cpp
PHP:
#include "PME_X.h"

void Hook::LvlupPoints(BYTE PTS)
{

	BYTE code[] = {0x83 , 0xC1 , PTS};
	MemcpyEx(0x00459149,(DWORD)&code,3);


}


i hope i do something usfull for someone here :)

Yours NOOR
 
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

Well, you could pretty much have done this:

PHP:
KalHook::SetLevelUpPoints(BYTE points)
{
    BYTE code[] = {0x83 , 0xC1 , points};
    MemoryCopy(0x00459149,(DWORD)&code,3);
}

The called it in the Attach

PHP:
KalHook::Attach()
{
    this->SetLevelUpPoints(5);
}

The rest seem rather unnessary and stupidly complex.
And namespaces are made of win.
 
Last edited:
Newbie Spellweaver
Joined
Oct 10, 2006
Messages
61
Reaction score
1
Re: [Guide] C++ DLL injection for Main Server

Well, you could pretty much have done this:

PHP:
KalHook::SetLevelUpPoints(BYTE points)
{
    BYTE code[] = {0x83 , 0xC1 , PTS};
    MemoryCopy(0x00459149,(DWORD)&code,3);
}

The called it in the Attach

PHP:
KalHook::Attach()
{
    this->SetLevelUpPoints(5);
}

The rest seem rather unnessary and stupidly complex.
And namespaces are made of win.
i just try to do something ^_^ and i said im beginner also
iknow that namespaces ire pretty better to be used but idont know how ^^
till this day as i understand how from your source :)
other thing that i didnt use this-> to call things "also i just know it today" xD
thanks again for code ....
 
Arrogant Wizard
Loyal Member
Joined
Mar 30, 2007
Messages
745
Reaction score
34
Re: [Guide] C++ DLL injection for Main Server

hehe, learning is always good. I learned ALOT while doing this.
3 days before I wrote this guide iknew pratical zero about the subject.

Also if you got more memory-copy samples, I would like to know :)
 
Newbie Spellweaver
Joined
Oct 10, 2006
Messages
61
Reaction score
1
Re: [Guide] C++ DLL injection for Main Server

yes sure ;)
3 days before I wrote this guide iknew pratical zero about the subject.
about 2 months befor i couldnt write header files xD

Also if you got more memory-copy samples, I would like to know :)
hmm what you mean by memory-copy samples?
other things to hook..?
or
other functions to copy memory << im sure not this

for first one ill try to add some other usefull hooks
 
Junior Spellweaver
Joined
Sep 16, 2006
Messages
187
Reaction score
0
Re: [Guide] C++ DLL injection for Main Server

Nice Guide :] dont have test the Code but i know it works ;)
gogo Part 3 maybe show some hooking tips or hmmm idk
 
Back
Top