Information Security Blog

Menu

F’Awk Yeah! Advanced sed and awk Usage (Parsing for Pentesters 3)

Intro

In a previous post,@bluscreenofjeff and I (@Sw4mp_f0x)discussed the importance of data parsing skills for penetration testers and detailed the basics of how to get started with it. We covered a few tools, but only scraped the surface of what’s possible with two very powerful tools: awk and sed. As a refresher, AWK is a programming language for pattern scanning and processing. You run AWK commands with the awk tool. sed is a stream editor tool that is primarily used to restructure input by performing pattern substitutions. In this post, we will be covering more advanced uses for awk and sed to further refine your parsing skills and efficiency.

Advanced awk Usage

In our previous post, Parsing for Pentesters: Post 1, we covered how awk can perform pattern searching and return specific fields from the output. This is only a small fraction of what awk can do.

Field Matching

Building granular and specific search patterns can become critical when dealing with complex text files. In addition to matching strings and regular expressions on entire lines, awk can limit searches to certain fields. Look at these lines from an Apache access log:

If we wanted to output a list of invalid URIs being requested from the server, matching on 200 or 404 alone will yield a number of false positives. Instead, if we tell awk to search only the 9th field for 404, we will only pull out the URIs for resources that were not found by the server. Here’s the syntax:

awk ‘$9 ~ /404/ { print }’ access.log

You can also do a negative match with the ! regular expression metacharacter placed before the tilde.

In the command above, awk will iterate through each line alternating printing True and False. It accomplishes this by defining a variable (myvar) and checking for the current value of the variable. If it’s “True,” it will print “True” and flip the value to False (and vice-versa). This processing is performed once for each line in file.txt. If you have multiple actions in either the if or else block, you must use curly braces. Otherwise, you can separate the parts of the command with a semicolon. else blocks are optional and may be omitted. awk also supports else if conditional expressions, following the same rules as else.

BEGIN/END Blocks

BEGIN and END blocks are processed before and after awk processes input records. They are typically used to change awk system variables, prepend or append records to output, or initialize variables the script will use. A key distinction between defining variables in the BEGIN block vs in the normal command is that the BEGIN block is processed before any input lines have been read by the script. Depending on what your code is doing, that may have a significant impact on the accuracy of your script.

Notice that there is an additional line break (\n) between each record, thanks to the ORS variable.

Loops

Awk supports three types of loops: for, while, and do-while. Loops in awk act similarly to loops in most other programming languages. Each loop type has its own usefulness, depending on your requirements.

For

The syntax for a for loop is as follows:

for (initialization; condition; increment) {actions}

For loops are useful when you need to perform an action (or actions) a defined number of times. Such as:

awk ‘BEGIN {for (x=0; x <= 255; x++){print “192.168.1.”x}}’

This example uses a BEGIN block (covered later in this post) to execute the awk commands without needing to act on a file.

While

While loops will run until a specified condition is no longer true. The syntax for while loops is:

while (condition) {actions}

While loops are useful when the number of times to iterate is unknown or based on a condition, rather than a count.

To rework the example from above:

awk ‘BEGIN {x = 0while (x <= 255){print “192.168.1.”xx++}}’

Do-While

The do-while loop is very similar to a while loop, except that the condition is placed at the end of the syntax:

do {actions}while (condition)

This means that if the condition is false on the first run, the actions are still executed once. In a normal while loop, the condition is checked prior to running actions, meaning if the condition was false on the first iteration, nothing would happen.

Break, Continue, Exit

Break is used to escape the innermost loop currently executing and continue executing awk. For example, if you wanted to run a certain action in a while loop and stop when a second condition (not defined in the while (condition) code) is met, you could use if to test for that second condition and run break when the it is met.

Continue is used to skip over remaining code in the current iteration. This is useful if you want to avoid certain processing only when certain conditions are met.

Exit not only breaks the current iteration or loop, but completely stops the awk execution. It should be used only when a certain condition terminates the need to continue processing, such as an error.

Arrays

Arrays in awk work similar to dictionaries in Python. You can store data in the index and value of the array. Values are referenced with the corresponding index, like so:

array[index]

To demonstrate the syntax, here’s an example that parses a simulated phishing email click log for how many times each user clicked.

