Documentation Index
Fetch the complete documentation index at: https://mintlify.com/fortra/impacket/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Impacket’sStructure class (impacket/structure.py) provides a powerful framework for defining and manipulating binary data structures. It’s the foundation for all protocol implementations in Impacket.
The Structure Class
Basic Concept
TheStructure class allows you to define data structures declaratively using format specifiers similar to Python’s struct module, with additional features for complex protocols.
Format Specifiers
Standard struct Format
From Python’sstruct module:
Extended Impacket Specifiers
Dynamic Specifiers
Creating Structures
Simple Packet
Variable-Length Fields
Length-Prefixed Data
Arrays
Nested Structures
Parsing Packets
From Binary Data
Conditional Fields
Real-World Examples
SMB Packet Construction
NTLM Authentication Packet
Custom Protocol
Advanced Techniques
Evaluated Fields
Common Header Pattern
Alignment
Debugging
Best Practices
- Use type hints in field names:
('length', '<H')is clearer than('len', '<H') - Specify default values:
('type', 'B=1')prevents uninitialized fields - Use length prefixes:
('<H-Data')auto-calculates lengths - Document complex formats: Add comments for protocol-specific fields
- Test with real data: Parse actual packets to validate structure definitions
- Handle endianness: Explicitly specify
<(little) or>(big) endian
Common Pitfalls
- String encoding: Use
b''for byte strings, not regular strings - Array lengths: Ensure count field is set before packing arrays
- Field order: Structure fields are packed in definition order
- Alignment: Be aware of padding in binary protocols
- Nested structures: Use
:format specifier, not direct structure assignment
References
- Python
structmodule documentation - Impacket source:
impacket/structure.py - Protocol RFCs and Microsoft specifications for real-world examples