本帖最后由 萧阳天 于 2016-4-10 01:46 编辑
模块的APIHOOK方法,过于复杂不易管理,且效率偏低还存在安全问题(程序执行环境),对此稍做改进,希望采纳。
改进的HOOK,支持任意API,或者其它模块的导出函数的拦截,不需要担心寄存器的保存以及堆栈问题,并且可以直接调用被HOOK的函数(通过声明一个新的
子程序交由HOOK函数进行编码,这使得你可以在拦截函数中直接调用原函数而不需要将你的HOOK钩子取消)
Hook效果:
SetApiHook (“user32.dll”, “MessageBoxA”, &NewMessageBoxA, &JmpMessageBoxA, , 真)标准输入 ()信息框 (“测试函数”, 0, , )标准输入 ()_临时子程序 () 返回 (0 ) |
NewMessageBoxA | 整数型 | | |
hWnd | 整数型 | | | | lpText | 整数型 | | | | lpCaption | 整数型 | | | | uType | 整数型 | | | | 调试输出 (“消息框文本”, 指针到文本 (lpText )) 返回 (JmpMessageBoxA (hWnd, lpText, lpCaption, uType )) |
JmpMessageBoxA | 整数型 | | |
hWnd | 整数型 | | | | lpText | 整数型 | | | | lpCaption | 整数型 | | | | uType | 整数型 | | | | 返回 (0 ) |
SetApiHook | 逻辑型 | | |
szModule | 文本型 | | | | szApiName | 文本型 | | | | lpfnHookAddress | 子程序指针 | | | | lpfnNewApiAddress | 子程序指针 | | | | dwSize | 整数型 | | | | bSwArginfo | 逻辑型 | | | |
变量名 | 类 型 | 静态 | 数组 | 备 注 | theApiAddress | 整数型 | | | theSubPtr | 整数型 | | | theJumpPtr | 整数型 | | | theRetVal | 整数型 | | | theSaveSize | 整数型 | | | theDevAddress | 整数型 | | | theOldProtect | 整数型 | | | theOldProtect2 | 整数型 | | |
theApiAddress = GetProcAddress (LoadLibraryA (szModule ), szApiName )theSubPtr = 到整数 (lpfnHookAddress )theJumpPtr = ToCallback (lpfnNewApiAddress, bSwArginfo ) + 3 theSaveSize = 选择 (dwSize = 0, 5, dwSize )如果真 (theApiAddress ≠ 0 )theDevAddress = theApiAddress - theSubPtr theDevAddress = 位取反 (theDevAddress )theDevAddress = theDevAddress - 4 如果真 (VirtualProtect (theApiAddress, theSaveSize, #PAGE_EXECUTE_READWRITE, theOldProtect ) 且 VirtualProtect (theJumpPtr, theSaveSize, #PAGE_EXECUTE_READWRITE, theOldProtect2 )) 如果真 (theSaveSize > 5 )memcpy (theJumpPtr, theApiAddress, theSaveSize )memset (theApiAddress + 5, 144, theSaveSize - 5 ) theSaveSize = theSaveSize - 5Xcode (theApiAddress, 0, 233, #XCODE_WRITE_BYTE )Xcode (theApiAddress, 1, theDevAddress, #XCODE_WRITE_DWORD )theDevAddress = theJumpPtr + theSaveSize (theApiAddress + 5 )theDevAddress = 位取反 (theDevAddress )theDevAddress = theDevAddress - 4 Xcode (theJumpPtr, theSaveSize, 233, #XCODE_WRITE_BYTE )Xcode (theJumpPtr, theSaveSize + 1, theDevAddress, #XCODE_WRITE_DWORD )VirtualProtect (theApiAddress, theSaveSize, theOldProtect, 0 )VirtualProtect (theJumpPtr, theSaveSize, theOldProtect2, 0 )返回 (真)返回 (假)|
ToCallback | 整数型 | | |
子程序 | 子程序指针 | | | | 是否有参数 | 逻辑型 | | | | 置入代码 ({ 62, 128, 125, 12, 1, 116, 17, 139, 69, 8, 131, 192, 4, 139, 200, 139, 0, 3, 193, 131, 192, 4, 235, 15, 139, 69, 8, 131, 192, 21, 139, 200, 139, 0, 3, 193, 131, 192, 4, 139, 229, 93, 194, 8, 0 })返回 (0 )|
Xcode | 整数型 | | |
ptr | 整数型 | | | | dev | 整数型 | | | | val | 整数型 | | | | flags | 整数型 | | | | 置入代码 ({ 139, 69, 20, 133, 192, 117, 13, 139, 77, 12, 139, 69, 8, 139, 4, 8, 93, 194, 16, 0, 131, 248, 1, 117, 14, 139, 77, 12, 139, 69, 8, 15, 183, 4, 8, 93, 194, 16, 0, 131, 248, 2, 117, 14, 139, 77, 12, 139, 69, 8, 15, 182, 4, 8, 93, 194, 16, 0, 131, 248, 4, 117, 18, 139, 69, 8, 139, 77, 12, 139, 85, 16, 137, 20, 8, 51, 192, 93, 194, 16, 0, 131, 248, 8, 117, 20, 139, 69, 8, 139, 77, 12, 102, 139, 85, 16, 102, 137, 20, 8, 51, 192, 93, 194, 16, 0, 131, 248, 16, 117, 19, 139, 69, 8, 139, 77, 12, 102, 139, 85, 16, 136, 20, 8, 51, 192, 93, 194, 16, 0 })返回 (0 )
代码:
ApiHook.rar
(3.09 KB, 下载次数: 38)
|