Readprocessmemory in C++ won't read - c++

I have been coding for a few years now, but in 2016 I took a couple years off. Now I decided to come back, so I wanted to play around with some reverse engineering.
I played around with a game called Black Ops 1: Zombies. I found the memory addresses, then I went into C++ to code a simple cheat for unlimited ammo (This is offline), but it didn't work, so tried using ReadProcessMemory instead to see if it gives the correct ammo. The console started printing out "0". I thought there might been something with the code so I looked up some of my older source code, and the code was the same, tried installing VS 2015 instead in case the compiler is the problem, still nothing, tried GNCC compiler, still nothing. Tried using it on my second computer and it worked. I tried disabling Anti-Virus and windows defender but still nothing.
Here is the source code if it means anything:
#include <iostream>
#include <Windows.h>
using namespace std;
DWORD Address = 0x180A6C8;
DWORD pID;
int points;
int main()
{
while (true)
{
HWND hWnd = FindWindowA(0, ("Call of Duty®: BlackOps"));
GetWindowThreadProcessId(hWnd, &pID);
HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pID);
ReadProcessMemory(pHandle, (LPVOID)Address, &points, sizeof(points), 0);
cout << points << endl;
Sleep(100);
}
}

A.) Can you read the Address in Cheat Engine etc
B.) Does it require a base address (e.g "client.dll")? If so get the module base address of the required module.

Related

How can i get pointer's address from other application without Cheat Engine?

I want to change or read the pointer's value which is in other program.But i need to know pointer's address.Can i get the address without Cheat Engine and how can i do that ? In the youtube/google/facebook they are using Cheat Engine to know the address.
#include <iostream>
#include <windows.h>
int main() {
DWORD pointer = 0x006DFEF8; // I learned this address from Cheat Engine.
DWORD pid;
int deger;
char program_isim[100];
std::cin >> program_isim;
HWND program = FindWindow(0, program_isim);
if (program == 0) {
std::cout << program_isim << ",bulunamadi." << std::endl;
}
else {
GetWindowThreadProcessId(program, &pid);
HANDLE hand = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
while (1) {
ReadProcessMemory(hand, (void*)pointer, &deger, sizeof(deger), 0);
std::cout << deger << std::endl;
}
}
return 0;
}
Cheat Engine is a disassembler and debugger. You can find pointers using both of these features, but everything is easiest when using a debugger at runtime. It allows you to set "break on read" and "break on write" breakpoints. The "find what accesses" function of Cheat Engine is just using read breakpoints and logging the addresses where the breakpoint is hit.
Cheat Engine will always be the best way to find pointers. Can you find a pointer via static analysis? Yes you can but it will be more difficult.
You can replicate Cheat Engine's behavior by writing your own program which registers itself as a debugger with the Windows API and set breakpoints on the target process.
But the reason you can't use Cheat Engine is because the game has anticheat that detects your debugger, it will also detect your debugger you write so this is not the solution.
The solution is to bypass the anticheat and then just use Cheat Engine as normal.

C++ Unable to Read Memory Address

I'm creating a simple HACK for educational purpose only. A simple Triggerbot that reads from memory the value of player aiming to enemy YES = 1 or NO = 0. I have made some other similar HACKS however I never found this problem .. in Rainbow Six Siege I have the memory address both static or dynamic however cheat engine read it well but when I try to read it from my C++ Program it does't work. Not sure why if it had work with other games. I'm new to this and maybe I did something wrong.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#define F6Key 0x75
using namespace std ;
int value ;
int main()
{
cout << "Open Game .." << endl ;
system("Pause") ;
LPCWSTR gameone = L"Rainbow Six";
HWND hwnd = FindWindow(0, gameone);
if (gameone == 0)
{
cout << "Not Found." << endl ;
system("Pause") ;
}
else
{
cout << "Success finding game." << endl;
DWORD processid ;
GetWindowThreadProcessId(hwnd, &processid) ;
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processid) ;
cout << processid ;
if (!ReadProcessMemory(process, (void *)0x25421AD9D6C, (void *)&value, sizeof(value), NULL)) {
cout << "Unable to read memory proccess";
}
else {
cout << "\n Read: " << value;
}
system("Pause");
}
return 0 ;
}
Here is the code simple Find the Window by name, gets its PID fine no problem. OpenProcess then when I call the method ReadProcessMemory with the process, address pointer value by parameter is unable to read it print the if condition and never the else of value read.
If I remove the function from the If condition just for testing if at least points to something it gives some random values... is weird that I'm unable to read memory It always work ::(
Can someone help me out? It is some king of security software or something?
First of all, you have to check OpenProcess return value. If your process does not have sufficient rights, it will return NULL and any attempts to use this handle will fail. Use GetLastError function to determine error reason.
Most likely it will be ERROR_ACCESS_DENIED.
Secondary, to successfully access external process memory space, you should open its handle with PROCESS_VM_READ right or enable seDebugPrivilege for you process token. Example how to do that you could see in the MSDN.
And lastly. If memory address (0x25421AD9D6C in your case) is invalid, ReadProcessMemory will fail. In that case value variable would not be initialized and any attempts to use it is an undefined behavior.
Also, if you managed to get process handle, it should be closed using CloseHandle function when you finish using it.
Upd: If ReadProcessMemory returns FALSE and GetLastError - ERROR_PARTIAL_COPY that means that a page fault has occured, you are trying to read from a buffer and at least part of it is not assigned to the physical memory. If you know your value offset, get module load address using PSAPI GetModuleInformation function and add offset to the lpBaseOfDll field of the MODULEINFO structure.

