分享

python学习笔记

 dbn9981 2016-09-24

    今天看看如何用python实现抓包。

    以前我们曾用过winpcap实现抓包,其实今天我们还是用它,只不过是它在python下的模块,在用之前我们必须先下载两个库,pcap库和dpkt库,这个可以在Google code里很容易找到,需要注意的是pcap只支持到python2.5 ,所以我们要用2.5来编写。

    安装完两个库以后,我们就先来认识一下两个库。

    首先看pcap。

    我们在命令行输入:import pcap如果回车后没有出错信息,则说明我们安装成功了,然后我们利用help命令来查看一下帮助信息。

 

    模块的说明:This module provides a high level interface to packet capture systems.All packets on the network, even those destined for other hosts, areaccessible through this mechanism.

    关于它的类,这里只说下最重要的一个pcap类:

 class pcap(__builtin__.object)

 pcap(name=None, snaplen=65535, promisc=True, immediate=False, timeout_ms=None) -> packet capture object。Open a handle to a packet capture descriptor.

 

name      -- name of a network interface or dumpfile to open,

               or None to open the first available up interface

snaplen   -- maximum number of bytes to capture for each packet

promisc   -- boolean to specify promiscuous mode sniffing

immediate -- disable buffering, if possible

timeout_ms -- requests for the next packet will return None if the timeout

                    (in milliseconds) is reached and no packets were received

                  (Default: no timeout)

    我们在一般使用的时候,参数都可省略,如:p=pcap.pcap( )

    Pcap的方法中常用的是setfilter()设置过滤,可以使协议,端口。

    另外一个库是dpkt库,是用来解析我们抓到的包的,好我们看一个简单的程序。

import pcap

import dpkt

a=pcap.pcap()

a.setfilter('tcp port 80')  #过滤功能,可以设置需要显示的

for i,j in a:

       tem=dpkt.ethernet.Ethernet(j)

       print('%s %x',i,tem)

 

('%s %x', 1251890284.9400771, Ethernet(src='\x00\x12?\x92\xb0A', dst='\x00\nB\xcd<\n', data=IP(src='\n\x01\x0eP', off=16384, dst='<\x1c\xaf"', sum=48598, len=40, p=6, ttl=128, id=14698, data=TCP(seq=2887809373L, ack=837333778, win=65274, sum=38843, flags=17, dport=80, sport=2757))))

('%s %x', 1251890285.1270909, Ethernet(src='\x00\nB\xcd<\n', dst='\x00\x12?\x92\xb0A', data=IP(src='<\x1c\xaf"', dst='\n\x01\x0eP', sum=19969, len=40, p=6, ttl=49, id=14400, data=TCP(seq=837333778, ack=2887809374L, win=6600, sum=31981, flags=17, dport=2757, sport=80))))

('%s %x', 1251890285.127131, Ethernet(src='\x00\x12?\x92\xb0A', dst='\x00\nB\xcd<\n', data=IP(src='\n\x01\x0eP', off=16384, dst='<\x1c\xaf"', sum=48583, len=40, p=6, ttl=128, id=14713, data=TCP(seq=2887809374L, ack=837333779, win=65274, sum=38842, flags=16, dport=80, sport=2757))))

('%s %x', 1251890285.2843239, Ethernet(src='\x00\x12?\x92\xb0A', dst='\x00\nB\xcd<\n', data=IP(src='\n\x01\x0eP', off=16384, dst='v\xe4\x94P', sum=40379, len=48, p=6, ttl=128, id=14727, data=TCP(seq=3061527654L, off_x2=112, sum=27037, dport=80, sport=2763))))

('%s %x', 1251890285.284482, Ethernet(src='\x00\x12?\x92\xb0A', dst='\x00\nB\xcd<\n', data=IP(src='\n\x01\x0eP', off=16384, dst='v\xe4\x94\x16', sum=40442, len=40, p=6, ttl=128, id=14730, data=TCP(seq=4170586011L, ack=215657518, sum=47158, flags=17, dport=80, sport=2759))))

('%s %x', 1251890285.2853069, Ethernet(src='\x00\x12?\x92\xb0A', dst='\x00\nB\xcd<\n', data=IP(src='\n\x01\x0eP', off=16384, dst='<\x1c\xaf"', sum=48548, len=48, p=6, ttl=128, id=14740, data=TCP(seq=818783773, off_x2=112, sum=40328, dport=80, sport=2765))))

这里列出一部分结果。

看到确实抓到啦。

对于结果显示的分析和进一步改进会在今后继续学习的,这里只是开了一个头。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约