As you can see, awk’s array support gives us the ability to easily build counts of unique pieces of information repeated throughout the script. For more practical examples of array usage, check out the post AWK Arrays Explained with 5 Practical Examples by Ramesh Natarajan.

System Variables

Awk has a number of built-in system variables that can be modified to change awk’s default behavior or return useful information about the files being parsed. These variables are typically configured using the BEGIN block of the command, which are described in the next section, but can be accessed or modified at any point during a script’s execution.

Here are some of the most-used variables:

FS – field separator (default: space(s) or tabs)RS – record separator (default: newline)NF – number of fields in the recordNR – number of the current recordOFS – output field separatorORS – output record separator

It’s important to note the differences between the FS/RS and OFS/ORS variables. FS/RS change how awk processes files (i.e. by default each field is space/tab-separated and each record is a newline), while OFS/ORS changes how awk outputs the results (i.e. after each field/record, insert a newline). The FS/RS variables allow you to perform multiline matching, covered in the next section, whereas OFS/ORS allow you to format the resulting output in a custom manner.

There are many more system variables. See the Predefined Variables section of the GNU Awk User’s Guide for more info.

Multiline Matching

By default, awk evaluates each input line as a separate record. By modifying the RS system variable, we can configure awk to treat multiple lines as a single record.

awk ‘BEGIN {FS=”\n”; RS=””} {print $1}’ textfile.txt

In the sample syntax above, awk will treat each line as a separate field (as opposed to the default space/tab-separated field) and will consider a blank line the record separator. The field and record separator variables can be set to any single character, including special characters like \n and \t.

To demonstrate practical multiline matching, we’ll parse a .nmap file and print out the number of open ports on each host. Given the following .nmap file:

Nmap scan report for 192.168.1.14Host is up.PORT STATE SERVICE80/tcp open http443/tcp open https8080/tcp open http-proxy8081/tcp filtered blackice-icecap8443/tcp filtered https-alt

This example sets the Record Separator to a blank line with “” and sets the Field Separator to the newline special character (\n) in the BEGIN block, which is run before awk parses the file. Next, awk will split the first field (line) of each record and add an entry for the IP (the fifth item in the split array of a) with the value of zero. Awk then iterates through the remaining fields (NF is the number of total fields) and attempts to find the string “open” in each field. If the string is present, and the port is open, it will increment the count for the IP in the host array. In the END block, awk prints each host and the number of total open ports for that host.

Script File

We’ve already seen how awk can match one search pattern, but what if we want to apply multiple rulesets to the same input file? awk can chain multiple search patterns and print commands together in script format. The file is called with the -f flag:

awk -f myscript.awk inputfile.txt

To illustrate this concept, we’ll look at parsing an Apache log. Suppose we wanted to return the URI for all successful web requests and source IPs from all invalid requests. We also want to output a unique list of entries for each request type (successes and failures), rather than outputting data as we parse the log.

Advanced sed Usage

As explained in our previous post, Parsing for Pentesters: Post 1, sed is a powerful stream editing tool by manipulating standard input and output. Typical sed usage consists of receiving stdout and performing substitution, and rightly so, but it also has some powerful pattern matching functionality similar to awk. In sed you can specify an address to execute commands at, such as substitution. Addresses can be either line numbers or patterns.

Line addressing

With line addresses, you are specifying which line numbers you want to target. Here are some examples of what this looks like:

sed ‘1,2d’

Delete lines 1 through 2.

sed -n ‘1~2p’

Print the first line and every 2nd line from that point (1, 3, 6, 9…). The ‘p’ flag tells sed to print those lines. The ‘-n’ switch needs to be used to suppress output so only the specified lines are printed.

sed ‘$d’

Delete the last line. Outside of regex, $ means the last line.

sed ‘2s/l/?/g’

Replace ‘l’ with ‘?’ on the second line. We are simply attaching a substitution command to a line number.

Pattern Addressing

Although awk is known for its pattern matching, sed has identical functionality. As I mentioned earlier, sed calls this an address, as in you are specifying an address at which you will execute a command or series of commands. Placing a pattern between forward slashes will place any line that matches into the pattern space for processing. Like awk, files and standard output are processed by sed one line at a time in a pattern space buffer. After a line is processed, the result is printed and the pattern space is replaced with the next line to be processed. There is also a second buffer called the hold space that can be used. To do a basic pattern match like awk, we print the line by appending a p flag. By default, sed will print the entire file. We will use the -n switch to suppress this behavior.

