小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

python學(xué)習(xí)筆記

 dbn9981 2016-09-24

    今天看看如何用python實現(xiàn)抓包。

    以前我們曾用過winpcap實現(xiàn)抓包,其實今天我們還是用它,只不過是它在python下的模塊,在用之前我們必須先下載兩個庫,pcap庫和dpkt庫,這個可以在Google code里很容易找到,需要注意的是pcap只支持到python2.5 ,所以我們要用2.5來編寫。

    安裝完兩個庫以后,我們就先來認(rèn)識一下兩個庫。

    首先看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.

    關(guān)于它的類,這里只說下最重要的一個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)

    我們在一般使用的時候,參數(shù)都可省略,如:p=pcap.pcap( )

    Pcap的方法中常用的是setfilter()設(shè)置過濾,可以使協(xié)議,端口。

    另外一個庫是dpkt庫,是用來解析我們抓到的包的,好我們看一個簡單的程序。

import pcap

import dpkt

a=pcap.pcap()

a.setfilter('tcp port 80')  #過濾功能,可以設(shè)置需要顯示的

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))))

這里列出一部分結(jié)果。

看到確實抓到啦。

對于結(jié)果顯示的分析和進(jìn)一步改進(jìn)會在今后繼續(xù)學(xué)習(xí)的,這里只是開了一個頭。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多