One thing that is huge in making sense of large volumes of data is sorting. Which makes having good sorting tools and methods a big deal when you are working through findings in a security assessment of pentest. Or - just as importantly - in day-to-day system administration.
I stumbled into a Twitter thread last week (as one does) about sorting by IP address, and it struck me that the lowly “sort” command has changed quite a bit since I last read the man page completely for it (back in the ‘80’s, in the Bell Labs Unix books. And yes, they were printed on paper).
No matter if you are in the red team or the blue team, you’re forever having to sort hostnames or IP addresses, sort findings / vulnerabilities by hostname or IP, or sort hostnames by vulnerabilities / findings.
So let’s look at sorting by IP. For this, we can use the “-V” (or --version-sort) argument of the sort command. This sorts things by “version” (as in decimal separated numeric strings). This option matches up very nicely to what you want if you are sorting by IPv4 address, which is also a series of point-separated numeric strings. Let’s find the IP’s in my lab that have SSH open, then reverse-sort them by IP address:
(thanks @flakpaket for this tidbit, this is an option that wasn’t in the paper Bell Labs Unix manuals back in the day!)
Or, what if you’ve got a list of files - for instance syslog files with IP addresses for filenames that you might want to sort? The option for ls to sort by version is a lower case “v”. Adding a “1” tells ls to give you the output as one line per file:
(also thanks to @flakpaket, this was also new to me!)
What if you’re on an older version of Linux – or (as I am some days), you’re on an older Windows host that has GNUtils installed instead of WSL? In that case, you can tell sort to delimit your output with a “.”, then tell it which fields to sort on (in this case, fields 1-2-3-4). This is an oldy, and the one that’s in my personal cheat-sheet from forever ago (but mentioned by @totalclaireity in this same thread)
Or, since everything is in the same /24 subnet, we can just sort by the 4th octet:
What about PowerShell? In that same thread, @mdjxkln shows us that there’s a version option for PowerShell as well:
Or, in a bit more readable format:
In another use case, let’s check all hosts in a domain (and yes, I did shorten this list), then sort them by IP:
How can you make sorting easier? Naming Conventions is the traditional answer to that. Naming conventions are like belly buttons – everyone has one, and everyone’s is different! The important thing when setting one up is to keep in mind that you'll be using tools like sort and grep (or the PowerShell / Python equivalents), find and findstr in Windows, or Excel once you start formatting your output, and work your naming convention to take advantage of that.
Have I missed any neat sort methods that you use daily? Or is there a more effiicient syntax for what I've shown in this post? Very likely – please, use our comment section to add to these methods!
Thanks of course to @flakpaket (Jon Gorenflo) who started the twitter thread:
$ man sort (of course)
And of course SANS SEC505: https://www.sans.org/cyber-security-courses/securing-windows-with-powershell/
Oct 8th 2021
|Thread locked Subscribe||
Oct 8th 2021
3 months ago