phaethon / kamene

Network packet and pcap file crafting/sniffing/manipulation/visualization security tool. Originally forked from scapy in 2015 and providing python3 compatibility since then.
GNU General Public License v2.0
868 stars 191 forks source link

Exception is thrown on sprintf("%TCP.flags%") with NS flag set #247

Closed Kkevsterrr closed 6 years ago

Kkevsterrr commented 6 years ago

Brief description

If packet flags are set randomly, if they are sprintf-ed to %TCP.flags%, an exception is thrown, even though every value for this field can be represented by flag letters.

Environment

How to reproduce

$ scapy
>>> p = IP()/TCP()
>>> p[TCP] = 0x100
>>> p.sprintf("%TCP.flags")

Actual result

$ scapy
>>> p = IP()/TCP()
>>> p[TCP] = 0x100
>>> p.sprintf("%TCP.flags")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scapy/packet.py", line 979, in sprintf
    val = self.payload.sprintf("%%%s%%" % sfclsfld, relax)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scapy/packet.py", line 977, in sprintf
    val = self.fieldtype[fld].i2repr(self,val)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scapy/fields.py", line 907, in i2repr
    r += self.names[i]
IndexError: string index out of range

Expected result

$ scapy
>>> p = IP()/TCP()
>>> p[TCP] = 0x100
>>> p.sprintf("%TCP.flags")
'N'

0x100 is 1000000000, which is the "NS" flag (https://tools.ietf.org/html/rfc3540). Any flag value that contains the N flag causes a 'string index out of range' error.

Related resources

Related resources: https://tools.ietf.org/html/rfc3540

phaethon commented 6 years ago

I assume you meant:

p[TCP].flags = 0x100
p.sprintf("%TCP.flags%")

This does not solve the issue, but necessary for the clarity of other potential readers. Looking into the issue now.

phaethon commented 6 years ago

It is working now in the latest github commit. PyPI package not released yet. Please, take into account renaming of the python package. See the Readme.