簡體   English   中英

在 C# 中從外部進程中彈出 DLL

[英]Eject DLL From External Process in C#

所以。 我很抱歉要求提供源代碼,但在嘗試制作一段時間后,我失去了想法,並詢問如何從 C# 中的外部進程中彈出 dll。 所以任何幫助將不勝感激。 我嘗試過的一些方法是遠程線程,noping 整個地址。 順便說一句,如果有幫助,這里是我的注入代碼。

public static void Eject(string moduleName)
{
    Process[] ProcessList = System.Diagnostics.Process.GetProcessesByName(gamename);
    if (ProcessList.Length > 0)
    {
        Process MYPROCESS = ProcessList[0];
        IntPtr BaseAddress = IntPtr.Zero;
        foreach (System.Diagnostics.ProcessModule Module in MYPROCESS.Modules)
        {
            if (Module.ModuleName.Contains(moduleName))
                BaseAddress = Module.BaseAddress;
        }
        if (BaseAddress != IntPtr.Zero)
        {
            IntPtr libaddy = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
            CreateRemoteThread(procHandle, IntPtr.Zero, 0, libaddy, BaseAddress, 0, IntPtr.Zero);
        }
    }
}

DLL 彈出有很多注意事項。 如果您安裝了鈎子或代碼正在積極執行,則會導致問題。 如果 DLL 只是坐在那里什么也不做,你就不會有任何問題。

  1. 獲取模塊基地址,這將用作句柄
  2. VirtuaAllocEx 獲取寫入模塊句柄的內存
  3. GetProcAddress 獲取 FreeLibrary 的地址
  4. CreateRemoteThread 調用 FreeLibrary,將句柄地址作為參數傳遞

這段代碼可能並不完美,但應該是 99% 的好代碼:

public static IntPtr GetModuleBaseAddress(Process proc, string modName)
{
    IntPtr addr = IntPtr.Zero;

    foreach (ProcessModule m in proc.Modules)
    {
        if (m.ModuleName == modName)
        {
            addr = m.BaseAddress;
            break;
        }
    }
    return addr;
}

IntPtr moduleAddress = GetModuleBaseAddress(proc, "modname");

IntPtr loc = VirtualAllocEx(proc.Handle, IntPtr.Zero, 4, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ReadWrite);

IntPtr bytesRead = IntPtr.Zero;

bool result = WriteProcessMemory(proc.Handle, loc, moduleAddress.ToInt32(), 4, out bytesRead);

if (!result || bytesRead.Equals(0))
{
    return false;
}

IntPtr freelibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "FreeLibrary");

IntPtr hThread = CreateRemoteThread(proc.Handle, IntPtr.Zero, 0, freelibraryAddr, loc, 0, out _);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM