• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

.NET [VB.NET] A simple DLL Injector [Source]

Status
Not open for further replies.

sQuo

~ KillmeMories ~
Shadow
User
Joined
Oct 16, 2011
Messages
5,851
Reputation
0
Reaction score
22,904
Points
688
Credits
0
‎13 Years of Service‎
24%
Flvw3.png


Code:
>Imports System.Diagnostics
Public Class Form1
   Dim ProcessIsReal As Integer
   Private TargetProcessHandle As Integer
   Private pfnStartAddr As Integer
   Private pszLibFileRemote As String
   Private TargetBufferSize As Integer

   Public Const PROCESS_VM_READ = &H10
   Public Const TH32CS_SNAPPROCESS = &H2
   Public Const MEM_COMMIT = 4096
   Public Const PAGE_READWRITE = 4
   Public Const PROCESS_CREATE_THREAD = (&H2)
   Public Const PROCESS_VM_OPERATION = (&H8)
   Public Const PROCESS_VM_WRITE = (&H20)
   Dim DLLFileName As String
   Public Declare Function ReadProcessMemory Lib "kernel32" ( _
   ByVal hProcess As Integer, _
   ByVal lpBaseAddress As Integer, _
   ByVal lpBuffer As String, _
   ByVal nSize As Integer, _
   ByRef lpNumberOfBytesWritten As Integer) As Integer

   Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" ( _
   ByVal lpLibFileName As String) As Integer

   Public 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

   Public Declare Function WriteProcessMemory Lib "kernel32" ( _
   ByVal hProcess As Integer, _
   ByVal lpBaseAddress As Integer, _
   ByVal lpBuffer As String, _
   ByVal nSize As Integer, _
   ByRef lpNumberOfBytesWritten As Integer) As Integer

   Public 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

   Public 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, _
   ByRef lpThreadId As Integer) As Integer

   Public Declare Function OpenProcess Lib "kernel32" ( _
   ByVal dwDesiredAccess As Integer, _
   ByVal bInheritHandle As Integer, _
   ByVal dwProcessId As Integer) As Integer

   Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
   ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Integer

   Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandleA" ( _
   ByVal hObject As Integer) As Integer


   Dim ExeName As String = IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)

   Private Sub Inject()

       On Error Resume Next

       Dim TargetProcess As Process() = Process.GetProcessesByName(ComboBox1.Text)
        TargetProcessHandle = OpenProcess(PROCESS_CREATE_THREAD Or  PROCESS_VM_OPERATION Or PROCESS_VM_WRITE, False, TargetProcess(0).Id)
       pszLibFileRemote = OpenFileDialog1.FileName
       pfnStartAddr = GetProcAddress(GetModuleHandle("Kernel32"), "LoadLibraryA")
       TargetBufferSize = 1 + Len(pszLibFileRemote)
       Dim Rtn As Integer
       Dim LoadLibParamAdr As Integer
       LoadLibParamAdr = VirtualAllocEx(TargetProcessHandle, 0, TargetBufferSize, MEM_COMMIT, PAGE_READWRITE)
       Rtn = WriteProcessMemory(TargetProcessHandle, LoadLibParamAdr, pszLibFileRemote, TargetBufferSize, 0)
       CreateRemoteThread(TargetProcessHandle, 0, 0, pfnStartAddr, LoadLibParamAdr, 0, 0)
       CloseHandle(TargetProcessHandle)
       Me.Show()
       Beep()
       Label1.Text = ComboBox2.Text + " Successfully Injected to " + ComboBox1.Text
       Timer1.Stop()
       Timer2.Enabled = True
       If CheckBox2.Checked = True Then
           Dim wannamaximized As String = ComboBox1.Text + ".exe"
           For Each p As Process In Process.GetProcessesByName(ComboBox1.Text)
               ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MAXIMIZE)
           Next p
       End If

       If CheckBox3.Checked = True Then
           ExitAfter.Enabled = True
       End If
       RadioButton3.Checked = False
       RadioButton1.Checked = True

   End Sub


   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick



       Dim TargetProcess As Process() = Process.GetProcessesByName(ComboBox1.Text)
       If TargetProcess.Length = 0 Then
           Me.Label1.Text = ("Waiting for " + ComboBox1.Text + ".exe")
           ProcessIsReal = 0

       Else

           Me.Label1.Text = "Process Found ! Now You Can Inject Your Dll"
           ProcessIsReal = 1
       End If
       Label2.Text = OpenFileDialog1.FileName
       Dim DllSafeFileName As String = OpenFileDialog1.SafeFileName.Replace(".dll", "")
       Label3.Text = "*_* " + DllSafeFileName + " *_*"

       If CheckBox1.Checked = True Then
           TextBox1.Enabled = True

           On Error Resume Next
           TimedInjection.Interval = TextBox1.Text * 1000
       Else
           TextBox1.Enabled = False
       End If




   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       On Error Resume Next

       OpenFileDialog1.Filter = "DLL (*.dll) |*.dll|(*.*) |*.*"
       OpenFileDialog1.ShowDialog()
       Dim FileName As String
       FileName = OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\"))
       Dim DllFileName As String = FileName.Replace("\", "")
       Me.ComboBox2.Text = (DllFileName)

   End Sub
   Private Sub CheckInjectiftimedornot()
       If CheckBox1.Checked = True Then
           TimedInjection.Enabled = True
           Button4.Text = "Please Wait ..."
           Button4.Enabled = False
       Else
           Call Inject()
       End If
   End Sub
   Private Sub CheckInject()
       If ComboBox2.Text  "" Then
           Call CheckInjectiftimedornot()
       Else
           MsgBox("Please select a Dll file", MsgBoxStyle.Information, "File not found")

       End If
   End Sub

   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       If ProcessIsReal = 0 Then
           MsgBox("Please select a process first", MsgBoxStyle.Information, "Process not found")
       Else
           Call CheckInject()
       End If

   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Try
           Dim allProcess As Process
           For Each allProcess In Process.GetProcesses()
               ComboBox1.Items.Add(allProcess.ProcessName)
           Next
       Catch exx As Exception
           ComboBox1.Items.Add("ERROR")
       End Try
   End Sub

   Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
       Timer1.Start()
       Timer2.Enabled = True


   End Sub


   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       On Error Resume Next
       OpenFileDialog2.Filter = "EXE (*.exe) |*.exe|(*.*) |*.*"
       OpenFileDialog2.ShowDialog()
       Dim FileExe As String
       FileExe = OpenFileDialog2.FileName.Substring(OpenFileDialog2.FileName.LastIndexOf("\"))
       Dim ExeFileNames As String = FileExe.Replace(".exe", "")
       Dim ExeFileName As String = ExeFileNames.Replace("\", "")
       Me.ComboBox1.Text = (ExeFileName)
   End Sub

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       End
   End Sub

   Private Sub TimedInjection_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimedInjection.Tick
       Call Inject()
       TimedInjection.Enabled = False
       Button4.Text = "Inject"
       Button4.Enabled = True
   End Sub

   Private Sub ExitAfter_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitAfter.Tick
       End
   End Sub

   Private Declare Function ShowWindow Lib "user32.dll" ( _
   ByVal hWnd As IntPtr, _
   ByVal nCmdShow As SHOW_WINDOW _
   ) As Boolean

   Private Enum SHOW_WINDOW As Integer
       SW_HIDE = 0
       SW_SHOWNORMAL = 1
       SW_NORMAL = 1
       SW_SHOWMINIMIZED = 2
       SW_SHOWMAXIMIZED = 3
       SW_MAXIMIZE = 3
       SW_SHOWNOACTIVATE = 4
       SW_SHOW = 5
       SW_MINIMIZE = 6
       SW_SHOWMINNOACTIVE = 7
       SW_SHOWNA = 8
       SW_RESTORE = 9
       SW_SHOWDEFAULT = 10
       SW_FORCEMINIMIZE = 11
       SW_MAX = 11
   End Enum

    Private Sub SelectFileToolStripMenuItem_Click(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  SelectFileToolStripMenuItem.Click
       On Error Resume Next
       OpenFileDialog2.Filter = "EXE (*.exe) |*.exe|(*.*) |*.*"
       OpenFileDialog2.ShowDialog()
       Dim FileExe As String
       FileExe = OpenFileDialog2.FileName.Substring(OpenFileDialog2.FileName.LastIndexOf("\"))
       Dim ExeFileNames As String = FileExe.Replace(".exe", "")
       Dim ExeFileName As String = ExeFileNames.Replace("\", "")
       Me.ComboBox1.Text = (ExeFileName)
   End Sub

    Private Sub SelectDllToolStripMenuItem_Click(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  SelectDllToolStripMenuItem.Click
       On Error Resume Next

       OpenFileDialog1.Filter = "DLL (*.dll) |*.dll|(*.*) |*.*"
       OpenFileDialog1.ShowDialog()
       Dim FileName As String
       FileName = OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\"))
       Dim DllFileName As String = FileName.Replace("\", "")
       Me.ComboBox2.Text = (DllFileName)

   End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
       End
   End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
       AboutBox1.Show()
   End Sub

    Private Sub InjectToolStripMenuItem_Click(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  InjectToolStripMenuItem.Click
       If ProcessIsReal = 0 Then
           MsgBox("Please select a process first", MsgBoxStyle.Information, "Process not found")
       Else
           Call CheckInject()
       End If
   End Sub

   Private Sub CheckAutoInject()
       If ComboBox2.Text  "" Then
           Call CheckInjectiftimedornot()
       Else


       End If
   End Sub

   Private Sub AutoInject()
       If ProcessIsReal = 0 Then

       Else
           Call CheckAutoInject()

       End If


   End Sub

   Private Sub AutoCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AutoCheck.Tick
       If RadioButton3.Checked = True Then


           Call AutoInject()
       End If
   End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  RefreshToolStripMenuItem.Click
       ComboBox1.Items.Clear()
       Try
           Dim allProcess As Process
           For Each allProcess In Process.GetProcesses()
               ComboBox1.Items.Add(allProcess.ProcessName)
           Next
       Catch exx As Exception
           ComboBox1.Items.Add("ERROR")
       End Try
   End Sub
End Class
 
Status
Not open for further replies.
Back
Top