Scapy: Processing partial TLS segments

前端 未结 1 1908
梦谈多话
梦谈多话 2021-01-06 17:17

I am trying to extract TLS meta-data from a pcap using Scapy. I am able to successfully parse the packets and individual messages such as the client-hello, server-hello etc

相关标签:
1条回答
  • 2021-01-06 18:08

    Not sure if there is a better solution, but here's what I did to get around my problem. The accumulated frame-sizes seem to agree with what Wireshark does when it reassembles TLS frame from multiple packets.

    Note: This solution assumes there are no misordered packets or duplicates. Also, the solution shown here is not complete. The code below only shows how to accumulate TLS frames when they span multiple IP packets for a single TCP stream. Please follow Janus's suggestion (mentioned in the comments below) or come up with your own solution to accumulate different the streams.

    def extractDataFromPcap(pcapfile):
      load_layer("tls")
    
      try:
        reader = PcapReader(pcapfile)
    
        # tls frame accumulation related variables.
        tls_accumulate = False
        tls_leftover_len = 0
        tls_blist = list()
    
        for pkt in reader:
          try:
            if tls_accumulate and pkt.haslayer(TCP):
              ip_tcp_hdr_overhead = ((pkt[IP].ihl*4) + (pkt[TCP].dataofs*4))
              pkt_payload_len = (pkt[IP].len - ip_tcp_hdr_overhead)
              tls_leftover_len = (tls_leftover_len - pkt_payload_len)
              tls_blist.append(raw(pkt[TCP].payload))
    
              if tls_leftover_len <= 0:
                # got complete TLS frame
                tls_raw_bytes = b''.join(tls_blist)
    
                # parse accumulated frame.
                tls = TLS(tls_raw_bytes)
                #... process tls object
    
                # reset accumlation variables.
                tls_accumulate = False
                tls_leftover_len = 0
                tls_blist = list()
              else:
                continue
    
            if pkt.haslayer(TCP) and pkt.haslayer(TLS):
              if not tls_accumulate:
                # Process new TLS frame.
                # Pkt over head: ip-hdr-len + tcp-hdr-len
                ip_tcp_hdr_overhead = ((pkt[IP].ihl*4) + (pkt[TCP].dataofs*4))
                pkt_payload_len = (pkt[IP].len - ip_tcp_hdr_overhead)
                if pkt[TLS].len > pkt_payload_len:
                  # partial TLS frame. Start accumulating.
                  tls_accumulate = True
                  tls_leftover_len = (pkt[TLS].len - pkt_payload_len)
                  tls_blist.append(raw(pkt[TCP].payload))
                else:
                  # complete TLS frame
                  # ... process complete TLS Frame 
          except Exception as error:
            pkt.show()
            print("tcp-processing error: %s", error)
            sys.exit(-1)
    
      except Exception as error:
        print("packet processing error: %s", error)
        sys.exit(-1)
    
    0 讨论(0)
提交回复
热议问题