Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Imports System.Runtime.InteropServices
  2.  
  3. Module ShellExec
  4.     <DllImport("kernel32.dll")> _
  5.     Private Function VirtualProtect(ByVal lpAddress As IntPtr, ByVal dwSize As UIntPtr, ByVal flNewProtect As UInt32, <Out()> ByRef lpflOldProtect As UInt32) As Boolean
  6.     End Function
  7.     Private Delegate Sub ShellCodeDelegate()
  8.     Public Sub ExecuteShellCode(ByVal pcode As Byte())
  9.   Try
  10.     Dim oldProtect As UInt32
  11.     VirtualProtect(VarPtr(pcode), New UIntPtr(CType(pcode.Length, UInt32)), &H40, oldProtect)
  12.     Dim execute As ShellCodeDelegate = DirectCast(Marshal.GetDelegateForFunctionPointer(VarPtr(pcode), GetType(ShellCodeDelegate)), ShellCodeDelegate)
  13.     execute.Invoke()
  14.     VirtualProtect(VarPtr(pcode), New UIntPtr(CType(pcode.Length, UInt32)), oldProtect, oldProtect)
  15.   Catch ex As Exception
  16.   End Try
  17.  
  18.     End Sub
  19.     Private Function VarPtr(ByVal e As Object) As Integer
  20.   Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
  21.   Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
  22.   GC.Free()
  23.   Return GC2
  24.     End Function
  25. End Module