zoukankan      html  css  js  c++  java
  • tcpdump命令格式及使用

    tcpdump采用命令行方式,它的命令格式为:tcpdump [ -adeflnNOpqStvx ] [ -c 数量 ] [ -F 文件名 ][ -i 网络接口 ] [ -r 文件名] [ -s snaplen ][ -T 类型 ] [ -w 文件名 ] [表达式 ]

     

    tcpdump的选项介绍

    -a        将网络地址和广播地址转帖变成名字;

    -d       将匹配信息包的代码以人们能够理解的汇编格式给出;

    -dd      将匹配信息包的代码以c语言程序段的格式给出;

    -ddd      将匹配信息包的代码以十进制的形式给出;

    -e       在输出行打印出数据链路层的头部信息;

    -f       将外部的Internet地址以数字的形式打印出来;

    -l        使标准输出变为缓冲行形式;

    -n        不把网络地址转帖换成名字;

    -t        在输出的每一行不打印时间戳;

    -v        输出一个稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息;

    -vv       输出详细的报文信息;

    -c        在收到指定的包的数目后,tcpdump就会停止;

    -F        从指定的文件中读取表达式,忽略其它的表达式;

    -i       指定监听的网络接口;

    -r       从指定的文件中读取包(这些包一般通过-w选项产生);

    -w       直接将包写入文件中,并不分析和打印出来;

    -T       将监听到的包直接解释为指定的类型的报文,常见的类型有rpc(远程过程调用)和snmp(简单网络管理协议;)

     

     

    tcpdump的表达式介绍

     

    表达式是一个正则表达式,tcpdump利用它作为过滤报文的条件,如果一个报文满足表达式的条件,则这个报文将会被捕获。如果没有给出任何条件,则网络上所有的信息包将会被截获。

    在表达式中一般如下几种类型的关键字:

    第一种是类型的关键字,主要包括host,net,port, 例如 host 210.27.48.2,指明 210.27.48.2是一台主机,net 202.0.0.0 指明202.0.0.0是一个网络地址,port 23 指明端口号是23。如果没有指定类型,缺省的类型是host.

    第二种是确定传输方向的关键字,主要包括src , dst ,dst or src, dst and src ,这些关键字指明了传输的方向。举例说明,src 210.27.48.2 ,指明ip包中源地址是210.27.48.2, dst net 202.0.0.0 指明目的网络地址是202.0.0.0 。如果没有指明方向关键字,则缺省是src or dst关键字。

    第三种是协议的关键字,主要包括fddi,ip ,arp,rarp,tcp,udp等类型。Fddi指明是在FDDI(分布式光纤数据接口网络)上的特定的网络协议,实际上它是"ether"的别 名,fddi和ether具有类似的源地址和目的地址,所以可以将fddi协议包当作ether的包进行处理和分析。其他的几个关键字就是指明了监听的包 的协议内容。如果没有指定任何协议,则tcpdump将会监听所有协议的信息包。

    除了这三种类型的关键字之外,其他重要的关键字如下:gateway, broadcast,less,greater,还有三种逻辑运算,取非运算是 'not ' '! ', 与运算是'and','&&';或运算 是'or' ,'||';这些关键字可以组合起来构成强大的组合条件来满足人们的需要,下面举几个例子来说明。

    (1)想要截获所有210.27.48.1 的主机收到的和发出的所有的数据包:

     #tcpdump host 210.27.48.1

    (2) 想要截获主机210.27.48.1 和主机210.27.48.2 或210.27.48.3的通信,使用命令:(在命令行中适用括号时,一定要

    #tcpdump host 210.27.48.1 and (210.27.48.2or 210.27.48.3 )

    (3) 如果想要获取主机210.27.48.1除了和主机210.27.48.2之外所有主机通信的ip包,使用命令:

    #tcpdump ip host 210.27.48.1 and !210.27.48.2

    (4)如果想要获取主机210.27.48.1接收或发出的telnet包,使用如下命令:

    #tcpdump tcp port 23 host 210.27.48.1

     

     

    tcpdump 的输出结果介绍

      下面我们介绍几种典型的tcpdump命令的输出信息

     

    数据链路层头信息

    使用命令#tcpdump --e host ice

    ice 是一台装有linux的主机,她的MAC地址是0:90:27:58:AF:1A 

    H219是一台装有SOLARIC的SUN工作站,它的MAC地址是8:0:20:79:5B:46;上一条命令的输出结果如下所示:

    21:50:12.847509eth0  ice.telnet 0:0(0)  ack 22535 win 8760 (DF)

    分析:21:50:12是显示的时间, 847509是ID号,eth0 表示从网络接口设备发送数据包, 8:0:20:79:5b:46是主机H219的MAC地址,它表明是从源地址H219发来的数据包. 0:90:27:58:af:1a是主机ICE的MAC地址,表示该数据包的目的地址是ICE . ip 是表明该数据包是IP数据包,60 是数据包的长度, h219.33357 > ice.telnet 表明该数据包是从主机H219的33357端口发往主机ICE的TELNET(23)端口. ack 22535表明对序列号是222535的包进行响应. win 8760表明发送窗口的大小是8760.

     

    ARP包的TCPDUMP输出信息

    使用命令

    #tcpdump arp

    得到的输出结果是:

    22:32:42.802509 eth0 > arp who-has routetell ice (0:90:27:58:af:1a)

    22:32:42.802902 eth0 表明从主机发出该数据包, arp表明是ARP请求包, who-has route tell ice表明是主机ICE请求主机ROUTE的MAC地址。 0:90:27:58:af:1a是主机ICE的MAC地址。

     

    TCP包的输出信息

    用TCPDUMP捕获的TCP包的一般输出信息是:

    src > dst: flags data-seqno ack windowurgent options

    src > dst:表明从源地址到目的地址, flags是TCP包中的标志信息,S 是SYN标志, F (FIN), P (PUSH) , R (RST) "." (没有标记); data-seqno是数据包中的数据的顺序号, ack是下次期望的顺序号, window是接收缓存的窗口大小, urgent表明数据包中是否有紧急指针. Options是选项.

     

    UDP包的输出信息

    用TCPDUMP捕获的UDP包的一般输出信息是:

    route.port1 > ice.port2: udp lenth

    UDP十分简单,上面的输出行表明从主机ROUTE的port1端口发出的一个UDP数据包到主机ICE的port2端口,类型是UDP,包的长度是lenth


    from:http://blog.csdn.net/chenyz_cn/article/details/6661921

    ===============================================================================================

    手机需要破解权限,具体的就是在APK程序里执行linux命令

    tcpdump -p -vv -s 0 -w /sdcard/capture.pcap
     
    这样就能够将抓到的包在sdcard上写一个capture.pcap文件,而这个文件可以用wireshark在PC上打开进行查看非常方便

    具体的源代码如下:

    MainHook.java 

    1. import java.io.IOException;  
    2. import android.app.Activity;  
    3. import android.os.Bundle;  
    4. import android.util.Log;  
    5. public class MainHook extends Activity {  
    6.       
    7.     @Override  
    8.     public void onCreate(Bundle savedInstanceState) {  
    9.         super.onCreate(savedInstanceState);  
    10.         setContentView(R.layout.main);  
    11.         try {  
    12.             //Runtime.getRuntime().exec("su");   
    13.             //Runtime.getRuntime().exec("dd if=/sdcard/tcpdump of=/data/local/tcpdump");   
    14.             //Runtime.getRuntime().exec("chmod 6755 /data/local/tcpdump");   
    15.             Runtime.getRuntime().exec("tcpdump -p -vv -s 0 -w /sdcard/capture.pcap");  
    16.             Log.i("run","success!!!!!!!!!");  
    17.         } catch (IOException e) {  
    18.             // TODO Auto-generated catch block   
    19.             e.printStackTrace();  
    20.             Log.i("run",e.toString());  
    21.         }  
    22.     }  
    23. }  

    AndroidManifest.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.       package="zy.hook"  
    4.       android:versionCode="1"  
    5.       android:versionName="1.0">  
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    7.         <activity android:name=".MainHook"  
    8.                   android:label="@string/app_name">  
    9.             <intent-filter>  
    10.                 <action android:name="android.intent.action.MAIN" />  
    11.                 <category android:name="android.intent.category.LAUNCHER" />  
    12.             </intent-filter>  
    13.         </activity>  
    14.     </application>  
    15.     <uses-sdk android:minSdkVersion="7" />  
    16.    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    17. </manifest>    

    尤其不要忘记了权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    的声明

    运行程序我们就可以抓包了

    会在sdcard上生成capture.pcap文件

    抓一段时间之后,将文件从sdcard上导出

    然后我们用wireshark打开

    可以非常方便的查看我们抓到的包

    from:http://blog.csdn.net/Zengyangtech/archive/2010/08/31/5853366.aspx

    进去看,评论中的探讨不错

    ===========================================================================================

    Android利用tcpdump

    Instructions

      http://source.android.com/porting/tcpdump.html

      Source Code and Documents

      http://www.tcpdump.org/

      Compiled Binary Download

      http://www.strazzere.com/android/tcpdump

      数据分析工具Wireshark

      http://www.wireshark.org/download.html

      Installing tcpdump

      Pushing the binary to an existing device

      Download tcpdump from http://www.tcpdump.org/, then execute:

      Cmd代码

      adb root

      adb remount

      adb push /wherever/you/put/tcpdump /system/xbin/tcpdump

      adb shell chmod 6755 /data/local/tmp/tcpdump

      adb root

      adb remount

      adb push /wherever/you/put/tcpdump /system/xbin/tcpdump

      adb shell chmod 6755 /data/local/tmp/tcpdump

      Running tcpdump

      You need to have root access on your device.

      Batch mode capture

      The typical procedure is to capture packets to a file and then examine the file on the desktop, as illustrated below:

      Cmd代码

      adb shell tcpdump -i any -p -s 0 -w /sdcard/capture.pcap

      # "-i any": listen on any network interface

      # "-p": disable promiscuous mode (doesn't work anyway)

      # "-s 0": capture the entire packet

      # "-w": write packets to a file (rather than printing to stdout)

      ... do whatever you want to capture, then ^C to stop it ...

      adb pull /sdcard/capture.pcap .

      sudo apt-get install wireshark # or ethereal, if you're still on dapper

      wireshark capture.pcap # or ethereal

      ... look at your packets and be wise ...

      adb shell tcpdump -i any -p -s 0 -w /sdcard/capture.pcap

      # "-i any": listen on any network interface

      # "-p": disable promiscuous mode (doesn't work anyway)

      # "-s 0": capture the entire packet

      # "-w": write packets to a file (rather than printing to stdout)

    ... do whatever you want to capture, then ^C to stop it ...

      adb pull /sdcard/capture.pcap .

      sudo apt-get install wireshark # or ethereal, if you're still on dapper

      wireshark capture.pcap # or ethereal

      ... look at your packets and be wise ...

      You can run tcpdump in the background from an interactive shell or from Terminal. By default, tcpdump captures all traffic without filtering. If you prefer, add an expression like port 80 to the tcpdump command line.

      Real time packet monitoring

      Execute the following if you would like to watch packets go by rather than capturing them to a file (-n skips DNS lookups. -s 0 captures the entire packet rather than just the header):

      Cmd代码

      adb shell tcpdump -n -s 0

      adb shell tcpdump -n -s 0

      Typical tcpdump options apply. For example, if you want to see HTTP traffic:

      Cmd代码

      adb shell tcpdump -X -n -s 0 port 80

    ============================================相关文章========================================

    Android上Tcpdump的使用

    http://www.kunli.info/2010/09/29/android-tcpdump/

    TCPDUMP for Android(抓TCP数据包工具)

    http://blog.csdn.net/metalwood/article/details/5697778

  • 相关阅读:
    asp.net如何实现删除文件的操作? (转)
    开始计算机英语的学习,先把这里当生词本用了。
    CSS Box Model 盒子模型
    生成网站快捷方式
    Microsoft .NET Framework 3.5 sp1离线安装解决方案
    asp.net生成网站快捷方式
    https://mail.google.com/tasks/ig?pli=1
    Sql server DATEDIFF DATEADD
    局域网 跨数据库 访问数据库
    asp.net 生成网站快捷方式
  • 原文地址:https://www.cnblogs.com/androidme/p/2790805.html
Copyright © 2011-2022 走看看