Fixing bugs on UL and DL Data payloads

This commit is contained in:
Sebastien Dudek 2021-01-08 22:52:58 +01:00
parent a5f0a9d65c
commit e185b68f2d
1 changed files with 30 additions and 14 deletions

View File

@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
# LoRa Scapy layers
# Copyright (C) 2020 Sebastien Dudek (@FlUxIuS) at @PentHertz
"""
Copyright (C) 2020 Sebastien Dudek (@FlUxIuS)
initially developed @PentHertz
and improve at @Trend Micro
"""
from scapy.packet import Packet
from scapy.fields import BitField, ByteEnumField, ByteField, \
@ -24,6 +27,19 @@ class FCtrl_DownLink(Packet):
return "", p
class FCtrl_Link(Packet):
name = "FCtrl_UpLink"
fields_desc = [BitField("ADR", 0, 1),
BitField("ADRACKReq", 0, 1),
BitField("ACK", 0, 1),
BitField("UpClassB_DownFPending", 0, 1),
BitFieldLenField("FOptsLen", 0, 4)]
# pylint: disable=R0201
def extract_padding(self, p):
return "", p
class FCtrl_UpLink(Packet):
name = "FCtrl_UpLink"
fields_desc = [BitField("ADR", 0, 1),
@ -507,7 +523,7 @@ class FOpts(Packet):
def FOptsDownShow(pkt):
try:
if pkt.FCtrl[0].FOptsLen > 0 and pkt.MType & 0b1 == 1 and pkt.MType <= 0b101: # noqa: E501
if pkt.FCtrl[0].FOptsLen > 0 and pkt.MType & 0b1 == 1 and pkt.MType <= 0b101 and (pkt.MType & 0b10 > 0): # noqa: E501
return True
return False
except Exception:
@ -516,7 +532,7 @@ def FOptsDownShow(pkt):
def FOptsUpShow(pkt):
try:
if pkt.FCtrl[0].FOptsLen > 0 and pkt.MType & 0b1 == 0 and pkt.MType >= 0b010: # noqa: E501
if pkt.FCtrl[0].FOptsLen > 0 and pkt.MType & 0b1 == 0 and pkt.MType >= 0b010 and (pkt.MType & 0b10 > 0): # noqa: E501
return True
return False
except Exception:
@ -530,15 +546,13 @@ class FHDR(Packet):
lambda pkt:(pkt.MType >= 0b010 and
pkt.MType <= 0b101)),
ConditionalField(PacketListField("FCtrl", b"",
FCtrl_DownLink,
FCtrl_Link,
length_from=lambda pkt:1),
lambda pkt:(pkt.MType & 0b1 == 1 and
pkt.MType <= 0b101)),
ConditionalField(PacketListField("FCtrl", b"",
FCtrl_UpLink,
length_from=lambda pkt:1),
lambda pkt:(pkt.MType & 0b1 == 0 and
pkt.MType >= 0b010)),
lambda pkt:((pkt.MType & 0b1 == 1 and
pkt.MType <= 0b101 and
(pkt.MType & 0b10 > 0)) or
(pkt.MType & 0b1 == 0 and
pkt.MType >= 0b010))),
ConditionalField(LEShortField("FCnt", 0),
lambda pkt:(pkt.MType >= 0b010 and
pkt.MType <= 0b101)),
@ -604,10 +618,10 @@ class RejoinReq(Packet): # LoRa 1.1 specs
class FRMPayload(Packet):
name = "FRMPayload"
fields_desc = [ConditionalField(StrField("DataPayload", "", remain=4), # Downlink # noqa: E501
fields_desc = [ConditionalField(StrField("DLDataPayload", "", remain=4), # Downlink # noqa: E501
lambda pkt:(pkt.MType == 0b101 or
pkt.MType == 0b011)),
ConditionalField(StrField("DataPayload", "", remain=6), # Uplink # noqa: E501
ConditionalField(StrField("ULDataPayload", "", remain=6), # Uplink # noqa: E501
lambda pkt:(pkt.MType == 0b100 or
pkt.MType == 0b010)),
ConditionalField(PacketListField("Join_Request_Field", b"",
@ -650,6 +664,7 @@ MTypes = {0b000: "Join-request",
class MHDR(Packet): # Same for 1.0 as for 1.1
name = "MHDR"
fields_desc = [BitEnumField("MType", 0b000, 3, MTypes),
BitField("RFU", 0b000, 3),
BitField("Major", 0b00, 2)]
@ -668,6 +683,7 @@ class LoRa(Packet): # default frame (unclear specs => taken from https://www.nc
name = "LoRa"
version = "1.1" # default version to parse
encrypted = True
fields_desc = [XBitField("Preamble", 0, 4),
XBitField("PHDR", 0, 16),
XBitField("PHDR_CRC", 0, 4),