2011-10-13 81 views
0

好的,所以我試圖做老黑客計算器教程在這裏: http://www.youtube.com/watch?v=I0zPwg4iUDk 但通過添加一個表單和一個按鈕,以注入新的價值到計算器給我自己的旋轉。但它不斷吐出「無法寫入內存」的錯誤。現在我不知道爲什麼,但我認爲這是因爲我想寫的內存地址是來自64位操作系統。誰能告訴我爲什麼這不起作用?64位內存地址不適合Win32(64?)的API WriteProcessMemory?

#include <iostream> 
#include <windows.h> 

#define IDBUTTON 102 

//prototypes 
void injectValue(); 

using namespace std; 

/* Declare Windows procedure */ 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); 

/* Make the class name into a global variable */ 
char szClassName[ ] = "CodeBlocksWindowsApp"; 
HINSTANCE g_hInst; 
int newValue = 500; 

int WINAPI WinMain (HINSTANCE hThisInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR lpszArgument, 
        int nCmdShow) 
{ 
    HWND hwnd;    /* This is the handle for our window */ 
    MSG messages;   /* Here messages to the application are saved */ 
    WNDCLASSEX wincl;  /* Data structure for the windowclass */ 

    /* The Window structure */ 
    g_hInst = hThisInstance; 
    wincl.hInstance = hThisInstance; 
    wincl.lpszClassName = szClassName; 
    wincl.lpfnWndProc = WindowProcedure;  /* This function is called by windows */ 
    wincl.style = CS_DBLCLKS;     /* Catch double-clicks */ 
    wincl.cbSize = sizeof (WNDCLASSEX); 

    /* Use default icon and mouse-pointer */ 
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); 
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); 
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW); 
    wincl.lpszMenuName = NULL;     /* No menu */ 
    wincl.cbClsExtra = 0;      /* No extra bytes after the window class */ 
    wincl.cbWndExtra = 0;      /* structure or the window instance */ 
    /* Use Windows's default colour as the background of the window */ 
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; 

    /* Register the window class, and if it fails quit the program */ 
    if (!RegisterClassEx (&wincl)) 
     return 0; 

    /* The class is registered, let's create the program*/ 
    hwnd = CreateWindowEx (
      0,     /* Extended possibilites for variation */ 
      szClassName,   /* Classname */ 
      "Calculator Trainer",  /* Title Text */ 
      WS_OVERLAPPEDWINDOW, /* default window */ 
      CW_USEDEFAULT,  /* Windows decides the position */ 
      CW_USEDEFAULT,  /* where the window ends up on the screen */ 
      544,     /* The programs width */ 
      375,     /* and height in pixels */ 
      HWND_DESKTOP,  /* The window is a child-window to desktop */ 
      NULL,    /* No menu */ 
      hThisInstance,  /* Program Instance handler */ 
      NULL     /* No Window Creation data */ 
      ); 


    /* Make the window visible on the screen */ 
    ShowWindow (hwnd, nCmdShow); 

    /* Run the message loop. It will run until GetMessage() returns 0 */ 
    while (GetMessage (&messages, NULL, 0, 0)) 
    { 
     /* Translate virtual-key messages into character messages */ 
     TranslateMessage(&messages); 
     /* Send message to WindowProcedure */ 
     DispatchMessage(&messages); 
    } 

    /* The program return-value is 0 - The value that PostQuitMessage() gave */ 
    return messages.wParam; 
} 


