网御智鉴!

import dpkt
import socket
import sys
import argparse

# 下载检测函数:检查请求中是否包含特定关键字,如.zip或loic,表明可能的恶意下载
# 参数 request 为接收到的网络请求数据(以字节形式),用于在其中查找特定的关键字来判断是否存在恶意下载行为
def detect_downloads(request):
    if b".zip" in request or b"loic" in request:
        print("[!] Potential malicious download detected")

# Hive Mind检测函数:检查数据中是否包含DDoS攻击的特定命令,如!lazor
# 参数 data 为网络数据包中的相关数据部分(以字节形式),通过查找特定命令字符串判断是否存在DDoS相关指令
def detect_hive_mind(data):
    if b'!lazor' in data:
        print("[!] Possible DDoS command detected")

# 攻击检测函数:如果从同一源IP到同一目标IP的数据包数量超过阈值,则报告可能的网络攻击
# 参数 src_ip 表示源IP地址(字符串形式),dst_ip 表示目标IP地址(字符串形式),
# packet_count 表示从源IP到目标IP已发送的数据包数量(整数),packet_threshold 表示判断攻击的数据包数量阈值(整数)
def detect_attack(src_ip, dst_ip, packet_count, packet_threshold):
    if packet_count > packet_threshold:
        print(f"[!] Potential attack detected from {src_ip} to {dst_ip}: {packet_count} packets")

# FTP检测函数:检查TCP数据是否以FTP命令(USER、PASS、RETR)开头,表明可能的FTP流量
# 参数 tcp_data 为TCP协议相关的数据部分(以字节形式),通过判断开头字节是否符合FTP命令格式来检测FTP流量
def detect_ftp(tcp_data):
    if tcp_data.startswith(b'USER') or tcp_data.startswith(b'PASS') or tcp_data.startswith(b'RETR'):
        print("[!] Possible FTP traffic detected")

# HTTP检测函数:检查TCP数据中是否包含HTTP请求方法(GET、POST),表明可能的HTTP流量
# 参数 tcp_data 为TCP协议相关的数据部分(以字节形式),通过查找特定的HTTP请求方法关键字来判断是否存在HTTP流量
def detect_http(tcp_data):
    if b'GET' in tcp_data or b'POST' in tcp_data:
        print("[!] Possible HTTP traffic detected")

# 解析数据包的主函数:遍历PCAP文件中的每个数据包,调用检测函数,并打印出检测到的网络活动
# 参数 pcap 是通过dpkt.pcap.Reader读取的PCAP文件中的数据包对象,包含多个时间戳和对应的数据缓冲区,
# packet_threshold 为判断网络攻击的数据包数量阈值(整数)
def parse_packet(pcap, packet_threshold):
    packet_counts = {}
    for (ts, buf) in pcap:
        try:
            # 从数据包缓冲区解析出以太网数据包对象
            eth = dpkt.ethernet.Ethernet(buf)
            # 确保以太网负载是IP数据包,只有是IP数据包才进行后续处理
            if isinstance(eth.data, dpkt.ip.IP):
                ip = eth.data
                # 确保IP地址长度正确(IPv4地址长度应为4字节),符合要求才进行后续处理
                if len(ip.src) == 4 and len(ip.dst) == 4:
                    # 将IP源地址从网络字节序转换为点分十进制格式
                    src = socket.inet_ntoa(ip.src)
                    # 将IP目标地址从网络字节序转换为点分十进制格式
                    dst = socket.inet_ntoa(ip.dst)
                    print(f"Time: {ts}, Ethernet Frame")
                    print(f"IP: {src} -> {dst}")

                    # 初始化源和目标IP的数据包计数,以元组 (src, dst) 作为键,记录对应数据包数量
                    if (src, dst) not in packet_counts:
                        packet_counts[(src, dst)] = 0
                    packet_counts[(src, dst)] += 1

                    # 确保IP数据负载是TCP数据包,只有是TCP数据包才进行后续对应协议相关检测
                    if isinstance(ip.data, dpkt.tcp.TCP):
                        tcp = ip.data
                        print(f"TCP: {tcp.sport} -> {tcp.dport}")

                        # 确保tcp.data是bytes对象,方便后续按字节进行内容判断
                        tcp_data = tcp.data
                        if tcp_data:
                            # 检测FTP流量,若TCP端口为21(FTP服务端口)则调用FTP检测函数
                            if tcp.dport == 21 or tcp.sport == 21:
                                detect_ftp(tcp_data)

                            # 检测HTTP/HTTPS流量,若端口为80(HTTP)或443(HTTPS)则调用HTTP检测函数
                            if tcp.dport == 80 or tcp.sport == 80 or tcp.dport == 443 or tcp.sport == 443:
                                detect_http(tcp_data)

                            # 检测下载行为,若目标端口为80(HTTP)且数据中包含GET请求则调用下载检测函数
                            if tcp.dport == 80 and b'GET' in tcp_data:
                                detect_downloads(tcp_data)

                            # 检测DDoS攻击命令,若目标端口为6667(IRC默认端口,常用于DDoS相关命令传输)则调用DDoS检测函数
                            if tcp.dport == 6667:
                                detect_hive_mind(tcp_data)

                    # 调用攻击检测函数,根据源和目标IP的数据包计数以及阈值判断是否存在潜在攻击
                    detect_attack(src, dst, packet_counts[(src, dst)], packet_threshold)
                else:
                    print("Invalid IP packet length")
            else:
                print("Ethernet payload is not an IP packet")
        except (dpkt.dpkt.NeedData, ValueError) as e:
            print(f"Error parsing packet: {e}")

# 主函数:负责打开PCAP文件并调用parse_packet函数进行分析
# 参数 pcap_file 为要分析的PCAP文件路径(字符串形式),packet_threshold 为判断网络攻击的数据包数量阈值(整数)
def main(pcap_file, packet_threshold):
    try:
        with open(pcap_file, 'rb') as f:
            pcap = dpkt.pcap.Reader(f)
            parse_packet(pcap, packet_threshold)
    except Exception as e:
        print(f"Error reading PCAP file: {e}")

if __name__ == "__main__":
    # 创建命令行参数解析器对象,用于解析输入的参数
    parser = argparse.ArgumentParser(description='Analyze PCAP files for network activity.')
    # 添加 -p/--pcap 参数,用于指定要分析的PCAP文件路径,该参数是必需的
    parser.add_argument('-p', '--pcap', required=True, help='Path to the PCAP file')
    # 添加 -t/--threshold 参数,用于指定判断攻击检测的数据包数量阈值,默认为100,该参数是可选的
    parser.add_argument('-t', '--threshold', type=int, default=100, help='Packet threshold for attack detection')

    args = parser.parse_args()

    main(args.pcap, args.threshold)

运行命令:

返回结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luky!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值