Advanced numeric, string, set, dictionary, and list techniques, plus generators, data classes, and property decorators — applied to security tooling.
Part of the [[Readme|Python for Security Professionals]] course.
This module moves past the basics into the data-structure and language features that make security scripts fast, memory-efficient, and maintainable. Each note ties a Python concept to a concrete offensive/defensive task — XOR obfuscation, scan diffing, log streaming, and modeling findings — with runnable examples and a short lab.
- Manipulate numbers at the bit level (XOR, masks, base conversion) for encoding and simple obfuscation.
- Move fluently between
strandbytes, and shape text with format specs andtranslate. - Use set algebra to diff hosts, ports, and wordlists between scans.
- Aggregate, group, and count parsed data with advanced dictionary idioms.
- Slice, sort, and flatten lists to rank and batch results.
- Stream multi-gigabyte logs and captures with generators in constant memory.
- Model hosts, findings, and credentials with data classes and validate them with property decorators.
- [[Python-Objects-and-Data-Structure-Basics/Readme|Python Objects & Data Structure Basics]] — the types being extended here.
- [[Object-Oriented-Programming/Readme|Object-Oriented Programming]] — data classes and properties are class features.
- [[Methods-and-Functions/Readme|Methods & Functions]] — decorators and generators build on functions.
- [[Advanced-Numbers-and-Math|Advanced Numbers and Math]] —
math, bitwise ops, hex/bin, XOR for obfuscation - [[Advanced-String-Operations|Advanced String Operations]] — encode/decode,
bytes, format specs,translate - [[Advanced-Set-Operations|Advanced Set Operations]] — union/intersection/difference for scan diffing
- [[Advanced-Dictionary-Techniques|Advanced Dictionary Techniques]] — comprehensions, merging,
get/setdefault, grouping - [[Advanced-List-Manipulation|Advanced List Manipulation]] — slicing, sorting with
key, nested lists - [[Generators-and-Iterators|Generators and Iterators]] —
yield, generator expressions, memory-efficient streaming - [[Data-Classes|Data Classes]] —
@dataclass,field,frozenfor modeling findings/hosts - [[Property-Decorators|Property Decorators]] —
@property, getters/setters, input validation
- Brute-force a single-byte XOR key and recover the plaintext.
- Diff two Nmap host lists with set operations to find new and disappeared hosts.
- Stream a large
auth.logthrough a generator pipeline and tally failed logins withCounter. - Model findings as frozen data classes, dedupe them in a set, and dump to JSON.
- Modelling findings — a
@dataclassforFindingorHostgives you__init__,__repr__, and__eq__for free, andfrozen=Truemakes records hashable so duplicates collapse in a set. - Validated attributes —
@propertysetters reject an out-of-range port or a malformed IP at assignment time, so invalid objects never exist. - Streaming large captures — generators process a multi-gigabyte log or pcap with constant memory, which is the difference between a tool that runs and one that is killed by the OOM killer.
- Diffing scan runs — set difference and intersection show exactly what appeared and disappeared between two enumeration passes.
- Bitwise operations — XOR, masks, and shifts for simple obfuscation exercises, flag fields, and subnet arithmetic.
- Grouping and merging results — dictionary comprehensions and
|merging combine per-host results from several tools into one report.
- Mutable defaults in a dataclass — use
field(default_factory=list), never= []. - Consuming a generator twice — it is exhausted after the first pass; build a list if you need to reuse it.
- Forgetting
frozen=Truewhen you need a dataclass to be hashable. - Property setters that recurse — assigning to
self.xinside the setter forxinstead ofself._x. - Building a list where a generator would do, defeating the memory benefit.
- Treating XOR as encryption — it is obfuscation, trivially reversible, and must never protect real data.
- Mutating a set while iterating over it.
- Use
@dataclassfor anything that mostly holds data; addfrozen=Truefor value objects. - Use
field(default_factory=...)for every mutable default. - Prefer generators for anything larger than memory, and document that the result is single-use.
- Put validation in a property setter so it happens exactly once.
- Use set operations rather than nested loops when comparing result sets.
- Choose the container by access pattern, and say why in a comment when it is not obvious.
- What is the difference between a generator function and a generator expression?
- When should you use a
@dataclassrather than a plain class or a dictionary? - What security issue can occur if XOR is used to protect stored credentials?
- How would you troubleshoot a generator that appears to produce results only the first time it is used?
- Why does a dataclass need
field(default_factory=list)instead of= []? - Write a Python example that streams a large log file and yields only lines matching a pattern.
# Explore these concepts interactively
python3 -q
# Time and measure memory of a streaming log parser
/usr/bin/time -v python3 stream_logs.py big.log
# Generate two host lists to diff with set operations
nmap -sn 10.0.0.0/24 -oG scan_old.gnmap
nmap -sn 10.0.0.0/24 -oG scan_new.gnmap- Python docs — The Python Standard Library
- Python docs — Data structures tutorial
- Real Python — tutorials
- [[Object-Oriented-Programming/Readme|Object-Oriented Programming]] — classes underpin data classes and properties
- [[Readme|Python for Security Professionals]]
- Previous: [[Advanced-Python-Modules/Readme|11. Advanced Python Modules]]
- Home: [[Readme|Course Home]]
- Next: [[Python-Automation-for-Security/Readme|13. Python Automation for Security]]