Skip to main content
se eliminaron 420 caracteres en el cuerpo
Origen Enlace
Sergio Ramos
  • 624
  • 4
  • 12
  • 36
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance,
                   LPSTR lpszCmdParam, 
                   int nCmdShow)
{
//int wmain(int argc, wchar_t* argv[]) {
    char cmd[] = "task.exe" ; // note: non-const (writeable array)
    HANDLE thread = nullptr ;
    auto myProc=Startpausedprocess( cmd, std::addressof(thread) ) ;
    if(myProc)
    {
        std::cout << "press enter to resume process... " && std::cin.get() ;
        ResumeThread(thread) ;

        //CloseHandle(thread) ;
        //CloseHandle(myProc) ;
    }

    assert(argc > 1);

    // build command line

    wchar_t commandLine[MAX_PATH * 2];
    ::lstrcpyW(commandLine, argv[1]);
    if (argc > 2) {
        ::lstrcatW(commandLine, L" ");
        ::lstrcatW(commandLine, argv[2]);
    }

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof(si) };

    // create the actual process with the debug flag to avoid an infinite loop

    BOOL bCreated = ::CreateProcessW(nullptr, commandLine, nullptr, nullptr, FALSE, DEBUG_PROCESS, nullptr, nullptr, &si, &pi);
    if (bCreated) {
        WCHAR path[MAX_PATH];
        ::GetModuleFileName(nullptr, path, MAX_PATH);
        *::wcsrchr(path, L'\\') = L'\0';
        ::wcscat_s(path, MAX_PATH, L"\\Injected.Dll");

        // create a semaphore which count represents the main thread ID

        HANDLE hSemaphore = ::CreateSemaphore(nullptr, pi.dwThreadId - 1, pi.dwThreadId, L"InjectedMainThread");
        assert(hSemaphore);

        // duplicate in the injected process so the semaphore survives after the injected process goes away

        HANDLE hTarget = nullptr;
        ::DuplicateHandle(::GetCurrentProcess(), hSemaphore, pi.hProcess, &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
        assert(hTarget);

        // allocate buffer for the DLL path name

        void* pPathBuffer = ::VirtualAllocEx(pi.hProcess, nullptr, MAX_PATH * sizeof(WCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        assert(pPathBuffer);

        // write the path

        SIZE_T written;
        ::WriteProcessMemory(pi.hProcess, pPathBuffer, path, MAX_PATH * sizeof(WCHAR), &written);

        // create a remote thread to load the DLL

        HANDLE hRemoteThread = ::CreateRemoteThread(pi.hProcess, nullptr, 0,
            (PTHREAD_START_ROUTINE)::GetProcAddress(::GetModuleHandle(L"kernel32"), "LoadLibraryW"),
            pPathBuffer, 0, nullptr);

        // allow the process to continue after this one exits

        ::DebugSetProcessKillOnExit(FALSE);

        // close handles (not really needed as we're existing)

        //::CloseHandle(hRemoteThread);
        //::CloseHandle(pi.hProcess);
        //::CloseHandle(pi.hThread);
        //::CloseHandle(hSemaphore);
    }

    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance,
                   LPSTR lpszCmdParam, 
                   int nCmdShow)
{
//int wmain(int argc, wchar_t* argv[]) {
    char cmd[] = "task.exe" ; // note: non-const (writeable array)
    HANDLE thread = nullptr ;
    auto myProc=Startpausedprocess( cmd, std::addressof(thread) ) ;
    if(myProc)
    {
        std::cout << "press enter to resume process... " && std::cin.get() ;
        ResumeThread(thread) ;

        //CloseHandle(thread) ;
        //CloseHandle(myProc) ;
    }

    assert(argc > 1);

    // build command line

    wchar_t commandLine[MAX_PATH * 2];
    ::lstrcpyW(commandLine, argv[1]);
    if (argc > 2) {
        ::lstrcatW(commandLine, L" ");
        ::lstrcatW(commandLine, argv[2]);
    }

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof(si) };

    // create the actual process with the debug flag to avoid an infinite loop

    BOOL bCreated = ::CreateProcessW(nullptr, commandLine, nullptr, nullptr, FALSE, DEBUG_PROCESS, nullptr, nullptr, &si, &pi);
    if (bCreated) {
        WCHAR path[MAX_PATH];
        ::GetModuleFileName(nullptr, path, MAX_PATH);
        *::wcsrchr(path, L'\\') = L'\0';
        ::wcscat_s(path, MAX_PATH, L"\\Injected.Dll");

        // create a semaphore which count represents the main thread ID

        HANDLE hSemaphore = ::CreateSemaphore(nullptr, pi.dwThreadId - 1, pi.dwThreadId, L"InjectedMainThread");
        assert(hSemaphore);

        // duplicate in the injected process so the semaphore survives after the injected process goes away

        HANDLE hTarget = nullptr;
        ::DuplicateHandle(::GetCurrentProcess(), hSemaphore, pi.hProcess, &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
        assert(hTarget);

        // allocate buffer for the DLL path name

        void* pPathBuffer = ::VirtualAllocEx(pi.hProcess, nullptr, MAX_PATH * sizeof(WCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        assert(pPathBuffer);

        // write the path

        SIZE_T written;
        ::WriteProcessMemory(pi.hProcess, pPathBuffer, path, MAX_PATH * sizeof(WCHAR), &written);

        // create a remote thread to load the DLL

        HANDLE hRemoteThread = ::CreateRemoteThread(pi.hProcess, nullptr, 0,
            (PTHREAD_START_ROUTINE)::GetProcAddress(::GetModuleHandle(L"kernel32"), "LoadLibraryW"),
            pPathBuffer, 0, nullptr);

        // allow the process to continue after this one exits

        ::DebugSetProcessKillOnExit(FALSE);

        // close handles (not really needed as we're existing)

        //::CloseHandle(hRemoteThread);
        //::CloseHandle(pi.hProcess);
        //::CloseHandle(pi.hThread);
        //::CloseHandle(hSemaphore);
    }

    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance,
                   LPSTR lpszCmdParam, 
                   int nCmdShow)
{
//int wmain(int argc, wchar_t* argv[]) {
    assert(argc > 1);

    // build command line

    wchar_t commandLine[MAX_PATH * 2];
    ::lstrcpyW(commandLine, argv[1]);
    if (argc > 2) {
        ::lstrcatW(commandLine, L" ");
        ::lstrcatW(commandLine, argv[2]);
    }

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof(si) };

    // create the actual process with the debug flag to avoid an infinite loop

    BOOL bCreated = ::CreateProcessW(nullptr, commandLine, nullptr, nullptr, FALSE, DEBUG_PROCESS, nullptr, nullptr, &si, &pi);
    if (bCreated) {
        WCHAR path[MAX_PATH];
        ::GetModuleFileName(nullptr, path, MAX_PATH);
        *::wcsrchr(path, L'\\') = L'\0';
        ::wcscat_s(path, MAX_PATH, L"\\Injected.Dll");

        // create a semaphore which count represents the main thread ID

        HANDLE hSemaphore = ::CreateSemaphore(nullptr, pi.dwThreadId - 1, pi.dwThreadId, L"InjectedMainThread");
        assert(hSemaphore);

        // duplicate in the injected process so the semaphore survives after the injected process goes away

        HANDLE hTarget = nullptr;
        ::DuplicateHandle(::GetCurrentProcess(), hSemaphore, pi.hProcess, &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
        assert(hTarget);

        // allocate buffer for the DLL path name

        void* pPathBuffer = ::VirtualAllocEx(pi.hProcess, nullptr, MAX_PATH * sizeof(WCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        assert(pPathBuffer);

        // write the path

        SIZE_T written;
        ::WriteProcessMemory(pi.hProcess, pPathBuffer, path, MAX_PATH * sizeof(WCHAR), &written);

        // create a remote thread to load the DLL

        HANDLE hRemoteThread = ::CreateRemoteThread(pi.hProcess, nullptr, 0,
            (PTHREAD_START_ROUTINE)::GetProcAddress(::GetModuleHandle(L"kernel32"), "LoadLibraryW"),
            pPathBuffer, 0, nullptr);

        // allow the process to continue after this one exits

        ::DebugSetProcessKillOnExit(FALSE);

        // close handles (not really needed as we're existing)

        //::CloseHandle(hRemoteThread);
        //::CloseHandle(pi.hProcess);
        //::CloseHandle(pi.hThread);
        //::CloseHandle(hSemaphore);
    }

    return 0;
}
se eliminaron 3 caracteres en el cuerpo
Origen Enlace
Sergio Ramos
  • 624
  • 4
  • 12
  • 36
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance,
                   LPSTR lpszCmdParam, 
                   int nCmdShow)
{
//int wmain(int argc, wchar_t* argv[]) {
    char cmd[] = "taskmgr"task.exe" ; // note: non-const (writeable array)
    HANDLE thread = nullptr ;
    auto myProc=Startpausedprocess( cmd, std::addressof(thread) ) ;
    if(myProc)
    {
        std::cout << "press enter to resume process... " && std::cin.get() ;
        ResumeThread(thread) ;

        //CloseHandle(thread) ;
        //CloseHandle(myProc) ;
    }

    assert(argc > 1);

    // build command line

    wchar_t commandLine[MAX_PATH * 2];
    ::lstrcpyW(commandLine, argv[1]);
    if (argc > 2) {
        ::lstrcatW(commandLine, L" ");
        ::lstrcatW(commandLine, argv[2]);
    }

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof(si) };

    // create the actual process with the debug flag to avoid an infinite loop

    BOOL bCreated = ::CreateProcessW(nullptr, commandLine, nullptr, nullptr, FALSE, DEBUG_PROCESS, nullptr, nullptr, &si, &pi);
    if (bCreated) {
        WCHAR path[MAX_PATH];
        ::GetModuleFileName(nullptr, path, MAX_PATH);
        *::wcsrchr(path, L'\\') = L'\0';
        ::wcscat_s(path, MAX_PATH, L"\\Injected.Dll");

        // create a semaphore which count represents the main thread ID

        HANDLE hSemaphore = ::CreateSemaphore(nullptr, pi.dwThreadId - 1, pi.dwThreadId, L"InjectedMainThread");
        assert(hSemaphore);

        // duplicate in the injected process so the semaphore survives after the injected process goes away

        HANDLE hTarget = nullptr;
        ::DuplicateHandle(::GetCurrentProcess(), hSemaphore, pi.hProcess, &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
        assert(hTarget);

        // allocate buffer for the DLL path name

        void* pPathBuffer = ::VirtualAllocEx(pi.hProcess, nullptr, MAX_PATH * sizeof(WCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        assert(pPathBuffer);

        // write the path

        SIZE_T written;
        ::WriteProcessMemory(pi.hProcess, pPathBuffer, path, MAX_PATH * sizeof(WCHAR), &written);

        // create a remote thread to load the DLL

        HANDLE hRemoteThread = ::CreateRemoteThread(pi.hProcess, nullptr, 0,
            (PTHREAD_START_ROUTINE)::GetProcAddress(::GetModuleHandle(L"kernel32"), "LoadLibraryW"),
            pPathBuffer, 0, nullptr);

        // allow the process to continue after this one exits

        ::DebugSetProcessKillOnExit(FALSE);

        // close handles (not really needed as we're existing)

        //::CloseHandle(hRemoteThread);
        //::CloseHandle(pi.hProcess);
        //::CloseHandle(pi.hThread);
        //::CloseHandle(hSemaphore);
    }

    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance,
                   LPSTR lpszCmdParam, 
                   int nCmdShow)
{
//int wmain(int argc, wchar_t* argv[]) {
    char cmd[] = "taskmgr.exe" ; // note: non-const (writeable array)
    HANDLE thread = nullptr ;
    auto myProc=Startpausedprocess( cmd, std::addressof(thread) ) ;
    if(myProc)
    {
        std::cout << "press enter to resume process... " && std::cin.get() ;
        ResumeThread(thread) ;

        //CloseHandle(thread) ;
        //CloseHandle(myProc) ;
    }

    assert(argc > 1);

    // build command line

    wchar_t commandLine[MAX_PATH * 2];
    ::lstrcpyW(commandLine, argv[1]);
    if (argc > 2) {
        ::lstrcatW(commandLine, L" ");
        ::lstrcatW(commandLine, argv[2]);
    }

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof(si) };

    // create the actual process with the debug flag to avoid an infinite loop

    BOOL bCreated = ::CreateProcessW(nullptr, commandLine, nullptr, nullptr, FALSE, DEBUG_PROCESS, nullptr, nullptr, &si, &pi);
    if (bCreated) {
        WCHAR path[MAX_PATH];
        ::GetModuleFileName(nullptr, path, MAX_PATH);
        *::wcsrchr(path, L'\\') = L'\0';
        ::wcscat_s(path, MAX_PATH, L"\\Injected.Dll");

        // create a semaphore which count represents the main thread ID

        HANDLE hSemaphore = ::CreateSemaphore(nullptr, pi.dwThreadId - 1, pi.dwThreadId, L"InjectedMainThread");
        assert(hSemaphore);

        // duplicate in the injected process so the semaphore survives after the injected process goes away

        HANDLE hTarget = nullptr;
        ::DuplicateHandle(::GetCurrentProcess(), hSemaphore, pi.hProcess, &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
        assert(hTarget);

        // allocate buffer for the DLL path name

        void* pPathBuffer = ::VirtualAllocEx(pi.hProcess, nullptr, MAX_PATH * sizeof(WCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        assert(pPathBuffer);

        // write the path

        SIZE_T written;
        ::WriteProcessMemory(pi.hProcess, pPathBuffer, path, MAX_PATH * sizeof(WCHAR), &written);

        // create a remote thread to load the DLL

        HANDLE hRemoteThread = ::CreateRemoteThread(pi.hProcess, nullptr, 0,
            (PTHREAD_START_ROUTINE)::GetProcAddress(::GetModuleHandle(L"kernel32"), "LoadLibraryW"),
            pPathBuffer, 0, nullptr);

        // allow the process to continue after this one exits

        ::DebugSetProcessKillOnExit(FALSE);

        // close handles (not really needed as we're existing)

        //::CloseHandle(hRemoteThread);
        //::CloseHandle(pi.hProcess);
        //::CloseHandle(pi.hThread);
        //::CloseHandle(hSemaphore);
    }

    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance,
                   LPSTR lpszCmdParam, 
                   int nCmdShow)
{
//int wmain(int argc, wchar_t* argv[]) {
    char cmd[] = "task.exe" ; // note: non-const (writeable array)
    HANDLE thread = nullptr ;
    auto myProc=Startpausedprocess( cmd, std::addressof(thread) ) ;
    if(myProc)
    {
        std::cout << "press enter to resume process... " && std::cin.get() ;
        ResumeThread(thread) ;

        //CloseHandle(thread) ;
        //CloseHandle(myProc) ;
    }

    assert(argc > 1);

    // build command line

    wchar_t commandLine[MAX_PATH * 2];
    ::lstrcpyW(commandLine, argv[1]);
    if (argc > 2) {
        ::lstrcatW(commandLine, L" ");
        ::lstrcatW(commandLine, argv[2]);
    }

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof(si) };

    // create the actual process with the debug flag to avoid an infinite loop

    BOOL bCreated = ::CreateProcessW(nullptr, commandLine, nullptr, nullptr, FALSE, DEBUG_PROCESS, nullptr, nullptr, &si, &pi);
    if (bCreated) {
        WCHAR path[MAX_PATH];
        ::GetModuleFileName(nullptr, path, MAX_PATH);
        *::wcsrchr(path, L'\\') = L'\0';
        ::wcscat_s(path, MAX_PATH, L"\\Injected.Dll");

        // create a semaphore which count represents the main thread ID

        HANDLE hSemaphore = ::CreateSemaphore(nullptr, pi.dwThreadId - 1, pi.dwThreadId, L"InjectedMainThread");
        assert(hSemaphore);

        // duplicate in the injected process so the semaphore survives after the injected process goes away

        HANDLE hTarget = nullptr;
        ::DuplicateHandle(::GetCurrentProcess(), hSemaphore, pi.hProcess, &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
        assert(hTarget);

        // allocate buffer for the DLL path name

        void* pPathBuffer = ::VirtualAllocEx(pi.hProcess, nullptr, MAX_PATH * sizeof(WCHAR), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        assert(pPathBuffer);

        // write the path

        SIZE_T written;
        ::WriteProcessMemory(pi.hProcess, pPathBuffer, path, MAX_PATH * sizeof(WCHAR), &written);

        // create a remote thread to load the DLL

        HANDLE hRemoteThread = ::CreateRemoteThread(pi.hProcess, nullptr, 0,
            (PTHREAD_START_ROUTINE)::GetProcAddress(::GetModuleHandle(L"kernel32"), "LoadLibraryW"),
            pPathBuffer, 0, nullptr);

        // allow the process to continue after this one exits

        ::DebugSetProcessKillOnExit(FALSE);

        // close handles (not really needed as we're existing)

        //::CloseHandle(hRemoteThread);
        //::CloseHandle(pi.hProcess);
        //::CloseHandle(pi.hThread);
        //::CloseHandle(hSemaphore);
    }

    return 0;
}
se eliminaron 5 caracteres en el cuerpo; etiquetas editadas
Origen Enlace
Ivan Botero
  • 6.8k
  • 10
  • 36
  • 57

Hola estoyEstoy modificando un pequeño el cual funciona solamente por consola y requiere de argumentos de consola. Pero ahora quería pasarlo a winmain(formulario) y obtener esos argumentos usando GetCommandLine y usarlos en un WinMain tal como hice en consola. El programa es el siguiente:

Hola estoy modificando un pequeño el cual funciona solamente por consola y requiere de argumentos de consola. Pero ahora quería pasarlo a winmain(formulario) y obtener esos argumentos usando GetCommandLine y usarlos en un WinMain tal como hice en consola. El programa es el siguiente:

Estoy modificando un pequeño el cual funciona solamente por consola y requiere de argumentos de consola. Pero ahora quería pasarlo a winmain(formulario) y obtener esos argumentos usando GetCommandLine y usarlos en un WinMain tal como hice en consola. El programa es el siguiente:

título editado
Enlace
Sergio Ramos
  • 624
  • 4
  • 12
  • 36
Loading
se añadieron 28 caracteres en el cuerpo
Origen Enlace
Sergio Ramos
  • 624
  • 4
  • 12
  • 36
Loading
se añadieron 4532 caracteres en el cuerpo
Origen Enlace
Sergio Ramos
  • 624
  • 4
  • 12
  • 36
Loading
Origen Enlace
Sergio Ramos
  • 624
  • 4
  • 12
  • 36
Loading