tcpdump 抓包 通过 Wireshark分析抓包文件

1. tcpdump的基本原理

1.1  tcpdump starce 的区别

在本机中的进程的系统行为调用跟踪,starce   是一个很好的工具;但是在网络问题的调试中,tcpdump 应该是一个必不可少的工具;能清晰分析网络通信的信息。

默认情况下,tcpdump 不会抓取本机内部通讯的报文   ;根据网络协议栈的规定,对于报文,即使是目的地是本机(自己和自己通信),也需要经过本机的网络协议层,所以本机通讯肯定是通过API进入内核,并且完成路由选择。【比如本机的TCP通信,也必须要socket通信的基本要素:src ip port    dst port】

1.2 linux下tcpdump 抓包的基本原理

Linux 的抓包是通过注册一种虚拟的底层网络协议来完成网络报文(准确的说是:网络设备)消息的处理权,当网卡接收到一个网络报文后,它会遍历系统中所有已经注册的网络协议,例如:以太网协议、x25协议处理模块来尝试进行报文的解析处理。这一点和文件系统的挂载相似,就是让系统中所有已经注册的文件系统来进行尝试挂载,如果哪一个认为自己可以处理,那么就完成挂载。
当抓包模块把自己伪装成一个网络协议的时候,系统在收到报文的时候就会这个伪协议一次机会,让它来对网卡收到的报文进行一次处理,此时该抓包模块就会趁机把报文完整的copy一份,然后把这个copy的数据给抓包模块(tcpdump)
 tcpdump 具体的工作原理:协议的注册、网络层对接收报文的处理  :http://tsecer.blog.163.com/blog/static/15018172012822114125900/
 

2.  tcpdump 命令详解

2.1 tcpdump 命令的基本简介

  • tcpdump :dump the traffic on a network 根据使用者的定义对网络上的数据包进行截获的分析工作;tcpdump 可以讲网络中传送的数据包的header完全截获下来进行分析;它支持对网络层(net  IP 段)、协议(TCP/UDP)、主机(src/dst host)、网络或端口(prot)的过滤,并提供and、or、not 等逻辑语句来帮助你去掉无用的信息。
  • tcpdump:默认情况下,sudo /usr/sbin/tcpdump 会监视 第一个网络接口 一般是eth0 的所有port 所有协议的 数据包

2.2 tcpdump 的主要选项

协议 TCP,UDP,IP,ARP,RARP,ETHER,FDDI
网络(net) 192.168.0.0/16 IP网段
方向(host) src,dst,src or dst,src and dst
端口(prot) 80 22 !22  9999 
逻辑 and,or,not  &&,||,!
  • 协议:tcp udp ip arp rarp ether fddi   协议这选项要放在 前面,因为要过滤数据包
  • 网络:net  ip网段     tcpdump net 192.168.1.0/24   监听这个网段
  • 方向:host  就是主机  src 是源主机、dst 是目的主机 or  and 是逻辑 tcpdump host test.hostname 
  • 端口:port  指定tcpdump 监听的端口  tcpdump src 192.168.1.11 and port 80  监听源IP是192.168.1.11 的80端口
  • 逻辑:是表示 与 或  即:同时满足或满足之一
        前面的四个选项:协议、net 、host、port 如果同时在 tcpdump 里面 时必须用 逻辑 and 连接起来;而且如用() 一定要转义,否则会包语法错误。    
 
[html] view plain copy
 
  1. sudo /usr/sbin/tcpdump tcp and srchosttest1.hostnameortest2.hostname and port 3333 and net 172.21.121.0/24  -c 10 -vvv  
 

3. tcpdump 的选项

3.1 主要选项

  • -i :指定网卡 默认是 eth0   
  • -n :线上ip,而不是hostname
  • -c :指定抓到多个包后推出
  • -A:以ASCII方式线上包的内容,这个选项对文本格式的协议包很有用
  • -x:以16进制显示包的内容
  • -vvv:显示详细信息
  • -s :按包长截取数据;默认是60个字节;如果包大于60个字节,则抓包会出现丢数据;所以我们一般会设置 -s 0 ;这样会按照包的大小截取数据;抓到的是完整的包数据
  • -r:从文件中读取【与 -w 对应,/usr/sbin/tcpdump -r test.out  读取 tcpdump -w  test.out】
  • -w:到处指向文件【一定要用,-w t.out ,然后用 -r t.out 来看抓包信息,否则可读性很差】

3.2 tcpdump 抓包的具体含义

       tcpmdump 抓包出来分析包的具体含义
  1.        包携带的标志:
  • S:S=SYC  :发起连接标志
  • P:P=PUSH:传送数据标志
  • F:F=FIN:关闭连接标志
  • ack:表示确认包
  • RST=RESET:异常关闭连接
  • 表示没有任何标志
  1.        抓取包的具体含义: 
