移植tcpdump到android

tcpdump的作用就不用多说了,顾名思义,tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。

    网上已经有很多相关的资料,不过多数都是交叉编译到arm-linux平台的,我这里对android平台做一些补充。

    1、获取tcpdump源码包

    首先到tcpdump的开源网站http://www.tcpdump.org/下载工具的源代码包,笔者此次下载了libpcap-1.0.0.tar.gztcpdump-4.0.0.tar.gz这两个版本。    

    2、NDK制作编译工具链

    参照我之前的文章,http://blog.chinaunix.net/uid-25272011-id-3491368.html,我这里使用arm-linux-androideabi-4.6工具。

    3、解压源码包至/home/work/tcpdump/libpcap-1.0.0,/home/work/tcpdump/tcpdump-4.0.0。

    4、交叉编译采用类似的格式

    ./configure --prefix=/home/jgf/work/tool/arm-linux-androideabi --host=arm-linux-androideabi CC=arm-linux-androideabi-gcc

    首先编译libpcap-1.0.0,如果直接运行上面的命令

    checking build system type... i686-pc-linux-gnulibc1
    checking host system type... Invalid configuration `arm-linux-androideabi': system `androideabi' not recognized
    configure: error: /bin/bash ./config.sub arm-linux-androideabi failed

    这是因为config.sub和config.guess还没有加入arm-linux-androideabi的支持,可以到下面这个网址下载这两个文件分别覆盖两个包下面的对应的文件:

     http://git.savannah.gnu.org/gitweb/?p=config.git;a=tree

    5、编译libpcap-1.0.0

    运行./configure --prefix=/home/jgf/work/tool/arm-linux-androideabi --host=arm-linux-androideabi CC=arm-linux-androideabi-gcc

    会出现configure: error: pcap type not determined when cross-compiling; use --with-pcap=...和

    configure: error: cannot determine linux version when cross-compiling

    这里提供一个方法,屏蔽configure文件中的这些行

    #if test -z "$with_pcap" && test "$cross_compiling" = yes; then
    #       { { echo "$as_me:$LINENO: error: pcap type not determined when cross-compiling; use --with-pcap=..." >&5
     #echo "$as_me: error: pcap type not determined when cross-compiling; use --with-pcap=..." >&2;}
    #   { (exit 1); exit 1; }; }
    #fi
    .
    .
    .
    #       if test $ac_cv_linux_vers = unknown ; then
    #               { { echo "$as_me:$LINENO: error: cannot determine linux version when cross-compiling" >&5
    #echo "$as_me: error: cannot determine linux version when cross-compiling" >&2;}
    #   { (exit 1); exit 1; }; }
    #       fi

    这样继续configure,跳过了上面两个错,但还有问题

    checking for flex... no
    checking for bison... no
    checking for capable lex... insufficient
    configure: error: Your operating system's lex is insufficient to compile

    libpcap.  flex is a lex replacement that has many advantages, including
    being able to compile libpcap.  For more information, see
    http://www.gnu.org/software/flex/flex.html .

    可以在ubuntu上安装这两个包flex和bison,然后make,make install

    /usr/bin/install: 无法创建普通文件"/home/jgf/work/tool/arm-linux-androideabi/bin/pcap-config": 没有那个文件或目录

    手动在/home/jgf/work/tool/arm-linux-androideabi/目录下建议个bin目录即可。

    到这里libpcap-1.0.0编译完毕。


    6、编译tcpdump-4.0.0

    ./configure --prefix=/home/jgf/work/tool/arm-linux-androideabi --host=arm-linux-androideabi CC=arm-linux-androideabi-gcc有错

    checking Linux kernel version... unknown
    configure: error: cannot determine linux version when cross-compiling

    按照上面第5节的方法,即可;然后make,又有错了

    ./print-udp.c:36:23: fatal error: arpa/tftp.h: No such file or directory

    到这个文件里面,注释掉这一行。继续make,还有错

    print-enc.o: In function `enc_if_print':
    print-enc.c:(.text+0xcc): undefined reference to `ip6_print'
    print-isakmp.o: In function `ikev1_id_print':
    print-isakmp.c:(.text+0x208c): undefined reference to `setprotoent'
    print-isakmp.c:(.text+0x20b8): undefined reference to `endprotoent'
    collect2: ld returned 1 exit status

    到print-enc.c里面注释掉

    case AF_INET6:
        ip6_print(p, length);
    break;

    到print-isakmp.c中注释掉setprotoent(1)和endprotoent()这两行即可。

   

    7、此时/home/jgf/work/tool/arm-linux-androideabi/sbin/目录下就出现了tcpdump了,我的手机是galaxy S1(T959),刷的是CM10,android4.1.2的系统。

需要将tcpdump工具adb push到/system/bin/目录下。首先要求系统root了,如果支持adb remount命令最好,直接adb push tcpdump /system/bin/,但是我的系统,需要adb shell,su切换到root,然后运行mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system

然后再adb push tcpdump /sdcard/,然后adb shell,cp /sdcard/tcpdump /system/bin/,rm /sdcard/tcpdump就可以了,有的系统可能需要chmod +x /system/bin/tcpdump,这样就可以在手机里面运行

    tcpdump -s 0 -w /sdcard/test.cap

    然后可以在ubuntu中adb pull /sdcard/test.cap /home/

    可以利用wireshark工具打开查看抓的包了


本文参考了http://blog.chinaunix.net/uid-20621895-id-196674.html

                 http://blog.csdn.net/wfh1988/article/details/6408837

                 http://blog.chinaunix.net/uid-20609878-id-1915871.html



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值