I used the dpkt python package to parse a pcap file, and did the following to get the tcp packets:
f = open(fname)
pcap = dpkt.pcap.Reader(f)
tcps = []
for ts, buff in pcap_in:
eth = dpkt.ethernet.Ethernet(buff)
ip = eth.data
tcp = ip.data
Now I want to see which ones had both SYN and ACK flags. I tried to put those with both of those flags in a list as follows:
syn_plus_ack = []
for tcp in tcps:
if ((tcp.flags & dpkt.tcp.TH_SYN) and (tcp.flags & dpkt.tcp.TH_ACK)):
syn_plus_ack.append(tcp)
I am not sure if this is doing what I want it to do, because I tried it on a sample pcap file and there were so many packets with a high number of SYNs but no ACK+SYNs.
I noticed the value of tcp.flags in those in syn_plus_ack is 18, dpkt.tcp.TH_SYN is 2, and dpkt.tcp.TH_ACK is 16. Is the tcp.flags value the sum of the value of all flags in the packet? Is there something I am doing wrong?