[html] view plain copy
 
  1. server.hostname: sudo /usr/sbin/tcpdump tcp port 80 and host clinet.hostname -c 30 -vvv -w t.out  
  2. sudo /usr/sbin/tcpdump -r t.out  
[html] view plain copy
 
  1. 13:59:29.012370 IP client.hostname.tbsite.net.50741 > server.hostname.http: S 562843056:562843056(0) win 14480 <mss 1460,sackOK,timestamp 230871413 4035264472,nop,wscale 7>  
  2. 13:59:29.012374 IP server.hostname.http > client.hostname.tbsite.net.50741: S 2306923370:2306923370(0) ack 562843057 win 5792 <mss 1460,sackOK,timestamp 4035271299 230871413,nop,wscale 9>  
  3. 13:59:29.012547 IP client.hostname.tbsite.net.50741 > server.hostname.http: . ack 1 win 114 <nop,nop,timestamp 230871413 4035271299>  
  4. 13:59:29.012582 IP client.hostname.tbsite.net.50741 > server.hostname.http: P 1:1006(1005) ack 1 win 114 <nop,nop,timestamp 230871413 4035271299>  
  5. 13:59:29.012593 IP server.hostname.http > client.hostname.tbsite.net.50741: . ack 1006 win 16 <nop,nop,timestamp 4035271299 230871413>  
  6. 13:59:29.025011 IP client.hostname.tbsite.net.50777 > server.hostname.http: S 2359624339:2359624339(0) win 14480 <mss 1460,sackOK,timestamp 230871425 4035264472,nop,wscale 7>  
  7. 13:59:29.025022 IP server.hostname.http > client.hostname.tbsite.net.50777: S 2305562654:2305562654(0) ack 2359624340 win 5792 <mss 1460,sackOK,timestamp 4035271312 230871425,nop,wscale 9>  
  8. 13:59:29.025197 IP client.hostname.tbsite.net.50777 > server.hostname.http: . ack 1 win 114 <nop,nop,timestamp 230871425 4035271312>  
  9. 13:59:29.025228 IP client.hostname.tbsite.net.50777 > server.hostname.http: P 1:837(836) ack 1 win 114 <nop,nop,timestamp 230871425 4035271312>  
  10. 13:59:29.025240 IP server.hostname.http > client.hostname.tbsite.net.50777: . ack 837 win 15 <nop,nop,timestamp 4035271312 230871425>  
      
  •      第一行: S:表示 clinet.hostname 的临时端口50741向 server.hostname 80 端口发起连接,client 的初始包序号是: 562843056  ;滑动窗口(win 14480)的大小是:14480 [14k]  滑动窗口即tcp 接收缓冲区的大小,用于tcp 拥塞控制;mss 1460:可以接收的最大包长度,通常是MTU - 40 byte;IP头和TCP头各20byte
  •     第二行: S:表示SYN状态;是server.hostname 对第一行 clinet.hostname 发起连接的请求的回应;同时带上client 端 初始包序号 + 1:ack 562843057 ,即server.hostname 下次等待接收这个包序号的包,用于tcp 字节流的顺序控制(?). server.hostname 初始包序列号:2306923370 
  •    第三行:client.hostname 再次确认,tcp连接完成三次握手
  •    第四行:P:推送数据    client.hostname  通过 50741 端口向 server.hostname 发送数据包;数据包大小是 1005byte ;第五行是 server.hostname 响应这个数据包发送,接收这个数据包。----> 当完成后会出现一个 server.hostname     F   关闭连接的数据包,这里没有抓取
  • 第6行   ---->10行是对 1-5行的重复;因为机器是web服务是并发的。
 

4. tcpdump的实例:

  • 起步1: 抓取指定端口的包
    tcpdump -i eth0 -c 100

  • 起步2:抓取指定协议的包
    tcpdump -i eth0 -c 100 tcp
  • 起步3:抓取指定协议,指定port的包【tcp ip port src dst host net】
    tcpdump -i eth0 -c 100 tcp port 5440
  • 起步4:组合过滤条件【and or not】
    tcpdump -i eth0 -c 100 tcp port 5440  and src host 192.1.1.2
  • 起步5:抓取指定网段的包
    tcpdump -i eth0 -c 100 tcp port 5440 and src net 192.1.1.0/24
  • 高级1:写入文件并用wireshark在windows下分析
    tcpdump tcp -i eth1 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap
    然后用ftp传输到windows下面,在用wireshark打开cap文件即可
  • 高级2:提取http包
    tcpdump  -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854 -c 20 -w ./target.cap
    其中-s 0表示不限制包的大小;tcp[20:2]从表示tcp包头第20个字节开始的2个字节等于0x4745(对应字符GE)或者等于0x4854(对应HT);
    这种方法适用于tcp头中OPTION为空的情况,如果不为空,需要从第24个字节开始。

  注意:在linux抓取包后用tcpdump -r 只能看到包的简单信息;如果需要查询抓取包的详细信息需要把抓取的包target.cap 通过 ftp服务器传输到windows机器上;借助wireshark工具来分析数据;wireshark是一款开源的分析网络抓包的工具;灰常的好用。

