Skip to content

Commit

Permalink
Added support for 1.0
Browse files Browse the repository at this point in the history
method/function call
gsc functions (vector/entity probably not working like 1.3)
Updated kung's injector loop with file check
  • Loading branch information
M-itch committed Jan 4, 2015
1 parent e41d3f4 commit a46c856
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 78 deletions.
3 changes: 3 additions & 0 deletions InjectDLL/InjectDLL.depend
Expand Up @@ -14,3 +14,6 @@
1388344331 source:c:\users\mitch\documents\codeblocks\injectdll\injectdll.rc
<winuser.h>

1388344331 source:c:\users\mitch\documents\github\libcod_win\injectdll\injectdll.rc
<winuser.h>

122 changes: 88 additions & 34 deletions InjectDLL/main.cpp
@@ -1,62 +1,116 @@
#include <stdio.h>
#define _WIN32_WINNT 0x500
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>

void EnableDebugPriv();
void CALLBACK WaitOrTimerCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
int GetProcessByName(char *name, HANDLE *outProcessHandle, int *outProcessID);
void InjectDLL(HANDLE hProcess, char *name);
void WaitForProcessAndInjectDLL(char *name_process, char *name_dll);
void LoopInjecting();

BOOL FileExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);

return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

void EnableDebugPriv() {
HANDLE hToken;
LUID luid;
TOKEN_PRIVILEGES tkp;

OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken );

LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &luid );

tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = luid;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

AdjustTokenPrivileges( hToken, false, &tkp, sizeof( tkp ), NULL, NULL );

CloseHandle( hToken );
}

int main( int, char *[] ) {
void CALLBACK WaitOrTimerCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired) {
//MessageBox(0, "The process has exited.", "INFO", MB_OK);
LoopInjecting();
}

int GetProcessByName(char *name, HANDLE *outProcessHandle, int *outProcessID) {
PROCESSENTRY32 entry;
entry.dwSize = sizeof( PROCESSENTRY32 );

HANDLE snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );
if ( Process32First( snapshot, &entry ) != TRUE )
return 0;
while (Process32Next( snapshot, &entry ) == TRUE) {
if (stricmp( entry.szExeFile, name) != 0)
continue;
// printf("Found: %s\n", entry.szExeFile);
// PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE
*outProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
*outProcessID = entry.th32ProcessID;
return 1;
}
CloseHandle( snapshot );
return 0;
}

