Assignment 1.3 - Linux Networking and Command Line Tools-2
Assignment 1.3 - Linux Networking and Command Line Tools-2
1. Overview
This laboratory exercise will expand your understanding of the Linux Terminal (sometimes called the
“shell”, command-line, or CLI [1]) and introduction to a powerful set of tools all cyber security
professionals should fully embrace. Linux and UN*X based operating systems are comprised of
thousands of “many small tools that do one thing well,” as the saying goes – a realization that only more
seasoned experts fully appreciate. This “many small tools” concept is the foundation of the UN*X
philosophy, and if fully embraced, greatly amplifies the user's ability to perform very complex
operations. It represents much of the hidden power Linux users have access to.
For the purposes of this lab, we will be focusing on learning more Linux command line tools and how
they work together to provide simple yet powerful functionality.
2. Resources required
Recommend using the latest Cyber Range: Cyber Basics environment, or in the very least, a Kali- Linux
environment with multiple machines to scan (e.g. target.example.com).
3. Initial Setup
This exercise requires a Kali Linux VM running in the Cyber Range. Login to your Kali Linux virtual
machine with (assumes you are using the latest Cyber Basics environment).
Once logged in to the Kali desktop, open a terminal window. You can launch a terminal window by
accessing it through the Applications menu at the top left and selecting Terminal Emulator.
4. Tasks
When working in network security, graphical/mouse/window or “GUI” apps can't always accomplish
everything you need, and the power of text-based command-line tools is often required, especially if
you have very specific technical needs or the graphical application you need is not, or cannot be,
installed.
For example, before you can scan your local network, you need to be able to tell what network you are
on and how large it is. You can quickly get your network card's public IP address with the hostname,
ifconfig or ip command-line tools. Getting your main public IP address is most easily done using the
hostname command:
$ hostname -I
172.31.107.239
That last command provided your machine's primary (non-localhost) IP address; however, before you
can scan your network, you still need to know how large (how many IPs) are on your local area network
(i.e. where to start and stop scanning). This information is defined by your LAN (Local Area Network)
netmask number (e.g. 255.255.255.0) or CIDR (/24) network mask size. The ifconfig or ip
commands can show you the size of your network.
Run the following commands and write down the size of your eth0 device's LAN size in netmask (255 or
VLSM) notation and in CIDR (or “slash”) notation:
EXTRA CREDIT: Calculate the number of IPs your network can handle.
EXTRA CREDIT HINT: (calculate 2(32 – your CIDR#) )
Now that you have your IP address, and your “/” CIDR network size, you can correctly use the nmap
command to ping-scan (-sP) your local network.
Run the following command where IP is the IP address of your machine and CIDR is the network size
you previously found when you executed the ip addr show eth0 command in the above
example:
On my network, my IP address is 172.31.107.239and my network size is 20, so I get this when I run
the following:
NOTE: You may see only one IP address (yourself), a couple of IPs (you and one other machine) or a
number of IPs (four in the example above). In this example, my machine is on a /20 sized network (4096
IPs max) along with three other machines, i.e. it tells me there are four (4) hosts up.
WARNING: Ping or port scanning is often used by hackers to enumerate all the machines on a network,
and as such can be seen as a prelude to an attack. So, if it's not your network, always ask the network
admin, system or network owner if it's okay before doing something as potentially proactive as a network
host scan.
QUESTION-2.1: Including yourself, how many IPs did your scan find?
If you wanted to harvest and record this information for auditing or later use, you can easily record this
info to a file called network-scan.txt by using the command line file re-direction “>” operator, like this:
LINUX TIP: If nmap or any other command-line process locks up or gets stuck, use CTRL-C to
break out of most command-line processes.
This command line file re-direction “>” operator sends the standard output (called stdout) of nmap
into the .txt file. Now you can examine this information over and over using cat (short for
concatenate), which just prints a file to the screen's standard output (stdout):
$ cat network-scan.txt
Starting Nmap 7.70 ( https://nmap.org ) at 2018-08-02 22:23 UTC
Nmap scan report for ip-172-31-102-150.ec2.internal (172.31.102.150)
Host is up (0.00077s latency).
Nmap scan report for ip-172-31-107-82.ec2.internal (172.31.107.82)
Host is up (0.00084s latency).
Nmap scan report for ip-172-31-107-239.ec2.internal (172.31.107.239)
Host is up (0.00024s latency).
Nmap scan report for ip-172-31-110-8.ec2.internal (172.31.110.8)
Host is up (0.00048s latency).
Nmap done: 4096 IP addresses (4 hosts up) scanned in 62.29 seconds
To filter this information down to something more useful, you can pipe the stdout using the pipe“|”
(see below) to direct the stdout of cat into another program (grep) to get back or filter out only what
we're interested in. The program called grep (global regular expression print) filters the output to look
for patterns and filters out everything else, as shown below:
In this example, grep filtered down only the lines that start “ ^” with "Nmap scan". When you pipe
(using “|”) one command's standard output is channeled into the next command's standard input (or
stdin), and the output of the first command serves as the data input to the second. In the above
example, the output from cat serves as the input to the grep command. The ^ is just a “regular
expression” anchor that tells grep it should only match starting from the beginning of each line.
NOTE: The grep command is a very powerful Linux command line tool that only does one thing. It
searches for strings in a stdout stream (or within a file name, if provided). Combined with other single-
purpose tools, the command line becomes very simple and elegantly powerful.
Once you have “grepped” the information you're looking for (just the lines starting with “Nmap”) for
example), you could even try piping that output (for example) through to the command line email
program called mail like this:
NOTE: This command “one liner” (with the “mail” tool) may not work in the Cyber Range, as the range
prevents you from sending data out of the range except via http/https web traffic.
This will pipe the output of your command and email it to the email address me@example.com.
grep "^..."— grep pattern “^” begins matching from the start of the line
\ — Putting \ as the last character a line allows you to hit the Enter key
to keep typing very long commands (not required; if you keep typing, the
command will wrap to the next line)
QUESTION-2.2: What do you get when you type the following command (do not type question mark):
See the man date command for more date output format options.
NOTE: You cannot actually send email from a Cyber Range VM, as you are walled off from the Internet,
except for web traffic through our web proxy. Try using the set command and piping it through grep
looking for the string PROXY to discover our proxy URL. Could you find it?
This illustrates that you can really do a lot of very powerful things from the command line and just piping
commands together. Just imagine how complicated writing a graphical tool or “App” to do a network
scan, filter down the output, and then email it to you would be. Learning and embracing this UN*X
philosophy of “many small tools that do one thing well” (and work together) puts a ton of power at your
fingertips, if you're willing to learn even just a dozen or so common command line interface (CLI)
commands.
QUESTION-2.3: Instead of using “>” to re-direct nmap's output to a text file, and then using cat to print
into | grep … | mail …, what one-liner command could have done the nmap + grep + mail
command all at once without going out to a text file first?
Extracting even more useful actionable output from tools like nmap requires a bit of automated
copying/pasting. In the command-line world there are several ways to auto copy more targeted, useful
data out of stdout data streams. One popular, easy-to-use tool is the cut command. From the nmap
man page, we find that using the “-oG -” option will give us “greppable” output to stdout like this:
This -oG - or “greppable” formatted stdout output is much more useful for shell scripting (as we'll see
in a moment), while the -oX is well suited for more formal programming in languages like python or
ruby (getting XML or XML to Json), while the -oS is great for impressing friends with your “mad hacker
skills.”
QUESTION-3.1: Using the -oG (greppable) output with nmap, but only focusing only on the grep part, if
we want to just grab the lines of output that contain IP addresses, what would we need to grep for?
HINT: If all you want are lines with usable IP addresses, then one way to get this info might be to
only return lines that begin with the string “Host:”.
Assuming you answered the previous question, your output should look something like this:
…
Host: 172.31.102.150 (ip-172-31-102-150.ec2.internal) Status: Up
Host: 172.31.107.82 (ip-172-31-107-82.ec2.internal) Status: Up
Now that we've grepped the more useful lines, the cut command allows you to treat the stdout stream
kind of like a spreadsheet, where you tell cut what the “field delimiters” (or special characters that
separate fields or cells) are.
For example, piping that last grep command's stdout into …| cut -f3 -d":", would cut out “field
3” with a “:” delimiter, resulting in the following output:
Up
Up
Up
Up
NOTE: In many raw text data files, delimiters are usually either commas, tab-characters, or spaces.
Which “field” and “delimiter” settings should we probably use to cut out only the IPs from of those
“Host:” output lines?
QUESTION-3.2: What x & y values for the -fx and -d"y" will return only the list of target IP addresses
from the nmap IP scan? Use the man cut manual if needed. What's the full nmap …| grep …|cut
… command you would use?
If you got that last question, then your output should look something like this:
172.31.102.150
172.31.107.82
172.31.107.239
172.31.110.8
The usage pattern nmap | grep | mail … or usage nmap | grep | cut … commands are
very useful, but there's even much more powerful things you can do here. Once you have a list of
machine IPs on your network, you can feed this list of IP addresses into all sorts of automation scripts to
act on that list of IPs:
NOTE: In the examples above, for these to work assumes that someone provided you the *.sh
script files, and that you have login access to and administrative permissions on those machine
IPs on your network.
While the examples above are beyond the scope of this exercise, you can see how building on the 3-4
little command line tools you just learned, the output of your commands can pass your network's
desktop and server IP addresses into automation scripts such as virus scanners, server reboot scripts, or
even network backup scripts, which is very powerful. All this power comes from just learning a handful
of command line commands.
As you can see, using standard-out and piping command's output to other small commands input, you
can do a great many and powerful things to systems all across your network, all without getting out of
your chair, or even touching a mouse.
QUESTIONS:
Extra Credit: Calculate the number of IPs your network can handle.
2.1 Including yourself, how many IPs did your scan find?
2.3 Instead of using “>” to send nmap's output to a text file, and then using cat to print it into |
grep … | mail …, what single piped commands could have done it all at once without going out to
a text file first?
3.1 Focusing only on the grep part of the one-liner, if we want to grep out just the lines of output that
contain IP addresses, what would we need to grep for?
nmap -sP -oG - … | grep _________
3.2 Building on the last nmap … | grep … command, what x & y values for the -fx and -d"y" will return
only the list of target IP addresses from the nmap IP scan? Use the man cut manual if needed. What's
the full nmap …| grep …|cut … command you would use?
5. References