没有C / C ++或汇编的托管Dll注入

时间:2015-10-23 15:30:50

标签: c# vb.net dll-injection codecave

如何使用VB / C#在远程进程中注入托管dll,而无需使用任何C / C ++ bootstrap dll或任何用汇编编写的代码洞。

1 个答案:

答案 0 :(得分:1)

导出函数作为本机代码

需要Dll导出

经典机制:

以下是经典dll注射的程序:

  • 创建C / C ++ Dll
  • 将Dll路径写入远程进程
  • 创建远程线程到LoadLibraryA以及参数为Dll Path
  • 将在此阶段调用Dll入口点

参考:Code project article

Codecave方法:

使用此方法,您可以跳过C / C ++ Dll,但需要具备Assembly

的基本知识
  • 在运行时创建代码洞作为字节数组并写入其他进程,或编写汇编程序(如函数)并将其编译为二进制代码,这将加载.net程序集
  • 将您的代码写入其他流程
  • 创建远程线程,可以加载.net程序集

参考:Code with example [原始链接似乎已过期,因此谷歌缓存版本]

现代方式:

这种方法易于使用,不需要C / C ++或汇编知识,以下是程序

  • 在当前进程中加载​​您的库并获取您想要调用的过程地址,它将使用带有一个参数的过程
  • 使用LoadLibrary和参数作为托管dll路径,在目标进程中调用create remote thread。这不会执行您的代码,而只是在目标进程中加载​​您的库
  • 等待线程退出然后获取返回代码,这是您的库模块句柄
  • 现在在远程进程的程序地址创建远程线程,完成后,将调用您的程序。

示例:

这是你的dll代码

Public Module Library

    <DllExport>
    Public Function Entry(Argument As String)
        MessageBox.Show("Injected With Argument: " + Argument)
        Return 0 'Success
    End Function

End Module

下面是示例注入代码,它只是原型,TODO:实现本机函数并将它们用于下面使用的扩展方法

Public Module Program

    Public Sub Inject(Proc As Process, dll As String)
        Dim K32 = GetModuleHandle("kernel32")
        Dim LLA_Proc = GetProcAddress(K32, "LoadLibraryA")
        'TODO: extension method of process WriteMemory(Byte())
        Dim lns = Proc.WriteMemory(Encoding.ASCII.GetBytes("C:\FAKE-PATH\Inject.dll"))
        'TODO: extension method of process RemoteCallWait(IntPtr, Arg)
        Dim z = Proc.RemoteCallWait(LLA_Proc, lns)  'Calls method and waits for exit and returns exit code
        'Z should not be zero, otherwise injection is incomplete

        Dim XPTR = GetPtr("C:\FAKE-PATH\Inject.dll", "Entry")
        ''TODO: extension method of process WriteMemory(Byte())
        Dim Loc = Proc.WriteMemory(Encoding.Default.GetBytes("hello world"))
        'TODO: extension method of process RemoteCallWait(IntPtr, Arg)
        z = Proc.RemoteCallWait(XPTR, Loc)
        'Z should be 0 now
    End Sub

    Private Function GetPtr(LibraryName As String, FuncName As String) As IntPtr
        Return CULng(GetProcAddress(LoadLibrary(LibraryName), FuncName))
    End Function

End Module