内存修改器

来源:互联网 发布:win10 磁盘系统优化 编辑:程序博客网 时间:2024/04/30 01:43


 

 

#include <windows.h>#include <iostream>#include <Tlhelp32.h>#include <process.h>#include <commctrl.h>#include "resource.h"#pragma comment(lib,"comctl32.lib")#define ID_STATUSBAR  1#define ID_TIME100using namespace std;BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);BOOL CALLBACK GetProcessList (HWND hwnd);BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);BOOL EnablePrivilege(PCTSTR szPrivilege, BOOL fEnable);HWNDhList1;HWNDhList2;HWNDhWinStatus;boollock;typedef struct {DWORDvalue;HANDLE  hProcess;}arglist;#include "Search.h"int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){InitCommonControls();EnablePrivilege(SE_DEBUG_NAME, TRUE);DialogBoxParam (hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc, 0);EnablePrivilege(SE_DEBUG_NAME, FALSE);return 1;}BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){HICONhIcon;static HINSTANCEhInstance= GetModuleHandle (NULL);static int ID,index;static HWNDhEdit1,hEdit2,hEdit3;static TCHARszBuffer[10];static arglistarg;static TCHAR szFree[]=L"   空闲中......";static TCHAR szSearch[]=L"  搜索中,请耐心等候......";static TCHAR szLock[]=L"取消锁定";static TCHAR szUnlock[]=L"内存数值锁定";static DWORD var,addr;static HBITMAPhBitmap;switch (message){case WM_INITDIALOG:hIcon= LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1) );SendMessage (hwnd,WM_SETICON,ICON_BIG, (LPARAM)hIcon);hEdit1= GetDlgItem(hwnd, IDC_EDIT1);hEdit2= GetDlgItem(hwnd, IDC_EDIT2);hEdit3= GetDlgItem(hwnd, IDC_EDIT3);SendDlgItemMessage (hwnd, IDC_EDIT1,EM_LIMITTEXT, 8,  0);SendDlgItemMessage (hwnd, IDC_EDIT2,EM_LIMITTEXT, 12, 0);SendDlgItemMessage (hwnd, IDC_EDIT3, EM_LIMITTEXT, 8, 0);hList1= GetDlgItem (hwnd, IDC_LIST1);hList2= GetDlgItem (hwnd, IDC_LIST2);EnableWindow (GetDlgItem (hwnd, IDC_BUTTON3), FALSE);//灰化修改按钮EnableWindow (GetDlgItem (hwnd, IDC_BUTTON1), FALSE);hWinStatus= CreateStatusWindow(WS_CHILD | WS_VISIBLE | SBS_SIZEGRIP,NULL,hwnd,ID_STATUSBAR);//SendMessage,hWinStatus,SB_SETPARTS,4,offset dwStatusWidthSendMessage(hWinStatus,SB_SETTEXT,0, (LPARAM)szFree);GetProcessList (hwnd);return true;case WM_COMMAND:switch (LOWORD (wParam)){case IDC_LIST1:break;case IDC_LIST2:char item[20];if (HIWORD(wParam) == LBN_SELCHANGE){index= SendMessage (hList2, LB_GETCURSEL,0, 0);SendMessageA (hList2, LB_GETTEXT, index, (LPARAM)item);SetDlgItemTextA (hwnd, IDC_EDIT2, item);}break;case IDOK:GetProcessList(hwnd);//刷新break;case IDCANCEL:EndDialog (hwnd, 0);break;case IDC_EDIT1:EnableWindow(GetDlgItem(hwnd, IDC_BUTTON1), GetDlgItemText (hwnd, IDC_EDIT1, szBuffer, 4)) ;break;case IDC_EDIT3:EnableWindow(GetDlgItem(hwnd, IDC_BUTTON3), GetDlgItemText (hwnd, IDC_EDIT3, szBuffer, 4)) ;break;case IDC_BUTTON1:DWORD value;HANDLEhProcess;value= GetDlgItemInt (hwnd, IDC_EDIT1,NULL, FALSE);//if (!value){//MessageBox (hwnd, TEXT("请输入搜索数值"), NULL, MB_OK);//break;}index= SendMessage (hList1, LB_GETCURSEL,0, 0);ID= SendMessage (hList1, LB_GETITEMDATA, index, 0);EnableWindow(GetDlgItem(hwnd, IDC_LIST1), FALSE) ;//if ( hProcess = OpenProcess (PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, NULL, ID) )if ( hProcess = OpenProcess (PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_VM_OPERATION, NULL, ID) )//if (!Search (hProcess, value))//MessageBox (hwnd, TEXT("搜索失败"), NULL, MB_OK);{arg.hProcess= hProcess;arg.value= value;SendMessage (hList2, LB_RESETCONTENT, 0, 0);_beginthread(Search,NULL, &arg);SendMessage(hWinStatus,SB_SETTEXT,0, (LPARAM)szSearch);}else{MessageBox (hwnd, TEXT("打开进程失败"), NULL, MB_OK);EnableWindow(GetDlgItem(hwnd, IDC_LIST1), TRUE) ;}break;case IDC_BUTTON2:SendMessage (hList2, LB_RESETCONTENT, 0, 0);Init();EnableWindow(GetDlgItem(hwnd, IDC_LIST1), TRUE) ;SendMessage(hWinStatus,SB_SETTEXT,0, (LPARAM)szFree);break;case IDC_BUTTON3:char szNum[12];GetDlgItemTextA(hwnd, IDC_EDIT2,szNum,sizeof(szNum));sscanf (szNum,"%lX",&addr);var= GetDlgItemInt (hwnd, IDC_EDIT3, NULL, FALSE);if (!var){MessageBox (hwnd, TEXT("请输入修改数值"), NULL, MB_OK);break;}if (WriteMemory (addr, var))MessageBox (hwnd, TEXT("修改成功!"), TEXT("提示"), MB_OK);elseMessageBox (hwnd, TEXT("修改失败!"), NULL, MB_OK);break;case IDC_BUTTON4:if (lock == false){SendMessage (hwnd, WM_COMMAND, IDC_BUTTON3, 0);if (!var){break;}EnableWindow (GetDlgItem (hwnd, IDC_BUTTON1), FALSE);//灰化按钮EnableWindow (GetDlgItem (hwnd, IDC_BUTTON2), FALSE);EnableWindow (GetDlgItem (hwnd, IDC_BUTTON3), FALSE);EnableWindow (hEdit1, FALSE);EnableWindow (hEdit2, FALSE);EnableWindow (hEdit3, FALSE);SetTimer (hwnd, ID_TIME, 100, NULL);SetDlgItemText(hwnd, IDC_BUTTON4, szLock);SendMessage(hWinStatus,SB_SETTEXT,0, (LPARAM)TEXT("内存锁定中..."));lock= true;}else{KillTimer (hwnd, ID_TIME);EnableWindow (GetDlgItem (hwnd, IDC_BUTTON1), TRUE);//激活按钮EnableWindow (GetDlgItem (hwnd, IDC_BUTTON2), TRUE);EnableWindow (GetDlgItem (hwnd, IDC_BUTTON3), TRUE);EnableWindow (hEdit1, TRUE);EnableWindow (hEdit2, TRUE);EnableWindow (hEdit3, TRUE);SetDlgItemText(hwnd, IDC_BUTTON4, szUnlock);SendMessage(hWinStatus,SB_SETTEXT,0, (LPARAM)szFree);lock= false;}break;case ID_ABOUT:DialogBox (GetModuleHandle (NULL), TEXT ("AboutBox"), hwnd, AboutDlgProc);break;/*case IDCANCEL:HANDLEh;index= SendMessage (hList, LB_GETCURSEL,0, 0);ID= SendMessage (hList, LB_GETITEMDATA, index, 0);if ( h = OpenProcess (PROCESS_TERMINATE, NULL, ID) ){TerminateProcess (h, -1);CloseHandle (h);Sleep (100);GetProcessList (hwnd);}elseMessageBox (hwnd, TEXT("无法结束指定进程"),NULL, MB_OK);break;*/}return true;case WM_TIMER:if (!WriteMemory (addr, var)){MessageBox (hwnd, TEXT("内存锁定失败"),NULL, MB_OK);lock= true;SendMessage (hwnd, WM_COMMAND, IDC_BUTTON4, 0);}return true;case WM_PAINT:HDChdc, hdcMem ;PAINTSTRUCT ps ;    hBitmap= LoadBitmap(hInstance, L"BitBlt");hdc = BeginPaint(hwnd, &ps);hdcMem = CreateCompatibleDC(hdc);SelectObject(hdcMem, hBitmap);StretchBlt (hdc, 0, 0, 500, 500,hdcMem, 0, 0, 500, 500, MERGECOPY) ;DeleteDC(hdcMem);EndPaint (hwnd, &ps);return true;case WM_CLOSE:KillTimer (hwnd, ID_TIME);EndDialog (hwnd, 0);return true;}return false;}BOOL CALLBACK GetProcessList (HWND hwnd){PROCESSENTRY32stProcess;HANDLEhSnapShot;RtlZeroMemory (&stProcess, sizeof (stProcess) );SendMessage (hList1, LB_RESETCONTENT, 0, 0);stProcess.dwSize= sizeof (stProcess);hSnapShot= CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);bool res= Process32First (hSnapShot, &stProcess);while ( res){int index= SendMessage (hList1, LB_ADDSTRING, 0, (LPARAM)stProcess.szExeFile);SendMessage (hList1, LB_SETITEMDATA, index, stProcess.th32ProcessID);res= Process32Next (hSnapShot, &stProcess);}CloseHandle (hSnapShot);return true;}BOOL CALLBACK AboutDlgProc (HWND hwnd, UINT message,                             WPARAM wParam, LPARAM lParam){     switch (message)     {     case WM_INITDIALOG :          return TRUE ;               case WM_COMMAND :          switch (LOWORD (wParam))          {  case IDOK :  EndDialog (hwnd, 0) ;  MessageBox (NULL, TEXT("因为箱子里放的是变压器"), TEXT("嗯嗯"), MB_OK);  return TRUE;          case IDCANCEL :               EndDialog (hwnd, 0) ;   MessageBox (NULL, TEXT("不想知道的是猪"), TEXT("haha"), MB_OK);               return TRUE ;          }          break ;     }     return FALSE ;}//提升当前进程权限(调试权限)BOOL EnablePrivilege(PCTSTR szPrivilege, BOOL fEnable) {   // Enabling the debug privilege allows the application to see   // information about service applications   BOOL fOk = FALSE;    // Assume function fails   HANDLE hToken;   // Try to open this process's access token   if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES,       &hToken)) {      // Attempt to modify the given privilege      TOKEN_PRIVILEGES tp;      tp.PrivilegeCount = 1;      LookupPrivilegeValue(NULL, szPrivilege, &tp.Privileges[0].Luid);      tp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;      AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);      fOk = (GetLastError() == ERROR_SUCCESS);      // Don't forget to close the token handle      CloseHandle(hToken);   }   return(fOk);}


 

