VB6实现Ring3下直接调用Ring0层函数,反一切R3下API Hook。

接论坛帖子:http://topic.csdn.net/u/20120518/18/9a00ec5c-b3d1-4a1f-9bc1-ba1a47b52463.html

例子应用如下。我只是给一个方法给大家,这个方法肯定很麻烦,有需求的人可以用。

添加Module1

[vb]  view plain copy
  1. Private asm_CallCode() As Byte, KiFastSystemCall&, KiIntSystemCall&  
  2. Private Declare Function CallWindowProcW& Lib "user32" (ByVal lpPrevWndFunc As LongByVal hWnd As LongByVal Msg As LongByVal wParam As LongByVal lParam As Long)  
  3. Private Declare Function LocalAlloc& Lib "kernel32" (ByVal f&, ByVal s&)  
  4. Private Declare Function LocalSize& Lib "kernel32" (ByVal m&)  
  5. Private Declare Function LocalFree& Lib "kernel32" (ByVal m&)  
  6. Private Declare Function GetModuleHandleA& Lib "kernel32" (ByVal n$)  
  7. Private Declare Function GetProcAddress& Lib "kernel32" (ByVal m&, ByVal n$)  
  8. Private Declare Function IsWow64Process& Lib "kernel32" (ByVal h&, IsWow64 As Boolean)  
  9. Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal Dst&, ByVal Src&, ByVal Size&)  
  10. Private Declare Sub PutMem1 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Byte)  
  11. Private Declare Sub PutMem2 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Integer)  
  12. Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Long)  
  13. Private Declare Sub PutMem8 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Currency)  
  14.   
  15. Public Function ReadKrnlFunctionIndex&(ByVal Name$, Optional ByVal DllFile$ = "ntdll.dll"'//读取内核函数索引  
  16. Dim pEntry&, dwIndex&  
  17. pEntry = GetProcAddress(GetModuleHandleA(DllFile), Name)  
  18. RtlMoveMemory VarPtr(dwIndex), pEntry + 1, 4  
  19. ReadKrnlFunctionIndex = dwIndex  
  20. End Function  
  21.   
  22. Public Function InitCallKernel() As Boolean  '//这里初始化call代码  
  23. Dim bWow64 As Boolean  
  24. IsWow64Process -1, bWow64  
  25. If bWow64 Then Exit Function '//不支持x64  
  26. ReDim asm_CallCode(11)  
  27. KiFastSystemCall = GetProcAddress(GetModuleHandleA("ntdll.dll"), "KiFastSystemCall")  
  28. KiIntSystemCall = GetProcAddress(GetModuleHandleA("ntdll.dll"), "KiIntSystemCall")  
  29. If KiFastSystemCall = 0 Then Exit Function  
  30. If KiIntSystemCall = 0 Then Exit Function  
  31. asm_CallCode(0) = &HBA  
  32. RtlMoveMemory VarPtr(asm_CallCode(1)), IIf(CheckKiFastSystemCallHook, VarPtr(KiIntSystemCall), VarPtr(KiFastSystemCall)), 4 '//这里检测Hook,如果KiFastSystemCall被Hook、修改,就使用KiIntSystemCall  
  33. asm_CallCode(5) = &HB8  
  34. RtlMoveMemory VarPtr(asm_CallCode(6)), VarPtr(0&), 4  
  35. asm_CallCode(10) = &HFF  
  36. asm_CallCode(11) = &HD2  
  37. InitCallKernel = True  
  38. End Function  
  39.   
  40. Public Function CheckKiFastSystemCallHook() As Boolean  
  41. Dim bChar As Byte  
  42. RtlMoveMemory VarPtr(bChar), KiFastSystemCall, 1  
  43. If bChar = &HE9 Then CheckKiFastSystemCallHook = TrueExit Function '//检测jmp Hook  
  44. If bChar = &H68 Then CheckKiFastSystemCallHook = TrueExit Function '//检测push Hook  
  45. Dim dw3Char&  
  46. RtlMoveMemory VarPtr(dw3Char), KiFastSystemCall, 3  
  47. If dw3Char <> 1037451 Then CheckKiFastSystemCallHook = TrueExit Function '//检测函数头  
  48. End Function  
  49.   
  50. Public Function CallKernelFunction&(ByVal Name$, ByVal DllFile$, ParamArray pParam())  
  51. Dim dwIndex&  
  52. dwIndex = ReadKrnlFunctionIndex(Name, DllFile)  
  53. If dwIndex = 0 Then CallKernelFunction = -1: Exit Function  
  54. Dim ret&, i%, offset&  
  55. Dim hMem&  
  56. hMem = LocalAlloc(0, ((UBound(pParam) + 2) * 5) + UBound(pParam) + 1 + 1 + 12 + 1) '//申请代码内存  
  57. offset = hMem  
  58. For i = UBound(pParam) To 0 Step -1 '//压栈  
  59. PutMem1 offset, &H68 'push Param  
  60. offset = offset + 1  
  61. PutMem4 offset, pParam(i)  
  62. offset = offset + 4  
  63. Next  
  64. PutMem1 offset, &H68 'push Return Address  
  65. PutMem4 offset + 1, VarPtr(ret)  
  66. offset = offset + 5  
  67. RtlMoveMemory VarPtr(asm_CallCode(6)), VarPtr(dwIndex), 4 '//设置内核函数索引  
  68. RtlMoveMemory offset, VarPtr(asm_CallCode(0)), 12 '//把初始化的代码整个复制过去,省得重造轮子  
  69. offset = offset + 12  
  70. For i = 0 To UBound(pParam) + 1 '//出栈  
  71. PutMem1 offset, &H59 'pop  
  72. offset = offset + 1  
  73. Next  
  74. PutMem1 offset, &HC3 'retn  
  75. PutMem1 hMem + LocalSize(hMem), &H90 '//nop一行代码  
  76. CallKernelFunction = CallWindowProcW(hMem, 0, 0, 0, 0) 'call  
  77. LocalFree hMem '//释放内存  
  78. End Function  
添加Form1,一个Command1

[vb]  view plain copy
  1. Private Declare Function TextOut& Lib "gdi32" Alias "TextOutA" (ByVal DC As LongByVal X As LongByVal Y As LongByVal Text As StringByVal Size As Long)  
  2. Private Declare Function CreateThread& Lib "kernel32" (Optional ByVal Attributes As LongOptional ByVal StackSize As LongOptional ByVal Address As LongOptional Parameter As LongOptional ByVal CreationFlags As LongOptional TIDs As Long)  
  3. Private Type CONTEXT  
  4. ContextFlags As Long  
  5. Dr(5) As Long  
  6. FloatSave(111) As Byte  
  7. SegGs As Long  
  8. SegFs As Long  
  9. SegEs As Long  
  10. SegDs As Long  
  11. Edi As Long  
  12. Esi As Long  
  13. Ebx As Long  
  14. Edx As Long  
  15. Ecx As Long  
  16. Eax As Long  
  17. Ebp As Long  
  18. Eip As Long  
  19. SegCs As Long  
  20. EFlags As Long  
  21. Esp As Long  
  22. SegSs As Long  
  23. End Type  
  24.   
  25. Private Function GetAddr&(ByVal aaa&)  
  26. GetAddr = aaa  
  27. End Function  
  28.   
  29. Private Sub Command1_Click()  
  30. Dim hThread&  
  31. hThread = CreateThread(0, 0, 0, 0, 4, 0)'线程状态为暂停(开始地址为0,直接执行会崩)  
  32. MsgBox hThread  
  33. Dim i As CONTEXT  
  34. i.ContextFlags = 65543'CONTEXT_FULL  
  35. Me.Caption = CallKernelFunction("ZwGetContextThread""ntdll.dll", hThread, VarPtr(i))  
  36. i.Eip = GetAddr(AddressOf aaa)'更改执行地址  
  37. Me.Caption = CallKernelFunction("ZwSetContextThread""ntdll.dll", hThread, VarPtr(i))  
  38. CallKernelFunction "ZwResumeThread""ntdll.dll", hThread, 0'恢复线程运行  
  39. End Sub  
  40.   
  41. Private Sub Form_Load()  
  42. InitCallKernel  
  43. End Sub  
  44.   
  45. Private Sub Form_Paint()  
  46. Dim hDC&  
  47. hDC = CallKernelFunction("GetDC""user32.dll"Me.hWnd) '频繁调用可测试稳定性  
  48. TextOut hDC, 5, 5, "123", 3  
  49.   
  50. Dim hProcess&  
  51. Dim objAttr&(5), cid&(1)  
  52. cid(0) = 1192 '改成你要打开的PID  
  53. CallKernelFunction "ZwOpenProcess""ntdll.dll", VarPtr(hProcess), 2035711, VarPtr(objAttr(0)), VarPtr(cid(0))  
  54. Me.Caption = hProcess  
  55. End Sub  
添加Module2

[vb]  view plain copy
  1. Public Declare Function ExitThread& Lib "kernel32" (ByVal ExitStatus&)  
  2. Public Sub aaa(ByVal Param&)  
  3. Dim i&  
  4. For i = 0 To 10000  
  5. Form1.Caption = i  
  6. Next  
  7. ExitThread 0  
  8. End Sub  

编译运行可测试效果

*******************************

下面这个可替换Module1,InitCallKernel时加True即可。

[vb]  view plain copy
  1. Private asm_CallCode() As Byte, asm_MyCallCode() As Byte, KiFastSystemCall&, KiIntSystemCall&, MyKiFastSystemCall#  
  2. Private Declare Function CallWindowProcW& Lib "user32" (ByVal lpPrevWndFunc As LongByVal hWnd As LongByVal Msg As LongByVal wParam As LongByVal lParam As Long)  
  3. Private Declare Function LocalAlloc& Lib "kernel32" (ByVal f&, ByVal s&)  
  4. Private Declare Function LocalSize& Lib "kernel32" (ByVal m&)  
  5. Private Declare Function LocalFree& Lib "kernel32" (ByVal m&)  
  6. Private Declare Function GetModuleHandleA& Lib "kernel32" (ByVal n$)  
  7. Private Declare Function GetProcAddress& Lib "kernel32" (ByVal m&, ByVal n$)  
  8. Private Declare Function IsWow64Process& Lib "kernel32" (ByVal h&, IsWow64 As Boolean)  
  9. Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal Dst&, ByVal Src&, ByVal Size&)  
  10. Private Declare Sub PutMem1 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Byte)  
  11. Private Declare Sub PutMem2 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Integer)  
  12. Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Long)  
  13. Private Declare Sub PutMem8 Lib "msvbvm60" (ByVal Ptr As LongByVal NewVal As Currency)  
  14.   
  15. Public Function ReadKrnlFunctionIndex&(ByVal Name$, Optional ByVal DllFile$ = "ntdll.dll"'//读取内核函数索引  
  16. Dim pEntry&, dwIndex&  
  17. pEntry = GetProcAddress(GetModuleHandleA(DllFile), Name)  
  18. RtlMoveMemory VarPtr(dwIndex), pEntry + 1, 4  
  19. ReadKrnlFunctionIndex = dwIndex  
  20. End Function  
  21.   
  22. Public Function InitCallKernel(Optional ByVal IsMySysenter As BooleanAs Boolean  '//这里初始化call代码  
  23. Dim bWow64 As Boolean  
  24. IsWow64Process -1, bWow64  
  25. If bWow64 Then Exit Function '//不支持x64  
  26. ReDim asm_CallCode(11)  
  27. KiFastSystemCall = GetProcAddress(GetModuleHandleA("ntdll.dll"), "KiFastSystemCall")  
  28. KiIntSystemCall = GetProcAddress(GetModuleHandleA("ntdll.dll"), "KiIntSystemCall")  
  29. If KiFastSystemCall = 0 Then Exit Function  
  30. If KiIntSystemCall = 0 Then Exit Function  
  31. asm_CallCode(0) = &HBA  
  32. RtlMoveMemory VarPtr(asm_CallCode(1)), IIf(CheckKiFastSystemCallHook, VarPtr(KiIntSystemCall), VarPtr(KiFastSystemCall)), 4 '//这里检测Hook,如果KiFastSystemCall被Hook、修改,就使用KiIntSystemCall  
  33. If IsMySysenter Then RtlMoveMemory VarPtr(asm_CallCode(1)), VarPtr(InitMyCallKernel), 4 '//自己写sysenter,省得R3下被各种Hook  
  34. asm_CallCode(5) = &HB8  
  35. RtlMoveMemory VarPtr(asm_CallCode(6)), VarPtr(0&), 4  
  36. asm_CallCode(10) = &HFF  
  37. asm_CallCode(11) = &HD2  
  38. InitCallKernel = True  
  39. End Function  
  40.   
  41. Public Function InitMyCallKernel&() '//本来不想写这招的,可见他们说各种Hook,啊那就(<ゝω·)☆  
  42. PutMem4 VarPtr(MyKiFastSystemCall), 873452683  
  43. PutMem2 VarPtr(MyKiFastSystemCall) + 4, -13117  
  44. InitMyCallKernel = VarPtr(MyKiFastSystemCall)  
  45. End Function  
  46.   
  47. Public Function CheckKiFastSystemCallHook() As Boolean  
  48. Dim bChar As Byte  
  49. RtlMoveMemory VarPtr(bChar), KiFastSystemCall, 1  
  50. If bChar = &HE9 Then CheckKiFastSystemCallHook = TrueExit Function '//检测jmp Hook  
  51. If bChar = &H68 Then CheckKiFastSystemCallHook = TrueExit Function '//检测push Hook  
  52. Dim dw3Char&  
  53. RtlMoveMemory VarPtr(dw3Char), KiFastSystemCall, 3  
  54. If dw3Char <> 1037451 Then CheckKiFastSystemCallHook = TrueExit Function '//检测函数头  
  55. End Function  
  56.   
  57. Public Function CallKernelFunction&(ByVal Name$, ByVal DllFile$, ParamArray pParam())  
  58. Dim dwIndex&  
  59. dwIndex = ReadKrnlFunctionIndex(Name, DllFile)  
  60. If dwIndex = 0 Then CallKernelFunction = -1: Exit Function  
  61. Dim ret&, i%, offset&  
  62. Dim hMem&  
  63. hMem = LocalAlloc(0, ((UBound(pParam) + 2) * 5) + UBound(pParam) + 1 + 1 + 12 + 1) '//申请代码内存  
  64. offset = hMem  
  65. For i = UBound(pParam) To 0 Step -1 '//压栈  
  66. PutMem1 offset, &H68 'push Param  
  67. offset = offset + 1  
  68. PutMem4 offset, pParam(i)  
  69. offset = offset + 4  
  70. Next  
  71. PutMem1 offset, &H68 'push Return Address  
  72. PutMem4 offset + 1, VarPtr(ret)  
  73. offset = offset + 5  
  74. RtlMoveMemory VarPtr(asm_CallCode(6)), VarPtr(dwIndex), 4 '//设置内核函数索引  
  75. RtlMoveMemory offset, VarPtr(asm_CallCode(0)), 12 '//把初始化的代码整个复制过去,省得重造轮子  
  76. offset = offset + 12  
  77. For i = 0 To UBound(pParam) + 1 '//出栈  
  78. PutMem1 offset, &H59 'pop  
  79. offset = offset + 1  
  80. Next  
  81. PutMem1 offset, &HC3 'retn  
  82. PutMem1 hMem + LocalSize(hMem), &H90 '//nop一行代码  
  83. CallKernelFunction = CallWindowProcW(hMem, 0, 0, 0, 0) 'call  
  84. LocalFree hMem '//释放内存  
  85. End Function  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值