fields: decode BitLenField integers with int.from_bytes - #5108
Merged
Conversation
AI-Assisted: yes (GPT-5.6-Cyber)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5108 +/- ##
=======================================
Coverage 80.59% 80.60%
=======================================
Files 390 390
Lines 96892 96895 +3
=======================================
+ Hits 78094 78099 +5
+ Misses 18798 18796 -2
🚀 New features to boost your workflow:
|
polybassa
approved these changes
Aug 27, 2026
KernelClint
added a commit
to KernelClint/scapy
that referenced
this pull request
Sep 2, 2026
Move the fix from Kerberos into the BER codec, as reviewed. BERcodec_INTEGER.do_dec shifted a growing Python integer one octet at a time, so the cost of each octet rose with the number already accumulated and decoding was quadratic in the encoded width. A sender could multiply parsing cost by padding any INTEGER with leading sign octets, in any protocol that uses BER, not only in a Kerberos etype. Decoding 64,000 content octets took 304 ms; int.from_bytes does the same two's-complement conversion in one pass, in 0.03 ms, and agrees with the old loop on every input tested. This drops the Kerberos-specific Int32 width check the first version of this pull request added. Nothing is rejected now that was accepted before: the field is parsed, only more cheaply. The regression test counts that the conversion happens once rather than per octet, the way the BitLenField test added in secdev#5108 does. AI-Assisted: yes (GPT-5.6-Cyber)
gpotter2
pushed a commit
that referenced
this pull request
Sep 2, 2026
* kerberos: reject encryption types wider than Int32 AI-Assisted: yes (GPT-5.6-Cyber) * asn1: decode BER integers with int.from_bytes Move the fix from Kerberos into the BER codec, as reviewed. BERcodec_INTEGER.do_dec shifted a growing Python integer one octet at a time, so the cost of each octet rose with the number already accumulated and decoding was quadratic in the encoded width. A sender could multiply parsing cost by padding any INTEGER with leading sign octets, in any protocol that uses BER, not only in a Kerberos etype. Decoding 64,000 content octets took 304 ms; int.from_bytes does the same two's-complement conversion in one pass, in 0.03 ms, and agrees with the old loop on every input tested. This drops the Kerberos-specific Int32 width check the first version of this pull request added. Nothing is rejected now that was accepted before: the field is parsed, only more cheaply. The regression test counts that the conversion happens once rather than per octet, the way the BitLenField test added in #5108 does. AI-Assisted: yes (GPT-5.6-Cyber) * asn1: trim the comment on the BER integer decode AI-Assisted: yes (GPT-5.6-Cyber)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mpint.lengthcontrols the width of an SSH key-exchange integer atscapy/layers/ssh.py:102-105._BitField.getfield()unpacks the value into individual bytes and performs one growing shift foreach at
scapy/fields.py:2432-2447,so an SSH peer can make numeric parsing quadratic.
Median decoding grew to 2,403.01 ms at 256,000 bytes, with a 1.89 exponent and a 1.2% noise floor.
Patched decoding took 0.21 ms at 256,000 bytes and 0.83 ms at 1,024,000 bytes, with a 0.98 exponent
and an 8.1% noise floor.
This change retains the existing short-buffer
struct.errorand converts the complete value withint.from_bytes(). The focused regression counted eight conversions before the patch and oneafter it.