Python—网络抓包与解包(pcap、dpkt)
pcap安装
[root@localhost ~]# pip install pypcap
抓包与解包
# -*- coding:utf-8 -*-
import pcap, dpkt
import re, threading, requests __black_ip = ['103.224.249.123', '203.66.1.212'] # 抓包:param1 eth_name 网卡名,如:eth0,eth3。 param2 p_type 日志捕获类型 1:sdk日志用例分析 2:目标域名过滤输出 3:原始数据包
def catch_pack(eth_name="enp5s0", packet_type=None):
sniffer = pcap.pcap(eth_name)
sniffer.setfilter("tcp") # 只抓取TCP包
# sniffer.setfilter('tcp port 80') # 设置监听过滤器
if sniffer:
for packet_time, packet_data in sniffer: # packet_time为收到的时间,packet_data为收到的数据
th = threading.Thread(target=check_pack, args=(packet_time, packet_data, packet_type))
th.setDaemon(True)
th.start() # 解包
def check_pack(packet_time, packet_data, packet_type):
packet = dpkt.ethernet.Ethernet(packet_data)
# 判断是否HTTP协议javascript:;
try:
m_tips = packet.data.data.data[:3]
except:
return False
if m_tips != 'GET' and m_tips != 'POS': return False # 取回头信息
tmp = get_header(packet, packet_time)
if not tmp: return False
print "tmp===>", tmp
# self.input_database(tmp) # 取header数据
def get_header(packet, packet_time):
if not packet: return False
tmp = {} # 获取基础头信息
tmp['dst'] = "%d.%d.%d.%d" % tuple(map(ord, list(packet.data.dst)))
if tmp['dst'] in __black_ip: return False
tmp['src'] = "%d.%d.%d.%d" % tuple(map(ord, list(packet.data.src)))
tmp['time'] = packet_time
tmp['dport'] = packet.data.data.dport
tmp['sport'] = packet.data.data.sport
# if not self.ip_exists(tmp['dst']): self.input_ip(tmp['dst']) # 截取前1024个字符
t = packet.data.data.data[:1024] # 获取host,uri,referer,method
o = re.search('(GET|POST)\s+(.+)\s+HTTP(.|\n)+Host:\s+(.+)\r(.|\n)+Referer:\s+(.+)\r', t)
if not o: return False
o1 = o.groups()
if len(o1) < 6: return False
tmp['method'] = o1[0]
tmp['host'] = o1[3]
tmp['uri'] = o1[1]
tmp['referer'] = o1[5]
# if self.is_exists(tmp['host'],tmp['dport']): return False
return tmp if __name__ == "__main__":
catch_pack("eth0", 1)
解析pcap文件
try:
import scapy.all as scapy
except ImportError:
import scapy
from scapy.utils import PcapReader packets = scapy.rdpcap('test.pcap')
for data in packets:
if 'UDP' in data:
s = repr(data)
print(data['UDP'].sport)
break
https://www.cnblogs.com/guanfuchang/p/6495533.html
https://www.cnblogs.com/KevinGeorge/p/8088915.html
Python—网络抓包与解包(pcap、dpkt)的更多相关文章
- Python 序列与映射的解包操作
解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...
- Python 序列与映射的解包操作-乾颐堂
解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...
- Python小技巧:使用*解包和itertools.product()求笛卡尔积
[问题] 目前有一字符串s = "['a', 'b'],['c', 'd']",想把它分开成为两个列表: list1 = ['a', 'b'] list2 = ['c', 'd'] ...
- Python 中星号作用:解包&打散
python中’*’和’**’的使用分两个方面,一个是计算,另一个是参数传递过程中元素的打包和解包. 计算方面 ‘*’和’**’在python中最常见的作用分别是‘相乘’和‘乘幂’,如下: > ...
- Python——序列封包与序列解包
一.序列封包与序列解包 把多个值赋给一个变量时,Python会自动的把多个值封装成元组,称为序列封包. 把一个序列(列表.元组.字符串等)直接赋给多个变量,此时会把序列中的各个元素依次赋值给每个变量, ...
- Python小技巧:使用*解包和itertools.product()求笛卡尔积(转)
leetcode上做提示时候看到有高人用这个方法解题 [问题] 目前有一字符串s = "['a', 'b'],['c', 'd']",想把它分开成为两个列表: list1 = [' ...
- python中收集函数的解包问题
收集参数的解包问题 - 把参数放入list或者字典中,直接把list/dict中的值放入收集参数中- 语法:参照案例 # 收集参数的问题 def stu(*args): print("=&q ...
- ISO8583组包、解包
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS. ...
- python 网络编程 缓冲和粘包
tcp:属于长连接,与一个客户端进行连接了以后,其他的客户端要等待,要连接另外一个,必须优雅的断开前面这个客户端的连接. 允许地址重用:在bind IP地址和端口之前加上,# server.setso ...
随机推荐
- Android 多选列表对话框 setMultiChoiceItems
private Button button; private final CharSequence items[] = { "北京", "上海", " ...
- 单片机固件烧录器 Firmware Writer Android APP
GitHub地址 :https://github.com/WallBreakerX/mcu_firmware_writing_via_androidphone 用途 可在安卓手机上实现向单片机的h ...
- PyCharm关闭按两次Shift进入搜索框的功能
1.按Ctrl + Shift + A 弹出搜索框 2.在弹出的搜索框内输入registry(如果汉化了输入“注册”),回车 3.在弹出的窗口中,往下找到“ide.suppress.double.cl ...
- CodeForces 862B(思维+二分图染色)
题意 https://vjudge.net/problem/CodeForces-862B 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 思路 因为题目是求的最多添加多少边,所以可 ...
- python 1 默写用递归实现无限极分类 2 默写用树实现无限极分类
data=[ {"cat_id":3,"name":"沙河","parent_id":1}, {"cat_id ...
- LeetCode 5126. 有序数组中出现次数超过25%的元素 Element Appearing More Than 25% In Sorted Array
地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/element-appearing-more-than-25-in-so ...
- 《2018:skymind.ai 发布了一份非常全面的开源数据集》
这是一份非常全面的开源数据集,你,真的不想要吗? 近期,skymind.ai 发布了一份非常全面的开源数据集.内容包括生物识别.自然图像以及深度学习图像等数据集,现机器之心将其整理如下:(内附链接 ...
- thinkphp5.1单模块设置
thinkphp5.1单模块 1. // 是否支持多模块'app_multi_module' => false, // 自动搜索控制器'controller_auto_search' => ...
- ASP.NET Core 模型验证的一个小小坑
今天在我们的一个项目中遇到一个 asp.net core 模型验证(model validation)的小问题.当模型属性的类型是 bool ,而提交上来的该属性值是 null ,asp.net co ...
- golang数据结构之栈
stack.go package stack import ( "errors" "fmt" ) type Stack struct { MaxTop int ...