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的更多相关文章
随机推荐
- numpy pandas 学习
一. 数组要比列表效率高很多 numpy高效的处理数据,提供数组的支持,python默认没有数组.pandas.scipy.matplotlib都依赖numpy. pandas主要用于数据挖掘,探索, ...
- xss靶场详解
一个XSS靶场练习记录 https://blog.csdn.net/qq_41500251/article/details/101116697 001.level 1 反射型 1.观察源码 <s ...
- equals()方法和hashCode()方法
1.equal()方法 2.hasCode()方法 2.1 Object的hashCode() 2.2 hashCode()的作用 3.String中equals()和hashCode()的实现 4. ...
- docker 安装 nexus3 初始密码不再是admin123
最近在docker上安装 nexus3 ,参照之前博客都提示 初始密码是admin/admin123 但是登录的时候出现如下提示: 很显然提示 admin用户的密码在/nexus-data/admi ...
- C++多元组tuple使用方法?你熟悉吗?快来看看吧
- 数据结构-kmp算法
定义 改进字符串的匹配算法 关键:通过实现一个包含了模式串的局部匹配信息的next()函数,利用匹配失败的信息,减少匹配次数. 1.BF算法 暴力匹配 给定 文本串S "BBC ABCDAB ...
- 2018 ACM-ICPC 焦作区域赛 E Resistors in Parallel
Resistors in Parallel Gym - 102028E 吐槽一下,网上搜索的题解一上来都是找规律,对于我这种对数论不敏感的人来说,看这种题解太难受了,找规律不失为一种好做法,但是题解仅 ...
- 2019牛客暑期多校训练营(第二场)E.MAZE(线段树+dp)
题意:给你一个n*m的矩阵 你只能向左向右相下走 有两种操作 q次询问 一种是把一个单位翻转(即可走变为不可走 不可走变为可走) 另一种是询问从(1,x) 走到 (n,y)有多少种方案 思路:题目n为 ...
- PAT(乙级)2020年春季考试
比赛链接:https://pintia.cn/market/item/1287964475579875328 7-1 对称日 题解 模拟,注意年月日不足位在前面补零. 代码 #include < ...
- hdu 4521 小明系列问题——小明序列 线段树
题意: 给你一个长度为n的序列v,你需要输出最长上升子序列,且要保证你选的两个相邻元素之间在原数组中的位置之差大于d 题解: 这个就是原来求最长上升子序列的加强版,这个思路和最长上升子序列的差不多 ...