WriteProcessMemory

Posts 1–13 of 13 · Page 1 of 1
WriteProcessMemory
I know there are more threads about this but i just dont get what im doing wrong.

Reading works easy.

Code:
DWORD MemoryManagement::readMemory(DWORD adress[]) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(DWORD), 0);
	for (int i = 1; i < MemoryManagement::toolmanager.getArrayLenght(adress); i++) {
		ReadProcessMemory(handle, (void*)(value + adress[i]), &value, sizeof(DWORD), 0);
	}
	return value;
}
Writing doesnt work but why?

Code:
void MemoryManagement::writeMemory(DWORD adress[], int enter) {
	DWORD value = MemoryManagement::readMemory(adress);
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	WriteProcessMemory(handle, (void*)value, &enter, sizeof(enter), NULL);
}
If i directly write to the adress with forexample, it works

Code:
void MemoryManagement::writeMemory(DWORD adress, int enter) {
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	WriteProcessMemory(handle, (void*)adress, &enter, sizeof(enter), NULL);
}
What are you trying to achieve when you are reading and then writing?

- - - Updated - - -

You are reading the address, which means you take whatever there is at that address, for example your current health, which is a 100, and put it into value as DWORD.

Then after, you are trying to write to value, which is not the address you want to write to, it is a 100 at that point. So you are trying to write to address 100.

Keep in mind that you can only write int's with this method.

Use C++ templates to make your life easier instead of making 10 functions for different datatypes.

If you want to write a bool, you have to make a new function or you will overwrite the next 3 bytes with nothing.

If you use templates, you can give the function the data type.

Code:
template <class MyDataType>
int WriteMemory(DWORD Address, MyDataType Value)
{
	if (WriteProcessMemory(hProcess, (LPVOID)Address, &Value, sizeof(MyDataType), NULL)) { return 0; } else return GetLastError();
}
Now you can write every value you want, even your own data structures.

WriteMemory<int>(Address, Value);

will write the size of an integer, if you use

WriteMemory<bool>(Address, Value);

it would only write the size of a bool.
I want to get the adress of LIFE. I saw in another thread that you should write to the DWORD you read but i know that its wrong thats why i ask here.
So how can i get the adress so i can write to it ? As i said i know how to write to one adress only.
But im struggling with multilevel Pointers.

For example
Code:
DWORD playerlife[] = { 0x109B74 , 0xF8 };
I also saw this function, but isnt it doing the same as me ?

Code:
DWORD FindDmaAddy(int PointerLevel, HANDLE hProcHandle, DWORD Offsets[], DWORD BaseAddress)
{
        DWORD pointer = BaseAddress;
        DWORD pTemp;
        DWORD pointerAddr;
     
        for (int c = 0; c < PointerLevel; c++)
        {
                if (c == 0)
                {
                        ReadProcessMemory(hProcHandle, (LPCVOID)pointer, &pTemp, sizeof(pTemp), NULL);
                }
                pointerAddr = pTemp + Offsets[c];
                ReadProcessMemory(hProcHandle, (LPCVOID)pointerAddr, &pTemp, sizeof(pTemp), NULL);
        }
        return pointerAddr;
}
Thanks for the template function. ( Java is so much easier *cry*)
Okay, you want to write to the Health of a your Player?

I don't know if you are still trying assault cube, but if so, i don't know the game but it might be the same.

From the looks of it, it looks like that ac_client.exe + 0x109B74 is a pointer to your address.

You want to read that pointer as a DWORD, so you get the actual address of your Player.

That's right so far, but after that, you are trying to write to that address, so you are trying to change the address of your LocalPlayer, which is not what you want.

Just grab that Pointer first, then read your health by reading PlayerBase + Offset.

Code:
DWORD BaseAddressOfModule;
DWORD PlayerBase;
int Health;

ReadProcessMemory ( MyHandle, (LPVOID)(BaseAddressOfModule + 0x109B74), &PlayerBase, sizeof (PlayerBase), NULL);

