加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

自己写的一个数据修改器(可以修改植物大战僵尸的太阳值)beta1.0版本

(2011-11-27 22:32:24)
标签:

进程

分类: 操作系统

#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string.h>

const unsigned int iMAX = 9000 ;
HANDLE g_hProcess ;  //目标进程句柄
int g_nListCnt = 0 ;   //有效地址的个数
DWORD g_arList[iMAX] ;   //地址列表


const DWORD dwOneGB = 1024 * 1024 * 1024; //1GB 的大小
const DWORD dwOnePage = 4 * 1024;  //页面大小 4KB

 

void GetBaseAddr(DWORD *dwBase)
{
 //查看操作系统类型,来决定开始的地址
 //说明:windows 98系列,4MB
 //  windows NT系列,64KB
 OSVERSIONINFO vi = {sizeof(vi)} ;
 ::GetVersionEx(&vi);
 if(VER_PLATFORM_WIN32_WINDOWS == vi.dwPlatformId )
 {
  *dwBase = 4* 1024 *1024;
 }
 else
 {
  *dwBase = 640 *1024;
 }
}


BOOL CompareAPage(DWORD dwBaseAddr , DWORD dwValue)
{
 BYTE arBytes[dwOnePage];
 if(!::ReadProcessMemory(g_hProcess , (LPVOID)dwBaseAddr , arBytes , dwOnePage , NULL))
  return FALSE ;

 //在这页内存中查找
 DWORD *pdw;
 int i;
 for( i = 0 ; i < (int)dwOnePage - 3 ; i++ )
 {
  pdw = (DWORD*) &arBytes[i] ;
  if( pdw[0] == dwValue)
  {
   if(g_nListCnt >= iMAX)
   {
    return FALSE;
   }
   //添加到全局变量中
   g_arList[g_nListCnt++] = dwBaseAddr + i;
  }
 }
 return TRUE;
}

 

BOOL CompareAllPage(DWORD dwBase,DWORD dwValue)
{
 unsigned int iNum = 0 ;
 for( ; dwBase < 2 * dwOneGB ; dwBase += dwOnePage)
 {
  if(CompareAPage(dwBase,dwValue))
   ++iNum;
 }
 printf("可读的页面数量为:%u\n", iNum);
 return TRUE;
}


BOOL FindFirst(DWORD dwValue)
{
 if(NULL == g_hProcess)
 {
  return FALSE;
 }

 DWORD dwBase ;
 GetBaseAddr(&dwBase) ;

 CompareAllPage(dwBase , dwValue);
 return TRUE;
}


void ShowList()
{
 int i;
 for( i =0 ; i < g_nListCnt ; ++i)
 {
  printf("LX\n",g_arList[i]);
 }
}


BOOL FindNext(DWORD dwValue)
{
 //保存m_arList数组中有效地址的个数,初始化新的g_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[i] , &dwReadValue , sizeof(DWORD) , NULL ) )
  {
   if(dwReadValue == dwValue)
   {
    g_arList[g_nListCnt++] = g_arList[i] ;
    bRet = TRUE;
   }
  }
 }
 return TRUE;
}

BOOL WriteMemory(DWORD dwAddr , DWORD dwValue)
{
 return ::WriteProcessMemory(g_hProcess , (LPVOID) dwAddr , &dwValue , sizeof(DWORD) , NULL );
}

BOOL CompareStr(CHAR szStr1[] ,CHAR szStr2[] )
{
 unsigned int iLen1 , iLen2 ;
 iLen1 = strlen(szStr1) ;
 iLen2 = strlen(szStr2) ;
 if(iLen1 != iLen2 )
 {
  return FALSE ;
 }
 
 unsigned int i ;
 for(i = 0 ; i < iLen1 ; ++i )
 {
  if(szStr1[i] != szStr2[i] )
  {
   return FALSE ;
  }
 }
 return TRUE ;
}

//遍历进程快照,寻找指定进程的信息
BOOL FindProcessMsg( DWORD *dwProcessID ,CHAR szFile[] , HANDLE hProcessSnap )
{

 PROCESSENTRY32 pe32 ;  //进程信息结构
 //在使用它之前,先设置它的大小
 pe32.dwSize = sizeof(pe32) ; 

 BOOL bMore = ::Process32First( hProcessSnap , &pe32 ) ;

 while(!CompareStr(pe32.szExeFile,szFile) && bMore)
 {
  bMore = ::Process32Next( hProcessSnap , &pe32 ) ;
 }

 if(!bMore)
 {
  return FALSE ;
 }
 else
 {
  *dwProcessID = pe32.th32ProcessID ;
  printf("找到的进程的信息:\n" ) ;
  printf("进程的文件名:%s\n\n",pe32.szExeFile ) ;
  return TRUE;
 }
}

//找到指定进程的信息
BOOL FindPlantsEXE( DWORD *dwProcessID , CHAR szFile[] )
{
 
 //给系统内的所有进程拍一个快照
 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

 if( INVALID_HANDLE_VALUE == hProcessSnap)
 {
  printf("CreateToolhelp32Snapshot 调用失败! \n") ;
  return FALSE ;
 }
 
 //遍历进程快照,寻找指定进程的信息
  if(!FindProcessMsg( dwProcessID , szFile , hProcessSnap ) )
  {
  ::CloseHandle( hProcessSnap ) ;
  return FALSE ;
  }

 //不要忘记清除snapshot对象
 ::CloseHandle( hProcessSnap ) ;
 return TRUE ;
}

