VirtualAlloc(), bringt nur den Zugriff auf eine unzulässige addresse. wieso?



  • hi, es waer toll wenn ich euch mal diesen code anschauen wuerded, speziel die aller letzte funktion.

    /*******************************************************************************
    
        Author  : Tobias Stein
        Date    : 9th July '07
        Project : Kill PAGE_GUARD
        File    : KillPG2.cpp
        Remark  : Many memory addresses will protected by PAGE_GUARD, if you want to
                  manipulate or read an address, PAGE_GUARD cause an exception, so
                  you got no access.
                  This application should turn off the fucking PAGE_GUARD.
    
        All Rights Reserved!
    
    *******************************************************************************/
    
    //--- Includes -----------------------------------------------------------------
    
    #include <windows.h>
    #include <fstream.h>
    #include <commctrl.h>
    
    //--- Globals ------------------------------------------------------------------
    
    #define ID_BUTTON_TURNOFF               1000
    #define ID_PROGRESSBAR_STATUS           1001
    
    char szAppName[ ] = "Kick the fucking PAGE_GUARD";
    char szAppTitle[ ] = "Turn off the fucking PAGE_GUARD";
    
    //--- Prototype ----------------------------------------------------------------
    
    LRESULT CALLBACK WinProc( HWND, UINT, WPARAM, LPARAM );
    void lasterror( );
    void EnableDebugPrivilege( );
    bool turnItOff( HWND );
    
    //--- Main ---------------------------------------------------------------------
    
    WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
    
        HWND hWnd;
        WNDCLASS wc;
        MSG msg;
    
        wc.style = CS_VREDRAW | CS_HREDRAW;
        wc.lpfnWndProc = WinProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
        wc.hCursor = LoadCursor( 0, IDC_ARROW );
        wc.hbrBackground = reinterpret_cast< HBRUSH >( GetStockObject( BLACK_BRUSH ) );
        wc.lpszMenuName = 0;
        wc.lpszClassName = szAppName;
    
        if( !( RegisterClass( &wc ) ) ) {
    
            MessageBox( 0, "Couldn't register window by windows!", "Shutdown error!", MB_OK | MB_ICONERROR );
            return 0;
        }
    
        hWnd = CreateWindowEx( 0,
                               szAppName, szAppTitle,
                               WS_SYSMENU | WS_MINIMIZEBOX,
                               CW_USEDEFAULT, CW_USEDEFAULT, 320, 220,
                               0,
                               0,
                               hInstance,
                               0 );
    
        if( !hWnd ) {
    
            MessageBox( 0, "Couldn't create the window!", "Shutdown error!", MB_OK | MB_ICONERROR );
            return 0;
        }
    
        ShowWindow( hWnd, nCmdShow );
        UpdateWindow( hWnd );
    
        while( GetMessage( &msg, 0, 0, 0 ) ) {
    
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    
        return( msg.wParam );
    }
    
    //--- Windowprocedur -----------------------------------------------------------
    
    LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
    
        HINSTANCE hInst;
        static RECT wndRect;
        static HWND hButton_to;
        static HWND hProgressBar;
    
        switch( message ) {
    
            case WM_CREATE: {
    
                hInst = ( (LPCREATESTRUCT)lParam )->hInstance;
                InitCommonControls( );
    
                hButton_to = CreateWindowEx( 0, "BUTTON", "KickIT", WS_CHILD | WS_VISIBLE,
                                             107, 110, 100, 30, hWnd, (HMENU)ID_BUTTON_TURNOFF,
                                             hInst, 0 );
    
                hProgressBar = CreateWindowEx( WS_EX_CLIENTEDGE, PROGRESS_CLASS, 0,
                                               WS_CHILD | WS_VISIBLE | WS_BORDER,
                                               0, 0, 0, 0, hWnd, (HMENU)ID_PROGRESSBAR_STATUS,
                                               hInst, 0 );
    
                SendMessage( hProgressBar, PBM_SETRANGE32, 0, 524256 );
                SendMessage( hProgressBar, PBM_SETSTEP, 1, 0 );
                return 0;
            }
    
            case WM_SIZE: {
    
                GetClientRect( hWnd, &wndRect );
                MoveWindow( hProgressBar, wndRect.left, wndRect.bottom - 20, wndRect.right, 20, true );
                return 0;
            }
    
            case WM_COMMAND: {
    
                switch( LOWORD( wParam ) ) {
    
                    case ID_BUTTON_TURNOFF: {
    
                        switch( HIWORD( wParam ) ) {
    
                            case BN_CLICKED: {
    
                                turnItOff( hProgressBar );
                                break;
                            }
                        }
                        break;
                    }
                }
                return 0;
            }
    
            case WM_DESTROY: {
    
                PostQuitMessage( 0 );
                return 0;
            }
        }
        return( DefWindowProc( hWnd, message, wParam, lParam ) );
    }
    
    //--- Definitions --------------------------------------------------------------
    
    void lasterror( ) {
    
        LPVOID lpMsgBuf;
    
        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                       0,
                       GetLastError(),
                       MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
                       (LPTSTR) &lpMsgBuf,
                       0,
                       NULL );
    
        // Display the string.
        MessageBox( NULL, (char*)lpMsgBuf, "GetLastError", MB_OK | MB_ICONINFORMATION );
        // Free the buffer.
        LocalFree( lpMsgBuf );
    }
    
    void EnableDebugPrivilege( ) {
    
        TOKEN_PRIVILEGES priv;
    	HANDLE hThis, hToken;
    	LUID luid;
    
    	hThis = GetCurrentProcess();
    
    	OpenProcessToken( hThis, TOKEN_ADJUST_PRIVILEGES, &hToken );
    
    	LookupPrivilegeValue( 0, "seDebugPrivilege", &luid );
    
    	priv.PrivilegeCount = 1;
    	priv.Privileges[ 0 ].Luid = luid;
    	priv.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED;
    
    	AdjustTokenPrivileges( hToken, false, &priv, 0, 0, 0 );
    
    	CloseHandle( hToken );
    	CloseHandle( hThis );
    }
    
    bool turnItOff( HWND hProgressBar ) {
    
        SYSTEM_INFO si;
        MEMORY_BASIC_INFORMATION mbi;
        DWORD dwOldProtect;
        LPVOID lpAddress;
        DWORD dwCurrentPage;
    
        EnableDebugPrivilege( );
        memset( &si, 0, sizeof( SYSTEM_INFO ) );
        memset( &mbi, 0, sizeof( MEMORY_BASIC_INFORMATION ) );
    
        GetSystemInfo( &si );
        dwCurrentPage = (DWORD)si.lpMinimumApplicationAddress;
    
       do {
    
            VirtualQuery( (LPVOID)dwCurrentPage, &mbi, sizeof( MEMORY_BASIC_INFORMATION ) );
            lpAddress = VirtualAlloc( mbi.BaseAddress, mbi.RegionSize, MEM_COMMIT, PAGE_READWRITE );
    
            if( !( VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_READWRITE, &dwOldProtect ) ) ) {
    
                lasterror( );
                return false;
            }
    
            VirtualFree( lpAddress, mbi.RegionSize, MEM_DECOMMIT );
            lpAddress = 0;
            SendMessage( hProgressBar, PBM_STEPIT, 0, 0 );
            dwCurrentPage += ( si.dwPageSize );
    
        } while( dwCurrentPage < (DWORD)si.lpMaximumApplicationAddress );
    
        return true;
    }
    
    //--- EXIT ---------------------------------------------------------------------
    

    ich will doch nur den speicher durchlaufen und alles speicherpages auf read/write setzen... wieso aber gibt er mir immer nur nen error wieder?

    Gruß Tobi.



  • Eventuell das Ziel nochmal überdenken.

    Alle Seiten auf read/write gesetzt bedeutet auch, dass kein Code ausgeführt wird / werden kann.

    *edit* Nachtrag :

    Wenn eine Seite nicht gelesen werden kann, heisst es nicht unbedingt, dass sie durch PAGE_GUARD geschützt ist.



  • Alle Seiten auf read/write gesetzt bedeutet auch, dass kein Code ausgeführt wird / werden kann.

    versteh ich jetzt net? abber soll das net gerade das bewirken das man vollen zugriff auf alle addressen hat?
    meinst du ich sollte noch vor alloc mit mbi.State auf free prüfen und schaun ob überhaup PG hat? und fals net continue machen?



  • mir ist mal aufgefallen das ich irgendwie nur die aller erste seite allocieren kann( also 0x00010000 - 0x00011000 ), wenn ueberhaupt, getlasterror meint immer "Unbekannte prozedur" oder so in der art. jetzt wollte ich mal versuchen die naechste seite( also 0x00011000 - 0x00012000 ) zu allozieren, aber da kam sofort wieder die Meldung, dass es ein Zugriff auf eine unzulässige Addresse ist. hm wieso denn ? -.-

    Gruß Tobi.


  • Mod

    Mal ganz grundsätzlich: Was bezweckst Du mit diesem Code?
    Was meinst Du mit "fucking" PAGE_GUARD?

    Ist Dir eigentlich klar, das:
    1. Page guards haben nichts mit geschützen Seiten zu tun (siehe Anmerkung von merker)
    2. Page Guards notwendig sind damit Dein Stack wachsen kann.
    3. Read only Seiten Deinen Exe Code schützen.
    4. Daten geschützt mit einem Read only Attribute auf der Seite damit diese wirklich konstant sind?


Anmelden zum Antworten