c++ readIntger writeIntger
类似CE的read/writeIntger函数(外部)
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <vector>
#include <regex>
#include <sstream>
#include <string>
// global
DWORD pid = 0;
HANDLE hProcess = 0;
// 获取进程名的pid
DWORD getPID(const wchar_t* name)
{
DWORD pid = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
pe.dwSize = sizeof(pe);
if (Process32First(hSnap, &pe))
{
do {
if (!_wcsicmp(pe.szExeFile, name)) {
pid = pe.th32ProcessID;
break;
}
} while (Process32Next(hSnap, &pe));
}
}
CloseHandle(hSnap);
return pid;
}
// 获取模块基址
uintptr_t getModuleBaseAddress(DWORD pid, const wchar_t* modName)
{
uintptr_t modBaseAddr = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 me;
me.dwSize = sizeof(me);
if (Module32First(hSnap, &me))
{
do {
if (!_wcsicmp(me.szModule, modName)) {
modBaseAddr = (uintptr_t)me.modBaseAddr;
break;
}
} while (Module32Next(hSnap, &me));
}
}
CloseHandle(hSnap);
return modBaseAddr;
}
std::string replaceString(std::string origenString, std::string replaceString, std::string newValue)
{
int startIndex = origenString.find(replaceString);
int endIndex = replaceString.size();
return origenString.replace(startIndex - 1, endIndex + 2, newValue);
}
uintptr_t hexStr2Hex(std::string hexStr)
{
uintptr_t r;
std::stringstream(hexStr) >> std::hex >> r;
return r;
}
struct SplitListItem
{
std::string key;
std::string value;
};
std::vector<SplitListItem> splitString(std::string origenString, std::regex pattern)
{
std::smatch result;
std::string::const_iterator iterStart = origenString.begin();
std::string::const_iterator iterEnd = origenString.end();
std::vector<std::string> splitList = {};
std::vector<std::string> splitKeys = {};
std::vector<SplitListItem> resultSplitList = {};
while (regex_search(iterStart, iterEnd, result, pattern))
{
splitList.emplace_back(iterStart, result[0].first);
splitKeys.push_back(result[0].str());
iterStart = result[0].second;
}
splitList.emplace_back(iterStart, iterEnd);
for (size_t i = 0; i < splitList.size(); i++)
{
resultSplitList.push_back(SplitListItem{ i > 0 ? splitKeys[i - 1] : "", splitList[i] });
}
return resultSplitList;
}
uintptr_t getOffsetsAddress(std::string address, uintptr_t nextValue = 0)
{
std::string str = std::regex_replace(address, (std::regex)"\\s", "") ;
std::smatch result;
std::regex pattern(".*\\[([^\\[\\]]+)\\].*");
std::regex_match(str, result, pattern);
if (result.size() == 0)
{
if (str.size() == 0) {
return nextValue;
}
std::vector<SplitListItem> r = splitString(str, (std::regex)"[+-]");
uintptr_t a = hexStr2Hex(r[0].value);
if (a == 0 && r[0].value != "0")
{
// 符号
a = getModuleBaseAddress(
pid,
std::wstring(r[0].value.begin(), r[0].value.end()).c_str()
);
}
uintptr_t b = hexStr2Hex(r[1].value);
if (r[1].key == "+") a += b;
if (r[1].key == "-") a -= b;
return a;
}
std::vector<SplitListItem> r = splitString(result[1], (std::regex)"[+-]");
uintptr_t data = 0;
for (size_t i = 0; i < r.size(); i++)
{
uintptr_t v = hexStr2Hex(r[i].value);
if (v == 0 && r[i].value != "0")
{
// 符号
data += getModuleBaseAddress(
pid,
std::wstring(r[i].value.begin(), r[i].value.end()).c_str()
);
}
else
{
if (r[i].key == "+") data += v;
if (r[i].key == "-") data -= v;
ReadProcessMemory(hProcess, (LPCVOID)data, &data, 4, 0);
}
}
std::stringstream hexData;
hexData << std::hex << data;
std::string newOrigenString = replaceString(str, result[1], hexData.str());
return getOffsetsAddress(newOrigenString, data);
}
uintptr_t readIntger(std::string address)
{
uintptr_t r = getOffsetsAddress(address);
if (r == 0) return 0;
ReadProcessMemory(hProcess, (LPCVOID)r, &r, 4, 0);
return r;
}
uintptr_t writeIntger(std::string address, uintptr_t newInt)
{
uintptr_t r = getOffsetsAddress(address);
if (r == 0) return 0;
WriteProcessMemory(hProcess, (LPVOID)r, (LPCVOID)&newInt, 4, 0);
return r;
}
int main()
{
// 地址: [game.exe+009E820C]+338
std::string mainname = "game.exe";
pid = getPID(std::wstring(mainname.begin(), mainname.end()).c_str());
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (hProcess == NULL) return 0;
std::cout << readIntger("game.exe+009E820C") << std::endl;
std::cout << readIntger("[game.exe + 009E820C] + 338") << std::endl;
writeIntger("[game.exe+ 009E820C] + 338", 20);
CloseHandle(hProcess);
return 0;
}
c++ readIntger writeIntger的更多相关文章
随机推荐
- 学习Python之路
陆续学习python已经有一段时间了,但是真正的安下心来学习还是在最近的一个月时间里,虽然每天学习的时间很有限,但是通过点滴的学习让自己感到从未有过的充实,完全打掉了以往认学学习一门语言难于登天的心理 ...
- loj10001种树
好久不写博客了,发现不好找做过和题!还得接着写啊! ------------------------------------------------------------------ 题目描述 某条 ...
- poj 2752Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d &am ...
- Docker容器内Mysql大小写敏感方案解决
Docker容器内Mysql大小写敏感方案解决 一.(lower_case_table_names)参数说明 二.Docker 部署 MySql 并修改为大小写不敏感 2.1直接在Docker启动的时 ...
- DEDECMS:修改DEDECMS会员中心发送邮件时,邮件内容里出现在DEDE链接
1.在member/index_do.php里,把文件里的 $mailbody .= "Power by http://www.dedecms.com 织梦内容管理系统!\r\n" ...
- OsgEarth开发笔记(三):Osg3.6.3+OsgEarth3.1+vs2019x64开发环境搭建(下)
前言 上一篇编译了proj6.2.0.gdal3.2.1,本篇继续. OsgEarth编译过程简介 OsgEarth的编译,是基于Osg和OsgEarth结合在一起的,先要编译Osg,然后 ...
- Codeforces 102394I Interesting Permutation 思维
题意: 你有一个长度为n的序列a(这个序列只能使用[1,n]区间内的数字,每个数字只能使用一次),通过a序列可以构造出来三个相同长度的序列f.g.h For each 1≤i≤n, fi=max{a1 ...
- codeforces632E. Thief in a Shop (dp)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
- java——字符串常量池、字符串函数以及static关键字的使用、数组的一些操作函数、math函数
字符串常量池: 字符串比较函数: 字符串常用方法: 字符串截取函数: 字符串截取函数: static关键字使用: 要调用类中的static类型的变量的时候,可以用"类名.变量名&quo ...
- Codeforces Round #327 (Div. 1) C. Three States
C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standard inp ...