利用VB编写dll注入其他进程的源代码(2).VB DLL thread inject(2)

接下来我们讲怎么把注入进去的dll卸载掉,其实与注入基本上是一样的,只不过用到的API要换一下。
我们先在目标进程当中执行GetModuleHandleA,得到我们dll的句柄,然后再远程执行FreeLibrary,把我们的dll卸载。
那么这里有个问题,如何得到GetModuleHandleA的返回值呢?
那就要用到WaitForSingleObject,这个函数的作用是等待远程线程执行完毕,之后我们只要调用GetExitCodeThread得到远程线程的退出码就行了。因为远程线程是调用GetModuleHandleA,所以退出码就是GetModuleHandleA的返回值。

代码如下:

 

程序代码 程序代码

Private Sub Enject()

Dim MySnapHandle As Long
Dim ProcessInfo As PROCESSENTRY32
Dim MyRemoteProcessId As Long
Dim MyDllFileLength As Long
Dim MyDllFileBuffer As Long
Dim MyReturn As Long
Dim MyAddr As Long
Dim MyResult As Long
Dim MyDllFileName As String
Dim dwHandle As Long

MySnapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
ProcessInfo.dwSize = Len(ProcessInfo)


If Process32First(MySnapHandle, ProcessInfo) <> 0 Then
Do
    If InStr(ProcessInfo.szExeFile, "explorer.exe") > 0 Then
        MyRemoteProcessId = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessInfo.th32ProcessID)
        If MyRemoteProcessId = 0 Then MsgBox "error OpenProcess"
        
        MyStartAddr = GetProcAddress(GetModuleHandle("Kernel32"), "GetModuleHandleA")
        '与注入一样,只不过这里换成了GetModuleHandleA. _
         原理就是先在目标进程执行GetModuleHandleA得到我们的dll的地址,然后再用FreeLibrary卸载掉我们的dll。
        If MyStartAddr = 0 Then MsgBox "error GetProcAddress"
        
        MyDllFileName = IIf(LCase(Right(Text1, 4)) = ".dll", Text1, Text1.Text & ".dll")
        MyDllFileLength = LenB(StrConv(MyDllFileName, vbFromUnicode)) + 1
        
        MyDllFileBuffer = VirtualAllocEx(MyRemoteProcessId, 0, MyDllFileLength, MEM_COMMIT, PAGE_READWRITE)
        If MyDllFileBuffer = 0 Then MsgBox "error VirtualAllocEx"
        
        MyReturn = WriteProcessMemory(MyRemoteProcessId, MyDllFileBuffer, ByVal (MyDllFileName), MyDllFileLength, temp)
        If MyReturn = 0 Then MsgBox "error WriteProcessMemory"

        MyResult = CreateRemoteThread(MyRemoteProcessId, 0, 0, MyAddr, MyDllFileBuffer, 0, temp)
        '都与注入一样。执行到这里就得到了
        WaitForSingleObject MyResult, INFINITE '等待远程线程执行完毕。
        GetExitCodeThread MyResult, dwHandle '这里可以得到远程线程的返回值,也就是GetModuleHandleA返回的我们的dll的句柄。
        
        VirtualFreeEx MyRemoteProcessId, MyDllFileBuffer, MyDllFileLength, MEM_DECOMMIT
        '释放掉我们申请的内存段
        CloseHandle MyResult
        
        MyAddr = GetProcAddress(GetModuleHandle("Kernel32"), "FreeLibrary")
        If MyAddr = 0 Then MsgBox "error FreeLibrary"
        MyResult = CreateRemoteThread(MyRemoteProcessId, 0, 0, MyAddr, dwHandle, 0, temp)
        '这次不需要再用WriteProcessMemory写入FreeLibrary的参数了, _
         因为我们的dll的句柄刚才已经保存在dwHandle里面了。
        If MyResult = 0 Then MsgBox "error CreateRemoteThread"
        CloseHandle MyResult
        CloseHandle MyRemoteProcessId
    End If
Loop While Process32Next(MySnapHandle, ProcessInfo) <> 0
End If


CloseHandle MySnapHandle

End Sub
 


我们再加上几个按钮的动作,整个注入程序就完成了
 

程序代码 程序代码

Private Sub Command1_Click()
Inject
End Sub

Private Sub Command2_Click()
Enject
End Sub

Private Sub Form_Load()
Text1 = GetSetting(App.Title, "dllname", "dllname")
End Sub

Private Sub Form_Unload(Cancel As Integer)
SaveSetting App.Title, "dllname", "dllname", Text1
End Sub

Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
 



在text1里面输入想要注入的dll文件名,然后点Command1就行了。

明天讲注入的dll怎么写……



[本日志由 蓝色炫影 于 2008-10-06 11:24 PM 编辑]
上一篇 | 下一篇 文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: dll 注入 远程线程
相关日志:
评论: 2 | 引用: 0 | 查看次数: -