资源简介
sniffer_get_body.py
代码片段和文件信息
import struct
import socket
ip_header_fmt = ‘!BBHHHBBH4s4s‘
tcp_header_fmt = ‘!HHLLBBHHH‘
udp_header_fmt = ‘!HHHH‘
IP_HEAD_LEN = struct.calcsize(ip_header_fmt) # 20字节
TCP_HEADER_LEN = struct.calcsize(tcp_header_fmt) # 20字节
UDP_HEAD_LEN = struct.calcsize(udp_header_fmt) # 8字节
IP_MAX_LEN = 65535
def unpack_ip_header(buf):
if len(buf) < IP_HEAD_LEN:
return
try:
iph = struct.unpack(ip_header_fmt buf[:IP_HEAD_LEN])
protocol_map = {1: ‘ICMP‘ 6: ‘TCP‘ 17: ‘UDP‘}
return {
‘version‘: iph[0] >> 4 # 高4位
‘ihl‘: (iph[0] & 0xF) * 4 # 低4位,每个长度单位表示4字节,最大为60字节
‘tos‘: iph[1] # 8位
‘len‘: iph[2] # 16位
‘id‘: iph[3] # 16位
‘offset‘: iph[4] # 16位
‘ttl‘: iph[5] # 8位
‘protocol‘: protocol_map.get(iph[6] str(iph[6])) # 8位
‘cks‘: iph[7] # 16位
‘src‘: iph[8] # 32位
‘dst‘: iph[9] # 32位
}
except Exception as e:
raise e
def unpack_tcp_header(buf):
if len(buf) < TCP_HEADER_LEN:
return
try:
tcph = struct.unpack(tcp_header_fmt buf[:TCP_HEADER_LEN])
return {
‘src‘: tcph[0] # 16位
‘dst‘: tcph[1] # 16位
‘seq‘: tcph[2] # 32位
‘ack‘: tcph[3] # 32位
‘thl‘: (tcph[4] >> 4) * 4 # 高4位
‘wlen‘: tcph[6] # 16位
}
except Exception as e:
raise e
def unpack_udp_header(buf):
if len(buf) < UDP_HEAD_LEN:
return
try:
udph = struct.unpack(udp_header_fmt buf[:UDP_HEAD_LEN])
return {
‘src‘: udph[0] # 16位
‘dst‘: udph[1] # 16位
‘dlen‘: udph[2] # 16位
相关资源
- python+ selenium教程
- 英文原版-Scientific Computing with Python
- CpuMemSets在Linux操作系统中的实现
- Python学习全系列教程永久可用
- 蓝奏云批量上传工具.zip
- python书籍 PDF
- 老男孩python项目实战
- Python.rar99111
- decision_tree_v2.py
- Python绝技运用Python成为顶级黑客.pdf
- python小波包文档及论文.zip
- Python黑帽子(黑客与渗透测试编程之
- FlaskWeb开发:基于Python的Web应用开发实
- Python基础教程第3版中英文源码.rar
- python数据结构与算法中文版.zip
- Python-冲顶大会芝士超人西瓜视频头脑
- time_series_forecasting_with_python.zip
- Python基础教程第三版PDF高清可复制.
- python编程从入门到实践.zip237878
- FlaskWeb开发:Python基于Web应用开发实战
- pythonBCRMDSJ.mobi
- 量化交易之路用Python做股票量化分析
- PYTHON自然语言处理中文版.pdf
- Python基础教程(第3版).rar
- GRAYHATPYTHON高清.英文.书签版.pdf
- Python简明教程第四版.rar
- Python编程:从入门到实践带书签完整
- Python基础教程(第3版).pdf109608
- vamei-从Python开始学编程.pdf
- 利用Python进行数据分析.pdf
评论
共有 条评论