问题
How can I read TLS information (record length, record type...) from a packet using scapy. I have used load_layer('tls') and I'm able to read some information when there is a single TLS record in a packet but when there is multiple TLS record in a packet I'm only able to read the first TLS record. For exemple this packet contain 3 TLS record and when I want to read the records length with scapy I only get the first record length
回答1:
Total TLS decoding
As you've found, scapy doesn't decode the entire packet. But this is fine, because we can manually decode the TLS sections that scapy currently considers as a "Raw load" of bytes.
>>> pkts = rdpcap("facebook.com.pcap")
>>> extra_tls_layers = TLS(pkts[5]["TLS"].load)
>>> # We can see that TLS is now decoded, with two new layers:
>>> extra_tls_layers.show()
###[ TLS ]###
type= application_data
version= TLS 1.2
len= 1017 [deciphered_len= 1017]
iv= b''
\msg\
|###[ TLS Application Data ]###
| data= '\x1f\x11\xc4\xab\x920l\xae]=\x10\xd4\x13\x81k\x14\x98e\x8b\xcd\xa0...
mac= b''
pad= b''
padlen= None
###[ TLS ]###
type= application_data
version= TLS 1.2
len= 1517 [deciphered_len= 240]
iv= b''
\msg\
|###[ TLS Application Data ]###
| data= '\xac\x0b\xda\xba\xe8z\x99\xad\x0b_\x82\x96c\xb3\xff\x9f\xcc...
mac= b''
pad= b''
padlen= None```
To access a value of each of these layers (the newly decoded layers start at 0, 2), use the deciphered_len
attribute.
>>> pkts[5][TLS][0].deciphered_len
122
>>> pkts[5][TLS][5].deciphered_len
1
>>> extra_tls_layers[0].deciphered_len
1017
>>> extra_tls_layers[2].deciphered_len
240
Verifying completeness
The length of the entire TLS section is 1400 here, which we get with len(pkts[5][TLS])
. Note that the TLS record header is 5 bytes (content type = 1 byte, version = 2 bytes, length = 2 bytes).
So with a budget of 1400 bytes, let's check the record lengths:
- record 1: 5 + 122 => 127
- record 2: 5 + 1 => 6
- record 3: 5 + 1017 => 1022
- record 4: 5 + 240 => 245
Checking that the 4 TLS records add up,
127 + 6 + 1022 + 245 = 1400 for the TLS section.
Raw packet #6
For future readers, if the pcap is not available, and you're in the scapy interpreter, this is the relevant packet bytes for packet 6:
>>> pkt = b'\x00\r:\x8a\x18P\x124Vx\x9a\xbc\x08\x00E\x00\x05\xa0E\xfc@\x00R\x06)E\x9d\xf0\x08#\n\x00\x04\x04\x01\xbb\xca`0l;1\xfa\x93\n\xc2P\x10\x00r<\xd3\x00\x00\x16\x03\x03\x00z\x02\x00\x00v\x03\x03\x94\x01\xb5\xcc1b\x86Jh\x85\xf0vG\xb7#\xe7\xd2\n\x1d\xd0\'\x01\x8d\xb6\xab\xa9\x8af\x92=h= \x98\x0fJKbJ\xff(sw\xcbdW\xae\x16\x17\xec\xec\xb7\xba\x139\x92/9\xed\xc2\xeb\xa3\x07\x88\xaa\x13\x01\x00\x00.\x00+\x00\x02\x03\x04\x003\x00$\x00\x1d\x00 \xed\xfd\x0f*\x87\x9a;Q\xbb\x88\n\xad*\x9d,C\x96\xdd\x14\xab\xd8\xd8}\xf9(\x8f\xcb\xb3\x10+\xa63\x14\x03\x03\x00\x01\x01\x17\x03\x03\x03\xf9\x1f\x11\xc4\xab\x920l\xae]=\x10\xd4\x13\x81k\x14\x98e\x8b\xcd\xa0\x9f\xca\xfd\xcf\xd5\xc2\xa3\xc9S\xd0\x86G\xf3\xdc\x08\x8a,\x15\xbe+\x84\xfd\x87\x8bk\x956zO\xb3;\x875\xf4\xbd\x01\xe7`\x0f=\x08\xc5\xd8\xe6\x9e\xa4\xd9\xa3\x89C^\x07y"\x85\xb9|\xfc:{\x19\x99r\x9av\x15{\xf6\xf4\x91\x97\xfd\xe6\x7f\xbf\x1c\x81\xb9\x81\xc7W\xbao\x98X>n\'\x91\x11X\x9660\x92\\ub\xb896\xce\r\x84\xe0\x82:r{\xff\xbet\xea*\x03\x97Iw\xc8\x8b\x1d\xe3m\xe2%\x054\xc7\x0e\x9e\xe2LQ")a\x11M\x92eY=\xcc\x89\x9cj\xae\xa73\xf0\x90\xf9.&\xf5\x14\xbc\\\x8f\xa5\xfc\x0e"bD\xce\x92\xb0\x9d\xc3\xddm\xc2\x94\x90\x93T\no\xc7\x10k\x1a\xdfP\xecF\xa9\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\x97\xcd\x1aF\x86\x84\xb2\x9b\xf9u}\x96\xc68\x89\x97\x17\x03\x03\x05\xed\xac\x0b\xda\xba\xe8z\x99\xad\x0b_\x82\x96c\xb3\xff\x9f\xcc\t\xbd \x9cM\x0bP\xe2\xb0\xa5@\xfb)\xda:K\x9b-\xb0\x0b\xb36p\xe2<oOj\x96\x10\xe2jEq o\xc8\x99\xf2\x0es\x9fj\xc5\x0c\xc5\xc0\x83\x92>\x9d\x05\x17\xed\x85\xc8,Q\xf1W\xa9\xac\x9ez\x19\x14\x90i\x1ef\xe8E\xd6\xf1\x9f\xe0\xc1@\xed\'\x88\xb13\xf5;pg\x18\xc19,\xe2u\xefTJ\xd2\x08\xb5\x8e\xf2\xcf\xd2\xce\xf0L\xfa]\x95\x05wk\x8f\x85\xa2\x8aQ\x00\x12\r\x0f\xa6\xa9\x88:4\xb3\xa3z\xa8\xf6\xeaV\x1c\x86w\xce\xe7\x97\xf4\xc3\x19.\n\xe7>\xb2\x8dj\xcf\\\xaf5{$\xa0L\x1e\x15\xb6\xd4\xc7\xdb\xbc\x99l"D\x890K\xa8\x03\x0fz\xfd\x88\xabH\xcb\xbe\xbc0\xa3\xbetp\x90\xd3_BGe\x93[\x98\x9c\xf86\xc8\xdd\xb3]\x1c\xf0\x83\xbf\xbfs\xccj\xbd\x8fR\x8d\x9e\t\xe8\xce\xd33R/'
>>> # To reconstitute, create an Ethernet packet
>>> new_pkt = Ether(pkt)
来源:https://stackoverflow.com/questions/61304869/how-to-read-a-packet-tls-meta-data-with-scapy-python