python基礎(chǔ)教程之獲取本機(jī)ip數(shù)據(jù)包示例
這幾天用到了raw socket,用python寫(xiě)了些demo程序,這里記錄下。
首先我們看一個(gè)簡(jiǎn)單的sniffer程序:
#! /usr/bin/python
# code for linux
import socket
#s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
while True:
print s.recvfrom(65535)
這里直接用raw socket接收數(shù)據(jù),直接print操作。這個(gè)就幾行代碼,也沒(méi)什么好解釋的了,不懂的google下。
得到IP數(shù)據(jù)包后,接下來(lái)的工作就是對(duì)IP頭進(jìn)行解析,在這之前,我們先看看RFC中是怎么定義的(RFC791 : http://www.ietf.org/rfc/rfc791.txt ):
即對(duì)應(yīng)的圖:
從RFC和上圖中可以看到IP數(shù)據(jù)包頭各個(gè)字段所占的位數(shù),我們可以根據(jù)這些定義去解析IP數(shù)據(jù)包頭,然后根據(jù)相應(yīng)的策略處理數(shù)據(jù)。
這里給出一段用python實(shí)現(xiàn)的解析IP頭的代碼(呵呵,是demo中的代碼,只解析了前20個(gè)字節(jié)):
def decodeIpHeader(packet):
mapRet = {}
mapRet["version"] = (int(ord(packet[0])) & 0xF0)>>4
mapRet["headerLen"] = (int(ord(packet[0])) & 0x0F)<<2
mapRet["serviceType"] = hex(int(ord(packet[1])))
mapRet["totalLen"] = (int(ord(packet[2])<<8))+(int(ord(packet[3])))
mapRet["identification"] = (int( ord(packet[4])>>8 )) + (int( ord(packet[5])))
mapRet["id"] = int(ord(packet[6]) & 0xE0)>>5
mapRet["fragOff"] = int(ord(packet[6]) & 0x1F)<<8 + int(ord(packet[7]))
mapRet["ttl"] = int(ord(packet[8]))
mapRet["protocol"] = int(ord(packet[9]))
mapRet["checkSum"] = int(ord(packet[10])<<8)+int(ord(packet[11]))
mapRet["srcaddr"] = "%d.%d.%d.%d" % (int(ord(packet[12])),int(ord(packet[13])),int(ord(packet[14])), int(ord(packet[15])))
mapRet["dstaddr"] = "%d.%d.%d.%d" % (int(ord(packet[16])),int(ord(packet[17])),int(ord(packet[18])), int(ord(packet[19])))
return mapRet
調(diào)用代碼:
proto = socket.getprotobyname('tcp') # only tcp
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, proto)
while True:
packet = sock.recvfrom(65535)[0]
if len(packet) == 0:
sck.close()
else:
#print str(packet)
mapIpTmp = decodeIpHeader(packet)
for k,v in mapIpTmp.items():
print k,"\t:\t",v
print ""
相關(guān)文章
Python多維/嵌套字典數(shù)據(jù)無(wú)限遍歷的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇Python多維/嵌套字典數(shù)據(jù)無(wú)限遍歷的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
使用Python對(duì)Syslog信息進(jìn)行分析并繪圖的實(shí)現(xiàn)
這篇文章主要介紹了使用Python對(duì)Syslog信息進(jìn)行分析并繪圖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
python反轉(zhuǎn)一個(gè)三位整數(shù)的多種實(shí)現(xiàn)方案
這篇文章主要介紹了python反轉(zhuǎn)一個(gè)三位整數(shù)的多種實(shí)現(xiàn)方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python實(shí)現(xiàn)孤立隨機(jī)森林算法的示例代碼
孤立森林(isolation?Forest)是一種高效的異常檢測(cè)算法,它和隨機(jī)森林類(lèi)似,但每次選擇劃分屬性和劃分點(diǎn)(值)時(shí)都是隨機(jī)的,而不是根據(jù)信息增益或基尼指數(shù)來(lái)選擇。本文將用Python實(shí)現(xiàn)這一算法,需要的可以參考一下2022-03-03
淺析python 定時(shí)拆分備份 nginx 日志的方法
本文給大家分享python 定時(shí)拆分備份 nginx 日志的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-04-04
Python三級(jí)目錄展示的實(shí)現(xiàn)方法
這篇文章主要介紹了Python三級(jí)目錄展示的實(shí)現(xiàn)方法的相關(guān)資料,本文通過(guò)圖文并茂的方式給大家介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

