2014-12-03 17 views
7

Utilizzando lo strumento di analisi/sniffing del pacchetto python Scapy, vorrei creare un pacchetto da una stringa di byte non elaborata. Mentre i dettagli del mio specifico caso d'uso sono più realistici, il seguente esempio illustra il mio problema e il mio tentativo attuale:Come creare un pacchetto Scapy da byte non elaborati

# Get an example packet (we won't really have an offline file in production.) 
pkt = sniff(offline="./example_packets/example_packets2.pcap") 

# Convert it to raw bytes -- oddly __str__ does this. 
raw_packet = str(pkt) 

# Current, broken, attempt to construct a new packet from the same bytes as the old. 
# In truth, there are easier ways to copy packets from existing Scapy packets, but 
# we are really just using that offline packet as a convenient example. 
new_packet = Packet(_pkt=raw_packet) 

# Sadly, while this packet has the bytes internally, it no longer has the 
# interpretations of the layers like the original packet did (such as saying that 
# the packet is ARP and has these field values, etc. 
print repr(new_packet) 

Come posso produrre un new_packet da byte prime che sarà lo stesso come se fosse annusò da un file pcap?

risposta

7

Non c'è modo per Scapy di indovinare il primo livello del pacchetto, quindi è necessario specificarlo.

È possibile farlo utilizzando la sottoclasse Packet appropriata. Ad esempio, supponiamo che il primo strato del pacchetto sia Ethernet, utilizzare Ether(raw_packet) anziché Packet(raw_packet).

+1

Se si utilizza la classe 'Ether', Scapy sarà in grado di determinare automaticamente gli altri livelli? Sembra che la funzione "sniff" determini in qualche modo gli altri livelli. – BlackVegetable

+1

Se per altri livelli intendete gli strati sopra il livello Ethernet, la risposta è sì, grazie al campo 'type'. – Pierre

+0

Ovviamente! Questo dovrebbe essere ovvio per me. Grazie per il chiarimento. (In quale altro modo i router sanno come interpretare gli altri livelli se non per le informazioni sul tipo?) – BlackVegetable

Problemi correlati