找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3707|回复: 1

一些可以自行实现的常用函数【代码是VB6写的】

[复制链接]
发表于 2016-8-9 21:53:12 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
首先是GetProcAddress,据说2009年的微点曾经Hook了这个函数
  1. Public Function GetProcAddress(ByVal hModule As Long, ByVal szProcName As String) As Long
  2. Dim DosHead As IMAGE_DOS_HEADER
  3. Dim NtHead As IMAGE_NT_HEADER
  4. Dim ExpDir As IMAGE_EXPORT_DIRECTORY
  5. Dim FuncRva() As Long
  6. Dim NameRva() As Long
  7. Dim OridRva() As Long
  8. Dim i As Long
  9. Dim s As String
  10. CopyMemory VarPtr(DosHead), hModule, Len(DosHead)
  11. If DosHead.e_magic = &H5A4D And DosHead.e_lfanew <> 0 Then
  12.     CopyMemory VarPtr(NtHead), hModule + DosHead.e_lfanew, Len(NtHead)
  13.     If NtHead.Signature = &H4550 And NtHead.OptionalHeader.DataDirectory(IMAGE_DIRECTORY_ENTRY_EXPORT).VirtualAddress <> 0 Then
  14.         CopyMemory VarPtr(ExpDir), hModule + NtHead.OptionalHeader.DataDirectory(IMAGE_DIRECTORY_ENTRY_EXPORT).VirtualAddress, Len(ExpDir)
  15.         ReDim FuncRva(1 To ExpDir.NumberOfFunctions)
  16.         ReDim OridRva(1 To ExpDir.NumberOfFunctions)
  17.         ReDim NameRva(1 To ExpDir.NumberOfNames)
  18.         CopyMemory VarPtr(FuncRva(1)), hModule + ExpDir.AddressOfFunctions, 4 * ExpDir.NumberOfFunctions
  19.         CopyMemory VarPtr(NameRva(1)), hModule + ExpDir.AddressOfNames, 4 * ExpDir.NumberOfNames
  20.         CopyMemory VarPtr(OridRva(1)), hModule + ExpDir.AddressOfNameOridinals, 2 * ExpDir.NumberOfFunctions
  21.         For i = 1 To ExpDir.NumberOfNames
  22.             s = AnsiStringFromPtr(hModule + NameRva(i))
  23.             If s = szProcName Then
  24.                 GetProcAddress = hModule + FuncRva(OridRva(i) + ExpDir.Base)
  25.                 Exit Function
  26.             End If
  27.         Next i
  28.     End If
  29. End If
  30. GetProcAddress = 0
  31. End Function
复制代码

一般这个函数还会和GetModuleHandle配套使用,GetModuleHandle的代码如下:
  1. Public Function GetModuleHandle(ByVal szModuleName As String) As Long
  2. Dim st As Long
  3. Dim pPeb As PEB
  4. Dim PebLdr As PEB_LDR_DATA
  5. Dim tLdr As LDR_MODULE
  6. Dim PBI As PROCESS_BASIC_INFORMATION
  7. Dim s As String
  8. st = ZwQueryInformationProcess(ZwCurrentProcess, ProcessBasicInformation, VarPtr(PBI), Len(PBI), vbNull)
  9. If NT_SUCCESS(st) And PBI.PebBaseAddress <> 0 Then
  10.     CopyMemory VarPtr(pPeb), PBI.PebBaseAddress, Len(pPeb)
  11.     CopyMemory VarPtr(PebLdr), pPeb.pLdr, Len(PebLdr)
  12.     CopyMemory VarPtr(tLdr), PebLdr.InLoadOrderModuleList.FLink, Len(tLdr)
  13.     Do Until tLdr.BaseAddress = 0
  14.         s = UnicodeStringFromPtr(tLdr.BaseDllName.pBuffer, tLdr.BaseDllName.Length)
  15.         If StrConv(s, vbLowerCase) = StrConv(szModuleName, vbLowerCase) Then
  16.             GetModuleHandle = tLdr.BaseAddress
  17.             Exit Function
  18.         End If
  19.         CopyMemory VarPtr(tLdr), tLdr.InLoadOrderModuleList.FLink, Len(tLdr)
  20.     Loop
  21. End If
  22. GetModuleHandle = 0
  23. End Function
复制代码

上述两份代码改一改就分别变成了枚举当前导出表和枚举模块!
上述代码中用到了UnicodeStringFromPtr,AnsiStringFromPtr以及strlen,我也一并贴出代码:
  1. Public Function strlen(ByVal lpString As Long) As Long
  2. Dim i As Long
  3. Dim p As Byte
  4. For i = 0 To 1000 Step 1
  5.     CopyMemory VarPtr(p), lpString + i, 1
  6.     If p = 0 Then strlen = i: Exit Function
  7. Next i
  8. strlen = 0
  9. End Function
  10. Public Function UnicodeStringFromPtr(ByVal pString As Long, ByVal Length As Long) As String
  11. Dim i As Long
  12. Dim pBuff() As Integer
  13. UnicodeStringFromPtr = ""
  14. If Length = 0 Then Exit Function
  15. ReDim pBuff(1 To Length \ 2)
  16. CopyMemory VarPtr(pBuff(1)), pString, Length
  17. For i = 1 To Length \ 2
  18.     UnicodeStringFromPtr = UnicodeStringFromPtr & ChrW(pBuff(i))
  19. Next i
  20. End Function
  21. Public Function AnsiStringFromPtr(ByVal pString As Long, Optional ByVal Length As Long = 0) As String
  22. Dim pLen As Long
  23. Dim pBuff() As Byte
  24. Dim i As Long
  25. If Length = 0 Then
  26.     pLen = strlen(pString)
  27. Else
  28.     pLen = Length
  29. End If
  30. AnsiStringFromPtr = ""
  31. If pLen = 0 Then Exit Function
  32. ReDim pBuff(1 To pLen)
  33. CopyMemory VarPtr(pBuff(1)), pString, pLen
  34. For i = 1 To pLen Step 1
  35.     AnsiStringFromPtr = AnsiStringFromPtr & Chr(pBuff(i))
  36. Next i
  37. End Function
复制代码
回复

使用道具 举报

发表于 2016-8-16 00:54:58 | 显示全部楼层
··················
回复 赞! 靠!

使用道具 举报

本版积分规则

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-11-22 10:22 , Processed in 0.034623 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表