Skip to content

pnio_dcp: compute block length and padding when building response blocks - #5233

Open
bIackr0se wants to merge 2 commits into
secdev:masterfrom
bIackr0se:fix/pnio-dcp-block-length-padding
Open

bIackr0se wants to merge 2 commits into
secdev:masterfrom
bIackr0se:fix/pnio-dcp-block-length-padding

Conversation

@bIackr0se

Copy link
Copy Markdown
Contributor

Description

Building PROFINET DCP response blocks writes wrong block lengths and stray padding bytes:

  • DCPIPBlock, DCPFullIPBlock, DCPDeviceIDBlock, DCPDeviceOptionsBlock and DCPOEMIDBlock use LenField("dcp_block_length", None), which counts the payload. A block alone gets length 0, and a block stacked before other blocks gets their length.
  • DCPMACBlock and DCPManufacturerSpecificBlock use FieldLenField without length_of, so building raises Field should have either length_of or count_of.
  • DCPDeviceInitiativeBlock leaves BlockInfo out of its length (2 instead of 4).
  • The padding default b"\x00" is written after every block, also when the block length is even.
p = ProfinetDCP(service_id=DCP_SERVICE_ID_IDENTIFY, service_type=DCP_RESPONSE) / \
    DCPNameOfStationBlock(name_of_station="plc-1") / \
    DCPIPBlock(ip="192.168.0.10") / DCPDeviceIDBlock() / DCPDeviceRoleBlock()

Before, the IP block has length 20 and is followed by a zero byte, and Wireshark decodes the IP block and then reports [Malformed Packet: PN-DCP]. After, the block lengths are 7, 14, 6 and 4, the only padding byte follows the odd-length NameOfStation block, and Wireshark decodes all four blocks.

Fixed-size blocks now default to their constant length, as DCPDeviceRoleBlock already does, and variable-size blocks use FieldLenField with length_of, as DCPNameOfStationBlock does. padding defaults to b"" and is written as one zero byte only after an odd-length block; a value set by hand is cut or zero-filled to that length. DCPFullIPBlock now defaults to four DNS addresses, the number its dissector reads. Dissection is unchanged.

This message was written with the help of AI (Claude Opus 5.5).

DCP response blocks built with scapy had wrong lengths: LenField counts
the payload, so IP, Full IP, DeviceID, DeviceOptions and OEMID blocks got
0 or the length of the blocks stacked after them; the MAC and
ManufacturerSpecific blocks raised because their FieldLenField had no
length_of; DeviceInitiative left BlockInfo out. Every block also wrote
its b"\x00" padding default, also after an even-length block.

Fixed-size blocks now default to their constant length, variable-size
blocks count their value plus BlockInfo, and padding is one zero byte
after an odd-length block only. DCPFullIPBlock defaults to four DNS
addresses, the number its dissector reads.

AI-Assisted: yes (Claude Opus 5.5, GPT-6 Astra)
Comment thread scapy/contrib/pnio_dcp.py Outdated
MultiEnumField("sub_option", 2, DCP_SUBOPTIONS, fmt='B',
depends_on=lambda p: p.option),
LenField("dcp_block_length", None),
LenField("dcp_block_length", 14),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is defeating the purpose of LenField.

Comment thread scapy/contrib/pnio_dcp.py
Comment on lines +176 to +184
def dcp_block_padding_length(pkt):
"""
A DCP block of odd length is followed by one padding byte. While the
block is built, dcp_block_length may still be None (computed field).
"""
length = pkt.get_field("dcp_block_length").i2m(pkt, pkt.dcp_block_length)
if isinstance(length, RawVal):
length = int.from_bytes(bytes(length), "big")
return length % 2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you need this.

LenField counts the payload; a fixed block length is a plain ShortField
with the constant as default.

AI-Assisted: yes (Claude Opus 5.5)
@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.16%. Comparing base (5376fcc) to head (b275168).
⚠️ Report is 45 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5233      +/-   ##
==========================================
- Coverage   80.89%   80.16%   -0.74%     
==========================================
  Files         394      375      -19     
  Lines       98710    98615      -95     
==========================================
- Hits        79853    79053     -800     
- Misses      18857    19562     +705     
Files with missing lines Coverage Δ
scapy/contrib/pnio_dcp.py 95.37% <100.00%> (+4.10%) ⬆️

... and 309 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants