ibennz

BatCave

Dec 17th, 2013
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.49 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2. Public Class Cave
  3.     <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
  4.     Private Shared Function MessageBoxW(ByVal hWnd As Integer, ByVal [Text] As String, ByVal Caption As String, ByVal uType As Integer) As Integer
  5.     End Function
  6.  
  7.     <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
  8.     Public Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
  9.     End Function
  10.  
  11.     <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True)> _
  12.     Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal procName As String) As UIntPtr
  13.     End Function
  14.  
  15.     <DllImport("kernel32.dll", SetLastError:=True, ExactSpelling:=True)> _
  16.     Private Shared Function VirtualAllocEx(ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, _
  17.      ByVal dwSize As UInteger, ByVal flAllocationType As UInteger, _
  18.      ByVal flProtect As UInteger) As IntPtr
  19.     End Function
  20.  
  21.     <DllImport("kernel32.dll", EntryPoint:="WriteProcessMemory", SetLastError:=True)> _
  22.     Private Shared Function WriteProcessMemory(ByVal process As IntPtr, ByVal baseAddress As Integer, ByVal buffer As IntPtr, ByVal bufferSize As Integer, ByRef bytesWritten As Integer) As Boolean
  23.     End Function
  24.  
  25.     <DllImport("kernel32.dll")> _
  26.     Private Shared Function OpenProcess(ByVal dwDesiredAccess As ProcessAccessFlags, <MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As IntPtr
  27.     End Function
  28.     <Flags()> _
  29.     Public Enum ProcessAccessFlags As Integer
  30.         AllAccess = CreateThread Or DuplicateHandle Or QueryInformation Or SetInformation Or Terminate Or VMOperation Or VMRead Or VMWrite Or Synchronize
  31.         CreateThread = &H2
  32.         DuplicateHandle = &H40
  33.         QueryInformation = &H400
  34.         SetInformation = &H200
  35.         Terminate = &H1
  36.         VMOperation = &H8
  37.         VMRead = &H10
  38.         VMWrite = &H20
  39.         Synchronize = &H100000
  40.     End Enum
  41.     <DllImport("kernel32.dll")> _
  42.     Private Shared Function CreateRemoteThread(ByVal ProcessHandle As IntPtr, ByVal lpThreadAttributes As IntPtr, ByVal dwStackSize As UInteger, ByVal lpStartAddress As IntPtr, ByVal lpParamater As IntPtr, ByVal dwCreationFlags As UInteger, <Runtime.InteropServices.Out()> ByVal lpThreadID As IntPtr) As IntPtr
  43.     End Function
  44.  
  45.  
  46.     Public Shared Function InjectMessageBox(ByVal pID As Integer) As Boolean
  47.  
  48.         Dim hProc As IntPtr = OpenProcess(ProcessAccessFlags.AllAccess, False, pID)
  49.  
  50.         Dim functionPointerForDelegate As IntPtr = Marshal.GetFunctionPointerForDelegate(New MessageBox(AddressOf hook))
  51.         Dim mFunct As New msgStruct
  52.         mFunct.funct = GetProcAddress(GetModuleHandle("User32.dll"), "MessageBoxA")
  53.         mFunct.title = "Hi"
  54.         mFunct.message = "faggot"
  55.         mFunct.handle = 0
  56.         mFunct.mtype = 0
  57.  
  58.         Dim mSize As Integer = 1000 ' CInt(functionPointerForDelegate) - CInt(mFunct.funct) - 5
  59.         Dim base As IntPtr = VirtualAllocEx(hProc, IntPtr.Zero, CUInt(mSize), &H3000, &H40)
  60.         If WriteProcessMemory(hProc, CInt(base), functionPointerForDelegate, mSize, 0) = False Then
  61.             Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
  62.         End If
  63.  
  64.         Dim pSize As Integer = Marshal.SizeOf(mFunct)
  65.         Dim ptrStrct As IntPtr = Marshal.AllocHGlobal(pSize)
  66.         Marshal.StructureToPtr(mFunct, ptrStrct, False)
  67.  
  68.         Dim paramsBase As IntPtr = VirtualAllocEx(hProc, IntPtr.Zero, CUInt(pSize), &H3000, &H40)
  69.         If WriteProcessMemory(hProc, CInt(paramsBase), ptrStrct, pSize, 0) = False Then
  70.             Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
  71.         End If
  72.  
  73.         If CreateRemoteThread(hProc, IntPtr.Zero, 0, base, paramsBase, 0, IntPtr.Zero) = IntPtr.Zero Then
  74.             Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
  75.         End If
  76.         Return True
  77.     End Function
  78.  
  79.     <UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError:=True)> _
  80.     Private Delegate Function MessageBox(ByVal parms As msgStruct) As Integer
  81.  
  82.     Private Shared Function hook(ByVal parms As msgStruct) As Integer
  83.         Return MessageBoxW(parms.handle, (parms.message), parms.title, parms.mtype)
  84.     End Function
  85.     Private Structure msgStruct
  86.         Dim funct As UIntPtr
  87.         Dim title As String
  88.         Dim message As String
  89.         Dim mtype As Integer
  90.         Dim handle As Integer
  91.     End Structure
  92. End Class
Advertisement
Add Comment
Please, Sign In to add comment