通过SCAPY从PCAP中选择数据包

发布于 2025-02-09 15:01:32 字数 2101 浏览 1 评论 0原文

我正在尝试使用Scapy从PCAP文件中获取密码。我只想从每个文件中获取第一个数据包,BeaCause我正在使用过滤器。

这是我的代码:

from scapy.all import *
from scapy.layers.radius import Radius
import re
import os
import pathlib

    

def get_ciphersuite():
    ciphersuites = {
        #'0000' : 'TLS_NULL_WITH_NULL_NULL',
        '002f' : 'TLS_RSA_WITH_AES_128_CBC_SHA',
        '0035' : 'TLS_RSA_WITH_AES_256_CBC_SHA',
        '003c' : 'TLS_RSA_WITH_AES_128_CBC_SHA256',
        '003d' : 'TLS_RSA_WITH_AES_256_CBC_SHA256',
        '009c' : 'TLS_RSA_WITH_AES_128_GCM_SHA256',
        '009d' : 'TLS_RSA_WITH_AES_256_GCM_SHA384',
        'c02c' : 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384'
    }
    dir_pcaps = "./pcaps/"
    pcaps = os.listdir(dir_pcaps)
    for pcap in pcaps:
        if pathlib.Path(pcap).suffix in [".pcap", ".cap", ".pcapng"]:
            packets = rdpcap(dir_pcaps+pcap)
            print(packets)
            for packet in packets:
                if packet.haslayer("Radius") and packet[IP].src == "10.10.10.40":
                    rp = packet.getlayer("Radius")
                    rp_hex = bytes_hex(rp).decode()
                    for ciphersuite in ciphersuites:
                        r = re.findall(ciphersuite, rp_hex)
                        if r:
                            print(ciphersuites[ciphersuite]," --> "+pcap)
                            
                            break
                        else:
                            pass
                        
get_ciphersuite()

这是输出:

<128CBCSHA.pcapng: TCP:0 UDP:20 ICMP:0 Other:1>
TLS_RSA_WITH_AES_128_CBC_SHA  --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256  --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256  --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256  --> 128CBCSHA.pcapng
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384  --> 128CBCSHA.pcapng
<test.pcapng: TCP:0 UDP:11 ICMP:0 Other:1>
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384  --> test.pcapng
TLS_RSA_WITH_AES_256_CBC_SHA256  --> test.pcapng

此脚本将每个PCAP文件中每个数据包的密码返回,但是我只需要每个文件中的第一个数据包。

I'm tryng to get ciphersuites from pcap file with Scapy. I would like to get only the first packet from every file, beacause I'm using a filter.

This is my code:

from scapy.all import *
from scapy.layers.radius import Radius
import re
import os
import pathlib

    

def get_ciphersuite():
    ciphersuites = {
        #'0000' : 'TLS_NULL_WITH_NULL_NULL',
        '002f' : 'TLS_RSA_WITH_AES_128_CBC_SHA',
        '0035' : 'TLS_RSA_WITH_AES_256_CBC_SHA',
        '003c' : 'TLS_RSA_WITH_AES_128_CBC_SHA256',
        '003d' : 'TLS_RSA_WITH_AES_256_CBC_SHA256',
        '009c' : 'TLS_RSA_WITH_AES_128_GCM_SHA256',
        '009d' : 'TLS_RSA_WITH_AES_256_GCM_SHA384',
        'c02c' : 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384'
    }
    dir_pcaps = "./pcaps/"
    pcaps = os.listdir(dir_pcaps)
    for pcap in pcaps:
        if pathlib.Path(pcap).suffix in [".pcap", ".cap", ".pcapng"]:
            packets = rdpcap(dir_pcaps+pcap)
            print(packets)
            for packet in packets:
                if packet.haslayer("Radius") and packet[IP].src == "10.10.10.40":
                    rp = packet.getlayer("Radius")
                    rp_hex = bytes_hex(rp).decode()
                    for ciphersuite in ciphersuites:
                        r = re.findall(ciphersuite, rp_hex)
                        if r:
                            print(ciphersuites[ciphersuite]," --> "+pcap)
                            
                            break
                        else:
                            pass
                        
get_ciphersuite()

This is the output:

<128CBCSHA.pcapng: TCP:0 UDP:20 ICMP:0 Other:1>
TLS_RSA_WITH_AES_128_CBC_SHA  --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256  --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256  --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256  --> 128CBCSHA.pcapng
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384  --> 128CBCSHA.pcapng
<test.pcapng: TCP:0 UDP:11 ICMP:0 Other:1>
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384  --> test.pcapng
TLS_RSA_WITH_AES_256_CBC_SHA256  --> test.pcapng

This script returns me the ciphersuite for every packet in every pcap file, but I need only the first packet in every file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2025-02-16 15:01:32

您可以尝试使用嗅探。例如,替换

packets = rdpcap(dir_pcaps+pcap)

packets = sniff(offline=dir_pcaps+pcap, count=1,
                lfilter=lambda x: x.haslayer("Radius") and x[IP].src == "10.10.10.40")

You could try using sniff. For instance, replace

packets = rdpcap(dir_pcaps+pcap)

with

packets = sniff(offline=dir_pcaps+pcap, count=1,
                lfilter=lambda x: x.haslayer("Radius") and x[IP].src == "10.10.10.40")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文