吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1063|回复: 5
上一主题 下一主题
收起左侧

[Python 原创] 分享一个简单的抓包demo 使用python实现有gui界面

[复制链接]
跳转到指定楼层
楼主
zhc5827710 发表于 2024-5-23 15:49 回帖奖励
本帖最后由 zhc5827710 于 2024-5-23 16:02 编辑



目前测试在macOS环境之下
使用python3.12版本 测试过3.9的版本 gui会有entry 和 label 组件不显示问题
运行前需要使用sudo 命令 赋予程序管理员权限 不然会报错 运行命令如下
sudo python3 protocol1.py  
protocol1 为你的文件名
众所周知python具有多平台兼容性 但是没有测试linux 与windows系统下的运行情况

[Python] 纯文本查看 复制代码
import threading  # 导入线程模块
import tkinter as tk  # 导入Tkinter模块用于GUI
from tkinter.scrolledtext import ScrolledText  # 导入ScrolledText用于滚动文本框
import scapy.all as scapy  # 导入Scapy模块用于网络数据包捕获和处理
import psutil  # 导入psutil模块用于获取网络接口信息

# 创建一个事件对象,用于控制线程的停止
stop_event = threading.Event()
# 创建一个列表,用于存储捕获到的数据包
packets = []


# 定义数据包捕获函数
def packet_capture(interface, ip):
    # 定义数据包回调函数
    def packet_callback(packet):
        # 如果数据包包含IP层并且目标IP是指定的IP
        if packet.haslayer(scapy.IP) and packet[scapy.IP].dst == ip:
            # 将数据包添加到packets列表中
            packets.append(packet)

    # 使用Scapy的sniff函数捕获数据包
    scapy.sniff(iface=interface, prn=packet_callback, stop_filter=lambda _: stop_event.is_set())


# 定义数据包处理函数
def packet_processing(output_text):
    # 当stop_event未设置或packets列表不为空时循环
    while not stop_event.is_set() or packets:
        # 如果packets列表不为空
        if packets:
            # 从packets列表中取出第一个数据包
            packet = packets.pop(0)
            # 在输出文本框中插入数据包摘要信息
            output_text.insert(tk.END, f"Packet: {packet.summary()}\n")
            # 滚动到文本框的末尾
            output_text.see(tk.END)


# 定义开始捕获函数
def start_capture(interface, ip, output_text):
    # 清除stop_event
    stop_event.clear()

    # 创建捕获线程
    capture_thread = threading.Thread(target=packet_capture, args=(interface, ip))
    # 创建处理线程
    processing_thread = threading.Thread(target=packet_processing, args=(output_text,))

    # 启动捕获线程
    capture_thread.start()
    # 启动处理线程
    processing_thread.start()


# 定义停止捕获函数
def stop_capture(output_text):
    # 设置stop_event
    stop_event.set()
    # 在输出文本框中插入停止成功信息
    output_text.insert(tk.END, "Stop Success\n")
    # 滚动到文本框的末尾
    output_text.see(tk.END)


# 获取网络接口列表
def get_network_interfaces():
    # 获取所有网络接口的地址信息
    interfaces = psutil.net_if_addrs()
    # 返回网络接口名称列表
    return list(interfaces.keys())


# 创建GUI界面
def create_gui():
    # 创建主窗口
    root = tk.Tk()
    # 设置窗口标题
    root.title("Packet Sniffer")
    # 设置窗口大小
    root.geometry("800x600")

    # 创建标签和输入框
    interface_label = tk.Label(root, text="Network Interface:")  # 创建网络接口标签
    interface_label.place(relx=0.05, rely=0.05)  # 设置标签位置
    interface_var = tk.StringVar(root)  # 创建字符串变量用于存储选择的网络接口
    interface_var.set(get_network_interfaces()[0])  # 默认选择第一个网络接口
    interface_menu = tk.OptionMenu(root, interface_var, *get_network_interfaces())  # 创建下拉菜单
    interface_menu.place(relx=0.25, rely=0.05, relwidth=0.65)  # 设置下拉菜单位置和宽度

    ip_label = tk.Label(root, text="Target IP:")  # 创建目标IP标签
    ip_label.place(relx=0.05, rely=0.15)  # 设置标签位置
    ip_entry = tk.Entry(root)  # 创建输入框
    ip_entry.place(relx=0.25, rely=0.15, relwidth=0.65)  # 设置输入框位置和宽度

    # 创建输出文本区域
    output_text = ScrolledText(root, wrap=tk.WORD)  # 创建滚动文本框
    output_text.place(relx=0.05, rely=0.25, relwidth=0.9, relheight=0.6)  # 设置文本框位置和大小

    # 创建按钮
    start_button = tk.Button(root, text="Start", command=lambda: start_capture(interface_var.get(), ip_entry.get(),
                                                                               output_text))  # 创建开始按钮并设置点击事件
    start_button.place(relx=0.25, rely=0.9, relwidth=0.2)  # 设置按钮位置和宽度

    stop_button = tk.Button(root, text="Stop", command=lambda: stop_capture(output_text))  # 创建停止按钮并设置点击事件
    stop_button.place(relx=0.55, rely=0.9, relwidth=0.2)  # 设置按钮位置和宽度

    # 启动主事件循环
    root.mainloop()


# 主函数入口
if __name__ == "__main__":
    create_gui()  # 创建并运行GUI界面



程序运行界面如下:




免费评分

参与人数 2吾爱币 +5 热心值 +2 收起 理由
抱歉、 + 1 用心讨论,共获提升!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
xinxiu 发表于 2024-5-23 23:03
感谢分享,等下研究研究
3#
Kbai 发表于 2024-5-24 09:49
4#
kings0b 发表于 2024-5-24 17:30
5#
ztqddj007 发表于 2024-5-26 07:23
谢谢分享 试试咋样
6#
Chengliang12138 发表于 2024-6-4 14:55
感谢分享,学习学习
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-16 09:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表