if ( Process32First( snapshot, &entry ) == TRUE ) {
while ( Process32Next( snapshot, &entry ) == TRUE ) {
if ( stricmp( entry.szExeFile, "CoD2MP_s.exe" ) == 0 ) {
std::cout << "Found CoD2MP_s.exe\n";
EnableDebugPriv();

char dirPath[MAX_PATH];
char fullPath[MAX_PATH];

GetCurrentDirectory( MAX_PATH, dirPath );

snprintf ( fullPath, MAX_PATH, "%s\\libcod_win.dll", dirPath );

std::cout << "Injecting: " << fullPath << "\n";

HANDLE hProcess = OpenProcess( PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, entry.th32ProcessID );
std::cout << "Process: " << entry.th32ProcessID << "\n";
LPVOID libAddr = (LPVOID)GetProcAddress( GetModuleHandle( "kernel32.dll" ), "LoadLibraryA" );
LPVOID llParam = (LPVOID)VirtualAllocEx( hProcess, NULL, strlen( fullPath ), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
void InjectDLL(HANDLE hProcess, char *name) {
char dirPath[MAX_PATH];
char fullPath[MAX_PATH];
GetCurrentDirectory( MAX_PATH, dirPath );
snprintf ( fullPath, MAX_PATH, "%s\\%s", dirPath, name);

if(FileExists(fullPath) == 1)
{
printf("Injecting: %s\n", fullPath);
LPVOID libAddr = (LPVOID)GetProcAddress( GetModuleHandle( "kernel32.dll" ), "LoadLibraryA" );
LPVOID llParam = (LPVOID)VirtualAllocEx( hProcess, NULL, strlen( fullPath ) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
printf("libAddr=%.8p llParam=%.8p\n", libAddr, llParam);
bool written = WriteProcessMemory( hProcess, llParam, fullPath, strlen( fullPath ) + 1, NULL );
HANDLE threadID = CreateRemoteThread( hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)libAddr, llParam, NULL, NULL );
//CloseHandle( hProcess );
printf("Finished injecting DLL success=%d thread #%d\n", written, threadID);
}
else
printf("File '%s' does not exist.\n", fullPath);
}

bool written = WriteProcessMemory( hProcess, llParam, fullPath, strlen( fullPath ), NULL );
HANDLE threadID = CreateRemoteThread( hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)libAddr, llParam, NULL, NULL );
CloseHandle( hProcess );
std::cout << "Finished injecting DLL (" << written << ") thread #" << threadID;
}
void WaitForProcessAndInjectDLL(char *name_process, char *name_dll) {
printf("WaitForProcessAndInjectDLL(process=%s, dll=%s);\n", name_process, name_dll);
HANDLE hProcess;
int processID;
while (1) {
int ret = GetProcessByName(name_process, &hProcess, &processID);
if (ret == 0) {
printf(".");
Sleep(1000);
continue;
}
printf("\nprocessID=%d\n", processID);
InjectDLL(hProcess, name_dll);
HANDLE hNewHandle;
RegisterWaitForSingleObject(&hNewHandle, hProcess, WaitOrTimerCallback, NULL, INFINITE, WT_EXECUTEONLYONCE);
break;
}
}

CloseHandle( snapshot );
int argc;
char **argv;
void LoopInjecting() {
// CoD2MP_s.exe
WaitForProcessAndInjectDLL(argv[1], argv[2]); // process, dll
}
int main(int c, char **v) {
argc = c;
argv = v;
if (argc < 2) {
printf("Please provide process-name and dll-name!\nExample: InjectDLL SERVER.exe libcod2_1_3.dll");
getchar();
return 1;
}
EnableDebugPriv();
LoopInjecting();
getchar();

return 0;
}
9 changes: 6 additions & 3 deletions libcod_win/include/functions.h
@@ -1,12 +1,15 @@
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>

typedef int (*Com_Printf_t)(const char*, ...);

#if COD_VERSION == COD2_1_3
#if COD_VERSION == COD2_1_0
static Com_Printf_t Com_Printf = (Com_Printf_t)0x0430540;
#elif COD_VERSION == COD2_1_3
static Com_Printf_t Com_Printf = (Com_Printf_t)0x0431EE0;
#else
static Com_Printf_t Com_Printf = (Com_Printf_t)NULL;
static Com_Printf_t Com_Printf = (Com_Printf_t)0;
#warning Com_Printf_t Com_Printf = NULL;
#endif

Expand All @@ -15,7 +18,7 @@ typedef int (*Cmd_ExecuteString_t)(const char *text);
#if COD_VERSION == COD2_1_3
static Cmd_ExecuteString_t Cmd_ExecuteString = (Cmd_ExecuteString_t)0x04214C0;
#else
static Cmd_ExecuteString_t Cmd_ExecuteString = (Cmd_ExecuteString_t)NULL;
static Cmd_ExecuteString_t Cmd_ExecuteString = (Cmd_ExecuteString_t)0;
#warning Cmd_ExecuteString_t Cmd_ExecuteString = NULL;
#endif

Expand Down
2 changes: 1 addition & 1 deletion libcod_win/include/gsc.h
Expand Up @@ -12,7 +12,7 @@
#define COD2_1_3 213
#define COD4_1_7 417

#if COD_VERSION == COD2_1_2 || COD_VERSION == COD2_1_3 || COD_VERSION == COD2_1_0
#if COD_VERSION == COD2_1_0 || COD_VERSION == COD2_1_2 || COD_VERSION == COD2_1_3
#define STACK_UNDEFINED 0
#define STACK_OBJECT 1
#define STACK_STRING 2
Expand Down
26 changes: 24 additions & 2 deletions libcod_win/libcod_win.cbp
Expand Up @@ -7,7 +7,7 @@
<Option compiler="gcc" />
<Build>
<Target title="Debug CoD2 1.3">
<Option output="bin/Debug/libcod_win" prefix_auto="1" extension_auto="1" />
<Option output="bin/Debug/libcod2_1_3" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="3" />
<Option compiler="gcc" />
Expand All @@ -30,8 +30,30 @@
<Variable name="COD_VERSION" value="COD2_1_3" />
</Environment>
</Target>
<Target title="Release CoD2 1.0">
<Option output="bin/Release/libcod2_1_0" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-O2" />
<Add option="-Wall" />
<Add option="-DCOD_VERSION=COD2_1_0" />
<Add option="-DBUILD_DLL" />
<Add option="-D_WIN32_WINNT=0x500" />
<Add directory="mysql" />
</Compiler>
<Linker>
<Add option="-s" />
<Add library="user32" />
<Add library="lib\libmysql.lib" />
<Add directory="mysql" />
</Linker>
</Target>
<Target title="Release CoD2 1.3">
<Option output="bin/Release/libcod_win" prefix_auto="1" extension_auto="1" />
<Option output="bin/Release/libcod2_1_3" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="3" />
<Option compiler="gcc" />
Expand Down
17 changes: 10 additions & 7 deletions libcod_win/libcod_win.depend
Expand Up @@ -37,25 +37,28 @@
1391122798 c:\users\mitch\documents\github\libcod_win\libcod_win\include\cracking.h
<string.h>

1391469900 source:c:\users\mitch\documents\github\libcod_win\libcod_win\src\gsc.cpp
1420325630 source:c:\users\mitch\documents\github\libcod_win\libcod_win\src\gsc.cpp
"../include/gsc.h"
"../include/functions.h"
<string.h>
<stdio.h>
<stdarg.h>

1394279879 c:\users\mitch\documents\github\libcod_win\libcod_win\include\gsc.h
1420324670 c:\users\mitch\documents\github\libcod_win\libcod_win\include\gsc.h
"gsc_player.h"
"gsc_mysql.h"
"gsc_utils.h"
"config.h"

1394290353 c:\users\mitch\documents\github\libcod_win\libcod_win\include\functions.h
1420371781 c:\users\mitch\documents\github\libcod_win\libcod_win\include\functions.h
<stdio.h>

1394285079 source:c:\users\mitch\documents\github\libcod_win\libcod_win\main.cpp
1420396948 source:c:\users\mitch\documents\github\libcod_win\libcod_win\main.cpp
"main.h"
<stdio.h>
"include/gsc.h"
"include/functions.h"
"include/cracking.h"
"include/gsc.h"

1387490054 c:\users\mitch\documents\github\libcod_win\libcod_win\main.h
<windows.h>
Expand All @@ -64,7 +67,7 @@

1394286785 c:\users\mitch\documents\github\libcod_win\libcod_win\include\config.h

1391452877 source:c:\users\mitch\documents\github\libcod_win\libcod_win\src\gsc_player.cpp
1418152427 source:c:\users\mitch\documents\github\libcod_win\libcod_win\src\gsc_player.cpp
"../include/gsc_player.h"
"../include/functions.h"
"../include/gsc.h"
Expand Down Expand Up @@ -120,7 +123,7 @@

1387826646 c:\users\mitch\documents\github\libcod_win\libcod_win\mysql\mysql\psi\psi_base.h

1394289723 source:c:\users\mitch\documents\github\libcod_win\libcod_win\src\gsc_utils.cpp
1420396697 source:c:\users\mitch\documents\github\libcod_win\libcod_win\src\gsc_utils.cpp
"../include/gsc_utils.h"
"../include/functions.h"
"../include/gsc.h"
Expand Down
14 changes: 12 additions & 2 deletions libcod_win/main.cpp
@@ -1,18 +1,27 @@
#include "main.h"
#include <stdio.h>
#include "include/gsc.h"
#include "include/functions.h"
#include "include/cracking.h"
#include "include/gsc.h"

DWORD WINAPI MyThread(LPVOID);
DWORD g_threadID;
HMODULE g_hModule;
static int isStarted = 0;

DWORD WINAPI MyThread(LPVOID)
{
if (isStarted) {
Com_Printf("Already started!\n");
return (int)NULL;
}
isStarted = 1;
Com_Printf("[PLUGIN LOADED]\n");

#if COD_VERSION == COD2_1_3
#if COD_VERSION == COD2_1_0
cracking_hook_call(0x46B83F, (int)Scr_GetCustomFunction);
cracking_hook_call(0x46BA83, (int)Scr_GetCustomMethod);
#elif COD_VERSION == COD2_1_3
cracking_hook_call(0x46E7BF, (int)Scr_GetCustomFunction);
cracking_hook_call(0x46EA03, (int)Scr_GetCustomMethod);
#endif
Expand All @@ -25,6 +34,7 @@ extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
//MessageBoxA( NULL, "[PLUGIN LOADED]", "libcod", MB_OK );
g_hModule = hinstDLL;
DisableThreadLibraryCalls(hinstDLL);
CloseHandle(CreateThread(NULL, 0, &MyThread, NULL, 0, &g_threadID));
Expand Down

0 comments on commit a46c856

Please sign in to comment.