scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Preventing problems with segments out of order

Open plutec opened this issue 3 years ago • 5 comments

Brief description

I detected an "out-of-order" TCP segment in the trace, could make the TCPSession stop inmediatly.

out_of_order_segment

The fix is pretty simple, some out of order segments could generate a sequence number < 0, that make the StringBuffer used to collect all the data from the segments raises an error. To prevent it, we just check the seq number is > 0 before to continue with the append of the data.

plutec avatar Feb 17 '22 16:02 plutec

The checks are not working because the last commit to the master branch does not pass all the tests: https://github.com/secdev/scapy/commit/4f42d2522915312ba2bbfb4c434a13ae9fc9ec8b

plutec avatar Feb 18 '22 09:02 plutec

I get the point although I'm not sure if the behavior in this case will be intended :/

gpotter2 avatar Feb 24 '22 17:02 gpotter2

What do you mean with "Intended"? If a tool is running the "sniff" function with TCPSession and it receives an "out-of-order" TCP segment, it will crash inmediatly. Under my point of view, I think is better to manage that, instead to stop the execution abruptly.

plutec avatar Feb 24 '22 17:02 plutec

An "out-of-order" TCP segment breaking TCPSession? Weird because the whole reason to use TCPSession is to reorder the segments that are out-of-order... Oh wait it's not doing its job at all because: https://github.com/secdev/scapy/blob/master/scapy/sessions.py#L323

shrddr avatar Apr 10 '22 10:04 shrddr

Hi @shrddr, The problem is not when the packet is missing, in that case nothing wrong must happen. The problem is when the sequence number is invalid (<0 after calculus).

plutec avatar Apr 10 '22 10:04 plutec

This sounds like a fun and useful bug. Will try to replicate and report back.

stryngs avatar Dec 06 '22 08:12 stryngs

I tested tonight, got this:

ValueError: Incorrect type of value for field seq:
struct.error('argument out of range')
To inject bytes into the field regardless of the type, use RawVal. See help(RawVal)

Set [TCP].seq = -3. Scapy did let me do this, but upon trying to assemble into something useful scapy throws an Exception. Can you produce a pcap that will crash scapy @plutec via a sniff()?

stryngs avatar Dec 07 '22 06:12 stryngs

Hi @stryngs Here is a video I made in February and the code to reproduce the error (included the pcap): https://github.com/plutec/scapy_poc

Regards,

plutec avatar Dec 14 '22 10:12 plutec

fixed in https://github.com/secdev/scapy/pull/4082

gpotter2 avatar Feb 04 '24 18:02 gpotter2