8000 parse_pcap_klap: use request_uri for matching the response (#1136) · msz-coder/python-kasa@130e1b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 130e1b6

Browse files
authored
parse_pcap_klap: use request_uri for matching the response (python-kasa#1136)
tshark 4.4.0 does not have response_for_uri, this fixes response detection by using request_uri, too.
1 parent d897503 commit 130e1b6

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

devtools/parse_pcap_klap.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929
from kasa.protocol import DEFAULT_CREDENTIALS, get_default_credentials
3030

3131

32+
def _is_http_response_for_packet(response, packet):
33+
"""Return True if the *response* contains a response for request in *packet*.
34+
35+
Different tshark versions use different field for the information.
36+
"""
37+
if not hasattr(response, "http"):
38+
return False
39+
if hasattr(response.http, "response_for_uri") and (
40+
response.http.response_for_uri == packet.http.request_full_uri
41+
):
42+
return True
43+
# tshark 4.4.0
44+
if response.http.request_uri == packet.http.request_uri:
45+
return True
46+
47+
return False
48+
49+
3250
class MyEncryptionSession(KlapEncryptionSession):
3351
"""A custom KlapEncryptionSession class that allows for decryption."""
3452

@@ -222,7 +240,7 @@ def main(
222240
while True:
223241
try:
224242
packet = capture.next()
225-
# packet_number = capture._current_packet
243+
packet_number = capture._current_packet
226244
# we only care about http packets
227245
if hasattr(
228246
packet, "http"
@@ -267,18 +285,16 @@ def main(
267285
message = bytes.fromhex(data)
268286
operator.local_seed = message
269287
response = None
288+
print(
289+
f"got handshake1 in {packet_number}, "
290+
f"looking for the response"
291+
)
270292
while (
271293
True
272294
): # we are going to now look for the response to this request
273295
response = capture.next()
274-
if (
275-
hasattr(response, "http")
276-
and hasattr(response.http, "response_for_uri")
277-
and (
278-
response.http.response_for_uri
279-
== packet.http.request_full_uri
280-
)
281-
):
296+
if _is_http_response_for_packet(response, packet):
297+
print(f"found response in {packet_number}")
282298
break
283299
data = response.http.get_field_value("file_data", raw=True)
284300
message = bytes.fromhex(data)

0 commit comments

Comments
 (0)
0