【问题标题】:Injecting c++ dll into an exe using c#使用 c# 将 c++ dll 注入到 exe 中
【发布时间】:2016-04-22 06:36:26
【问题描述】:

为什么我的 C# 代码没有将 dll 注入 exe,但程序显示消息框“已注入!” ? 它自己的 .dll 是用 c++ 编码的,而 exe 是用 C++ 编码的 我正在尝试注入我的 C# 代码,它是如何不起作用的? 这是我的注射器方法

[DllImport("kernel32")]
public static extern IntPtr CreateRemoteThread(
  IntPtr hProcess,
  IntPtr lpThreadAttributes,
  uint dwStackSize,
  UIntPtr lpStartAddress, // raw Pointer into remote process
  IntPtr lpParameter,
  uint dwCreationFlags,
  out IntPtr lpThreadId
);

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(
    UInt32 dwDesiredAccess,
    Int32 bInheritHandle,
    Int32 dwProcessId
    );

[DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(
IntPtr hObject
);

[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool VirtualFreeEx(
    IntPtr hProcess,
    IntPtr lpAddress,
    UIntPtr dwSize,
    uint dwFreeType
    );

[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern UIntPtr GetProcAddress(
    IntPtr hModule,
    string procName
    );

[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAllocEx(
    IntPtr hProcess,
    IntPtr lpAddress,
    uint dwSize,
    uint flAllocationType,
    uint flProtect
    );

[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(
    IntPtr hProcess,
    IntPtr lpBaseAddress,
    string lpBuffer,
    UIntPtr nSize,
    out IntPtr lpNumberOfBytesWritten
);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(
    string lpModuleName
    );

[DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
internal static extern Int32 WaitForSingleObject(
    IntPtr handle,
    Int32 milliseconds
    );

public Int32 GetProcessId(String proc)
{
    Process[] ProcList;
    ProcList = Process.GetProcessesByName(proc);
    return ProcList[0].Id;
}

public void InjectDLL(IntPtr hProcess, String strDLLName)
{
    IntPtr bytesout;

    // Length of string containing the DLL file name +1 byte padding
    Int32 LenWrite = strDLLName.Length + 1;
    // Allocate memory within the virtual address space of the target process
    IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40); //allocation pour WriteProcessMemory

    // Write DLL file name to allocated memory in target process
    WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
    // Function pointer "Injector"
    UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    if (Injector == null)
    {
        MessageBox.Show(" Injector Error! \n ");
        // return failed
        return;
    }

    // Create thread in target process, and store handle in hThread
    IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
    // Make sure thread handle is valid
    if (hThread == null)
    {
        //incorrect thread handle ... return failed
        MessageBox.Show(" hThread [ 1 ] Error! \n ");
        return;
    }
    // Time-out is 10 seconds...
    int Result = WaitForSingleObject(hThread, 10 * 1000);
    // Check whether thread timed out...
    if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
    {
        /* Thread timed out... */
        MessageBox.Show(" hThread [ 2 ] Error! \n ");
        // Make sure thread handle is valid before closing... prevents crashes.
        if (hThread != null)
        {
            //Close thread in target process
            CloseHandle(hThread);
        }
        return;
    }
    // Sleep thread for 1 second
    Thread.Sleep(1000);
    // Clear up allocated space ( Allocmem )
    VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
    // Make sure thread handle is valid before closing... prevents crashes.
    if (hThread != null)
    {
        //Close thread in target process
        CloseHandle(hThread);
    }
    // return succeeded
    return;
}

然后我尝试运行一些程序并用我的 dll 注入它

private void metroButton2_Click(object sender, EventArgs e)
{
    String strDLLName = @"spd.dll";
    String strProcessName = "app";
    System.Diagnostics.Process.Start("app.exe", "!#@$$$!");                                   
    Int32 ProcID = GetProcessId(strProcessName);
    if (ProcID >= 0)
    {
        IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
        if (hProcess == null)
        {
            MessageBox.Show("OpenProcess() Failed!");
            return;
        }
        else
        {
            InjectDLL(hProcess, strDLLName);
            MessageBox.Show("Injected!");  
        }

    }

}

它显示输出:“注入!”但是在 .exe 上 .dll 没有注入 我应该怎么办 ?在 Inject 之前/在运行 .exe 之后给予更多 Thread.Sleep ? 任何帮助将不胜感激!

【问题讨论】:

  • 嘿,Bayu Anggara,这是非常有用的代码,伙计!您介意将工作版本发送到 fischermartin91@gmail.com 吗?我最近一直在找这个。
  • @bytecode77 嘿,字节!很高兴听你这样说!上面的代码是工作版本,对于更新,我只是回答这个问题,你可以在下面看我的回答:)
  • 谢谢。我现在也让它工作了。我将尝试构建一个rootkit。让我们看看我能把这个想法带到多远:)

标签: c# .net dll


【解决方案1】:

请注意C++'sNULL(0) 与C#'s null 不同。您正在寻找的等价物是 IntPtr.Zero


GetProcAddress函数为例:

返回值

如果函数成功,则返回值是导出的函数或变量的地址。

如果函数失败,返回值为NULL。要获取扩展的错误信息,请调用 GetLastError。

来源:https://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx

NULL 这里是一个 c++ 宏,定义为:

#define NULL 0

null 不等于IntPtr.Zero,但(IntPtr)null 等于IntPtr.Zero

【讨论】:

  • 您好,感谢您的评论,所以我需要将 (IntPtr)null 更改为 InPtr.Zero ?
  • 好的,我明白了, (IntPtr)null 与 IntPtr.Zero 不同,所以我需要在我的注射器方法上将 (IntPtr)null 更改为 InPtr.Zero 吗?请更详细:)
  • 对不起,我之前的评论有误。请参阅我更新的答案以进行更正。
【解决方案2】:

我知道这是可耻的,我的许多 stackoverflow 问题都是我自己回答的, 这个想法总是来晚了(在我发布问题之后) 所以这就是答案 上面的喷油器工作正常,为什么喷油器不喷? 是的,这就像我之前的想法,注入器没有成功注入 dll,因为我需要在启动应用程序之后和注入 .dll 之前给 Thread.Sleep(1000),然后使用 Worker,就像这样:

void worker_DoWork2(object sender, DoWorkEventArgs e)
        {
            System.Diagnostics.Process.Start("app.exe", "!#@$$$!");

        }

public void metroButton2_Click(object sender, EventArgs e)
        {
            var worker2 = new BackgroundWorker();
            worker2.DoWork += new DoWorkEventHandler(worker_DoWork2);
            worker2.RunWorkerAsync();

            Thread.Sleep(1000);

            String strDLLName = "spd.dll";
            String strProcessName = "app";    
            Int32 ProcID = GetProcessId(strProcessName);
            if (ProcID >= 0)
            {
                IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
                if (hProcess == null)
                {
                    return;
                }
                else
                {
                    InjectDLL(hProcess, strDLLName);
                }  
            }
            Application.Exit();
        }

现在注入器成功运行,我需要以管理员权限运行此应用程序。 谢谢!

【讨论】:

  • 嘿伙计,我知道为时已晚,但我需要你的帮助。我用 C# 制作了一个 Windows 窗体应用程序,并使用 AutoFrost 和 GrayFrost 项目创建了 DLL,它引导 .NET 运行时并在注入时启动 C# 应用程序。当我在(例如)C++ 虚拟项目中手动导入生成的 Loader.DLL 时,它工作正常。但是,当我将 DLL 注入正在运行的进程中时,它没有任何效果:(你现在有一个工作示例吗?
  • @user218046 嗨对不起,我不这么认为,因为目前我的应用程序在运行时没有注入dll,但是我认为如果你想将DLL注入到正在运行的进程中,你需要学习dll钩。你可以谷歌 Hook dll 进入正在运行的进程。希望对您有所帮助:)
猜你喜欢
  • 1970-01-01
  • 2011-10-18
  • 2013-08-03
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多