#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[index].szExeFile);
if(id==0)
{
printf("this process not exist!");
return;
}
HANDLE phand=OpenProcess(PROCESS_ALL_ACCESS,false,id);
//err=CreateProcess(NULL,SysProcess[index].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[4096];
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[i];
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;
}