zoukankan      html  css  js  c++  java
  • unix常用抓包方法

    Unix&Linux网络抓包方法

     

     

    情境

             在网络故障诊断过程中,经常需要判断对方发送的网络包是否到达主机,进而了解问题出在网络层面还是主机应用层面。如果能够抓到包,但是应用数据仍然没有,可能应用程序解析出了问题,或者被主机自身防火墙屏蔽;否则可能是网络某处包被丢弃。解决此问题的一个重要手段就是在主机上抓包。

    原理

             抓包程序为了能够捕获所有流经本网卡的网络包,需要将网卡设置为“混杂模式”,这样所有的网络包都被送到抓包程序进行分析。如果网卡工作在“正常模式”,那么只有发送到本机的IP包,才会被内核接收,传送给应用程序。

    常用Unix系统的抓包方法

    HP-UX

    a)        启动抓包服务

    nettl -start

    b)        开始抓包

    格式:nettl -traceon [all|kind] -entity [ip|tcp|udp|all] -n num_files -tracemax tracefile_size*2 -file tracefile_path

    	-entity		用nettl –status获得
    	 Kind	
                             keyword      mask             keyword       mask
                              _____________________        ______________________
                              hdrin     0x80000000          state      0x04000000
                              hdrout    0x40000000          error      0x02000000
                              pduin     0x20000000          logging    0x01000000
                              pduout    0x10000000          loopback   0x00800000
                              proc      0x08000000
    
                              hdrin          Inbound Protocol Header.
                              hdrout         Outbound Protocol Header.
                              pduin          Inbound Protocol Data Unit (including header and data).
                              pduout         Outbound Protocol Data Unit(including header and data).
                              proc           Procedure entry and exit.
                              state          Protocol or connection states.
                              error          Invalid events or condition.
                              logging        Special kind of trace that contains a log message.
                              loopback       Packets whose source and destination system are the same.
                         For multiple kinds, the masks can be specified
                         separately or combined into a single number.  For
                         example, to enable both pduin and pduout (to trace all
                         packets coming into and out of the node) use either
                         pduin pduout or 0x10000000 0x20000000 or the
                         combination 0x30000000.
    
    

            

     

    举例:nettl -traceon pduin pduout -entity ns_ls_ip -n 5 -tracemax 88888 -file /tmp/wdraw0

               抓取流入和流出的ip包,最多5trace文件,文件大小44444KB.

    c)         停止抓包

    nettl -traceoff  -entity ns_ls_ip

    d)        停止抓包服务

    nettl –stop [-entity ns_ls_ip|all]

    e)        抓包记录文件分析

    1)      编辑过滤文件

    filter.conf:
     filter ip_saddr 192.6.2.1
     filter ip_daddr 192.6.2.1
     filter udp_sport 2049
     filter udp_dport 2049
     filter tcp_sport 23
     filter tcp_dport 23

    2)      查看抓包内容

    使用行模式来显示(这种模式下不会看到包的具体数据)

    netfmt -N -n -l -1 -c /tmp/filter.conf -f /tmp/wdraw0.TRC000

    在每行的显示前加上时间戳

    netfmt -T -n -l -1 -c /tmp/filter.conf -f /tmp/wdraw0.TRC000

    Solaris

             格式:snoop –d <ifname> host <主机ip>  port <端口号>

             举例:snoop -d bge0 host 192.168.1.1 port 1521

      包分析举例:snoop -r -P -d bge0 -c 3 -v -x 0 dst port 514

        -r  禁止dns解析

             -P  禁用混杂模式

             -d  指定抓包网卡

        -c   指定抓包数

            -v   显示链路层、IP层、和TCP层包头信息

            -x  显示包内容  

    Linux

             格式:tcpdump –I <ifname> dst port <端口号>

       举例:tcpdump -i eth0 dst port 1521

      包分析举例:tcpdump -n -p -i eth0 -c 3   -vvv -XX port 514

        -n    禁止域名解析

        -p 禁用混杂模式

        -i     指定抓包网卡

                   -c     指定抓包数

                  -vvv   显示包头信息

                  -XX 显示包内容,含链路层信息,

    AIX

    a)      Tcpdump

    b)      iptraceipreport命令配合使用

           iptrace产生trace文件;ipreport输出trace文件内容

      抓包

    # startsrc -s iptrace -a "-i en4 -p 9999,9998 /tmp/wdlog1"
    0513-059 已启动“iptrace 子系统”。子系统 PID 为 717180。
    # ps -ef|grep iptrace
        root  606546  311358   0 11时41分18秒  pts/7  0:00 grep iptrace 
        root  717180  143518   0 11时41分11秒      -  0:00 /usr/sbin/iptrace -i en4 -p 9999,9998 /tmp/wdlog1 
    # ls -l /tmp/wdlog1
    -rw-r--r--    1 root     system          511 12月30 11时41 /tmp/wdlog1
    # ls -l /tmp/wdlog1
    -rw-r--r--    1 root     system         1015 12月30 11时41 /tmp/wdlog1
    # stopsrc -s iptrace
    0513-044 请求停止 iptrace 子系统。
    # ps -ef|grep iptrace
        root  786762  311358   0 11时42分24秒  pts/7  0:00 grep iptrace 
    # ls -l /tmp/wdlog*
    -rw-r--r--    1 root     system         4437 12月30 11时42 /tmp/wdlog1
    

     分析包

      ipreport -ns /tmp/wdlog1

     
  • 相关阅读:
    Django跨域问题
    DOM,jquery,vue
    from和modelform的用法和介绍
    元类的__call__和__new__方法的作用
    Django学习之路由分发和反向解析
    Django 自定义auth_user
    Django创建对象的create和save方法
    Flask--(登录注册)抽取视图函数
    Flask--(项目准备)--添加日志
    Flask--(项目准备)--框架搭建,配置文件抽取,业务逻辑抽取
  • 原文地址:https://www.cnblogs.com/itfriend/p/1866866.html
Copyright © 2011-2022 走看看