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++ OpenProcess/WriteProcessMemory

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Unc3nZureD
Newbie cheater
Reputation: 0

Joined: 17 Jul 2013
Posts: 13

PostPosted: Sat Aug 17, 2013 2:21 pm    Post subject: C++ OpenProcess/WriteProcessMemory Reply with quote

Hi guys, I'm trying to make a DLL which modifies the memory when it's injected into a process.

Here's the code which is called on injection:

Code:
DWORD WINAPI memoryPatch( LPVOID )
{
   Sleep(5000);

   int newValue = 0;

   DWORD proc_id = GetCurrentProcessId();
   HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
   if( !hProcess )
      ExitProcess( 1 );

   BYTE EnableTestBytes[48] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";

   int isSuccessful = WriteProcessMemory( hProcess, (LPVOID)0x006A6F24, &EnableTestBytes, 48, NULL );

   if (isSuccessful > 0)
   {
      MessageBox(NULL, "Success", "title", NULL);
   }
   else
   {
      MessageBox(NULL, "FATAL ERROR", "title", NULL);
   }

   return true;
}


The problem is that it auto-exits. I'm using winxp and the process should have all privileges. What am I doing wrong?
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: Sat Aug 17, 2013 5:31 pm    Post subject: Reply with quote

Firstly you are injected, you don't need to use API.
Just use memcpy / memset to alter memory (or directly access it via pointers).

As for auto-exiting, more than likely because of:

Code:
   HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
   if( !hProcess )
      ExitProcess( 1 );


PROCESS_ALL_ACCESS fails on Windows Vista and higher unless you adjust the debug token of the process. As stated already though, you don't need to use API when injected.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Unc3nZureD
Newbie cheater
Reputation: 0

Joined: 17 Jul 2013
Posts: 13

PostPosted: Sun Aug 18, 2013 1:27 pm    Post subject: Reply with quote

Thanks for your reply.
I'm quite a novice user, could you give me some examples?
I mean I can't see any address parameter at memset / memcpy. How could I write thoose bytes from the 0x006A6F24 address?
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: Mon Aug 19, 2013 10:01 am    Post subject: Reply with quote

For starters:
Code:
BYTE EnableTestBytes[48] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";

   int isSuccessful = WriteProcessMemory( hProcess, (LPVOID)0x006A6F24, &EnableTestBytes, 48, NULL );


Given that you are only writing 00's you can use memset for that. Like this:
memset( (LPVOID)0x006A6F24, 0x00, 48 );

If you want to use memcpy to do the same thing, you can use:
memcpy( (LPVOID)0x006A6F24, &EnableTestBytes, 48 );

memset/memcpy have direct access to the current process memory. If you are injected, you have direct access to that processes memory then.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Unc3nZureD
Newbie cheater
Reputation: 0

Joined: 17 Jul 2013
Posts: 13

PostPosted: Mon Aug 19, 2013 10:05 am    Post subject: Reply with quote

Oh, the 1st parameter is the address I want to access. MemSet seems to be awesome since it's much easier Smile

Currently I can't test it, but thanks a lot!
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