ReadProcessMemory ( MyHandle, (LPVOID)(PlayerBase+ 0xF8), &Health, sizeof (Health), NULL);
Since it's a pointer to your Player, you want to get the actual address of your Player, which you get with your first read.

After that, you got the real address of your Player, and you can add the Offset onto it to get the actual Health.

I also saw this function, but isnt it doing the same as me ?

Code:
DWORD FindDmaAddy(int PointerLevel, HANDLE hProcHandle, DWORD Offsets[], DWORD BaseAddress)
{
        DWORD pointer = BaseAddress;
        DWORD pTemp;
        DWORD pointerAddr;
     
        for (int c = 0; c < PointerLevel; c++)
        {
                if (c == 0)
                {
                        ReadProcessMemory(hProcHandle, (LPCVOID)pointer, &pTemp, sizeof(pTemp), NULL);
                }
                pointerAddr = pTemp + Offsets[c];
                ReadProcessMemory(hProcHandle, (LPCVOID)pointerAddr, &pTemp, sizeof(pTemp), NULL);
        }
        return pointerAddr;
}
This function reads the actual address of the Player first and then reads the health by reading PlayerBase + Offset, just like i said above.

You were trying to write to the actual player address first.
ERR:

Code:
Severity	Code	Description	Project	File	Line
Error	LNK2019	unresolved external symbol "public: void __thiscall MemoryManagement::writePlayerMemory<int>(unsigned long * const,int)"

Main
Code:
int main() {
	DWORD playerlife[] = { 0x109B74 , 0xF8 };
	MemoryManagement manager("ac_client.exe");
	manager.initialize();
	manager.writePlayerMemory<int>(playerlife, 300);
	cin.get();
	return 0;
}

CPP
Code:
public:
	template <class Object>
	void writePlayerMemory(DWORD adress[], Object enter);
H
Code:
template <class Object>
void MemoryManagement::writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
How do i create a generic class like object ?


:b
Code:
template <class Object>

void MemoryManagement::writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
Quote Originally Posted by Magical View Post
How do i create a generic class like object ?


:b
Code:
template <class Object>

void MemoryManagement::writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
You don't have to create a class for it, you can just use it like that.

Code:
ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
Exchange the sizeof ( Object ) with sizeof ( value ) as you always want it to read the size of the DWORD because it's an address.

Code:
WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
Are you passing in 'enter' as reference?

If you pass in variables as a parameter, they get copied, if you pass them in as a reference, they don't get copied because you give the function the actual address of the variable.

If the function has the actual address of the variable outside of the function, it can change the value of the variable you passed in as a refernce through the address of that variable.

Either pass it in as a reference, or create a buffer and return the buffer:

Code:
template <class Object>
Object MemoryManagement::writePlayerMemory(DWORD adress[]) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(value), 0);
        Object Buffer;
	WriteProcessMemory(handle, (void*)(value + adress[1]), &Buffer, sizeof(Buffer), NULL);
        return Buffer;
}
The C++ template basically replaces every occurance of Object with the datatype you are giving it.

Code:
int Health = MemoryManagement::writePlayerMemory<int>({ 0x109B74, 0xF8 });
if you return the value.

So everywhere where Object is mentioned in the code will be int for this call.

If you call the function with <float>, everything will change to float.

If you don't want to return, you can use this:

Code:
template <class Object>
void MemoryManagement::writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(value), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &Buffer, sizeof(Buffer), NULL);
}
Code:
int Health;
MemoryManagement::writePlayerMemory<int>({ 0x109B74, 0xF8 }, &Health);
Notice that i pass in Health as a reference using the &. It will give the function the address of my variable, so it can change it directly.
Is there any particular reason why you declare in your .cpp file and define it in your .h?

Templates are fked up when you define them in your cpp file, i don't know about declaring them in your .cpp file, i just define them in .h and leave .cpp clear.

I also made a mistake above where i explained some stuff. I misunderstood you first.

