At its core, sed (Stream Editor) is a powerful text processing utility that parses and transforms text on the fly. Unlike traditional text editors that require you to open a file and manually change lines, sed takes input from a file or a network stream, applies a set of rules, and outputs the modified text. For security researchers, it is the ultimate tool for scrubbing log files, modifying exploit payloads dynamically, and restructuring massive data dumps without ever leaving the command line.
The utility was developed in 1974 by Lee E. McMahon at Bell Labs. He based it heavily on the scripting features of the ed interactive text editor. As one of the earliest tools to support regular expressions, sed became a cornerstone of Unix data processing, allowing system administrators and hackers alike to automate complex text transformations.
Essential Flags and Arguments
To master stream editing, you must understand how to construct substitution commands and control the output flow.
The substitution syntax is the heart of the utility. By defining s/old_text/new_text/g, you tell the editor to search for a specific string and replace it globally across the entire stream.
When you want to modify a file directly rather than just printing the changes to the terminal, you use the i flag for in-place editing. This permanently alters the target file, which is highly useful when dropping persistence mechanisms or altering configuration files on a compromised host.
By default, the utility prints every line it processes. If you only want to see the lines you specifically modified or requested, you use the n flag to suppress automatic printing, usually paired with the p command to print only the explicit matches.
Basic Payload Substitution
During an engagement, you often need to swap local IP addresses for external listener IPs inside your exploit scripts. You can pipe the payload directly into the stream editor to swap the values dynamically.
Command:
Result:
Extracting Specific Lines
When analyzing massive configuration files, you rarely need the whole document. You can extract a specific line number or a range of lines directly.
Command:
Result:
Conclusion
Understanding how to manipulate text streams on the fly is critical for weaponizing exploits and processing reconnaissance data. Whether you are scrubbing your tracks from a bash history file or adapting a reverse shell payload, stream editing gives you programmatic control over your text. Follow and keep in touch for more deep dives into the command line.