美文网首页Network
十九、tcpdump抓包

十九、tcpdump抓包

作者: 胖虎喜欢小红 | 来源:发表于2020-01-27 19:29 被阅读0次

    tcpdump是一款强大的网络抓包工具,运行在Linux平台上。熟悉tcpdump的使用能够帮助我们分析、调试网络数据。但是要想很好地掌握tcpdump, 就必须对网络报文(TCP/IP协议)有一定的了解。不过对于简单的使用来说,只要有网络基础概念就行了。

    作为互联网上经典的的系统管理员必备工具,tcpdump以其强大的功能,灵活的截取策略,成为每个高级的系统管理员分析网络,排查问题等所必备的工具之一。在实际工作中,需要以root权限去执行该命令。

    一、常用命令选项

    -e #在输出行打印出数据链路层的头部信息
    -c #指定收取数据包的次数,即在收到指定数量的数据包后退出tcpdump
    -f #将外部的Internet地址以数字的形式打印出来,即不显示主机名
    -i #指定监听网络接口
    -n #不把网络地址转换为名字
    -nn #进行ip和端口名称的转换,一个n不要ip解析到域名,不要端口解析到协议,有时候会很慢
    -w #将捕获到的信息保存到文件中,且不分析和打印在屏幕
    -r #从指定的文件中读取数据,一般是-w保存的文件
    -s #从每个组中读取在开始的snaplen个字节,而不是默认的68个字节,68字节不适用与ip,icmp,tcp协议
    -v #输出稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息
    -vv#输出详细的报文信息
    

    二、安装tcpdump工具

    [root@linux_server ~]# yum -y install tcpdump
    

    实验环境

    1.准备两台机器
    192.168.94.128 ----linux_server
    192.168.94.129 ----client
    2.两台机器均关闭防火墙和selinux
    

    监视指定网络接口的数据包

    [root@linux_server ~]# tcpdump -i ens32
    

    监听网卡端口为80的应用发出和接收的所有http协议包

    [root@linux_server ~]# ss -lnta | grep :80
    LISTEN     0      128         :::80                      :::*
    [root@linux_server ~]# tcpdump -i ens32 port 80
    [root@client ~]# curl http://192.168.94.128   //测试
    

    监视ens32网卡上源地址192.168.94.129的所有网络包

    [root@linux_server ~]# tcpdump -i ens32 src 192.168.94.129
    [root@client ~]# ssh root@192.168.94.128  //测试
    

    监视ens32网卡上目的地址是192.168.94.129的所有网络包

    [root@linux_server ~]# tcpdump -i ens32 dst 192.168.94.129
    

    抓取所有经过 ens32,目的或源地址是 192.168.94.129 的网络数据:

    [root@linux_server ~]# tcpdump -i ens32 host 192.168.94.129
    
    两台机器同时操作
    [root@linux_server ~]# ip a
    抓来自192.168.94.129这台机器的imcp协议的包
    [root@linux_server ~]# tcpdump -i ens32 icmp -vv -nn
    
    client:
    [root@client ~]# ping -c 1 192.168.94.128  #ping tcpdump的机器一次
    
    server:
    [root@linux_server ~]# tcpdump -i ens32 icmp -vv -nn
    tcpdump: listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
    18:56:46.411645 IP (tos 0x0, ttl 64, id 3764, offset 0, flags [DF], proto ICMP (1), length 84)
        192.168.94.129 > 192.168.94.128: ICMP echo request, id 6142, seq 1, length 64
    18:56:46.411673 IP (tos 0x0, ttl 64, id 44288, offset 0, flags [none], proto ICMP (1), length 84)
        192.168.94.128 > 192.168.94.129: ICMP echo reply, id 6142, seq 1, length 64
    
    echo request :请求包
    echo reply:回复包
    tos:服务类型:0x0表示正常的数据包,
    
    实验二 : 抓一个tcp的包
    server:
    [root@linux_server ~]# yum install -y epel*
    [root@linux_server ~]# yum install -y nginx
    [root@linux_server ~]# systemctl start nginx
    [root@linux_server ~]# netstat -lntp | grep :80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6252/nginx: master
    [root@linux_server ~]# tcpdump -i ens32 tcp -vv -nn -w tcp.txt
    
    client:
    [root@client ~]# yum install -y elinks   #test机器安装elinks客户端工具
    [root@client ~]# elinks --dump http://192.168.94.128/    //访问
    
    server:
    [root@linux_server ~]# ls
    anaconda-ks.cfg  tcp.txt   ---抓包生成的文件,不能用vim查看
    [root@linux_server ~]# tcpdump -r tcp.txt   #读生成的文件
    

    导入wireshark

    [root@linux_server ~]# tcpdump -i ens32 tcp -vv -nn | less
    [root@client ~]# elinks --dump http://192.168.94.128/   --访问,文本模式下
    
    image.png
    8个标记
    SYN(synchronous建立联机) 
    ACK(acknowledgement 确认) 用.表示
    PSH(push传送)
    FIN(finish结束) 
    RST(reset重置) 
    URG(urgent紧急)
    CWR
    ECE
    ==========
    Sequence number(顺序号码) 
    

    重新抓包一次并保存。

    [root@linux_server ~]# rm tcp.txt 
    rm: remove regular file ‘tcp.txt’? y
    [root@linux_server ~]# tcpdump -i ens32 tcp -vv -nn -w tcp.txt
    [root@client ~]# elinks --dump http://192.168.94.128/
    [root@linux_server ~]# sz tcp.txt   #下载到桌面
    
    image-20191130180626150.png image-20191130180649842.png

    相关文章

      网友评论

        本文标题:十九、tcpdump抓包

        本文链接:https://www.haomeiwen.com/subject/ncsethtx.html