Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Injecting DLL to memory hack

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
doggyx
How do I cheat?
Reputation: 0

Joined: 08 Jul 2013
Posts: 8

PostPosted: Tue Jul 09, 2013 8:50 am    Post subject: [C++] Injecting DLL to memory hack Reply with quote

Hi all,

I'm trying to make a DLL hack for Cube World. Currently I have one in C#, but I need to write all the opcode bytes to the memory and it's really tiresome. That's why I want to give C++ a try, since inline ASM is awesome!

I'm a real rookie, so please don't be too harsh Very Happy.

So in this Cube World game, the player base can be found like this:
- ("Cube.exe"+0x3691C8) + 0x394

Then for example the health, which is a float, is at player base + 0x16C:
- dwPlayerBase + 0x16C

So what I tried in C++ already, is to try to get the player base and check the health. If it's not zero, stop the loop. Then we know we're ingame.
Code:

#include <Windows.h>

#define CUBEBASE 0x905A4D

BOOL ingame = false;

void detectIngame()
{
   BOOL loop = true;
   while(loop)
   {
      DWORD dwPlayerBase = *(DWORD*)(*(DWORD*)(CUBEBASE+0x3691C8) + 0x394);
      DWORD dwHPAddress = *(DWORD*)(dwPlayerBase + 0x16C);

      if(*(float*)dwHPAddress != 0)
         loop = false;
   }
}

int __stdcall DllMain( HINSTANCE instance, DWORD reason, LPVOID reserved ) {
   if ( reason == DLL_PROCESS_ATTACH )
   {
      DisableThreadLibraryCalls( reinterpret_cast< HMODULE >( instance ) );

      CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)detectIngame, NULL, NULL, NULL);
   }

   return 1;
}


But it just crashes when I inject it. It runs the thread just fine (I checked and it got executed), but it crashes when I try to read the health. I have no clue what I'm doing wrong. I'm not even writing to the memory, so VirtualProtect wouldn't be needed, would it?

As I said, I'm really new with this, so any help is appreciated!

Thanks

Edit:
I read a bit more on this topic and found out that I was doing it completely wrong. I updated the memory reading as I now think is correct, but it's still crashing. Any clues?

I also thought maybe I have to include the base of the process? But still crashes
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Jul 09, 2013 2:02 pm    Post subject: Reply with quote

It sounds like your offsets are wrong or being read incorrectly.

You can attach Visual Studio to the process and debug your DLL as well. (Debug -> Attach To Process) which will let you set breakpoints and step through your code to find where the crash is at.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
doggyx
How do I cheat?
Reputation: 0

Joined: 08 Jul 2013
Posts: 8

PostPosted: Tue Jul 09, 2013 3:32 pm    Post subject: Reply with quote

I was using the Express edition so couldn't attach it. Downloaded Ultimate edition (Dreamspark ftw) and found out the memory was read protected.

Code:

      DWORD oldProtect;
      VirtualProtect((LPVOID)0xBD91C8, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtect);
      DWORD* tmp1 = (DWORD*)0xBD91C8;
      VirtualProtect((LPVOID)0xBD91C8, sizeof(DWORD), oldProtect, &oldProtect);

      VirtualProtect((LPVOID)(*tmp1+0x394), sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtect);
      DWORD* dwPlayerBase = (DWORD*)(*tmp1+0x394);
      VirtualProtect((LPVOID)(*tmp1+0x394), sizeof(DWORD), oldProtect, &oldProtect);

      VirtualProtect((LPVOID)(*dwPlayerBase+0x16C), sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtect);
      float* dwHPAdd = (float*)(*dwPlayerBase+0x16C);
      VirtualProtect((LPVOID)(*dwPlayerBase+0x16C), sizeof(DWORD), oldProtect, &oldProtect);

      float hp = *dwHPAdd;
      *dwHPAdd = 2000;


This works just fine Smile. And indeed, the base was wrong.

This is solved. Thanks
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites