开发者

64bit memory address won't fit in Win32(64?)'s API WriteProcessMemory?

开发者 https://www.devze.com 2023-04-12 13:17 出处:网络
Alright, so I\'m trying to do the old hack the calculator tutorial here: http://www.youtube.com/watch?v=I0zPwg4iUDk

Alright, so I'm trying to do the old hack the calculator tutorial here: http://www.youtube.com/watch?v=I0zPwg4iUDk But give it my own spin by adding a form and a button to inject the new value into the calculator. But it keeps spitting out the "can't write to memory" error. Now I dunno why, but I think it's cause my memory address I'm trying to write is from a 64 bit OS. Can anyone tell me why this doesn't work?

#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;
        }
    }
}


Firstly, whenever you have problems with WINAPI, you should use GetLastError to find the * specific* error.

In this case, I'm pretty sure you lack debug privileges, so the OS is denying write permissions, see AdjustTokenPrivilages and this example, you want SE_DEBUG_NAME privilege.

However, it should be noted that you should never use a fixed virtual address (0xA4283C508C in your case), as most programs will be relocated, invalidating your address (either due to ASLR, code page overlaps or jsut a pure lack of a preferred load address)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号