0

I want to write an auto for gaming. I use Cheat Engine and found the following Assembly code:

push 014656DC
call 00633940
add esp,04
ret

I inject this code into the app using Cheat Engine and it always worked.

ASM in Cheat

Now, I want to use C++ code so I wrote a DLL:

  • header file:
#ifdef DIVLIB_EXPORTS
#define DIVLIB_API __declspec(dllexport)
#else
#define DIVLIB_API __declspec(dllimport)
#endif

extern "C" {
    DIVLIB_API void ClickID();
}
  • source file:
void ClickID()
{
    _asm {
        push esi;
        push eax;
        mov esi, 0x014656DC;
        mov eax, 0x00633940;
        push esi;
        call eax;
        add esp, 0x04;
    }
}

And I have the Main.exe to call this DLL

auto h_process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
char dll[] = "D:\\project\\DivHook\\divhook\\Debug\\divlib.dll";
HMODULE hinstDLL = LoadLibraryA(dll);
LPVOID LoadLibAddress = (LPVOID)GetProcAddress(hinstDLL, "ClickID");
LPVOID MemAlloc = (LPVOID)VirtualAllocEx(h_process, NULL, strlen(dll)+1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(h_process, (LPVOID)MemAlloc, dll, strlen(dll) + 1, NULL);
CreateRemoteThread(h_process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddress, (LPVOID)MemAlloc, NULL, NULL);
CloseHandle(h_process);
VirtualFreeEx(h_process, (LPVOID)MemAlloc, 0, MEM_RELEASE | MEM_DECOMMIT);

However, my game always crashes when Main.exe is run.

I have tried the above Assembly over and over with the Cheat Engine and made sure it works perfectly, but C++ code always crashes.

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
Divide
  • 19
  • 6
  • If you don't check the return codes for errors, you'll have to find the errors the hard way. – user4581301 Sep 20 '21 at 16:36
  • Fair warning this is highly non-trivial, particularly on modern systems with DEP and CFG. Cheatengine is likely handling that on its own. Doing that manually will be ugly. – Mgetz Sep 20 '21 at 16:38
  • 1
    The code you've provided is basically an arbitrary jump as it is. Are you certain `0x00633940` is valid each time the program runs? It's very possible that the binary uses randomized addressing. What is the purpose for this code you are attempting to inject? – h0r53 Sep 20 '21 at 16:38
  • No pops needed for the pushes ? – kalyanswaroop Sep 20 '21 at 16:40
  • @h0r53 my guess based on the fact it's 32bit code is that this is an older non-ALSR game and the address is within the main exe. But that still doesn't change the fact that the approach being used is suspect. – Mgetz Sep 20 '21 at 16:43
  • @h0r53 I'm sure – Divide Sep 20 '21 at 16:55
  • I can explain about fixed values ​​as follows. I opened the game and used the cheat engine to find those values ​​(I wanted to do a quick test, so I omitted the pointer and add offset). Run the code on Cheat engine Then use it in C++ – Divide Sep 20 '21 at 17:01
  • 1
    As far as I can tell, you are loading that dll incorrectly. I assume that you are injecting it in a arbitrary process!? You write dll path to other process memory but you dont load it using LoadLibrary, instead you use LoadLibrary in your own process. – The Average Google User Sep 20 '21 at 17:20
  • @TheAverageGoogleUser I want to controller another application and I don't have source code. So, I use Main.exe to inject the function ClickID. I have no experience with this so I just simulate what the cheat engine is doing is call the Assembly function. – Divide Sep 21 '21 at 03:24

1 Answers1

1

Well, Its not that easy to convert assembly to C++, Its actually impossible, Unless you can de-compile the main executable then yes you can do it, But for converting the Assembly code used by cheat engine that is impossible.

You can try to get it working by executing the assembly code using a DLL injected into the games process, But you will need to manage its memory and give it certain instructions.

Try and de-compile the main Executable first and see where that gets you. Once you find out what piece of source code the instruction is called from then you can try to Reverse Engineer the instruction by using again DLL Injection but at least you will know what to do, But just straight assembly into C++ won't be happening.

PackedUP32
  • 17
  • 4
  • Whilst your comment is relevant, and I believe you were being sarcastic, be aware that what you suggest may be illegal in certain legal territories, see... https://en.wikipedia.org/wiki/Reverse_engineering – Tiger4Hire Dec 02 '21 at 14:49
  • Ah, I guess it is illegal in some areas. – PackedUP32 Dec 03 '21 at 15:04