sed -n ‘/multi/p’ test.txt

Instead of printing the line, we can use pattern matching to specify lines to manipulate by appending a substitution command.

sed -n ‘/multi/s/-line/\ line/’ test.txt

Multiple expressions can be used in a single sed command by separating them with a semicolon or specifying each expression in a separate -e switch. Here we are using ampersand, which represents the pattern specified.

sed -n ‘/multi/s/-line/\ line/;s/text/&less/’ test.txt

Referencing Matched Patterns

Much like awk, sed can reference matched patterns using parenthesis. With sed, these parenthesis must be escaped.

sed ‘s/\(text\) \(file\)/\2 \1/’ test.txt

Manipulating Newlines (‘\n’)

In order to use sed to manipulate newline characters, it is important to understand how sed handles output while looking for patterns. This presents a challenge if you want to include newlines (‘\n’) in your regex pattern, because sed will never encounter them. As you can see, \n is not being processed.

In order to process newlines, you must use some more advanced sed features. When I originally encountered this constraint, I found this StackOverflow reply. In it, Zsolt Botykai demonstrates that this can be done with the following command:

sed ‘:a;N;$!ba;s/\n/, /g’ test.txt

Let’s break down what this command is doing so we understand it enough to mold it to fit our needs. This is a series of sed commands separated by semicolons, or a sed script. Each of these commands can be referenced by your favorite sed cheat sheet. This is a good one. The individual commands are:

:a

Creates a label titled a. This is used as a sort of bookmark to call back to with flow control commands.

N

Adds the next line to the pattern space, creating a multiline pattern space. In order to become a multiline pattern space, the newline character is included at the end of each line.

$!ba

This command will return flow to the a label using the branch command (b) if the current line is NOT the last line ($ meaning ‘last line’ and ! meaning ‘not’). From left to right you can read this as “if not the last line, branch to label a”. The result with this three commands is that the entire file is put into the pattern space, along with all the newline characters.

s/\n/, /g

Lastly, every newline character in the pattern space is replaced with a comma and space.

Using this one-liner, you can manipulate all of the newline characters in a document or stdout as you need.

Multiline Matching

Matching and manipulating multiple lines is fairly simple with sed. It is done by matching a line as the beginning of a multi-line section and then matching the final line. The lines that match are placed in the pattern space like an individual line normally would be. The syntax for this is:

Sed ‘/start/,/end/’ test.txt

We could use this in a situation where we have an Nmap scan that includes hosts from an out-of-scope network, 192.168.2.0/24. We want to remove them, but the hosts are scattered throughout the file because we used the –randomize-hosts switch when scanning. For this scenario we will use the .nmap file that we used in the awk Multiline Matching, but will change the middle host to another 192.168.2.193:

Nmap scan report for 192.168.1.14Host is up.PORT STATE SERVICE80/tcp open http443/tcp open https8080/tcp open http-proxy8081/tcp filtered blackice-icecap8443/tcp filtered https-alt

In order to remove these out of scope hosts from our scan file, we will use the following command:

sed ‘/192\.168\.2/,/^$/d’ scan.nmap

In this command, any line with ‘192.168.2’ in it will be the beginning of a range. The end of the range is the blank line between hosts, ‘/^$/’. We use the d flag to delete the entire range.

Reference

Substitution Flags

d – deletep – print linew [file] – output to a filen – The numbered instance of pattern match in the line to substituteg – Run substitution on all instances of pattern match. By default, only the first is substituted

Commands

$ – Last linen – Read next line into pattern spaceN – Appends next line into the pattern space, creating a multi-line pattern space.: – Create a labelb – Branch, or jump, to a label! – Add to the end of an address to invert the match (i.e, match everything except this)h – Copy pattern space to hold spaceH – Append pattern space to hold spaceg – Copy hold space to pattern spaceG – Append hold space to pattern space

Miscellaneous

Summary

In this post we covered advanced awk and sed usage. We demonstrated some functionality the tools provide that often gets overlooked, but provides powerful options to rework text and get exactly the output you want. The next time you are faced with parsing a wall of text, consider using these tools rather than grinding through it manually.