简体   繁体   English

可以进行C ++ / CLI注入吗?

[英]C++/CLI injection is possible?

so I have my C++/CLI application which injects itself into a target process. 所以我有自己的C ++ / CLI应用程序,可将自身注入目标进程。 But while the injection seems succesfull the entry point doesnt get called. 但是,尽管注入似乎成功,但没有调用入口点。 This is my injector: 这是我的注射器:

#include "stdafx.h"
#include <Windows.h>
#include <TlHelp32.h>
#include <string>
#include <stdlib.h>
#include <comdef.h>
#include "Hooking.h"
#include "HCommonEnsureCleanup.h"

VOID Hooking::HookProcess()
{
    DWORD firefox = Hooking::FindProcessId("firefox.exe");
    Inject(firefox);
}
BOOL Hooking::Inject(DWORD pID)
{
    const wchar_t* DLL_NAME = (const wchar_t*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(System::Windows::Forms::Application::ExecutablePath);
    HANDLE Proc;
    HMODULE hLib;
    char buf[50] = { 0 };
    LPVOID RemoteString, LoadLibAddy;
    if (!pID)
        return false;
    Proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);
    if (!Proc)
    {
        return false;
    }
    LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryA");
    RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, (wcslen(DLL_NAME) + 1) * sizeof(wchar_t), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(Proc, (LPVOID)RemoteString, DLL_NAME, (wcslen(DLL_NAME) + 1) * sizeof(wchar_t), NULL);
    CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, (LPVOID)RemoteString, NULL, NULL);
    CloseHandle(Proc);
    return true;
}
DWORD Hooking::FindProcessId(const char *processname)
{
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;
    DWORD result = NULL;
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (INVALID_HANDLE_VALUE == hProcessSnap) return(FALSE);
    pe32.dwSize = sizeof(PROCESSENTRY32);
    if (!Process32First(hProcessSnap, &pe32))
    {
        CloseHandle(hProcessSnap);
        return(NULL);
    }
    do
    {
        if (0 == strcmp(processname, _bstr_t(pe32.szExeFile)))
        {
            result = pe32.th32ProcessID;
            break;
        }
    } while (Process32Next(hProcessSnap, &pe32));
    CloseHandle(hProcessSnap);
    return result;
}

and then(since my program is an executeable) I made a dll entry point: 然后(因为我的程序是可执行的),我做了一个dll入口点:

int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    Hooking::HookProcess();
    return 0;
}
BOOL WINAPI DllMain(
    HINSTANCE hinstDLL,
    DWORD fdwReason,
    LPVOID lpvReserved
)
{
    MessageBoxA(NULL, "Injection OK", "Injection OK", NULL);
}

However, neither of my entry points, either Dll or exe gets called. 但是,我的两个入口点(Dll或exe)都没有被调用。 So I was thinking that this may be because this exe contains .Net code but before changing it all I wanted to ask it here. 所以我在想这可能是因为该exe包含.Net代码,但是在更改它之前,我想在这里提出。

Thank you for your time 感谢您的时间

Firstly, change "(wcslen(DLL_NAME) + 1) * sizeof(wchar_t)" to "wcslen(DLL_NAME)", that will be sufficient. 首先,将“((wcslen(DLL_NAME))+ 1)* sizeof(wchar_t)”更改为“ wcslen(DLL_NAME)”,就足够了。 Make sure to update the memory allocation as well. 确保也更新内存分配。

Secondly, you're passing a unicode-encoded buffer to LoadLibraryA. 其次,您要将一个Unicode编码的缓冲区传递给LoadLibraryA。 That isn't going to work because LoadLibraryA doesn't accept unicode-encoded buffers, it expects an Ascii-encoded buffer. 那是行不通的,因为LoadLibraryA不接受unicode编码的缓冲区,它需要一个Ascii编码的缓冲区。 Use LoadLibraryW instead. 请改用LoadLibraryW。

Thirdly, you aren't doing any error checking. 第三,您不执行任何错误检查。 You cannot just assume that the two virtual memory operations and the remote thread creation operation will be successful, you need to check the return status of each operation to ensure it was successful, otherwise stop pursuing the injection routine. 您不能仅仅假定两个虚拟内存操作和远程线程创建操作将成功,您需要检查每个操作的返回状态以确保成功,否则就停止执行注入例程。 This will also shine insight as to why the operation is failing. 这也将使您了解操作失败的原因。 You need to attach a debugger to the targeted process to ensure that the memory is as it should be after allocating and writing to it, too. 您需要将调试器附加到目标进程,以确保在分配和写入内存后内存也应该是正确的。

Last but not least, make sure you have the sufficient privileges to target the targeted process. 最后但并非最不重要的一点是,确保您具有足够的特权来定位目标进程。 For example, you cannot target a process which is elevated if you're running with standard rights, and you cannot target a traditional security solution which has self-protection enabled. 例如,如果您以标准权限运行,则无法定位提升权限的进程,也无法定位已启用自我保护功能的传统安全解决方案。 There's many different factors here. 这里有很多不同的因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM