Help with DLL Injector!

Posts 1–2 of 2 · Page 1 of 1
Help with DLL Injector!
Down Below is my code for the dll injector... but it always fails at WriteProcessMemory...

can anyone tell me why?



Code:
Imports System.IO
Imports System.Diagnostics
Public Class Form1
    Private Declare Function CreateRemoteThread Lib "kernel32.dll" (ByVal hProcess As Int32, ByRef lpThreadAttributes As Security_Attributes, ByVal dwStackSize As Int32, ByRef lpStartAddress As Int32, ByVal lpParameter As Int32, ByVal dwCreationFlags As Int32, ByRef lpThreadId As Int32) As Int32
    Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpAddress As Int32, ByRef dwSize As Int32, ByVal flAllocationType As Int32, ByVal flProtect As Int32) As Int32
    Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpBaseAddress As Int32, ByRef lpBuffer As Byte(), ByVal nSize As Int32, ByRef lpNumberOfBytesWritten As Int32) As Int32
    Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpAddress As Int32, ByRef dwSize As Int32, ByVal dwFreeType As Int32) As Int32
    Private Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (ByVal idHook As Int32, ByVal lpfn As Int32, ByVal hmod As Int32, ByVal dwThreadId As Int32) As Int32
    Private Declare Function GetExitCodeThread Lib "kernel32.dll" (ByVal hThread As Int32, ByRef lpExitCode As Int32) As Int32
    Private Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Int32
    Private Declare Function LoadLibrary Lib "kernel32.dll" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Int32
    Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Int32, ByVal lpProcName As String) As Int32
    Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Int32, ByVal bInheritHandle As Int32, ByVal dwProcessId As Int32) As Int32
    Private Declare Function CreateRemoteThreadex Lib "kernal32.dll" (ByVal HProcess As IntPtr, ByVal dwstacksize As Int32, ByVal LpStartAddress As Int32, ByVal LpParameter As Int32, ByVal DwCreationFlags As UInt32, ByVal lpAttributeList As Integer, ByRef lpThreadid As Int32) As Int32
    Private Process_All_Access As Integer = &H1F0FFF

    Public Structure Security_Attributes
        Dim nLength As UInteger
        Dim LpSecurityDescriptor As IntPtr
        Dim bInheritHandle As Boolean
    End Structure


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        With ofd
            .Filter = "Dll's (*.dll)|*.dll"
            .Title = "Select a dll to inject.."
            .CheckPathExists = True
            .CheckFileExists = True
        End With
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim lvi As New ListViewItem(ofd.SafeFileName)
            lvi.SubItems.Add(ofd.FileName)
            ListView1.Items.Add(lvi)
        End If
    End Sub


    Public Function DllInjection(ByVal DllBfile() As Byte, ByVal ProcessHandle As Int32)
        Try
            Dim DllVirtLoc As Int32
            Dim DllLength As Int32
            Dim Inject As Int32
            Dim LibAddress As Int32
            Dim CreateThread As Int32
            Dim ThreadID As Int32
            DllLength = UBound(DllBfile)
            DllVirtLoc = VirtualAllocEx(ProcessHandle, 0, DllLength, &H1000, &H4)
            If DllVirtLoc = 0 Then
                MessageBox.Show("Call to VirtualAllocEx Failed", "Error", MessageBoxButtons.OK)
                Exit Function
            Else
                Inject = WriteProcessMemory(ProcessHandle, DllVirtLoc, DllBfile, DllLength, Nothing)
                If Inject = 0 Then
                    MessageBox.Show("Call To WriteProcessMemory", "Error", MessageBoxButtons.OK)
                    Exit Function
                Else
                    Dim mHandle As Int32
                    mHandle = GetModuleHandle("Kernal32.dll")
                    If mHandle = 0 Then
                        MessageBox.Show("Call to GetModuleHandle failed.", "Error", MessageBoxButtons.OK)
                        Exit Function
                    Else
                        LibAddress = GetProcAddress(mHandle, "LoadLibraryA")
                        If LibAddress = 0 Then
                            MessageBox.Show("Error Getting Address of LoadLibrary", "Error", MessageBoxButtons.OK)
                            Exit Function
                        Else
                            CreateThread = CreateRemoteThread(ProcessHandle, Nothing, 0, LibAddress, DllVirtLoc, 0, ThreadID)
                            If CreateThread = 0 Then
                                MessageBox.Show("Call to CreateRemoteThread Failed", "Error", MessageBoxButtons.OK)
                                Exit Function
                            Else
                                StatusStrip1.Text = "Injected!"
                                StatusStrip1.ForeColor = Color.Green
                            End If
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
        End Try
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim splits() As String
        Dim myprocess As Process() = Process.GetProcessesByName(TextBox1.Text)
        Dim Hhandle As Int32
        Dim i As Integer

        splits = Split(TextBox1.Text, ".")
        If TextBox1.Text = "" Then
            Exit Sub
        ElseIf UBound(splits) <> 0 Then
            MessageBox.Show("Dont include the '.exe' at the end of the Process Name", "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        Try
            Hhandle = OpenProcess(Process_All_Access, 0, myprocess(0).Id)
        Catch ex As Exception
        End Try
        For i = 0 To ListView1.Items.Count - 1
            Dim items As String
            items = ListView1.Items(i).SubItems(1).Text
            Dim bfile() As Byte
            Dim Fileinfo1 As New FileInfo(ListView1.Items(i).SubItems(1).Text)
            Dim Fs As FileStream
            Fs = Fileinfo1.OpenRead()
            bfile = New Byte((Fs.Length - 1)) {}
            Fs.Read(bfile, 0, Fileinfo1.Length)
            DllInjection(bfile, Hhandle)
        Next i
    End Sub
End Class
Ill tell you. Your writing the whole dll as a byte array to the process not as its path(string). All attach my new source. I was wondering where you got my old source at? Also if your on vista or 64 bit OS set debugPrivileges.

P.S: It also is not your code.
Vb.net_Injector.rarattachment deleted · 30 downloads before removal
Posts 1–2 of 2 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us