scapy icon indicating copy to clipboard operation
scapy copied to clipboard

_pack_subnet method will lose 1 byte when converting 101.132.0.0/23 to bytes

Open wenxuan70 opened this issue 8 months ago • 1 comments

Brief description

According to the description in rfc7871, the length of the ADDRESS field depends on the SOURCE PREFIX-LENGTH field.

FYI: Image

https://datatracker.ietf.org/doc/html/rfc7871#autoid-6:~:text=o%20%20ADDRESS%2C%20variable%20number%20of%20octets%2C%20contains%20either%20an%20IPv4%20or%0A%20%20%20%20%20%20IPv6%20address%2C%20depending%20on%20FAMILY%2C%20which%20MUST%20be%20truncated%20to%20the%0A%20%20%20%20%20%20number%20of%20bits%20indicated%20by%20the%20SOURCE%20PREFIX%2DLENGTH%20field%2C%0A%20%20%20%20%20%20padding%20with%200%20bits%20to%20pad%20to%20the%20end%20of%20the%20last%20octet%20needed.

Scapy version

2.7.0

Python version

3.13.5

Operating system

Linux 5.4.143

Additional environment information

No response

How to reproduce

I have 1 minimal reproducible code snippet here:

from scapy.layers.dns import EDNS0ClientSubnet
b = EDNS0ClientSubnet(source_plen=23, address='101.132.0.0')
# actual:   b'\x00\x08\x00\x06\x00\x01\x17\x00e\x84'
# expected: b'\x00\x08\x00\x07\x00\x01\x17\x00e\x84\x00'
print(bytes(b))

Actual result

No response

Expected result

No response

Related resources

No response

wenxuan70 avatar Mar 13 '26 09:03 wenxuan70

I'm working on a fix for this.

The root cause is that _pack_subnet strips trailing zero bytes rather than truncating to ceil(source_plen / 8) bytes as required by RFC 7871. The fix passes source_plen from the packet into _pack_subnet so it can apply the correct truncation when the prefix length is explicitly set.

A PR will follow shortly.

T3pp31 avatar Apr 07 '26 14:04 T3pp31