Python Scapy Introduce a TCP option

2.7k views Asked by At

I am trying to build a packet with a TCP option called user timeout. Does scapy support this? Adding options like MSS seems pretty straightforward. Here is the RFC for the option: https://www.rfc-editor.org/rfc/rfc5482#section-2

Any pointers?

1

There are 1 answers

3
abarnert On

The documentation doesn't seem to explain any way to set an arbitrary option by number.

Digging through the code, it looks like TCPOptionsField.i2m lets you do so just by passing an int instead of a str. So, try using 28 the same places you'd use MSS. It looks like you need to compose the rest of the option field as a string—the length, then the high-order byte of the UTO+granularity, then the low-order byte.

Or just modify the code to handle it:

I think what you want is the TCPOptions tuple in scapy/layers/inet.py:

TCPOptions = (
          { 0 : ("EOL",None),
            ...
            28 : ("UserTimeout",!H)
            },
          { "EOL":0,
            ...
            "UserTimeout":28,
            } )

Then you can (hopefully) set the UTO the same way you'd set the MSS.

Either way, you're responsible for composing the 1-bit granularity and the 15-bit timeout before passing the result as an option value, but I think that's simpler than changing it to take a tuple of a bool and an int as the option value and composing it inside TCP.

Of course unless you've patched the kernel on both sides, and made sure the intervening network doesn't have any devices that bail on or strip out unknown TCP options, it's not going to be very interesting anyway.