I thought you want to read again and save it into Health. I misread the WriteProcessMemory.
Quote Originally Posted by WasserEsser View Post
Templates are fked up when you define them in your cpp file, i don't know about declaring them in your .cpp file, i just define them in .h and leave .cpp clear.
Thats because templates can only be defined in header files! :P


@Magical your problem is likely that you are trying to define a template function in a .cpp, this is not possible with c++, the definition must be located inside the header file!
H

Code:
	template <class Object>
	void writePlayerMemory(DWORD adress[], Object enter);
How is the syntax then for the templated function in the cpp ?
Leaving it "clear" doesnt work because he doesnt know what Object is.
It says requires template <> syntax. But everything i tried turned into an syntax or system error :S

Code:
void MemoryManagement::writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
Quote Originally Posted by Magical View Post
H

Code:
	template <class Object>
	void writePlayerMemory(DWORD adress[], Object enter);
How is the syntax then for the templated function in the cpp ?
Leaving it "clear" doesnt work because he doesnt know what Object is.
It says requires template <> syntax. But everything i tried turned into an syntax or system error :S

Code:
void MemoryManagement::writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
You just define it in your .h file, and leave the .cpp file empty.

Declaring is when you tell the compiler that you have a variable/function but you don't give it a value/expression.

Code:
int Health;
Code:
void MyFunction();
These declared functions are often called Function Prototypes if not in a class.

You define something when you give them a value / expression:

Code:
Health = 100;
Code:
void MyFunction()
{
	Health = 100;
}
You can also declare and define them in one step:

Code:
int Health = 100;
Declaring and defining functions in one step doesn't have a special syntax, so you can do it like written above.

You want to define them right away in your memory class and don't do anything for that function in your .cpp file.

Code:
template <class Object>
void writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(Object), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
Also, you forgot changing the sizeof(Object) in ReadProcessMemory to sizeof(value). If you use the function with <BYTE> for example, you won't read the initial pointer correctly, because you read the size of a BYTE, which is one, and the DWORD is 4 BYTES in size, so you won't read all of the pointers data and you are pointing in a wrong direction.

Use this code in your .h file and don't do anything in your .cpp file:

Code:
template <class Object>
void writePlayerMemory(DWORD adress[], Object enter) {
	DWORD value = 0x0;
	HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, MemoryManagement::manager.mainprocess_->th32ProcessID);
	ReadProcessMemory(handle, (void*)(MemoryManagement::manager.processAddress_ + adress[0]), &value, sizeof(value), 0);
	WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
}
You don't need to specify the scope for this function declaration/definition as you already are in the scope of the class, so you can leave the MemoryManagement:: off.
Ah now i know what you mean by leaving it "clear" in the cpp.
I didnt understand that because I read that you should always have the full function in the cpp and the declaration in the H.
C++ is weird
It didnt work unless you said I "unscoped".

Code:
void writePlayerMemory(DWORD adress[], Object enter) {
		DWORD value = 0x0;
		HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, manager.mainprocess_->th32ProcessID);
		ReadProcessMemory(handle, (void*)(manager.processAddress_ + adress[0]), &value, sizeof(value), 0);
		WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
	}
Quote Originally Posted by Magical View Post
Ah now i know what you mean by leaving it "clear" in the cpp.
I didnt understand that because I read that you should always have the full function in the cpp and the declaration in the H.
C++ is weird
It didnt work unless you said I "unscoped".

Code:
void writePlayerMemory(DWORD adress[], Object enter) {
		DWORD value = 0x0;
		HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, 0, manager.mainprocess_->th32ProcessID);
		ReadProcessMemory(handle, (void*)(manager.processAddress_ + adress[0]), &value, sizeof(value), 0);
		WriteProcessMemory(handle, (void*)(value + adress[1]), &enter, sizeof(Object), NULL);
	}
Yeah, clear was the wrong word to choose at this point.
Posts 1–13 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us