forked from secdev/scapy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for websockets (secdev#4578)
- Loading branch information
1 parent
ab975bf
commit 68670b4
Showing
4 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# WebSocket layer unit tests | ||
# Copyright (C) 2024 Lucas Drufva <[email protected]> | ||
# | ||
# Type the following command to launch start the tests: | ||
# $ test/run_tests -P "load_contrib('websocket')" -t test/contrib/websocket.uts | ||
|
||
+ Syntax check | ||
= Import the WebSocket layer | ||
from scapy.contrib.websocket import * | ||
|
||
+ WebSocket protocol test | ||
= Packet instantiation | ||
pkt = WebSocket(wsPayload=b"Hello, world!", opcode="text", mask=True, maskingKey=0x11223344) | ||
pkt.show() | ||
import binascii | ||
print(binascii.hexlify(bytes(pkt))) | ||
assert pkt.wsPayload == b"Hello, world!" | ||
assert pkt.mask == True | ||
assert pkt.maskingKey == 0x11223344 | ||
|
||
|
||
= Packet dissection | ||
raw = b'\x01\x0dHello, world!' | ||
pkt = WebSocket(raw) | ||
pkt.show() | ||
|
||
assert pkt.fin == 0 | ||
assert pkt.rsv == 0 | ||
assert pkt.opcode == 0x1 | ||
assert pkt.mask == False | ||
assert pkt.payloadLen == 13 | ||
assert pkt.wsPayload == b'Hello, world!' | ||
|
||
= Dissect masked packet | ||
raw = b'\x01\x8d\x11\x22\x33\x44\x59\x47\x5f\x28\x7e\x0e\x13\x33\x7e\x50\x5f\x20\x30' | ||
pkt = WebSocket(raw) | ||
pkt.show() | ||
|
||
assert pkt.fin == 0 | ||
assert pkt.rsv == 0 | ||
assert pkt.opcode == 0x1 | ||
assert pkt.mask == True | ||
assert pkt.payloadLen == 13 | ||
assert pkt.wsPayload == b'Hello, world!' | ||
|
||
= Session with compression | ||
|
||
bind_layers(TCP, WebSocket, dport=5000) | ||
bind_layers(TCP, WebSocket, sport=5000) | ||
|
||
from scapy.sessions import TCPSession | ||
|
||
filename = scapy_path("/test/pcaps/websocket_compressed_session.pcap") | ||
pkts = sniff(offline=filename, session=TCPSession) | ||
|
||
assert len(pkts) == 13 | ||
|
||
assert pkts[7][WebSocket].wsPayload == b'Hello' | ||
assert pkts[8][WebSocket].wsPayload == b'"Hello"' | ||
assert pkts[10][WebSocket].wsPayload == b'Hello2' | ||
assert pkts[11][WebSocket].wsPayload == b'"Hello2"' |
Binary file not shown.