CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    83

    Simple dll injection issue

    I followed this tut on injecting a dll file into a running process:

    http://www.codeproject.com/KB/threads/winspy.aspx
    (part II)

    This is the example code used:

    Code:
    HANDLE hThread;
    char    szLibPath[_MAX_PATH];  // The name of our "LibSpy.dll" module
                                   // (including full path!);
    void*   pLibRemote;   // The address (in the remote process) where 
                          // szLibPath will be copied to;
    DWORD   hLibModule;   // Base address of loaded module (==HMODULE);
    HMODULE hKernel32 = ::GetModuleHandle("Kernel32");
    
    // initialize szLibPath
    //...
    
    // 1. Allocate memory in the remote process for szLibPath
    // 2. Write szLibPath to the allocated memory
    pLibRemote = ::VirtualAllocEx( hProcess, NULL, sizeof(szLibPath),
                                   MEM_COMMIT, PAGE_READWRITE );
    ::WriteProcessMemory( hProcess, pLibRemote, (void*)szLibPath,
                          sizeof(szLibPath), NULL );
    
    
    // Load "LibSpy.dll" into the remote process
    // (via CreateRemoteThread & LoadLibrary)
    hThread = ::CreateRemoteThread( hProcess, NULL, 0,
                (LPTHREAD_START_ROUTINE) ::GetProcAddress( hKernel32,
                                           "LoadLibraryA" ),
                 pLibRemote, 0, NULL );
    ::WaitForSingleObject( hThread, INFINITE );
    
    // Get handle of the loaded module
    ::GetExitCodeThread( hThread, &hLibModule );
    
    // Clean up
    ::CloseHandle( hThread );
    ::VirtualFreeEx( hProcess, pLibRemote, sizeof(szLibPath), MEM_RELEASE );

    Makes sense, but how do i "// initialize szLibPath"?

    I have now:

    Code:
    HANDLE hThread;
    char    szLibPath[_MAX_PATH];  // The name of our "LibSpy.dll" module
                                   // (including full path!);
    void*   pLibRemote;   // The address (in the remote process) where 
                          // szLibPath will be copied to;
    DWORD   hLibModule;   // Base address of loaded module (==HMODULE);
    HMODULE hKernel32 = ::GetModuleHandle("Kernel32");
    
    // initialize szLibPath
    //...
    szLibPath = "C:\x.dll";                              <==
    HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS,0,2296);
    
    // 1. Allocate memory in the remote process for szLibPath
    // 2. Write szLibPath to the allocated memory
    pLibRemote = ::VirtualAllocEx( hProcess, NULL, sizeof(szLibPath),
                                   MEM_COMMIT, PAGE_READWRITE );
    ::WriteProcessMemory( hProcess, pLibRemote, (void*)szLibPath,
                          sizeof(szLibPath), NULL );
    
    
    // Load "LibSpy.dll" into the remote process
    // (via CreateRemoteThread & LoadLibrary)
    hThread = ::CreateRemoteThread( hProcess, NULL, 0,
                (LPTHREAD_START_ROUTINE) ::GetProcAddress( hKernel32,
                                           "LoadLibraryA" ),
                 pLibRemote, 0, NULL );
    ::WaitForSingleObject( hThread, INFINITE );
    
    // Get handle of the loaded module
    ::GetExitCodeThread( hThread, &hLibModule );
    
    // Clean up
    ::CloseHandle( hThread );
    ::VirtualFreeEx( hProcess, pLibRemote, sizeof(szLibPath), MEM_RELEASE );
    Note the "szLibPath = "C:\x.dll"; "

    But MSVC++08 complains:
    error C2153: Hex constants have to contain at least one hexadecimal number


    ?

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Simple dll injection issue

    \x tells C++ that the next two characters are hexidecimal characters.

    "Hello\xA9World" = "Hello World"

    \ is an escape character.

  3. #3
    Join Date
    May 2010
    Posts
    83

    Re: Simple dll injection issue

    thx!

    But why would i have to do that in this case?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured