Jump to content
Returning Members: Password Reset Required ×

[Help] Injecting into Memory


Recommended Posts

Posted

Hello guys! :D

I've been trying to mess with injection and I'm building a general DLL that will injects anything I want into an existing process. The objective is currently to achieve a custom screen size for the Conquer Online Client.

I currently have this piece of code:

DWORD idProcess = GetCurrentProcessId();
       HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, idProcess);
       if (hProcess) 
       {
           const LPVOID BASE_W_ADDR = (LPVOID) (0x00469DEF + 6);
           const LPVOID BASE_H_ADDR = (LPVOID) (0x00469DFC + 6);

           int width = GetPrivateProfileIntA("GameResolution", "Width", 1024, CONFIG_FILE);
           int height = GetPrivateProfileIntA("GameResolution", "Height", 768, CONFIG_FILE);

           int read = 0;
           SIZE_T bytes_read = 0, bytes_written = 0;
           DWORD error = 0;
           if (ReadProcessMemory(hProcess, BASE_W_ADDR, &read, 4, &bytes_read) || GetLastError() == ERROR_PARTIAL_COPY) {
               if (bytes_read == 0)
                   MessageBoxA(NULL, "Could not get width memory offset.", "ReadProcessMemory error", MB_OK);
               
               if (!WriteProcessMemory(hProcess, BASE_W_ADDR, (LPCVOID)width, 4, &bytes_written))
                   MessageBoxA(NULL, "Could not write to width memory offset.", "WriteProcessMemory error", MB_OK);

               error = GetLastError();
               if (error != 0)
               {
                   sprintf_s(msg, "Error writing to memory! %d" , error);
                   MessageBoxA(NULL, msg, "WriteProcessMemory error", MB_OK);
               }
               else 
               {
                   sprintf_s(msg, "Width edited with success! :D Old X: %d", read);
                   MessageBoxA(NULL, msg, "Success?", MB_OK);
               }
               error = 0;
           }

           if (ReadProcessMemory(hProcess, BASE_H_ADDR, &read, 4, &bytes_read) || GetLastError() == ERROR_PARTIAL_COPY) {
               if (bytes_read == 0)
                   MessageBoxA(NULL, "Could not get height memory offset.", "ReadProcessMemory error", MB_OK);
               if (!WriteProcessMemory(hProcess, BASE_H_ADDR, (LPCVOID)height, 4, &bytes_written))
                   MessageBoxA(NULL, "Could not write to height memory offset.", "WriteProcessMemory error", MB_OK);
               
               error = GetLastError();
               if (error != 0)
               {
                   sprintf_s(msg, "Error writing to memory! %d", error);
                   MessageBoxA(NULL, msg, "WriteProcessMemory error", MB_OK);
               }
               else 
               {
                   sprintf_s(msg, "Height edited with success! :D Old X: %d", read);
                   MessageBoxA(NULL, msg, "Success?", MB_OK);
               }                
           }
       }

It will give me an error related to ERROR_FILE_NOT_FOUND on width change and will give no error on height change, but neither work. I have tried a lot of solutions from Stack Overflow and etc but idk what's wrong there. The process can currently ReadProcessMemory with success, it will return the current screen resolution at that memory pointer but I can't set the one in my ini file.

Any lights on this?

Posted

You shouldn't be using GetLastError unless you know there is an error (i.e ReadProcessMemory returns false).

WriteProcessMemory and ReadProcessMemory expect a buffer/pointer to be passed in, and casting the variable to LPCVOID doesn't turn it into a pointer, use the & sign instead (i.e &width).

when you are using OpenProcess on the same process you are injected to you should use the handle from GetCurrentProcess and cast it to DWORD and use GetCurrentProcess when asked for a handle to read or write memory.

Posted

Is the injected library the same architecture as Conquer? I think Conquer only runs in 32-bit.

Yes, I'm building it as x86 and the injector too.

You shouldn't be using GetLastError unless you know there is an error (i.e ReadProcessMemory returns false).

WriteProcessMemory and ReadProcessMemory expect a buffer/pointer to be passed in, and casting the variable to LPCVOID doesn't turn it into a pointer, use the & sign instead (i.e &width).

when you are using OpenProcess on the same process you are injected to you should use the handle from GetCurrentProcess and cast it to DWORD and use GetCurrentProcess when asked for a handle to read or write memory.

I changed the code and now it works! :D

int read = 0;
           SIZE_T bytes_read = 0, bytes_written = 0;
           if (ReadProcessMemory(hProcess, BASE_W_ADDR, &read, 4, &bytes_read) || GetLastError() == ERROR_PARTIAL_COPY) {
               if (bytes_read == 0)
                   MessageBoxA(NULL, "Could not get width memory offset.", "ReadProcessMemory error", MB_OK);
               
               if (!WriteProcessMemory(hProcess, BASE_W_ADDR, &width, 4, &bytes_written))
               {
                   sprintf_s(msg, "Error writing to memory! %d", GetLastError());
                   MessageBoxA(NULL, msg, "WriteProcessMemory error", MB_OK);
               }

               /*sprintf_s(msg, "Width edited with success! :D NewX: %d Old X: %d", width, read);
               MessageBoxA(NULL, msg, "Success?", MB_OK);*/
           }

This made the job :] Thank you

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...