/* This function is called by the Windows function DispatchMessage() */ 

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    HWND hwndButton; 
    switch (message)     /* handle the messages */ 
    { 
     case WM_COMMAND: 
      if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)){ 
      switch(LOWORD(wParam)){ 
       case IDBUTTON:{ 
        injectValue(); 
        break; 
       } 
       default: 
        break; 
      } 
      } 
      break; 


     case WM_CREATE: 
      hwndButton = CreateWindowEx(0,     /* more or ''extended'' styles */ 
        TEXT("BUTTON"),       /* GUI ''class'' to create */ 
        TEXT("Inject Value"),      /* GUI caption */ 
        WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, /* control styles separated by | */ 
        10,          /* LEFT POSITION (Position from left) */ 
        10,          /* TOP POSITION (Position from Top) */ 
        200,         /* WIDTH OF CONTROL */ 
        30,          /* HEIGHT OF CONTROL */ 
        hwnd,         /* Parent window handle */ 
        (HMENU)IDBUTTON,      /* control''s ID for WM_COMMAND */ 
        g_hInst,        /* application instance */ 
        NULL); 
      break; 

     case WM_DESTROY: 
      PostQuitMessage (0);  /* send a WM_QUIT to the message queue */ 
      break; 

     default:      /* for messages that we don't deal with */ 
      return DefWindowProc (hwnd, message, wParam, lParam); 
    } 

    return 0; 
} 

void injectValue(){ 
    cout << "button pushed" << endl; 

    HWND chwnd = FindWindow(0, "Calculator"); 
    if(chwnd == 0) 
     cerr << "HWND not found!" << endl; 

    else{ 
     DWORD pID; 
     GetWindowThreadProcessId(chwnd, &pID); 
     HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID); 

     if(!hProc) 
      cerr << "Can't open hProc!" << endl; 

     else{ 
      int success = WriteProcessMemory(hProc, (LPVOID) 0xA4283C508C, &newValue, (DWORD_PTR) sizeof(newValue), NULL); 

      if(success > 0) 
       cout << "wrote to memory" << endl; 

      else 
       cerr << "Can't write to memory" << endl; 
     } 
    } 
} 
+0

確定0xA4283C508C是否正確?也許首先檢查你是否可以讀取它,然後寫一些新的東西。如果你想寫入64位程序,那麼我認爲你應該在64位模式下編譯你的應用程序,以便你可以在大指針上進行操作。 – Zuljin

+0

是的,地址是正確的。此外我使用code :: blocks。我如何編譯我的程序在64位模式? – CyanPrime

+0

您需要具有64位mingw或64位版本的Visual Studio(例如Windwos SDK 7.1)。 如果你的計算器是64位版本這個地址,如果肯定錯了。 本示例中使用的CheatEngine正在使用32位版本的計算器,因此指針長度爲4個字節,而對於64位操作系統,您需要查找長度爲8個字節的指針。我不確定CheatEngine是否適用於64位應用程序,因此您很難找到這個指針。 – Zuljin

回答

0

首先,只要你有WINAPI的問題,您應該使用GetLastError找到*具體*錯誤。

在這種情況下,我敢肯定你沒有調試特權,所以OS否認寫入權限,請參閱AdjustTokenPrivilagesthis例如,你想SE_DEBUG_NAME特權。

然而,應該指出的是,你不應該使用(你的情況0xA4283C508C)固定的虛擬地址,因爲所有的程序將搬遷, 無效地址(由於ASLR,代碼頁重疊或者僅僅指剛純缺少首選的加載地址)

+0

您只需要調試權限即可打開在不同用戶下運行的進程的句柄。 – pezcode

+0

@pezcode:不,你需要它來打開需要管理或調試權限的程序(或者其他你沒有權限訪問的程序)的句柄,這就是爲什麼ring3調試器需要在管理模式下運行,所以他們可以訪問保護的進程內存。也許你應該閱讀訪問控制:http://msdn.microsoft.com/en-us/library/windows/desktop/aa374860(v=vs.85).aspx – Necrolis

+0

ring3調試器在非管理員帳戶上工作就好了,只要您不嘗試附加到另一個用戶(包括SYSTEM)的進程。如果您自己瀏覽了該鏈接,則會發現此頁面:http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx另一個: http://support.microsoft.com/kb/131065/en-us – pezcode