Skip to content
Extraits de code Groupes Projets
Valider c6a9ada4 rédigé par François De Keersmaeker's avatar François De Keersmaeker
Parcourir les fichiers

Added support for CoAP

parent 8d5e6259
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #60764 réussi
"""
Anonymize CoAP packets.
"""
from enum import Enum
from scapy.contrib.coap import CoAP
class CoapFields(Enum):
"""
CoAP fields.
"""
TYPE = "type"
CODE = "code"
OPTIONS = "options"
URI_PATH = "Uri-Path"
def anonymize_coap(coap: CoAP) -> None:
"""
Anonymize a packet's CoAP layer.
Args:
coap (scapy.contrib.coap.CoAP): CoAP layer to anonymize
"""
# Remove all fields other than type and code
for field in coap.fields.copy():
if field not in [f.value for f in CoapFields]:
delattr(coap, field)
# Remove all options other than Uri-Path
options = coap.getfieldval(CoapFields.OPTIONS.value)
new_options = []
for k, v in options:
if k == CoapFields.URI_PATH.value:
new_options.append((k, v))
coap.setfieldval(CoapFields.OPTIONS.value, new_options)
from scapy.contrib.coap import CoAP
from pcap_anonymize.layers.coap import CoapFields, anonymize_coap
### TEST FUNCTIONS ###
def test_anonymize_coap() -> None:
# Build CoAP layer
options = [('Uri-Host', 'host'), ('Uri-Path', 'sensors'), ('Uri-Path', 'temperature')]
coap = CoAP(type=0, code=1, msg_id=0x1234, token=b"token", options=options)
anonymize_coap(coap)
# Check remaining fields
assert coap.type == 0
assert coap.code == 1
# Ensure other fields have been deleted
for field in coap.fields:
assert field in [f.value for f in CoapFields]
for k, v in coap.getfieldval(CoapFields.OPTIONS.value):
assert k == CoapFields.URI_PATH.value
assert v in ["sensors", "temperature"]
from scapy.layers.inet import TCP
from scapy.layers.http import HTTP, HTTPRequest, HTTPResponse
from scapy.layers.http import HTTPRequest, HTTPResponse
from pcap_anonymize.layers.http import (
HttpFields,
get_http_layer,
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter