Search code examples
pythonscapy

How to set TCP options (Timestamp and SAckOk) via Scapy?


I have following information for each packet I want to generate via Scapy, it is tcpdump output:

1509472682.813373 MAC1 > MAC2, ethertype IPv4 (0x0800), length 74: (tos 0x0, ttl 64, id 64271, offset 0, flags [DF], proto TCP (6), length 60) IP1.port1 > IP2.port2: Flags [S], cksum 0x4a0b (incorrect -> 0xe5b4), seq 1763588570, win 65535, options [mss 1460,sackOK,TS val 1098453 ecr 0,nop,wscale 6], length 0

I have generated TCP packets as follow, but when I check them via wireshark it seems that the Timestamp option is not set at all and Sack is not set as I have expected.

for r in (("mss","MSS"), ("sackOK","SAck"), ("nop","NOP"), ("TS ", "Timestamps "), ("val", "TSval"), ("ecr", "TSecr"), ("wscale","WScale")):
    opt = opt.replace(*r)

opt=opt.split(",") 

for op in opt:       
    op = op.split()
    if len(op) == 2:
        options.append((op[0],int(op[1])))
    elif op[0] == "Timestamps": ## Need some modification, so that Scapy do not ignore it.
        options.append((op[0],(int(op[2]),int(op[4]))))
    elif op[0] == "SAck": ## How to set SAck option to be SAck Permitted?
        options.append((op[0], '')) 
    else: # NOP
        options.append((op[0], ()))

ip = ether/IP(src=ipsrc, dst=ipdst, len=ipLen, tos=frameTos, ttl=frameTtl, offset=frameOffset, id=frameId, flags=frameFlags, proto=protocol.lower())

if ack_n is None:
    pkt = ip / TCP(sport=srcport, dport=dstport , flags=frameFlag, seq=int(seq_n), chksum=cksum, window=win, options=options) / secrets.token_bytes(frameLen-54)                  
else:                        
    pkt = ip / TCP(sport=srcport, dport=dstport , flags=frameFlag, seq=int(seq_n), ack=ack_n, chksum=cksum, window=win, options=options) / secrets.token_bytes(frameLen-54)                  

pkt.time = frametime

wrpcap(output, pkt, append=True)

Here is what is passed to options field for the packet I have provided its info at the beginning:

[('MSS', 1460), ('SAck', ''), ('Timestamps', (1098453, 0)), ('NOP', ()), ('WScale', 6)]

But when I check the packet via Wireshark the Timestamps option is not set, it seems that Scapy has ignored it, and the SAck option is not set as I have expected.

Here is how this packet options field looks like in Wireshark:

enter image description here

Here is what I have expected it to be:

enter image description here

So the question here are:

  • How to set timestamps, so that the Scapy does not ignore it?
  • How to set SAck, so that it is marked as permitted.

Edit 1:

I have solved the problem with SAck, I should pass it as ('SAckOK', '')


Solution

  • Finally I have find what I have set wrong:

    As I mentioned in my first edit, to set Selective Acknowledgment Permitted, I should pass option a tuple as ('SAckOK', '').

    To set timestamp I should pass option a tuple as ('Timestamp', (1098453, 0)) in the inner tuple the first argument is Val and the second one is Ecr.