BOOL GetAccessAbility( DWORD dwProcessID )
{
 BOOL bRet = FALSE ; //假设访问失败
 g_hProcess = ::OpenProcess( PROCESS_ALL_ACCESS , FALSE , dwProcessID ) ;
 
 if( NULL != g_hProcess )
 {
  bRet = TRUE ;
 }

 return bRet ;
}

BOOL FindModifyNum( DWORD dwValue )
{
 //进行第一次查找
 FindFirst( dwValue ) ;
 //打印出搜索的结果
 printf("请等待,正在查找......\n\n");
 //ShowList();
 while(g_nListCnt > 1)
 {
  printf(" 输入要修改的当前值 val=" ) ;   //输入要更改的值
  scanf("%d" , &dwValue ) ;
  //进行下次搜索
  if(!FindNext(dwValue ) )
  {
   printf("没有找到要寻找的值!!!") ;
   return FALSE ;
  }
  //显示搜索的结果
  printf("请等待,正在查找......\n\n");
  ShowList();
 }
 if(g_nListCnt)
 {
  printf("提示:已找到进程中要更改的数据*********\n");
  printf("可以进行修改了!!!\n");
  printf("当前的要更改的数据的内存为:\n");
  ShowList();
  return TRUE;
 }
 else
 {
  printf("提示:查找失败,原因不明!!!\n\n");
  return FALSE;
 }

}

BOOL ModifyNum()
{
 int iVal ;
 printf("请输入新值:New Value =") ;
 scanf("%d" , &iVal );
 //写入新值
 if(WriteMemory( g_arList[0] , iVal ) )
 {
  printf("Write data success\n" );
  printf("****************************************\n");
  return TRUE ;
 }
 else
 {
  printf("修改数据失败!\n") ;
  printf("****************************************\n");
  return FALSE ;
 }
}

BOOL CheckFile(CHAR szFile[] )
{
 unsigned char cLen = strlen(szFile ) ;
 unsigned char cTemp[] = ".exe";
 if(cLen >= 20 )
 {
  return FALSE ;
 }

 //检查是否是在szFile中有0x2e----'.'字符
 if(szFile[cLen - 1 - 3] != 0x2e)
 {
  szFile[cLen++] = cTemp[0] ;
  szFile[cLen++] = cTemp[1] ;
  szFile[cLen++] = cTemp[2] ;
  szFile[cLen++] = cTemp[3] ;
  szFile[cLen]  = '\0' ;
 }
 return TRUE ;
}

int main()
{
 CHAR szFile[20] ;
 DWORD dwProcessID ;
 int iVal ;
 printf("*************************欢迎使用小生数据修改器beta 1.0*******************\n") ;
 printf("说明:\n");
 printf("此程序可以修改一般的游戏参数(可变的)\n");
 printf("植物大战僵尸已经调试成功。");
 printf("1,首先请运行要执行的程序\n") ;
 printf("2,然后输入要修改程序的程序的文件名\n") ;
 printf("3,输入要修改的数据当前值 ;\n") ;
 printf("4,改变要更改的数据的值在要被修改的程序当中.\n") ;
 printf("**************************************************************************\n") ;
 printf("\n");
 printf("该修改器适合于Windows 2000系列和Windows NT系列\n");
 printf("该修改器仅供娱乐与学习,不得用于商业目的,天幕不负任何责任......\n");
 printf("大家如果发现bug或者有什么意见建议或者希望交流的的欢迎Q我:547774613;\n");
 printf("\n");
 printf("\n");
 printf("***************************************************************************\n\n") ;
 
 while(1)
 {
  memset(g_arList,0,sizeof(g_arList) ) ;
  printf("请输入你要修改的程序的全名!!\n");
  printf("FileName=") ;
  scanf("%s",szFile) ;
  
  if(!CheckFile(szFile))
  {
   printf("输入文件错误!!!\n");
   continue ;
  }
  printf("\n") ;
  if(!FindPlantsEXE( &dwProcessID , szFile ) )  //找到指定进程的信息
  {
   printf("%s没有运行,请先运行!!并重新运行修改器!!!\n",szFile);
   printf("并重新输入要修改的程序!!!\n\n");
   continue ;
  }
  
  if( !GetAccessAbility( dwProcessID ) )  //得到访问权限
  {
   printf("没有得到访问权限!!!\n" ) ;
   continue ;
  }
  else
  {
   printf("得到权限\n\n" );
  }
  
  //已经得到权限,已实现
  //::TerminateProcess(g_hProcess,0 ) ;
  
  printf(" 输入sunshine的当前值 val=" ) ;   //输入要更改的值
  scanf("%d" , &iVal ) ;
  
  if(!FindModifyNum(iVal ) )    //查找要修改的值得
  {
   ::CloseHandle( g_hProcess );
   continue ;
  }
  
  ModifyNum() ;     //进行修改
  
  ::CloseHandle( g_hProcess );
 }
 return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有