Linker issue with EnumPrinters Sample

First of all, I understand there are many topics on this function, but I did not find any about this particular problem, sorry if I am repeating...
I have been working on a program in C++ that works with printers and I need to get the list of printers in the system.
I am using the EnumPrinters API and I am getting a compile error I don't understand.
This is my code:
#include <iostream>
#include <windows.h>
#include <winspool.h>
using namespace std;
int main()
{
PRINTER_INFO_5 pi;
PBYTE buffer[99];
DWORD bufferSize = 0;
DWORD bufferNeeded = 0;
DWORD Entries = 0;
bool r;
r = EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 5, NULL, bufferSize, &bufferNeeded, &Entries);
if (!r)
{ cout << "No printer found" << endl; }
else { cout << "Found printers" << endl; }
}
When I try to compile (codeBlocks typical installation w/ gcc), I get this error:
C:\Programação\C++\lab\main.cpp 18 undefined reference to 'EnumPrintersA#28'
I think this may be a linker problem, but I don't know how to solve it...
Thank you!
SOLVED!
After some help I found out that the problem was that I wasn't importing the correct library. I thought including the header would be enough.
I needed to follow these steps (using 'winspool' instead of 'gdi32').
By the way, adding 'winspool.lib' did not solve it. Use 'winspool' instead (no '.lib')
Your linker is missing a .lib-file. If you lookup EnumPrinters at the MSDN documentation, you will see which library you have to add (somewhere at the bottom of the page, right before the comments).
In this case it's Winspool.lib. For gcc, add the commandline option: -lwinspool.

SystemParametersInfo returns 0

#include <iostream>
#include <windows.h>
using namespace std;
int main(){
LPWSTR test = L"C:/aizen.png";
int result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, test, SPIF_UPDATEINIFILE);
if(result)
cout << "Wallpaper set!";
else
cout << "NOOOOO!" << result;
cin >> result;
return 0;
}
Very simple code, but result returns 0. What am I doing wrong?
Do what the documentation says, and call GetLastError to find out the reason behind the error.
Some possible causes spring to mind:
SystemParametersInfo does not like forward slashes as separators.
The system doesn't like .png files for wallpaper.
The file doesn't exist.
You have an ANSI/Unicode encoding mismatch.
You don't have rights to modify the wallpaper.
Really, the list is endless. So let the system tell you.
Note that the documentation says:
When the SPI_SETDESKWALLPAPER flag is used, SystemParametersInfo always returns TRUE.
But this is a great big fat lie. It's trivially easy to call the function, pass SPI_SETDESKWALLPAPER, and receive FALSE in return.
Don't know with the information we have.
This return value indicates that the call failed. You'll need to call GetLastError() for information about why.

Change Windows 7 Wallpaper in C++

I saw someone at school who had a program that was supposed to change the background, and log you out (so the change would take effect) but it only worked on his computer, and he "lost" the source code.
I have been looking online for a while for code that would do that, so I could experiment with it.
I found this code (C++)
#include <windows.h>
int main()
{
SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, (PVOID)"image.jpg", SPIF_UPDATEINIFILE );
}
which sort of worked..
It worked twice with an image that was located in the same folder, and I tested two images, and after the second one it just stopped working.
I can't for the life of me figure out why it stopped working (I have tried multiple file extensions, so I know that isn't the issue).
I am working in Dev-C++, if that makes a difference to anyone.
Also, I would like to be able to modify the code to use a url instead of a local image...would that be possible?
Thanks!
Edit: If I change image.jpg to image.bmp it changes every time..But that means that it would only work with *.bmp? I had it working with a jpeg before.
#include <windows.h>
int main()
{
int i;
for(i=0;;i++)
{
Sleep(800);
if(i%2==0)
{
const wchar_t *filenm = L"C:\\Pictures\\image1.jpg"; //ADDRESS of first image
bool isWallSet=SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,(void*)filenm,SPIF_UPDATEINIFILE);
}
else
{
const wchar_t *filenm = L"C:\\Pictures\\image2.jpg"; //ADDRESS of second image
bool isWallSet=SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,(void*)filenm,SPIF_UPDATEINIFILE);
}
}
return 0;
}