簡體   English   中英

無需C / C ++或匯編的托管Dll注入

[英]Managed Dll Injection without C/C++ or Assembly

如何在不使用任何C / C ++引導dll或任何用匯編語言編寫的代碼洞穴的情況下,使用VB / C#在遠程進程中注入托管dll。

Dll導出是將功能導出為本機代碼所必需的

經典機制:

以下是經典dll注入的過程:

  • 創建C / C ++ Dll
  • 將DLL路徑寫入遠程進程
  • 創建到LoadLibraryA的遠程線程以及參數作為Dll路徑
  • 在此階段將調用Dll入口點

參考: 代碼項目文章

Codecave方法:

使用這種方法,您可以跳過C / C ++ Dll,但需要匯編的基本知識

  • 在運行時以字節數組的形式創建代碼洞,並寫入其他進程,或者編寫匯編過程(類似於c的函數)並將其編譯為二進制代碼,這將加載.net匯編
  • 將您的代碼編寫到其他過程
  • 創建遠程線程,即可加載.net程序集

參考: 帶有示例的代碼 [原始鏈接似乎已過期,因此Google緩存版本]

現代方式:

此方法非常易於使用,並且不需要C / C ++或Assembly的知識,以下為過程

  • 在當前進程中加載​​您的庫並獲取您要調用的過程地址,它將與帶有一個參數的過程一起使用
  • 在目標進程中使用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

暫無
暫無

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

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