5. tcpdump 抓包看rt

[html] view plain copy
 
  1. sudo tcpdump -s 0 -w /tmp/a.cap host 10.246.38.44  
  2.   
  3. tcpdump -A -r /tmp/a.cap   
[html] view plain copy
 
  1. <<<<<<<<<<<<上面 client、server的三次握手省略;下面是三次握手成功后直接push 数据>>>>>>>>>>>>>>  
  2. 17:46:30.138829 IP client.hostname.55445 >server.dns.com: P 1:741(740)  
  3. ack 1 win 46 <nop,nop,timestamp 221364282 177661952>  
  4. E.....@.@...  
  5. ..%  
  6. .&,...P......uH....mG.....  
  7. 1.:  
  8. ...POST /ecpmdsp_bid HTTP/1.1    <<<<<<POST 说明;client 发送query 数据;在上面的的行中找到时间点:【17:46:30.138829】  
  9. User-Agent: Mozilla/5.0 (compatible; Tanx/1.0; Linux)  
  10. Host: server.dns.com                         <<<<<<<<<<HTTP服务、web (apache、nginx)>>>>>>>>>>  
  11. Accept: */*  
  12. Content-Type:application/octet-stream  
  13. Connection:Keep-Alive  
  14. Content-Length: 524  
  15.   
  16. ...  
  17. 0af62e25315451c420f6000000000001..BpNFCj5dkAgCAQYTS24VMRxz".10.246.157.167*..Mozilla/4.0  
  18. (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR  
  19. 2.0.50727; .NET CLR 3.5.30729; .NET CLR  
  20. 3.0.30722.mcdonalds.com.cn:Ihttp://static.atm.youku.com/Youku2013/201304/0424/22371/600-90-under.html@...H.R.....ad_pid_pid_pid..600x90(.8.@.H.X.`.h...................................................................................................................................................................  
  21. 17:46:30.138927 IP server.dns.com.http >  
  22. client.hostname.55445: . ack 741 win 125 <nop,nop,timestamp  
  23. 177661952 221364282>  
  24. E..4.H@.?.4?  
  25. .&,  
  26. ..%.P....uH.......}.^.....  
  27.   
  28. 1.:  
  29. 17:46:30.219197 IP client.hostname.55445 >  
  30. server.dns.com.http: F 741:741(0) ack 1 win 46  
  31. <nop,nop,timestamp 221364362 177661952>  
  32. E..4..@.@...  
  33. ..%  
  34. .&,...P......uH..........  
  35. 1..  
  36. ...  
  37. 17:46:30.223900 IP server.dns.com.http >   client.hostname.55445: P 1:154(153) ack 742 win 125    <nop,nop,timestamp 177662037 221364362>  
  38. E....I@.?.3.  
  39. .&,  
  40. ..%.P....uH.......}Y......  
  41.   
  42. 1..HTTP/1.1 200 OK              <<<<<<<<<<<<server.dns.com 的respose 包  200 OK;所以返回数据了;这时找到下面行的时间点【17:46:30.223910】  
  43. Server: Tengine  
  44. Date: Fri, 21 Jun 2013 09:46:30 GMT  
  45. Content-Length: 36  
  46. Connection: keep-alive  
  47.   
  48. ... 0af62e25315451c420f6000000000001  
  49. 17:46:30.223910 IP client.hostname.55445 > server.dns.com.http: R 2868451528:2868451528(0) win 0  
  50. E..(..@.@...  
  51. ..%  
  52. .&,...P........P.......  
  53. 17:46:30.223947 IP server.dns.com.http >client.hostname.55445: F 154:154(0) ack 742 win 125<nop,nop,timestamp 177662037 221364362>   <<<<<<<<<<<<此次通信结束;F = final >>>>>>>>>>  
  54. E..4.J@.?.4=  
  55.    
  56. 所以,我们可以计算出从发出query;到收到respose的时间是:  【17:46:30.223910】  -  【17:46:30.138829】 = 0.085081us  == 85.08ms  
原文地址:https://www.cnblogs.com/lishijia/p/5699217.html