NoOps

Ops make no ops | Ops的目标是没有Ops,嗯!

tcpxm with python pylibcap

作者: |   2,035 浏览  | 

tcpxm 基于pylibcap的抓包工具

原理

  • python开发,调用pylibcap进行抓包
  • 3个线程:一个负责抓包并分析内容,一个负责写日志,一个用来清除过期数据

使用tcpdump抓取的数据还需要2次分析,tcpxm可以很方便的抓取和分析tcp请求,打印成需求的日志形式。

我们用它来抓取和分析米聊用户登陆时间,当然可以用它来抓取微信或网站访问等时间,计算用户建立TCP链接时间,第一次发包时间等等

打印的日志类似如下格式:
2012-09-13 21:25:25 tcpxm.py [line:229] [INFO]  221.179.36.189:3103->xxx.xxx.xxx.xx:2424 [usr:54298295] [login(t6-t0+rtt):2760]  [t1:0] [rtt:217] [t3:137] [t4:0] [t5:118] [t6:2069] [t7:193]

Login Time = t6(发送<success>的时间) – t0(收到SYN的时间)  + rtt(估算出的收到SYN包和发送ACK包的路径时间)

具体每个t代表的时间含义如下,日志中的t3 = T3 – T2,  t4 = T4 – T3, rrt(t2) = T2 – T1 ,……

图片

安装

启动

sudo ./tcpxm.py -i eth0 -f "port 80 and not host ip1 and not host ip2“
具体filter参考pcap man 7 pcap-filter

测试

因为还没有做成可配置,所以代码里面是抓取米聊login时间,匹配到<success/>后完成一次tcp请求的记录。

测试时,可以修改成DEBUG模式,它匹配到Content-Type后,完成一次记录

步骤

  1. 修改tcpxm.py中DEBUG = True
  2. 启动一个简单的twisted web, sudo twistd web --path=/home/work/tcpxm/ -p 80, 在浏览器中使用ip访问本机80端口,默认会展现tcpxm/index.html
  3. 启动tcpxm sudo ./tcpxm.py -i eth0 -f "port 80“ ,访问http页面,查看log/日志记录

抓包本地http 80的 tcp访问

6 Comments

  1. 2013/05/20 at 9:42 下午

    你好,按照你的方法安装,clone的时候总是提示
    Please make sure you have the correct access rights
    and the repository exists.

    • wilbur
      2013/05/20 at 10:53 下午

      啊,我明天看看,你用http clone或者read-only clone 地址试试

    • wilbur
      2013/05/21 at 10:04 上午

      git clone git://github.com/xiaomi-sa/tcpxm.git

  2. young
    2013/09/12 at 5:49 下午

    你好,按照你的方法安装,clone的时候总是提示fatal: destination path ‘tcpxm’ already exists and is not an empty directory.

    • wilbur
      2013/09/12 at 5:52 下午

      你本地存在有tcpxm这个目录?

      • young
        2013/09/18 at 3:22 下午

        确实是存在tcpxm这个目录。。安装的时候有下面这些错误,不懂要怎么解决才好
        [root@2101 pylibpcap-0.6.4]# sudo python setup.py install
        running install
        running build
        running build_ext
        building ‘_pcapmodule’ extension
        gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector –param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -DSWIG_COBJECT_TYPES -I/usr/include/python2.4 -c pcap.c -o build/temp.linux-x86_64-2.4/pcap.o
        pcap.c:2519:18: error: pcap.h: No such file or directory
        In file included from pcap.c:2520:
        pypcap.h:24: error: expected specifier-qualifier-list before ‘pcap_t’
        pypcap.h:72: error: expected ‘)’ before ‘*’ token
        pcap.c: In function ‘init_pcap’:
        pcap.c:4260: warning: implicit declaration of function ‘pcap_lib_version’
        pcap.c:4260: warning: passing argument 3 of ‘PyModule_AddStringConstant’ makes pointer from integer without a cast
        error: command ‘gcc’ failed with exit status 1

发表评论