PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : VB.NET DLL Injection Modul



br00_pwn
03.02.2010, 11:46
Mit diesen Code kann man sehr einfach eine DLL in einen Prozess injizieren.
Diesen Sourcecode einfach in ein Modul einfügen:




Imports System.Runtime.InteropServices
Imports System.Text
Module ModInject
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As Integer
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByVal lpBuffer() As Byte, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As UInteger) As Boolean
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Integer, ByVal lpProcName As String) As Integer
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Integer
Private Declare Function CreateRemoteThread Lib "kernel32" (ByVal hProcess As Integer, ByVal lpThreadAttributes As Integer, ByVal dwStackSize As Integer, ByVal lpStartAddress As Integer, ByVal lpParameter As Integer, ByVal dwCreationFlags As Integer, ByVal lpThreadId As Integer) As Integer
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
Public Function inject(ByVal ProcessID As Long, ByVal DLLPath As String) As Boolean
On Error GoTo exiterror
Dim DProc As Integer
Dim DAdd As Integer
Dim DWrote As UInteger
Dim DAll As Integer
Dim DThe As Integer
Dim DMHD As Integer
DProc = OpenProcess(&H1F0FFF, 1, ProcessID)
DAdd = VirtualAllocEx(DProc, 0, DLLPath.Length, &H1000, &H4)
If (DAdd > 0) Then
Dim DByte() As Byte
DByte = StrChar(DLLPath)
WriteProcessMemory(DProc, DAdd, DByte, DLLPath.Length, DWrote)
DMHD = GetModuleHandle("kernel32.dll")
DAll = GetProcAddress(DMHD, "LoadLibraryA")
DThe = CreateRemoteThread(DProc, 0, 0, DAll, DAdd, 0, 0)
If (DThe > 0) Then
WaitForSingleObject(DThe, &HFFFF)
CloseHandle(DThe)
Return True
Else
GoTo exiterror
End If
Else
GoTo exiterror
End If
inject = True
Exit Function
exiterror:
inject = False
End Function
Private Function StrChar(ByRef strString As String) As Byte()
Dim bytTemp() As Byte
Dim i As Short
ReDim bytTemp(0)
For i = 1 To Len(strString)
If bytTemp(UBound(bytTemp)) <> 0 Then ReDim Preserve bytTemp(UBound(bytTemp) + 1)
bytTemp(UBound(bytTemp)) = Asc(Mid(strString, i, 1))
Next i
ReDim Preserve bytTemp(UBound(bytTemp) + 1)
bytTemp(UBound(bytTemp)) = 0
StrChar = bytTemp
End Function
End Module
Injizieren kann man dann wie folgt:


inject(ProcessID,"c:\PathToDLL")
funzt einwandfrei....selbst getestet...viel spass


gruß br00_pwn

chrixko
03.02.2010, 18:19
Wow!
Aber habe leider kleine Probleme bei der Umsetzung.
Kann ich .Net-Dlls einbinden oder müssen das native DLL's sein?

Habe es mit einer .Net-Dll( ein Modul mit einer Main Sub) versucht und es scheint bei mir nicht zu funktionieren(also zumindest wird die Main-Sub der DLL nicht ausgeführt). Oder wird dort dann nicht automatisch beim Einsprungspunkt ausgeführt?

//EDIT: Also löschen kann ich die dll schon mal nicht, weil ein Handle drauf liegt. Also scheint die DLL schon richtig injected zu werden. Jetzt bleibt halt nur noch die Frage, wie ich die Funktionen in der DLL zum Laufen kriege.

AgainsT
12.02.2010, 12:06
Ich würde auch gerne wissen, wie man die eigenen Methoden starten kann... PLZ!