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 


Scanning memory functions in C

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

Joined: 24 Mar 2011
Posts: 5

PostPosted: Thu Mar 24, 2011 8:46 pm    Post subject: Scanning memory functions in C Reply with quote

I would like to use the functions to scan the memory in c.

I haven't found any c source that can read/write to memory of another process as your program does, many sources I've found is way too complicated for me.

I use a different programming language altogether (QB64) which can include C code (as functions) so I was planning on making C functions to read/write memory of a different process and using them within QB64.

I've tried to study your code but I don't understand delphi, so I was wondering if there is some easy to use C example of some of the fundamental aspects (like reading/writing a byte of memory). It could also be useful to the developer of QB64.

Nice program btw, I used GameHack with Windows 98 back in the days and it was great fun, now I can have the same fun again!

_________________
A new GameHack!
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Mar 25, 2011 1:46 am    Post subject: Reply with quote

Read/WriteProcessMemory()
Back to top
View user's profile Send private message
Krähne
Expert Cheater
Reputation: 0

Joined: 06 Jun 2010
Posts: 108
Location: Inside of my Kernel

PostPosted: Fri Mar 25, 2011 2:58 am    Post subject: Reply with quote

You should read the Cheat Engine 1.3 source code, can help so much, because... is fucking easy to understand... Very Happy (i mean, you doesn't has to be a delphi programmer for get it).

You also has to read something about the Win32 API and... you'll be ready. (Pinvoke.net should be enough).

_________________
Excuse me if you don't understand what I just said, but "english" isn't my native language.
Back to top
View user's profile Send private message MSN Messenger
Cyperium
How do I cheat?
Reputation: 0

Joined: 24 Mar 2011
Posts: 5

PostPosted: Thu Apr 07, 2011 5:18 pm    Post subject: Reply with quote

Thanks, yes Read/WriteProcessMemory works but is way too complicated as you can't just read/write to any process as you would think, instead you have to do a lot of work before you get the ability to change anything or find any value. In my opinion it would be better if someone constructed a simple function to do this for you instead of having to override the manic security settings all the time. After all, isn't that what functions are for so you don't have to reinvent the wheel all the time?

I came this far, and was able to read but not write, and since I couldn't write I couldn't be certain what I read was correct either:

Code:

BOOL EnablePriv(LPCSTR lpszPriv, HANDLE tprocid) // by Napalm
   {
       HANDLE hToken;
       LUID luid;
       TOKEN_PRIVILEGES tkprivs;
       ZeroMemory(&tkprivs, sizeof(tkprivs));
        
       if(!OpenProcessToken(tprocid, (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), &hToken))
           return FALSE;        
       if(!LookupPrivilegeValue(NULL, lpszPriv, &luid)){
           CloseHandle(hToken); return FALSE;
       }
        
       tkprivs.PrivilegeCount = 1;
       tkprivs.Privileges[0].Luid = luid;
       tkprivs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        
       BOOL bRet = AdjustTokenPrivileges(hToken, FALSE, &tkprivs, sizeof(tkprivs), NULL, NULL);
       CloseHandle(hToken);
       return bRet;
   }
   // Called as: EnablePriv(SE_DEBUG_NAME);


int getbyte(int procid, int address)
   {
   int c;
   HANDLE mprocess;   
   mprocess=(HANDLE)procid;
        EnablePriv(SE_DEBUG_NAME, mprocess);
        HANDLE hProcess;
   unsigned char ucMem;
   DWORD dwMemAddr = (DWORD)address;
   SIZE_T stBytes = 0;
   hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE, (DWORD)mprocess);
   ReadProcessMemory(hProcess,(LPCVOID)dwMemAddr, &ucMem, 1,&stBytes);
   CloseHandle(hProcess);
   c = (int)ucMem;
   return (c);
   }

int writebyte(int procid, int address, int value)
   {
   int c;
   HANDLE mprocess;   
   mprocess=(HANDLE)procid;
        EnablePriv(SE_DEBUG_NAME, mprocess);
        HANDLE hProcess;
   unsigned char ucMem;
   ucMem=(unsigned char)value;
   DWORD dwMemAddr = (DWORD)address;
   SIZE_T stBytes = 0;
   hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE, (DWORD)mprocess);
   c=(int)WriteProcessMemory(hProcess, (LPVOID)dwMemAddr, (LPCVOID)&ucMem, 1,NULL);
   CloseHandle(hProcess);
   return (c);
   }

C'mon there must be a easier way than this?

I have looked at the source code, but I don't fully understand the structure of Delphi and I'm only a beginner at C, so trying to decipher Delphi to C is too big a task for me, what would be superb is a function to prepare the Process so it can be read from and written to, and a function to actually read and write to it. That's all I need, yet so hard to do.

I hope you understand my concern, this is not something that should be difficult to do, it should be easy. If I could only get it to read and write one single byte successfully then that would be a BIG step in the right direction.

