tcpdump dump 网络流量

 tcpdump dump 网络流量

描述:  Tcpdump 打印一个包的内容的描述在一个网络接口  匹配布尔值表达式

它也可以运行使用-w flag, 这样会保存包的数据到一个文件用于后面分析,

使用-r flag 可以从保存的packet 文件而不是从网络接口读取

在所有的例子,只有packets 匹配表达式才会被tcpdump处理

Tcpdump 如果不带-c 运行, 会继续捕获数据包知道被信号打断(通常,例如,输入典型的 control-C) 


或者 一个SIGTERM signal (kill 命令)

如果你运行-c选项,它会捕获包直到 被SIGINT or SIGTERM signal  或者指定的数量的包后

当tcpdump 完成捕获数据包后,它会报告:

1.packets "捕获的" ( 这是包的数量tcpdump 已经接受和处理的)


2.packets "过滤器接收的"(这个取决于操作系统 ,如果一个filter 是在命令行指定,

在一些OSes上 它统计包不管 他们是否被过滤器表达式,即使它们被表达式过滤器匹配,

不管 tcpdump 已经读或者处理他们,在其他系统上它只统计 表达式匹配的包 

3. packets "dropped by kernel" (这个是被丢弃的包,由于缺少buffer space,如果OS 报告信息给应用 如果没有,它是报告为0)



tcpdump -i eth0 -nn -vv port 53

tcpdump -i eth1 -s 0 -l -w - dst port 3306 | strings |

-i 侦听接口,如果没有指定,tcpdump 搜索系统接口列表

-nn  不要转换协议和端口号


      -s     Snarf  snaplen  bytes of data from each packet rather than the default of 65535 bytes.  Packets truncated because of a
              limited snapshot are indicated in the output with ‘‘[|proto]’’, where proto is the name of the protocol level at which
              the  truncation has occurred.  Note that taking larger snapshots both increases the amount of time it takes to process
              packets and, effectively, decreases the amount of packet buffering.  This may cause packets to be  lost.   You  should
              limit snaplen to the smallest number that will capture the protocol information you’re interested in.  Setting snaplen
              to 0 sets it to the default of 65535, for backwards compatibility with recent older versions of tcpdump.

      抽取 数据的快照长度字节 从每个包而不是默认的65535字节。

      包被截断因为一个受限的快照是致命在输出 使用 ‘‘[|proto]’’, 

      proto 是协议的名字
     
      注意大的快照会增加处理包的总的时间,效率,降低包缓存的总量
      
      这个也会导致包丢失,你需要限制快照长度到小的值
      
      设置snaplen 为0 是默认值



Setting snaplen
              to 0 sets it to the default of 65535



       -l     Make stdout line buffered.  Useful if you want to see the data while capturing it.  E.g.,
              ‘‘tcpdump  -l  |  tee dat’’ or ‘‘tcpdump  -l   > dat  &  tail  -f  dat’’.
           
        让标准输出 buffered,有用的如果你需要看数据当捕捉时 
        

        

  -w     Write  the  raw  packets  to  file  rather  than parsing and printing them out.  They can later be printed with the -r
              option.  Standard output is used if file is ‘‘-’’.  See pcap-savefile(5) for a description of the file format.


        写raw 数据包到文件 而不是解析和打印它们。
        它们可以随后被打印使用-r选项,标准输出被使用,如果file 是"-"


    -r     Read packets from file (which was created with the -w option).  Standard input is used if file is ‘‘-’’.

    从文件读取包(使用-w选项创建) 标准输入是使用file "-"

 抓取所有经过 eth1,目的或源端口是 25 的网络数据
# tcpdump -i eth1 port 25
- 源端口
# tcpdump -i eth1 src port 25
- 目的端口
# tcpdump -i eth1 dst port 25网络过滤


strings 提取2进制数据

tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings



- 抓取所有经过 eth1,目的或源端口是 25 的网络数据
# tcpdump -i eth1 port 25
- 源端口
# tcpdump -i eth1 src port 25
- 目的端口
# tcpdump -i eth1 dst port 25网络过滤


这个前提是必须走网卡



#!/bin/bash

#this script used montor mysql network traffic.echo sql

tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | perl -e '

while(<>) { chomp; next if /^[^ ]+[ ]*$/;

if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL)/i)

{

if (defined $q) { print "$q\n"; }

$q=$_;

} else {

$_ =~ s/^[ \t]+//; $q.=" $_";

}

}'





posted @ 2016-12-06 19:49  czcb  阅读(245)  评论(0编辑  收藏  举报