关于内存搜索的疑问
#include <Windows.h>#include <TlHelp32.h> //声明快照函数的头文件
#include <stdio.h>
BOOL FindFirst(DWORD dwValue); //在目标进程空间中进行第一次搜索
BOOL FindNext(DWORD dwValue); //在目标进程空间进行第二三四次搜索
DWORD g_arList; //地址列表
int g_nListCnt; //有效地址的个数
HANDLE g_hProcess; //目标进程句柄
BOOL CompareAPage(DWORD dwBaseAddr,DWORD dwValue) //比较目标进程内存中一页大小的内存
{
//读取一页内存
BYTE arBytes;
if(!ReadProcessMemory(g_hProcess,(LPVOID)dwBaseAddr,arBytes,4096,NULL));
return FALSE;//此页不可读
//在这一页内存中查找
DWORD* pdw;
for (int i=0;i<(int)4*1024-3;i++)
{
pdw=(DWORD*)&arBytes;
if (pdw==dwValue)//等于要查找的值?
{
if(g_nListCnt>=1024)
return false;
g_arList=dwBaseAddr+i;//添加到全局变量中
}
}
return true;
}
BOOL FindFirst(DWORD dwValue)
{
const DWORD dwOneGB=1024*1024*1024;//1GB
const DWORD dwOnePage=4*1024;//4KB
if(g_hProcess==NULL)
return false;
//查看操作系统类型,以决定开始地址
DWORD dwBase;
OSVERSIONINFO vi={sizeof(vi)};
GetVersionEx(&vi);
if (vi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
dwBase=4*1024*1024;//windows98系列,4MB
}
else
dwBase=640*1024;//windows NT系列,64KB
//在开始地址到2GB的地址空间进行查找
for (;dwBase<2*dwOneGB;dwBase+=dwOnePage)
{
//比较一页大小的内存
CompareAPage(dwBase,dwValue);
}
return TRUE;
}
void ShowList()
{
for (int i=0;i<g_nListCnt;i++)
{
printf("%081x\n",g_arList);
}
}
BOOL FindNext(DWORD dwValue)
{
//保存m_arList数组中有效地址的个数,初始化新的m_nListCnt值
int nOrgCnt=g_nListCnt;
g_nListCnt=0;
//在m_arList数组记录的地址处查找
BOOL bRet=FALSE;//假设失败
DWORD dwReadValue;
for (int i=0;i<nOrgCnt;i++)
{
if (ReadProcessMemory(g_hProcess,(LPVOID)g_arList,&dwReadValue,sizeof(DWORD),NULL))
{
if (dwReadValue==dwValue)
{
g_arList=g_arList;
bRet=TRUE;
}
}
}
return bRet;
}
BOOL WriteMemory(DWORD dwAddr,DWORD dwValue)
{
return WriteProcessMemory(g_hProcess,(LPVOID)dwAddr,&dwValue,sizeof(DWORD),NULL);
}
int main(int argc,char* argv[])
{
char szFileName[]="";//这里是修改内存的程序的名字
STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;
::CreateProcess(NULL,szFileName,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
//关闭线程句柄,既然我们不需要它
::CloseHandle(pi.hThread);
g_hProcess=pi.hProcess;
int iVal;//输入要修改的值
printf("Input val=");
scanf("%d",&iVal);
FindFirst(iVal);//进行第一次查找
ShowList();//打印出搜索的结果
while (g_nListCnt>1)
{
printf("Input val=");
scanf("%d",&iVal);//进行下次搜索
FindNext(iVal);
ShowList();//显示搜索结果
}
printf("New value=");//取得新值
scanf("%d",&iVal);
if(WriteMemory(g_arList,iVal))//写入新值
printf("Write data sucess\n");
CloseHandle(g_hProcess);
return 0;
}
首先printf("%081x\n",g_arList);%081X不是输出81位吗,地址应该是8位输出吧,我感觉应该是%08x\n
还有谁能讲一下关于系统平台和dwBase的值的选择的关系呢
@小甲鱼 顶起来 单机吗{:10_269:} @ 小甲鱼 @小甲鱼 听说这样可以改格式,哥们你这样的程序没人愿意看的
#include <Windows.h>
#include <TlHelp32.h> //声明快照函数的头文件
#include <stdio.h>
BOOL FindFirst(DWORD dwValue); //在目标进程空间中进行第一次搜索
BOOL FindNext(DWORD dwValue); //在目标进程空间进行第二三四次搜索
DWORD g_arList; //地址列表
int g_nListCnt; //有效地址的个数
HANDLE g_hProcess; //目标进程句柄
BOOL CompareAPage(DWORD dwBaseAddr,DWORD dwValue) //比较目标进程内存中一页大小的内存
{
//读取一页内存
BYTE arBytes;
if(!ReadProcessMemory(g_hProcess,(LPVOID)dwBaseAddr,arBytes,4096,NULL));
return FALSE;//此页不可读
//在这一页内存中查找
DWORD* pdw;
for (int i=0;i<(int)4*1024-3;i++)
{
pdw=(DWORD*)&arBytes;
if (pdw==dwValue)//等于要查找的值?
{
if(g_nListCnt>=1024)
return false;
g_arList=dwBaseAddr+i;//添加到全局变量中
}
}
return true;
}
BOOL FindFirst(DWORD dwValue)
{
const DWORD dwOneGB=1024*1024*1024;//1GB
const DWORD dwOnePage=4*1024;//4KB
if(g_hProcess==NULL)
return false;
//查看操作系统类型,以决定开始地址
DWORD dwBase;
OSVERSIONINFO vi={sizeof(vi)};
GetVersionEx(&vi);
if (vi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
dwBase=4*1024*1024;//windows98系列,4MB
}
else
dwBase=640*1024;//windows NT系列,64KB
//在开始地址到2GB的地址空间进行查找
for (;dwBase<2*dwOneGB;dwBase+=dwOnePage)
{
//比较一页大小的内存
CompareAPage(dwBase,dwValue);
}
return TRUE;
}
void ShowList()
{
for (int i=0;i<g_nListCnt;i++)
{
printf("%081x\n",g_arList);
}
}
BOOL FindNext(DWORD dwValue)
{
//保存m_arList数组中有效地址的个数,初始化新的m_nListCnt值
int nOrgCnt=g_nListCnt;
g_nListCnt=0;
//在m_arList数组记录的地址处查找
BOOL bRet=FALSE;//假设失败
DWORD dwReadValue;
for (int i=0;i<nOrgCnt;i++)
{
if (ReadProcessMemory(g_hProcess,(LPVOID)g_arList,&dwReadValue,sizeof(DWORD),NULL))
{
if (dwReadValue==dwValue)
{
g_arList=g_arList;
bRet=TRUE;
}
}
}
return bRet;
}
BOOL WriteMemory(DWORD dwAddr,DWORD dwValue)
{
return WriteProcessMemory(g_hProcess,(LPVOID)dwAddr,&dwValue,sizeof(DWORD),NULL);
}
int main(int argc,char* argv[])
{
char szFileName[]="";//这里是修改内存的程序的名字
STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;
::CreateProcess(NULL,szFileName,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
//关闭线程句柄,既然我们不需要它
::CloseHandle(pi.hThread);
g_hProcess=pi.hProcess;
int iVal;//输入要修改的值
printf("Input val=");
scanf("%d",&iVal);
FindFirst(iVal);//进行第一次查找
ShowList();//打印出搜索的结果
while (g_nListCnt>1)
{
printf("Input val=");
scanf("%d",&iVal);//进行下次搜索
FindNext(iVal);
ShowList();//显示搜索结果
}
printf("New value=");//取得新值
scanf("%d",&iVal);
if(WriteMemory(g_arList,iVal))//写入新值
printf("Write data sucess\n");
CloseHandle(g_hProcess);
return 0;
} 好吧没有用
你要把格式写好
代码写得太挫了。起码得有空格对齐 。 vcvycy 发表于 2016-8-12 21:50
代码写得太挫了。起码得有空格对齐 。
我已经知道了结果了,下面贴上我的代码 #include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>//快照函数的头文件
#include <vector>
using namespace std;
struct addrinfo
{
LPVOID addr;//存放地址
bool falg;//一个标记,如果为true,则需进行删除
};
vector<PROCESSENTRY32> SysProcess;//存储系统进程的变量
vector<struct addrinfo> addrlist;//地址列表
vector<struct addrinfo> templist;//临时地址列表
int cnt=0;//进程数量
int g_nlist=0;//存放有效地址数量
HANDLE g_hProcess;//全局进程句柄
void PrintProcess();//列举所有的系统进程
void SelectExc();//选择一个进程进行内存修改
bool FindFirst(DWORD value);//第一次进行搜索
bool CompareAPage(DWORD dwBase,DWORD ivalue);//前一个参数代表搜索基址,后一个参数代表要搜索的值
bool FindNext(DWORD value);//第二,三,四,五次进行搜索
void ShowList();//显示地址列表
DWORD GetProcessidFromName(LPCTSTR proname);//从名字得到进程ID 成功返回非0,失败返回0
void main()
{
DWORD ival;
PrintProcess();
SelectExc();
printf("Input val=");
scanf("%d",&ival);//进行初值的输入
FindFirst(ival);
ShowList();
while(g_nlist>1)
{
printf("new Input val=");
scanf("%d",&ival);//进行初值的输入
FindNext(ival);
ShowList();
}
if(g_nlist==1)
{
printf("now we can correct the value,new value=");
scanf("%d",&ival);//进行初值的输入
vector<struct addrinfo>::iterator it=addrlist.begin();
DWORD written;
WriteProcessMemory(g_hProcess,(DWORD*)(it->addr),&ival,sizeof(DWORD),&written);
}
else
{
printf("sorry,we can't find the address");
}
CloseHandle(g_hProcess);
}
void PrintProcess()
{
HANDLE handle;
PROCESSENTRY32 pi;
handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(handle==INVALID_HANDLE_VALUE)
{
printf("调用列举函数失败\n");
return;
}
pi.dwSize=sizeof(pi);
if(Process32First(handle,&pi))
{
SysProcess.push_back(pi);//打印进程名称,进程ID,线程数量,父进程ID
printf("%d\t %s\t %d\t %d\t %d\t\n",cnt++,pi.szExeFile,pi.th32ProcessID,pi.cntThreads,pi.th32ParentProcessID);
}
while(Process32Next(handle,&pi))
{
SysProcess.push_back(pi);//打印进程名称,进程ID,线程数量,父进程ID
printf("%d\t %s\t %d\t %d\t %d\t\n",cnt++,pi.szExeFile,pi.th32ProcessID,pi.cntThreads,pi.th32ParentProcessID);
}
CloseHandle(handle);
}
void SelectExc()
{
int index;
//int err;
DWORD id;
//STARTUPINFO si={sizeof(si)};
//PROCESS_INFORMATION pi;
printf("please select a process:");
scanf("%d",&index);
id=GetProcessidFromName(SysProcess.szExeFile);
if(id==0)
{
printf("this process not exist!");
return;
}
HANDLE phand=OpenProcess(PROCESS_ALL_ACCESS,false,id);
//err=CreateProcess(NULL,SysProcess.szExeFile,NULL,NULL,false
/* ,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
if(err==0)
{
printf("创建进程出错!\n");
return;
}
*/
//CloseHandle(pi.hThread);
g_hProcess=phand;
}
bool FindFirst(DWORD value)
{
const DWORD dwOneGB=1024*1024*1024;
const DWORD dwOnePage=4*1024;
if(g_hProcess==NULL)
return false;
DWORD dwBase;//搜索基地址
OSVERSIONINFO osi;
GetVersionEx(&osi);
if(osi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
dwBase=4*1024*1024;//4M
}
else
{
dwBase=640*1024;//64KB
}
for(;dwBase<2*dwOneGB;dwBase+=dwOnePage)
{
CompareAPage(dwBase,value);
}
return true;
}
bool CompareAPage(DWORD dwBase,DWORD ivalue)//前一个参数代表搜索基址,后一个参数代表要搜索的值
{
DWORD bytesread;
DWORD pdw;
BYTE buffer;
struct addrinfo temp;//临时存放地址和标记的变量
bool success=ReadProcessMemory(g_hProcess,(void*)dwBase,buffer,4096,&bytesread);
if(!success)
{
return false;
}
//for(;(void*)dwBase<(void*)(dwBase+4096-3);dwBase++)
for(int i=0;i<(4096-3);i++)
{
pdw=*(DWORD*)&buffer;
if(pdw==ivalue)
{
if(g_nlist>=1024*1024*1024)
return false;
temp.addr=(void*)(dwBase+i);
temp.falg=true;
g_nlist++;
addrlist.push_back(temp);
}
}
return true;
}
bool FindNext(DWORD value)
{
DWORD bytesread;
DWORD newvalue;
vector<struct addrinfo>::iterator it;
if(g_hProcess==NULL)
return false;
for(it=addrlist.begin();it!=addrlist.end();it++)
{
if(ReadProcessMemory(g_hProcess,it->addr,&newvalue,sizeof(DWORD),&bytesread))
{
if(newvalue!=value)
{
it->falg=false;
g_nlist--;
}
else
{
templist.push_back(*it);
}
}
}
addrlist.erase(addrlist.begin(),addrlist.end());
printf("%d\n",addrlist.size());
for(it=templist.begin();it!=templist.end();it++)
{
addrlist.push_back(*it);
}
templist.erase(templist.begin(),templist.end());
return true;
}
void ShowList()
{
vector<struct addrinfo>::const_iterator it;
for(it=addrlist.begin();it!=addrlist.end();it++)
{
printf("%08X\n",it->addr);
}
printf("%d\n",g_nlist);
}
DWORD GetProcessidFromName(LPCTSTR proname)//成功返回非0,失败返回0
{
DWORD id=0;
bool flag=false;
HANDLE handle;
PROCESSENTRY32 pi;
handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(handle==INVALID_HANDLE_VALUE)
{
printf("调用列举函数失败\n");
return id;
}
pi.dwSize=sizeof(pi);
if(!Process32First(handle,&pi))
return id;
while(Process32Next(handle,&pi))
{
if(!strcmp(pi.szExeFile,proname))
{
id=pi.th32ProcessID;
break;
}
}
CloseHandle(handle);
return id;
}
其实一楼的代码是我在鱼c论坛上看到的,并不是我写的,只不过,我认为其中有些东西有些错误,又没有人说它是错的,我产生了疑惑,所以才问问大家,不过,我已经有结果了 支持
页:
[1]