📄 利用hook拦截封包原理—编程爱好者网站http--www_programfan_com.htm
字号:
FILE_ATTRIBUTE_NORMAL, 0 ); <BR>} <BR>} <BR><BR>if( hOldHook
!= NULL ) <BR>{ <BR>return CallNextHookEx( hOldHook, nCode,
wParam, lParam ); <BR>} <BR><BR>return 0; <BR>}
<BR><BR>上面的钩子函数,只有第一次运行时有用,就是把三个函数的首8字节修改一下(实际上只需要7个)。btNewBytes中的指令实际就是
<BR>mov eax, 0x400000 <BR>jmp eax
<BR>这里的0x400000就是新的函数的地址,比如new_recv/new_send/new_GetMessage,此时,偷梁换柱已经完成。再看看我们的函数中都干了些什么。以GetMessageA为例:
<BR><BR>BOOL _stdcall new_GetMessage( LPMSG lpMsg, HWND hWnd,
UINT wMsgFilterMin, UINT wMsgFilterMax ) <BR>{ <BR>DWORD
dwSize; <BR>char szTemp[256]; <BR>BOOL r = false;
<BR><BR>//Watch here before it's executed. <BR>sprintf(
szTemp, "Before GetMessage : HWND 0x%8.8X, msgMin 0x%8.8X,
msgMax 0x%8.8x \r\n", hWnd, wMsgFilterMin, wMsgFilterMax );
<BR>::WriteFile( hDebug, szTemp, strlen(szTemp), &dwSize,
0 ); <BR>//Watch over <BR><BR>// restore it at first
<BR>::WriteProcessMemory( INVALID_HANDLE_value, (void
*)pGetMessage, (void *)dwOldBytes[2], sizeof(DWORD)*2,
&dwSize ); <BR><BR>// execute it <BR>r = pGetMessage(
lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax ); <BR><BR>// hook
it again <BR>*(DWORD *)( btNewBytes + 1 ) =
(DWORD)new_GetMessage; <BR>::WriteProcessMemory(
INVALID_HANDLE_value, (void *)pGetMessage, (void *)btNewBytes,
sizeof(DWORD)*2, &dwSize ); <BR><BR>//Watch here after
it's executed <BR>sprintf( szTemp, "Result of GetMessage is
%d.\r\n", r ); <BR>::WriteFile( hDebug, szTemp, strlen( szTemp
), &dwSize, 0 ); <BR>if( r ) <BR>{ <BR>sprintf( szTemp,
"Msg : HWND 0x%8.8X, MSG 0x%8.8x, wParam 0x%8.8X, lParam
0x%8.8X\r\nTime 0x%8.8X, X %d, Y %d\r\n", <BR>lpMsg->hwnd,
lpMsg->message, <BR>lpMsg->wParam, lpMsg->lParam,
lpMsg->time, <BR>lpMsg->pt.x, lpMsg->pt.y );
<BR>::WriteFile( hDebug, szTemp, strlen( szTemp ),
&dwSize, 0 ); <BR>} <BR>strcpy( szTemp, "\r\n" );
<BR>::WriteFile( hDebug, szTemp, strlen( szTemp ),
&dwSize, 0 ); <BR><BR>//Watch over <BR><BR>return r; <BR>}
<BR><BR>先将截获下来的参数,写入到一个log文件中,以便分析。然后恢复原先保留下来的GetMessageA的首8字节,然后执行真正的GetMessageA调用,完毕后再将执行结果也写入log文件,然后将GetMessageA的执行结果返回给调用者。
<BR>整个截获的过程就是这样。你可以把其中的写log部分改成你自己想要的操作。这里有个不足的地方是,截获动作是不能够并发进行的,如果目标进程是多线程的,就会有问题。解决办法是,可以在每次new_GetMessage中加入一个CriticalSection的锁和解锁,以使调用变为串行进行,但这个我没有试验过。
<BR><BR>
<P align=right>【<A
href="http://www.programfan.net/comment.asp?id=2402">对此文发表评论</A>】 【<A
href="http://www.programfan.net/club">编程爱好者论坛</A>】 【<B><A
href="http://www.programfan.net/ad/ad.asp?id=12"
target=_blank>Borland专题讨论区</A></B>】 【<A
href="javascript:window.close()">关闭窗口</A>】<BR></P></TD></TR></TBODY></TABLE></DIV></CENTER></CENTER></TD></TR>
<TR>
<TD height=237>
<P align=center><A
href="http://www.programfan.net/club"><B>[论坛]</B></A> <A
href="http://www.programfan.net/club/showbbs.asp?id=17608"
style="COLOR: #ff0000"><B>编程爱好者论坛回帖有奖活动</B></A><B><A
style="COLOR: #ff0000"> </A><A
href="http://www.programfan.net/club/expoint.asp"
style="COLOR: #ff0000">专家分排行榜</A><A
style="COLOR: #ff0000"> </A></B><BR><B><A
href="http://www.programfan.net/bookcd.asp"
title=编程书籍刻盘服务>[<STRONG>刻盘服务]</STRONG></A><FONT color=#0000ff><A
href="http://www.programfan.net/bookcd/msdn.htm"
style="COLOR: #ff0000">MSDN 系列光盘</A></FONT></B> <A
href="http://www.programfan.net/ad/ad.asp?id=9"
style="COLOR: #ff0000"><B>编程爱好者光盘第一期</B></A><A
href="http://www.programfan.net/ad/ad.asp?id=9"
style="COLOR: #ff0000"><B><BR></B></A><A
href="http://www.programfan.net/upload">编程爱好者网站推出文件交流服务</A> <BR><A
href="http://www.programfan.net/ad/ad.asp?id=13"
style="COLOR: #ff0066" target=_blank>站长尊容首次亮相,大家要看的快来看啊!</A><BR><A
href="http://www.programfan.net/club/bbs.asp?bbsid=53"
style="COLOR: #ff0000"
target=_blank>新增二手书籍交流栏目,让你多余的编程书籍发挥余热</A><BR><A
href="http://www.programfan.net/download.asp">[编程工具]</A><B> </B><A
href="http://www.programfan.net/downcolumn.asp?id=1"
style="COLOR: #ff0000">经典编程工具系列 </A><BR><A
href="http://www.programfan.net/job.asp">全新推出招聘求职,为您提供人才交流的平台。</A>
<CENTER>
<P align=center><A href="http://www.programfan.net/club"
target=_blank><IMG border=0
src="利用HOOK拦截封包原理—编程爱好者网站http--www_programfan_com.files/ad1.gif"></A></P>
<DIV align=center>
<TABLE border=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD vAlign=top width="50%">
<DIV align=center>
<TABLE border=1 borderColorDark=#ffffff
borderColorLight=#000000 cellPadding=0 cellSpacing=0
width="95%">
<TBODY>
<TR>
<TD bgColor=#99ccff height=20 width="100%">
<P align=center><STRONG>本 栏 热 门 文 章</STRONG></P></TD></TR>
<TR>
<TD width="100%"><FONT style="LINE-HEIGHT: 150%"><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=183">Windows中断编程</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=21">Windows下DLL编程技术及应用</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=223">WINDOWS键盘事件的挂钩监控原理及其应用技术</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=163">轻松使用自己的回调函数</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=52">"陷阱"技术探秘──动态汉化Windows技术的分析</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=178">Windows95下多线程编程技术及其实现</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=262">如何实现win9X进程间数据通讯技术</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=894">WINDOWS消息说明</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=773">屏幕取词核心内幕</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=785">应用程序之间互相通讯的几种方法</A><BR></FONT></TD></TR></TBODY></TABLE></DIV></TD>
<TD vAlign=top width="50%">
<DIV align=center>
<TABLE border=1 borderColorDark=#ffffff
borderColorLight=#000000 cellPadding=0 cellSpacing=0
width="95%">
<TBODY>
<TR>
<TD bgColor=#99ccff height=20 width="100%">
<P align=center><STRONG>本 栏 最 新 文 章</STRONG></P></TD></TR>
<TR>
<TD width="100%"><FONT style="LINE-HEIGHT: 150%"><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=2401">消息钩子函数入门篇</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=2402">利用HOOK拦截封包原理</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=2226">用Win32
API实现串行通信</A><BR><FONT color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=2192">Windows编程中的映射模式和坐标转换</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=2156">Win32程序函数调用时堆栈变化情况分析</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=1402">分析与理解通知消息-WM_NOTIFY</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=1339">Windows函数错误处理</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=1330">WINDOWS数据类型</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=1224">注册ActiveX控件的几种方法</A><BR><FONT
color=#ff0000>● </FONT><A
href="http://www.programfan.net/showarticle.asp?id=1162">托盘编程</A><BR></FONT></TD></TR></TBODY></TABLE></DIV></TD></TR></TBODY></TABLE></DIV></CENTER></TD></TR></TBODY></TABLE></DIV></CENTER></TD></TR></TBODY></TABLE></DIV>
<DIV align=center>
<CENTER>
<TABLE border=0 cellPadding=0 cellSpacing=1 width=755>
<TBODY>
<TR>
<TD width="100%">
<HR color=#ff0000 noShade SIZE=1>
<P align=center><A href="http://www.programfan.net/intro.asp">网站简介</A> -
<A href="http://www.programfan.net/daohang.asp">网站导航</A> - <A
href="http://www.programfan.net/ad.asp">广告服务</A> - <A
href="http://www.programfan.net/joinus.htm">诚邀加盟</A> - <A
href="http://www.programfan.net/contact.asp?subject=关于网站">联系站长</A> -
<A href="http://www.programfan.net/link.htm">友情链接</A><BR>Copyright©
1999-2004 Programfan.com. All Rights Reserved<BR>网站制作&维护:Hannibal
QQ:
15987743</P></TD></TR></TBODY></TABLE></CENTER></DIV>
<SCRIPT
src="利用HOOK拦截封包原理—编程爱好者网站http--www_programfan_com.files/counter.htm"></SCRIPT>
</CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -