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的更多相关文章
随机推荐
- Language Guide (proto3) | proto3 语言指南(五)使用其他消息类型
Using Other Message Types - 使用其他消息类型 可以将其他消息类型用作字段类型.例如,假设您希望在每个SearchResponse消息中包含Result消息--为此,您可以在 ...
- react+ant design 项目执行yarn run eject 命令后无法启动项目
如何将内建配置全部暴露? 使用create-react-app结合antd搭建的项目中,项目目录没有该项目所有的内建配置, 1.执行yarn run eject 执行该命令后,运行项目yarn sta ...
- MySQL按照(windows)及常用命令
MySQL语法规则 关键字与函数名称全部大写 数据库名称.表名称.字段名称全部小写 SQL 语句必须以分号结尾 MySQL安装 MySQL配置: 在cmd中输入 mysql,提示['mysql' 不是 ...
- 通过脚本本地下载Jar包
通过脚本本地下载Jar包 1.脚本 2.pom.xml 1.脚本 download.bat # !/bin/bash mvn -f pom.xml dependency:copy-dependenci ...
- 虚拟局域网(VLAN)__语音VLAN
1.语音VLAN特性使得访问端口能够携带来自IP电话的IP语音流量.当交换机连接到Cisco IP电话时,IP电话就用第3层IP优先级(precedence)和第2层服务级别(class of ser ...
- HDU-6148 Valley Number (数位DP)
当一个数字,从左到右依次看过去数字没有出现先递增接着递减的"山峰"现象,就被称作 Valley Number.它可以递增,也可以递减,还可以先递减再递增.在递增或递减的过程中可以出 ...
- 2019 Multi-University Training Contest 2 Harmonious Army(最小割)
题意:给你n个点 每个点都有两种选择 成为战士或者法师 现在给你m个关系 对应这两个人的对应关系的权值A,B,C 思路:按照下面的思路建图跑最小割(要注意权值要乘2 可能存在不整除的情况) #incl ...
- AtCoder Beginner Contest 177
比赛链接:https://atcoder.jp/contests/abc177/tasks A - Don't be late #include <bits/stdc++.h> using ...
- 【noi 2.2_7891】一元三次方程求解(二分枚举+输出程序运行时间)
对于noi上的题有2种解法: 1.数据很小(N=100),可以直接打for循环枚举和判断. 2.不会"三分",便用二分.利用"两根相差>=1"和 f(x1 ...
- hdu1394Minimum Inversion Number
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...