(QB64 doesn't have a certain types of variables so I convert to and fro int instead if you wonder about that)

_________________
A new GameHack!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Thu Apr 07, 2011 5:47 pm    Post subject: Reply with quote

Cyperium wrote:
Thanks, yes Read/WriteProcessMemory works but is way too complicated as you can't just read/write to any process as you would think, instead you have to do a lot of work before you get the ability to change anything or find any value. In my opinion it would be better if someone constructed a simple function to do this for you instead of having to override the manic security settings all the time. After all, isn't that what functions are for so you don't have to reinvent the wheel all the time?


If it's too complicated you should slow down and take your time. Go read the API documentation for each API you are using on MSDN. Then read the pages that are involved with using those API. You aren't going to get around using them so you need to understand them. It's not a lot of work either, it is how the system is designed.

As for doing things for you, there are plenty of wrappers on the net that do all of this already, but it is not beneficial to you to use them unless you understand what is happening under the wrappers. If something doesn't work or breaks later on you'll have no idea how to fix it and in the end you land up right back here asking the same questions.

Cyperium wrote:

I came this far, and was able to read but not write, and since I couldn't write I couldn't be certain what I read was correct either:

Code:

 -- snipped out



You are using a lot of the API wrong. Casting PIDs to Handles, returning invalid information, etc. Take the time to go read the API pages on MSDN. You are not using things correctly which is why you are getting issues.

Also EnablePriv function isn't needed if you use OpenProcess with the specific flags you need rather then passing PROCESS_ALL_ACCESS.


Cyperium wrote:
C'mon there must be a easier way than this?


Bottom line, nope. This is how Windows works.

Cyperium wrote:
I have looked at the source code, but I don't fully understand the structure of Delphi and I'm only a beginner at C, so trying to decipher Delphi to C is too big a task for me, what would be superb is a function to prepare the Process so it can be read from and written to, and a function to actually read and write to it. That's all I need, yet so hard to do.

I hope you understand my concern, this is not something that should be difficult to do, it should be easy. If I could only get it to read and write one single byte successfully then that would be a BIG step in the right direction.

(QB64 doesn't have a certain types of variables so I convert to and fro int instead if you wonder about that)


Pretty much summing it up; as I said above take your time. You are rushing trying to get what you want rather then learning in the process. Research what you are doing, research the functions/API you are using, and so on.

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

Joined: 24 Mar 2011
Posts: 5

PostPosted: Mon Apr 11, 2011 6:16 pm    Post subject: Reply with quote

ReadProcessMemory isn't too complicated.

OpenProcess isn't too complicated.

I can do all that, I just open it with PROCESS_ALL_ACCESS and the handle to it is retrieved.

BUT the documentation must be wrong, cause I STILL can't read the correct addresses or change them, so what do I do? Well, what I did was find a page that said how the priviliges had to be changed for the process. So I change the priviliges and hope it will work then (and mind you; I've tried many types of variables and HANDLE was the one it could take) but it DIDN'T work.

I've been trying to read/write to process memory for years and I can't seem to understand it, so instead of directing me to everywhere else like some **** telephone company give me some straight answers...

At least direct me to ANY site that shows SIMPLISTICALLY how it should be done, OR tell me what I can do to change my code so that it works! Do I really need a 1000 lines program to read a byte??

_________________
A new GameHack!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Mon Apr 11, 2011 6:36 pm    Post subject: Reply with quote

Cyperium wrote:
ReadProcessMemory isn't too complicated.

OpenProcess isn't too complicated.

I can do all that, I just open it with PROCESS_ALL_ACCESS and the handle to it is retrieved.


You shouldn't be using PROCESS_ALL_ACCESS. Use the needed flags specific to what you are doing. PROCESS_ALL_ACCESS is not compatible cross-versions of Windows due to changes in the flag structure. You will more then likely run into issues with your application(s) between XP and Vista/Win7.

Cyperium wrote:
BUT the documentation must be wrong, cause I STILL can't read the correct addresses or change them, so what do I do? Well, what I did was find a page that said how the priviliges had to be changed for the process. So I change the priviliges and hope it will work then (and mind you; I've tried many types of variables and HANDLE was the one it could take) but it DIDN'T work.


The documentation is correct. Do you honestly thing Windows would be this old with API documentations that are incorrect? Let alone millions of people using it daily and having no issue with it. Not to be harsh but you are obviously either using it wrong or understanding it incorrectly.

Cyperium wrote:
I've been trying to read/write to process memory for years and I can't seem to understand it, so instead of directing me to everywhere else like some **** telephone company give me some straight answers...

At least direct me to ANY site that shows SIMPLISTICALLY how it should be done, OR tell me what I can do to change my code so that it works! Do I really need a 1000 lines program to read a byte??


Getting an attitude isn't going to help you any here. You need to learn the API before you assume you can do anything with it. Perhaps start with something simple and then get into more complex things.

Another issue is that since you are using QB64 from what you said above, if you are using x64 processes your code is not x64 compatible and will run into issues.

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

Joined: 24 Mar 2011
Posts: 5

PostPosted: Tue Apr 12, 2011 5:04 pm    Post subject: Reply with quote

Sorry for the attitude, I'm just frustrated that it doesn't work as suspected.

Seriously though, since as you say PROCESS_ALL_ACCESS doesn't work as intended then the documentation is indeed wrong.

