`
xitong
  • 浏览: 6158069 次
文章分类
社区版块
存档分类
最新评论

tcpdump抓包工具的高级应用

 
阅读更多

tcpdump -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854

0x4745 为"GET"前两个字母"GE"

0x4854 为"HTTP"前两个字母"HT"

<wbr><br><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 说明: 通常情况下:一个正常的TCP连接,都会有三个阶段:1、TCP三次握手;2、数据传送;3、TCP四次挥手</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 里面的几个概念:</p> <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>SYN: (同步序列编号,Synchronize Sequence Numbers)</li> <li>ACK: (确认编号,Acknowledgement Number)</li> <li>FIN: (结束标志,FINish)</li> </ul> <h2 style="line-height:37px"> <a name="tcp%E9%93%BE%E6%8E%A5%E7%9A%84%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81-TCP%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B(%E5%88%9B%E5%BB%BAOPEN)" rel="nofollow" style="color:rgb(21,80,103)"></a>TCP三次握手(创建 OPEN)</h2> <ol style="margin:5px 0px 5px 40px; padding:0px"> <li>客户端发起一个和服务创建TCP链接的请求,这里是SYN(J)</li> <li>服务端接受到客户端的创建请求后,返回两个信息: SYN(K) + ACK(J+1)</li> <li>客户端在接受到服务端的ACK信息校验成功后(J与J+1),返回一个信息:ACK(K+1)</li> <li>服务端这时接受到客户端的ACK信息校验成功后(K与K+1),<span style="color:rgb(255,0,0)">不再返回信息,后面进入数据通讯阶段</span> </li> </ol> <h2 style="line-height:37px"> <a name="tcp%E9%93%BE%E6%8E%A5%E7%9A%84%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81-%E6%95%B0%E6%8D%AE%E9%80%9A%E8%AE%AF" rel="nofollow" style="color:rgb(21,80,103)"></a>数据通讯</h2> <ol style="margin:5px 0px 5px 40px; padding:0px"><li>客户端/服务端 read/write数据包</li></ol> <h2 style="line-height:37px"> <a name="tcp%E9%93%BE%E6%8E%A5%E7%9A%84%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81-TCP%E5%9B%9B%E6%AC%A1%E6%8F%A1%E6%89%8B(%E5%85%B3%E9%97%ADfinish)" rel="nofollow" style="color:rgb(21,80,103)"></a>TCP四次握手(关闭 finish)</h2> <ol style="margin:5px 0px 5px 40px; padding:0px"> <li>客户端发起关闭请求,发送一个信息:FIN(M)</li> <li>服务端接受到信息后,首先返回ACK(M+1),表明自己已经收到消息。</li> <li>服务端在准备好关闭之前,最后发送给客户端一个 FIN(N)消息,询问客户端是否准备好关闭了</li> <li>客户端接受到服务端发送的消息后,返回一个确认信息: ACK(N+1)</li> <li>最后,服务端和客户端在双方都得到确认时,各自关闭或者回收对应的TCP链接。</li> </ol> <h2 style="line-height:37px"> <a name="tcp%E9%93%BE%E6%8E%A5%E7%9A%84%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81-%E8%AF%A6%E7%BB%86%E7%9A%84%E7%8A%B6%E6%80%81%E8%AF%B4%E6%98%8E" rel="nofollow" style="color:rgb(21,80,103)"></a>详细的状态说明(以及linux相关参数调整)</h2> <ol style="margin:5px 0px 5px 40px; padding:0px"> <li>SYN_SEND <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>客户端尝试链接服务端,通过open方法。也就是<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5LiJ5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP三次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>中的第1步之后,<span style="color:rgb(255,0,0)">注意是客户端状态</span> </li> <li>sysctl -w net.ipv4.tcp_syn_retries = 2 ,做为客户端可以设置SYN包的重试次数,默认5次(大约180s)引用校长的话:仅仅重试2次,现代网络够了</li> </ul> </li> <li>SYN_RECEIVED <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>服务接受创建请求的SYN后,也就是<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5LiJ5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP三次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>中的第2步,发送ACK数据包之前</li> <li><span style="color:rgb(255,0,0)">注意是服务端状态,一般15个左右正常,如果很大,怀疑遭受SYN_FLOOD攻击</span></li> <li>sysctl -w net.ipv4.tcp_max_syn_backlog=4096 , 设置该状态的等待队列数,默认1024,调大后可适当防止syn-flood,可参见<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;title=man+7+tcp&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">man 7 tcp<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a> </li> <li>sysctl -w net.ipv4.tcp_syncookies=1 , 打开syncookie,在syn backlog队列不足的时候,提供一种机制临时将syn链接换出</li> <li>sysctl -w net.ipv4.tcp_synack_retries = 2 ,做为服务端返回ACK包的重试次数,默认5次(大约180s)引用校长的话:仅仅重试2次,现代网络够了</li> </ul> </li> <li>ESTABLISHED <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>客户端接受到服务端的ACK包后的状态,服务端在发出ACK在一定时间后即为ESTABLISHED</li> <li>sysctl -w net.ipv4.tcp_keepalive_time = 1200 ,默认为7200秒(2小时),<span style="color:rgb(255,0,0)">系统针对空闲链接会进行心跳检查,如果超过net.ipv4.tcp_keepalive_probes * net.ipv4.tcp_keepalive_intvl = 默认11分,终止对应的tcp链接,可适当调整心跳检查频率</span> </li> <li>目前线上的监控 waring:600 , critial : 800</li> </ul> </li> <li>FIN_WAIT1 <ul style="margin:5px 0px 5px 40px; padding:0px"><li>主动关闭的一方,在发出FIN请求之后,也就是在<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5Zub5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP四次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>的第1步</li></ul> </li> <li>CLOSE_WAIT <ul style="margin:5px 0px 5px 40px; padding:0px"><li>被动关闭的一方,在接受到客户端的FIN后,也就是在<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5Zub5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP四次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>的第2步</li></ul> </li> <li>FIN_WAIT2 <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>主动关闭的一方,在接受到被动关闭一方的ACK后,也就是<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5Zub5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP四次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>的第2步</li> <li>sysctl -w net.ipv4.tcp_fin_timeout=30, 可以设定被动关闭方返回FIN后的超时时间,有效回收链接,避免syn-flood.</li> </ul> </li> <li>LASK_ACK <ul style="margin:5px 0px 5px 40px; padding:0px"><li>被动关闭的一方,在发送ACK后一段时间后(确保客户端已收到),再发起一个FIN请求。也就是<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5Zub5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP四次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>的第3步</li></ul> </li> <li>TIME_WAIT <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>主动关闭的一方,在收到被动关闭的FIN包后,发送ACK。也就是<a rel="nofollow" href="http://www.4ucode.com/pages/createpage.action?spaceKey=ccbu&amp;encodedTitle=VENQ5Zub5qyh5o-h5omL&amp;linkCreation=true&amp;fromPageId=45003990" style="text-decoration:initial; color:rgb(21,80,103)">TCP四次握手<sup style="line-height:21px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/plus.gif" width="7" height="7" style="border:0px; max-width:100%"></sup></a>的第4步</li> <li>sysctl -w net.ipv4.tcp_tw_recycle = 1 , 打开快速回收TIME_WAIT,<span style="color:rgb(255,0,0)">Enabling this option is not recommended since this causes problems when working with NAT (Network Address Translation)</span> </li> <li>sysctl -w net.ipv4.tcp_tw_reuse =1, 快速回收并重用TIME_WAIT的链接,<span style="color:rgb(255,0,0)">貌似和tw_recycle有冲突,不能重用就回收?</span> </li> <li>net.ipv4.tcp_max_tw_buckets: 处于time_wait状态的最多链接数,默认为180000.</li> </ul> </li> </ol> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <div> <table border="0" style="line-height:25px"> <colgroup> <col width="24"> <col> </colgroup> <tbody><tr> <td valign="top"> <h2 style="line-height:37px"><img border="0" alt="" align="absMiddle" src="http://www.4ucode.com/images/icons/emoticons/check.gif" width="16" height="16" style="border:0px; max-width:100%"></h2> </td> <td> <h2 style="line-height:37px"><span style="color:rgb(255,0,0)">相关说明</span></h2> <ol style="margin:5px 0px 5px 40px; padding:0px"> <li>主动关闭方在接收到被动关闭方的FIN请求后,发送成功给对方一个ACK后,将自己的状态由FIN_WAIT2修改为TIME_WAIT,而必须 再等2倍的MSL(Maximum Segment Lifetime,MSL是一个数据报在internetwork中能存在的时间)时间之后双方才能把状态 都改为CLOSED以关闭连接。目前RHEL里保持TIME_WAIT状态的时间为60秒</li> <li>keepAlive策略可以有效的避免进行三次握手和四次关闭的动作</li> </ol> </td> </tr></tbody> </table> </div> <h3 style="line-height:28px; margin:0px; padding:0px"> <a name="tcp%E9%93%BE%E6%8E%A5%E7%9A%84%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81-%E5%85%B6%E4%BB%96%E7%BD%91%E7%BB%9C%E9%87%8D%E8%A6%81%E5%8F%82%E6%95%B0" rel="nofollow" style="color:rgb(21,80,103)"></a>其他网络重要参数</h3> <h3 style="line-height:28px; margin:0px; padding:0px"> <a name="tcp%E9%93%BE%E6%8E%A5%E7%9A%84%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%8A%B6%E6%80%81-net.ipv4.tcprmem%E5%8F%82%E6%95%B0" rel="nofollow" style="color:rgb(21,80,103)"></a>net.ipv4.tcp_rmem 参数</h3> <ul style="margin:5px 0px 5px 40px; padding:0px"><li>默认值: min=4096 default=87380 max=4194304</li></ul> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h3 style="line-height:28px; margin:0px; padding:0px">net.ipv4.tcp_wmem 参数</h3> <ul style="margin:5px 0px 5px 40px; padding:0px"><li>默认值: min=4096 default=16384 max=4194304</li></ul> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <hr> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h1 style="line-height:50px">tcpdump</h1> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> tcpdump是linux系统自带的抓包工具,主要通过命令行的方式,比较适合在线上服务器进行抓包操作,如果是windows或者ubuntu完全可 以选择一些图形化的工具,ubuntu比较推荐用wireshark,安装方式很简单sudo apt一下即可。</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h2 style="line-height:37px">命令行格式:</h2> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> tcpdump [ -adeflnNOpqStvx ] [ -c 数量 ] [ -F 文件名 ][ -i 网络接口 ] [ -r 文件名] [ -s snaplen ][ -T 类型 ] [ -w 文件名 ] [表达式 ]</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h2 style="line-height:37px">常用的参数:</h2> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> -l    使标准输出变为缓冲行形式;<br> -n    不把网络地址转换成名字;</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> -c    在收到指定的包的数目后,tcpdump就会停止;<br> -i    指定监听的网络接口;<br> -w    直接将包写入文件中,并不分析和打印出来;<br> -s 指定记录package的大小,常见 -s 0 ,代表最大值65535,一半linux传输最小单元MTU为1500,足够了</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> -X 直接输出package data数据,默认不设置,只能通过-w指定文件进行输出</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h2 style="line-height:37px">常用表达式:</h2> <ol style="margin:5px 0px 5px 40px; padding:0px"> <li>关于类型的关键字,主要包括host,net,port</li> <li>传输方向的关键字,主要包括src , dst ,dst or src, dst and src</li> <li>协议的关键字,主要包括fddi,ip ,arp,rarp,tcp,udp等类型</li> <li>逻辑运算,取非运算是 'not ' '! ', 与运算是'and','&amp;&amp;';或运算 是'or' ,'||'</li> <li>其他重要的关键字如下:gateway, broadcast,less,greater<br> </li> </ol> <h2 style="line-height:37px">实际例子:</h2> <h3 style="line-height:28px; margin:0px; padding:0px">1. http数据包抓取 (直接在终端输出package data)</h3> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> tcpdump tcp port 80 -n -X -s 0 指定80端口进行输出</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h3 style="line-height:28px; margin:0px; padding:0px">2. 抓取http包数据指定文件进行输出package</h3> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> tcpdump tcp port 80 -n -s 0 -w /tmp/tcp.cap</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 对应的/tmp/tcp.cap基本靠肉眼已经能看一下信息,比如http Header , content信息等</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h3 style="line-height:28px; margin:0px; padding:0px">3. 结合管道流</h3> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> tcpdump tcp port 80 -n -s 0 -X -l | grep xxxx</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 这样可以实时对数据包进行字符串匹配过滤</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h3 style="line-height:28px; margin:0px; padding:0px">4. mod_proxy反向代理抓包</h3> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 线上服务器apache+jetty,通过apache mod_proxy进行一个反向代理,80 apache端口, 7001 jetty端口</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> apache端口数据抓包: tcpdump tcp port 80 -n -s 0 -X -i eth0   注意:指定eth0网络接口</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> jetty端口数据抓包: tcpdump tcp port 7001 -n -s 0 -X -i lo 注意:指定Loopback网络接口</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h3 style="line-height:28px; margin:0px; padding:0px">5. 只监控特定的ip主机</h3> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> tcpdump tcp host 10.16.2.85 and port 2100 -s 0 -X </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 需要使用tcp表达式的组合,这里是host指示只监听该ip</p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p> <h2 style="line-height:37px"><span style="color:rgb(255,0,0)">小技巧:</span></h2> <h2 style="line-height:37px">1. 可结合tcpdump(命令) + wireshark(图形化)</h2> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 操作: </p> <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>在服务器上进行tcpdump -w /tmp/tcp.cap 指定输出外部文件</li> <li>scp /tmp/tcp.cap 拷贝文件到你本地</li> <li>wireshark &amp;  启动wireshark</li> <li>通过 File -&gt; Open  打开拷贝下来的文件,这样就可以利用进行数据包分析了</li> <li>剩下来的事就非常方便了</li> </ul> <h2 style="line-height:37px">2. 服务器上直接安装wireshark + X11协议反向输出到本机</h2> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 注意: </p> <ul style="margin:5px 0px 5px 40px; padding:0px"> <li>wireshark如果要开启网络监控,需要通过root方式启动,否则无法直接通过网卡进行数据抓包</li> <li>X11的反向输出,需要客户机支持X11协议,如果是ubuntu天生支持很方便,如果是windows需要安装个软件</li> </ul></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics