Skip to content

Commit ac97ba1

Browse files
authored
feat: make it easier to use packet (#63)
now users can copy-paste packets from Ether layer in most cases
1 parent fa74634 commit ac97ba1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

utils/packet_input.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import subprocess
55

6-
from scapy.all import IP, TCP, hexdump, import_hexcap
6+
from scapy.all import IP, TCP, Ether, hexdump, import_hexcap
77

88
FIREWALL_COMMANDS_HELP = "\r\n( · - · · · \r\n\
99
You may need to temporarily block RST output packets in your firewall.\r\n\
@@ -113,24 +113,30 @@ def _ask_yesno(cls, question):
113113

114114
@classmethod
115115
def _supported_or_correct(cls, copied_packet):
116-
return (copied_packet[IP].version == 4)
116+
return (copied_packet.haslayer(IP) and (copied_packet[IP].version == 4))
117117

118118
@classmethod
119119
def _read_pasted_packet(cls, show=False):
120120
print(" ********************************************************************** ")
121121
print(" paste here the packet hex dump start with the IP layer and then enter :")
122122
print(" . . . - . . . . - . . . . - . . . . - . ")
123-
p1 = IP(import_hexcap())
123+
packet_string = import_hexcap()
124124
print(" . . . - . . . . - . . . . - . . . . - . ")
125-
if not cls._supported_or_correct(p1):
126-
raise BADPacketException(
127-
"it's not IPv4 or the hexdump is not started with IP layer")
128-
p1[IP].src = '127.1.2.7'
125+
packet_object = None
126+
if not cls._supported_or_correct(IP(packet_string)):
127+
if not cls._supported_or_correct(Ether(packet_string).payload):
128+
raise BADPacketException(
129+
"it's not IPv4 or the hexdump is not started with IP layer")
130+
else:
131+
packet_object = Ether(packet_string).payload
132+
else:
133+
packet_object = IP(packet_string)
134+
packet_object[IP].src = '127.1.2.7'
129135
if show:
130136
print(" . . . - . developed view of this packet:")
131-
p1.show()
132-
print(" . . . - . . . . - . . . . - . . . . - . ")
133-
return p1
137+
packet_object.show()
138+
print(" . . . - . (make sure it's correct) . . . - . ")
139+
return packet_object
134140

135141
@classmethod
136142
def from_stdin(cls, os_name: str, trace_retransmission: bool):

0 commit comments

Comments
 (0)