Also; the official documentation on OpenProcess states:

"To open a handle to another local process and obtain full access rights, you must enable the SeDebugPrivilege privilege. For more information, see Changing Privileges in a Token."

So it basically tells me to set privileges and you tell me not to. Still you tell me to trust the documentation? (not meant as a attitude but simply a fair question).

Perhaps you mean that I don't need full access rights and only need to use PROCESS_VM_READ and PROCESS_VM_WRITE?

I will try that and get back to you. One last thing, QB64 isn't meant for x64 only, it can run on both 32-bit and 64-bit systems (and on Linux and MacOSX but my function isn't meant to be compatible with those systems).

I can get by pretty good by trial&error if only the function I'm creating is simple enough (which this should be, it's not like I'm creating a rocket, I just want to read and write a byte from another process memory, at least at this stage of the project, I just need a stepping stone).

I've also used API to great extent in VB 5.0 (yeah it was a long time ago) and this was when I first tried to read/write to another process memory. I created a program called WinlOOk which could manipulate the windows in many ways, including editing the text on the title bar, enable/disable windows, hide/show windows, I even made a function that would "kidnap" a window by changing it's parent ;D, it was a fun program to make and I still have use for it today! So I'm not really a beginner in API, I'm more of a beginner in C and I haven't got Read/WriteProcessMemory to work correctly for me yet so you could say I'm a beginner at that particular area as well.

_________________
A new GameHack!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Tue Apr 12, 2011 5:23 pm    Post subject: Reply with quote

I'm saying not to use PROCESS_ALL_ACCESS because the flags changed across OS versions. The flag changed between XP and Vista/Win7:
Code:
#if (NTDDI_VERSION >= NTDDI_VISTA)
#define PROCESS_ALL_ACCESS        (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | \
                                   0xFFFF)
#else
#define PROCESS_ALL_ACCESS        (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | \
                                   0xFFF)
#endif


This doesn't make the documentation wrong, what they say in the docs is correct.
What I'm telling you to do is not use this flag at all and use specifically the ones you need. For example:

Code:
HANDLE hHandle = OpenProcess( PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, dwProcId );


Which does not require the token to be adjusted since you aren't asking for all privileges.

The next step you need to do is start checking error returns and obtaining the error code from the system after the API fails. For example:

Code:
// Obtain process handle..
HANDLE hHandle = OpenProcess( PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, dwProcId );
if( hHandle == NULL )
{
   // OpenProcess failed.. read the error from the system..
   DWORD dwLastError = GetLastError();
   
   // Handle error here..
}

// Attempt to read a DWORD from memory..
DWORD dwValue = 0;
if( !ReadProcessMemory( hHandle, 0x12345678, &dwValue, sizeof( dwValue ), NULL ) )
{
   // ReadProcessMemory failed.. read the error from the system..
   DWORD dwLastError = GetLastError();

   // Handle error here..

   // Be sure to cleanup the handle and other objects..
   CloseHandle( hHandle );
   return ;
}


One other thing to keep in mind, the API is 'dumb'. It has no knowledge of what sits between the call you make and the result it will give you. Meaning if the target process has any security features implemented, the systems API has no idea. So your targets could also be blocking calls to things like OpenProcess, ReadProcessMemory / WriteProcessMemory and so on.

Try starting on something basic like Minesweeper. Get the idea and understanding down on altering memory on something that is completely unprotected and then move onto other things.

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

Joined: 24 Mar 2011
Posts: 5

PostPosted: Tue Apr 12, 2011 5:59 pm    Post subject: Reply with quote

Ok, thanks. I usually make my own program so I know what to look for, so there shouldn't be any access problems.

I think that beginners might have a hard time to understand this, since there are so many pitfalls. If I succeed in making this program I will try to document it so that others in my situation will know what to do, cause usually when I search for sources they are way to complicated to be easily dissected into just the basic parts that build everything. It's those parts that I'm after, the foundation (I guess I'm being a bit too philosophical about this, but that's the way I perceive it).

Anyway, thanks again!

_________________
A new GameHack!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Wed Apr 13, 2011 2:08 pm    Post subject: Reply with quote

Cyperium wrote:
Ok, thanks. I usually make my own program so I know what to look for, so there shouldn't be any access problems.

I think that beginners might have a hard time to understand this, since there are so many pitfalls. If I succeed in making this program I will try to document it so that others in my situation will know what to do, cause usually when I search for sources they are way to complicated to be easily dissected into just the basic parts that build everything. It's those parts that I'm after, the foundation (I guess I'm being a bit too philosophical about this, but that's the way I perceive it).

Anyway, thanks again!


Nothing wrong with that way of thinking, wanting to understand the base is better then just copy pasting and expecting it to work. At least you have that desire, a lot of people that get into hacking just want the end result without having to learn so they typically never get past copy pasting something and then giving up cause they don't know what to do.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Apr 13, 2011 3:41 pm    Post subject: Reply with quote

Btw the docs actually state not to use PROCESS_ALL_ACCESS:
http://msdn.microsoft.com/en-us/library/ms684880(v=vs.85).aspx
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