Set tcp MSS in python

1.3k views Asked by At

I am trying to do the following:

(1) Receive a network packet, (2) Change the tcp mss (max. segment size) to a different value, (3) Send the packet out.

I am using python, iptables and dpkt to do this, but I am not able to change the MSS in the tcp header (in dpkt, how do I change mss in pkt.tcp.opts)?

Can someone please help me with changing the MSS in the TCP header?

Thanks!

1

There are 1 answers

0
Groot On
buf = pkt.tcp.opts
buf2 = ""
while buf:
      o = ord(buf[0])
      if o > tcp.TCP_OPT_NOP:
           l = ord(buf[1])
           d = buf[2:l]
           if o == tcp.TCP_OPT_MSS:
                mss = struct.unpack(">H", d)[0]
                mss2 = <newmss>
                d2 = struct.pack(">H", mss2)

                buf = buf[0:2] + d2 + buf[l:]
           buf2 += buf[0:l]
           buf = buf[l:]
      else:
           buf2 += buf[0]
           d, buf = '', buf[1:]

pkt.tcp.opts = buf2