zoukankan      html  css  js  c++  java
  • tcpdump 的正确食用方法

    tcpdump 使用笔记

    重要报文头 字段表

    ip header

    offset type len
    2 total len 2
    8 ttl 1
    9 protocol number 1
    12 src 4
    16 dst 4

    tcp header

    offset type len
    0 src port 2
    2 dst port 2
    12 tcp seg len 1
    13 Flags 1
    14 win size 2
    16 urgent pointer 1

    Flags

    需要特别说明下Flags 选项

       Flags: 0x018 (PSH, ACK)
            000. .... .... = Reserved: Not set
            ...0 .... .... = Nonce: Not set
            .... 0... .... = Congestion Window Reduced (CWR): Not set
            .... .0.. .... = ECN-Echo: Not set
            .... ..0. .... = Urgent: Not set
            
      # 前6位作为保留字段未用,主要为后6位,通过这个可以过滤出报文的类型
      
            .... ...1 .... = Acknowledgment: Set
            .... .... 1... = Push: Set
            .... .... .0.. = Reset: Not set
            .... .... ..0. = Syn: Not set
            .... .... ...0 = Fin: Not set
            [TCP Flags: *******AP***]
    
     
    

    基础使用

    tcpdump -i interface #这个会指定所使用的网卡
    
    -x #会显示每一个包的header,用16进制显示
    -X #和上面一个类似但是会显示assic码
    -v/-vv #会尽可能的显示
    -b #可以指定协议 但建议只用于链路层的协议
    -n #不转化地址 保留原来的样子
    -c #指定报文截取的数量
    -w #指定报文输出到指定文件,并且是wireshark可认的格式
    -F #指定报文的入,如离线的报文。。一般都用wireshark分析
    

    高级版本

    tcpdump有一个很智能的规则可以指定,有点类似ip指令,如果你对TCP/IP足够了解的话可以过滤出各种复杂的网络报文出来例如

    指定ip

    # 我内网的有一个服务器172.168.50.175
    # 我只想要它发出的所有报文
    > tcpdump -nvvxXe "src host 172.16.50.175" 
    
    cpdump: data link type PKTAP
    tcpdump: listening on pktap, link-type PKTAP (Packet Tap), capture size 262144 bytes
    20:46:52.438847 78:a1:06:ef:ca:74 > 80:e6:50:15:d3:68, ethertype IPv4 (0x0800), length 296: (tos 0x0, ttl 62, id 64687, offset 0, flags [DF], proto TCP (6), length 282)
        172.16.50.175.6800 > 192.168.1.139.62139: Flags [P.], cksum 0x2419 (correct), seq 1283498242:1283498472, ack 943155554, win 1452, options [nop,nop,TS val 194911394 ecr 1260591022], length 230
    	0x0000:  80e6 5015 d368 78a1 06ef ca74 0800 4500  ..P..hx....t..E.
    ...
    ...
    	0x0110:  6e74 656e 742d 456e 636f 6469 6e67 3a20  ntent-Encoding:.
    	0x0120:  677a 6970 0d0a 0d0a                      gzip....
    	
    	
    20:46:52.439205 78:a1:06:ef:ca:74 > 80:e6:50:15:d3:68, ethertype IPv4 (0x0800), length 1514: (tos 0x0, ttl 62, id 64688, offset 0, flags [DF], proto TCP (6), length 1500)
        172.16.50.175.6800 > 192.168.1.139.62139: Flags [.], cksum 0xafed (correct), seq 230:1678, ack 1, win 1452, options [nop,nop,TS val 194911394 ecr 1260591022], length 1448
    	0x0000:  80e6 5015 d368 78a1 06ef ca74 0800 4500  ..P..hx....t..E.
    	0x0010:  05dc fcb0 4000 3e06 9978 ac10 32af c0a8  ....@.>..x..2...
    	0x0020:  018b 1a90 f2bb 4c80 a1e8 3837 6962 8010  ......L...87ib..
    	0x0030:  05ac afed 0000 0101 080a 0b9e 1ca2 4b23  ..............K#
    ....
    ....
    
    2 packets captured
    27 packets received by filter
    0 packets dropped by kernel
    

    指定端口(协议)

    # 我内网的有一个服务器172.168.50.175 
    # 我在内网开启了 aria2的rpc 服务端口是6800
    # 我只想要它发出的所有报文
    > tcpdump -nvvxXe "src host 172.16.50.175 and tcp[0:2] = 6800"  
    # 这里的tcp[0:2] 代表了tcp包的从0个字节开始的2个字节的数值的十进制大小为6800 ,tcp[0:2]就代表了源端口
    # tcp[2:2]代表了目的端口
    # 而tcp包的表我会在后面附上,熟悉网络协议的会觉得很简单
    
    tcpdump: data link type PKTAP
    tcpdump: listening on pktap, link-type PKTAP (Packet Tap), capture size 262144 bytes
    20:56:53.181223 78:a1:06:ef:ca:74 > 80:e6:50:15:d3:68, ethertype IPv4 (0x0800), length 296: (tos 0x0, ttl 62, id 64914, offset 0, flags [DF], proto TCP (6), length 282)
        172.16.50.175.6800 > 192.168.1.139.62139: Flags [P.], cksum 0xb405 (correct), seq 1283638867:1283639097, ack 943210754, win 1452, options [nop,nop,TS val 195061573 ecr 1261190685], length 230
    	0x0000:  80e6 5015 d368 78a1 06ef ca74 0800 4500  ..P..hx....t..E.
    ...
    	0x0050:  4b0d 0a44 6174 653a 204d 6f6e 2c20 3037  K..Date:.Mon,.07
    	0x0060:  204d 6172 2032 3031 3620 3132 3a35 363a  .Mar.2016.12:56:
    

    指定ttl(通过ttl能够确定系统的类型)

    # 我内网的有一台Linux服务器 一台surface book跑着win10
    # 我在内网开启了 aria2的rpc 服务端口是6800
    # 我只想要win10 1080 (shadowsocks 服务)端口发出的包
    # ttl 128 一般为win 62 一般为linux,详细可以google 
    > tcpdump -nvvxXe "ip[8] = 128 and tcp[0:2] = 3008"  
    21:33:05.902559 78:a1:06:ef:ca:74 > 80:e6:50:15:d3:68, ethertype IPv4 (0x0800), length 507: (tos 0x0, ttl 128, id 18357, offset 0, flags [DF], proto TCP (6), length 493)
        182.92.112.147.3008 > 192.168.1.139.49200: Flags [P.], cksum 0xd42b (correct), seq 154:595, ack 1374, win 503, options [nop,nop,TS val 95961558 ecr 1263358254], length 441
    	0x0000:  80e6 5015 d368 78a1 06ef ca74 0800 4500  ..P..hx....t..E.
    ...
    	0x01e0:  6123 5232 0b91 7078 595c 0fcc dac8 7765  a#R2..pxY....we
    	0x01f0:  e23f 046f 0bef 8394 8afb f2              .?.o.......
    
    

    指定报文类型

    # 我内网的有一台Linux服务器 
    # 我想完整的抓取 172.16.50.175 的最后一个报文 即 Flags 中为 FIN 报文
    # 而Flags 只关心FIN为1 所以 可以 tcp[13] & 0x1 =1
    > tcpdump -nvvxXe  " tcp[13] & 0x1 ==1 "
    
    21:44:43.620802 80:e6:50:15:d3:68 > 78:a1:06:ef:ca:74, ethertype IPv4 (0x0800), length 54: (tos 0x0, ttl 64, id 62960, offset 0, flags [DF], proto TCP (6), length 40)
        192.168.1.139.49368 > 17.134.62.234.443: Flags [F.], cksum 0x4ebd (correct), seq 3968770207, ack 2517715103, win 8192, length 0
    	0x0000:  78a1 06ef ca74 80e6 5015 d368 0800 4500  x....t..P..h..E.
    	0x0010:  0028 f5f0 4000 4006 323c c0a8 018b 1186  .(..@.@.2<......
    	0x0020:  3eea c0d8 01bb ec8e a09f 9611 489f 5011  >...........H.P.
    	0x0030:  2000 4ebd 0000                           ..N...
    
  • 相关阅读:
    Jdk1.8 HashMap源码分析
    瘦身部署应用
    Redis 创建和使用集群(yum方式安装低版本)
    Redis 创建和使用集群
    记录一下
    Oracle 开发人员权限控制
    Linux 免密登录远程服务器及执行相关命令
    Oracle 连接表空间并执行SQL文件
    MySQL 8.0.13安装教程(windows 64位) (转)
    scrapy中Selector的使用
  • 原文地址:https://www.cnblogs.com/Qingluan/p/rocedu.html
Copyright © 2011-2022 走看看