#ifndef SEARCH_H#define SEARCH_H#include <windows.h>#define MAX_SIZE 20*1024void Search(void *arg);//bool Search(HANDLE hProc, DWORD dwValue);bool FirstSearch(DWORD dwValue);bool NextSearch (DWORD dwValue);bool WriteMemory(DWORD dwAddr, DWORD dwValue);bool CompareAPage (DWORD baseAdd, DWORD dwValue);DWORDList[MAX_SIZE];DWORDListcnt;boolFirst = 1;HANDLEhProcess;const DWORD dwOneGB = 1024*1024*1024;// 1GBconst DWORD dwOnePage = 4*1024;// 4KBvoid Init(){memcpy (List, "\0", sizeof (List) );Listcnt = 0;First= 1;}void Search(void *arg){TCHARszResult[20];hProcess= ((arglist*)arg)->hProcess;if ( First){FirstSearch(((arglist*)arg)->value);    First= 0;}elseNextSearch (((arglist*)arg)->value);wsprintf (szResult, L"搜索完毕,发现%lu个结果", Listcnt);SendMessage(hWinStatus,SB_SETTEXT,0, (LPARAM)szResult);}bool FirstSearch(DWORD dwValue){if(hProcess == NULL)return FALSE;// 查看操作系统类型,以决定开始地址DWORD dwBase;OSVERSIONINFO vi = { sizeof(vi) };GetVersionEx(&vi);if (vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)dwBase = 4*1024*1024;// Windows 98系列,4MBelsedwBase = 640*1024;// Windows NT系列,64KBfor (dwBase = 1024*1024;dwBase < 2 * dwOneGB ; dwBase += dwOnePage)CompareAPage (dwBase, dwValue);return true;}bool NextSearch (DWORD dwValue){DWORD dwReadValue, num=Listcnt;Listcnt= 0;char Item[20];SendMessage (hList2, LB_RESETCONTENT, 0, 0);for (unsigned int i=0; i < num; i++){if( ReadProcessMemory(hProcess, (LPVOID)List[i], &dwReadValue, sizeof(DWORD), NULL))if (dwReadValue == dwValue){List[Listcnt++]= List[i];sprintf (Item, "0x%08lX", List[i]);SendMessageA (hList2, LB_ADDSTRING, 0, (LPARAM)Item);}}return true;}bool WriteMemory(DWORD dwAddr, DWORD dwValue){return WriteProcessMemory (hProcess, (LPVOID)dwAddr, &dwValue, sizeof (DWORD), NULL);}bool CompareAPage (DWORD baseAdd, DWORD dwValue){BYTE PageData[4096];char Item[20];if ( !ReadProcessMemory (hProcess, (LPCVOID)baseAdd, PageData, dwOnePage, NULL) )return false;DWORD*p;for (unsigned int i=0; i < (DWORD)4*1024 - 3; i++){p= (DWORD*)&PageData[i];if (p[0] == dwValue){if (Listcnt >= MAX_SIZE)return false;List[Listcnt]= baseAdd + i;sprintf (Item, "0x%08lX", List[Listcnt]);  SendMessageA (hList2, LB_ADDSTRING, 0, (LPARAM)Item);Listcnt++;}}return true;}#endif


 

//{{NO_DEPENDENCIES}}// Microsoft Visual C++ generated include file.// Used by 内存修改器.rc//#define IDD_DIALOG1                     101#define IDD_DIALOG                      101#define IDI_ICON                        102#define IDR_MENU                        103#define IDI_ICON1                       110#define IDB_BITMAP1                     111#define IDC_LIST1                       1001#define IDC_LIST2                       1002#define IDC_EDIT1                       1003#define IDC_BUTTON1                     1004#define IDC_BUTTON2                     1005#define IDC_EDIT2                       1006#define IDC_EDIT3                       1007#define IDC_BUTTON3                     1008#define IDC_BUTTON4                     1009#define ID_40001                        40001#define ID_ABOUT                        40002// Next default values for new objects// #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE        112#define _APS_NEXT_COMMAND_VALUE         40003#define _APS_NEXT_CONTROL_VALUE         1010#define _APS_NEXT_SYMED_VALUE           101#endif#endif