Lecture23
Lecture23
April 6, 2019
3:33pm
Goals:
• Port scanners
• The nmap port scanner
• Vulnerability scanners
• The Nessus vulnerability scanner
• Packet sniffers
• Intrusion detection
• The Metasploit Framework
• The Netcat utility
CONTENTS
• See Section 21.1 of Lecture 21 for the mapping between the ports
and many of the standard and non-standard services. As men-
tioned there, each service provided by a computer monitors a
specific port for incoming connection requests. There are 65,535
different possible ports on a machine.
• The main goal of port scanning is to find out which ports are
open, which are closed, and which are filtered.
3
Computer and Network Security by Avi Kak Lecture 23
• Port scanning may involve all of the 65,535 ports or only the ports
that are well-known to provide services vulnerable to different
security-related exploits.
4
Computer and Network Security by Avi Kak Lecture 23
#include <sys/socket.h>
5
Computer and Network Security by Avi Kak Lecture 23
6
Computer and Network Security by Avi Kak Lecture 23
8
Computer and Network Security by Avi Kak Lecture 23
9
Computer and Network Security by Avi Kak Lecture 23
-sP : This option, also known as the “ping scanning” option, is for ascertaining as
to which machines are up in a network. Under this option, nmap sends out
ICMP echo request packets to every IP address in a network. Hosts that respond are
up. But this does not always work since many sites now block echo request packets.
To get around this, nmap can also send a TCP ACK packet to (by default) port 80.
If the remote machine responds with a RST back, then that machine is up. Another
possibility is to send the remote machine a SYN packet and wait for an RST or a
SYN/ACK. For root users, nmap uses both the ICMP and ACK techniques
in parallel. For non-root users, only the TCP connect() method is used.
-sV : This is also referred to as “Version Detection”. After nmap figures out which
TCP and/or UDP ports are open, it next tries to figure out what service is actually
running at each of those ports. A file called nmap-services-probes is used to
determine the best probes for detecting various services. In addition to determine
the service protocol (http, ftp, ssh, telnet, etc.), nmap also tries to determine the
application name (such as Apache httpd, ISC bind, Solaris telnetd, etc.), version
number, etc.
10
Computer and Network Security by Avi Kak Lecture 23
-sT : The “-sT” option carries out a TCP connect() scan. See Section 23.1.1 for
port scanning with calls to connect().
-sU : This option sends a dataless UDP header to every port. As mentioned earlier
in this section, the state of the port is inferred from the ICMP response packet (if
there is such a response at all).
• To carry out a port scan of your own machine, you could try
(called as root)
nmap -sS localhost
The “-sS” option carries out a SYN scan. If you wanted to carry
out an “aggressive” SYN scan of, say, moonshine.ecn.purdue.edu, you
would call as root:
nmap -sS -A moonshine.ecn.purdue.edu
where you can think of the “-A” option as standing for either
“aggressive” or “advanced.” This option enables OS detection,
version scanning, script scanning, and more. [IMPORTANT: If the
target machine has the DenyHosts shield running to ward off the dictionary attacks
(See Lecture 24 for what that means) and you repeatedly scan that machine with
the ’-A’ option turned on, your IP address may become quarantined on the target
11
Computer and Network Security by Avi Kak Lecture 23
machine (assuming that port 22 is included in the range of the ports scanned). When
that happens, you will not be able to SSH into the target machine. The reason I
mention this is because, when first using nmap, most folks start by scanning the machines
they normally use for everyday work. Should the IP address of your machine become
inadvertently quarantined in an otherwise useful-to-you target machine, you will have
to ask the administrator of the target machine to restore your SSH privileges there.
This would normally require deleting your IP address from six different files that are
maintained by DenyHosts.]
• You can limit the range of ports to scan with the “-p” option, as
in the following call which will cause only the first 1024 ports to
be scanned:
nmap -p 1-1024 -sT moonshine.ecn.purdue.edu
port scan.pl shown in Lecture 16. But, obviously, that scanner comes nowhere close to matching the
12
Computer and Network Security by Avi Kak Lecture 23
The ’-P0’ option (the second letter is ’zero’) tells nmap to not
use ping in order to decide whether a machine is up.
the values in the various header fields. These may include the
sequence number field, the initial window size field, etc. Based
on these values, nmap then constructs an OS “signature” of the
target machine and sends it to a database of such signatures to
make a guess about the OS running on the target machine.
14
Computer and Network Security by Avi Kak Lecture 23
15
Computer and Network Security by Avi Kak Lecture 23
• According to the very useful web site “Top 125 Network Secu-
rity Tools” (http://sectools.org), the source code for Nes-
sus, which started out as an open-source project, was closed in
2005. Now for commercial applications you have to maintain a
paid subscription to the company Tenable Computer Networks
for the latest vulnerability signatures. However, it is still free for
personal and non-commercial use. [The http://sectools.org website is a very
useful place to visit to get an overview of the most commonly used computer security tools today. This website
is maintained by the same folks who bring you the nmap scanner. ]
• The security tests for the Nessus system are written in a special
scripting language called Network Attack Scripting Language
17
Computer and Network Security by Avi Kak Lecture 23
• Nessus can detect services even when they are running on ports
other than the standard ports. That is, if the HTTP service is
running at a port other than 80 or TELNET is running on a port
other than port 23, Nessus can detect that fact and apply the
applicable tests at those ports.
18
Computer and Network Security by Avi Kak Lecture 23
/etc/init.d/nessusd start
You can see that the Nessus server is up and running by doing
any of the following:
netstat -n | grep tcp
netstat -tap | grep LISTEN
netstat -pltn | grep 8834
Any of these commands will show you that the Nessus server
is running and monitoring port 8834 for scan requests from
Nessus clients.
– Now, in accordance with the message you saw when you in-
stalled the debian package, point your web browser to https://pixie:8834/
(with “pixie” replaced by the name you have given to your ma-
chine) to start up the web based wizard for installing the rest
of the server software (mainly the plugins you need for the
scans) through a feed from http://support.tenable.com . The web-
based wizard will take you directly to this URL after you have
indicated whether you want a home feed or a professional feed.
Go for home feed for now — it’s free. I believe the professional
feed could set you back by around $1500 a year. When you
register your server at the URL, you will receive a feed key
that you must enter in the wizard for the installation to con-
tinue. If you are running a spam filter, make sure that it can
accept email from nessus.org.
– After you have entered the feed key in the install wizard in
your web browser, you will be asked for a username and a pass-
20
Computer and Network Security by Avi Kak Lecture 23
– After you you have entered the above info, the Nessus server
will download all the plugins. I think there are over 40,000
of these plugins for all sorts of vulnerability scans. Each
plugin is based on a unique vulnerability signature.
Eventually, you will see a screen with the heading ”Nessus
Vulnerability Scanner”. Under the header, you will see a bar
that has ”Listing Scans” on the left and a button for ”New
Scan” on the right. Click on the ”New Scan” button to create
a test scan to play with.
• By the way, you can update your plugins by executing the com-
mand ‘sudo ./nessus-update-plugins’ in the /opt/nessus/sbin/
21
Computer and Network Security by Avi Kak Lecture 23
• You will find all the plugins in the following directory machine
where the server is installed:
/opt/nessus/lib/nessus/plugins/
After you have updated the plugins, you can do ‘ls -last | more’
in the above directory to see what sort of plugins were installed
in the latest update.
attacker to send specially crafted heartbeat packets to a server. That triggers a buffer over-read through which
an attacker can download 64 kilobytes of process memory with each exchange of the heartbeat message. (See
Section 20.4.4 of Lecture 20 for what I mean by heartbeat messages). In general, this memory will contain the
private keys, the passwords, etc., that have been cached by the server for its interaction with the clients. CVE-
2014-0160 is the official reference to this bug. CVE (Common Vulnerabilities and Exposures) is the Standard
22
Computer and Network Security by Avi Kak Lecture 23
23
Computer and Network Security by Avi Kak Lecture 23
• The name network analyzer is justified by the fact that you can
use a packet sniffer to localize a problem in a network. As an
example, suppose that a packet sniffer says that the packets are
indeed being put on the wire by the different hosts. If the network
interface on a particular host is not seeing the packets, you can
be a bit more certain that the problem may be with the network
interface in question.
24
Computer and Network Security by Avi Kak Lecture 23
that are plugged into the same router will see all the
packets. On wireless LANs, all the interfaces on the
same channel see all the packets meant for all of the
hosts that have signed up for that channel.
25
Computer and Network Security by Avi Kak Lecture 23
• In the OSI model of the TCP/IP protocol stack [see Section 16.2 of
Lecture 16 for the OSI model], it is the Data Link Layer’s job to map the
ever, at a local level a machine cannot communicate with another machine or a router or a
switch unless it has the MAC address for the destination interface. Coming up with a scalable
and dynamic solution to the problem of how to obtain the MAC address that goes a with a
given IP address that your machine wants to send a packet to was perhaps one of the
greatest engineering accomplishments that ultimately resulted
in the worldwide internet as we know it today. You could ask why
not use the IP addresses directly as MAC addresses for communications in
a local network. That would not be practical since we must allow a host to possess multiple
communication interfaces. If you did not allow for that, how would you get a router to work?
With the clean separation between IP addresses and MAC addresses, a single host with a unique
IP address is allowed to have an arbitrary number of interfaces, each with its own MAC address.
With this separation between the addressing schemes, and with IP addresses representing the
main identity of a host, we are faced with the problem of discovering the MAC address asso-
ciated with an interface for a host with a given IP address. (Obviously, when a host possesses
multiple interfaces, only one can participate in a single LAN.) That’s where the ARP protocol
27
Computer and Network Security by Avi Kak Lecture 23
comes in. The next bullet explains briefly what this protocol does. ]
• The Data Link Layer uses a protocol called the Address Resolu-
tion Protocol (ARP) to figure out the destination MAC address
corresponding to the destination IP address. [In Section 9.8.1 of Lecture 9
I showed how ARP packets can be used to crack the encryption key in a locked WiFi.] As a first
step in this protocol, the system looks into the locally available
ARP cache. If no MAC entry is found in this cache, the system
broadcasts an ARP request for the needed MAC address. As this
request propagates outbound toward the destination machine, ei-
ther en-route gateway machine supplies the answer from its own
ARP cache, or, eventually, the destination machine supplies the
answer. The answer received is cached for a maximum of 2 min-
utes. [If you want to see the contents of the ARP cache at any given moment, simply execute the
command “arp -n” or “arp -a” from the command line. The latter version with the “-a” option
also tries to associate symbolic hostnames with the IP addresses. Either version of the command will
show you the IP addresses and the associated MAC addresses currently in the cache. You don’t have
to be root to execute this command. Do man arp on your Ubuntu machine to find out more about the
arp command. By the way, you can yourself flush the ARP cache by executing the command “sudo
ip -s -s neigh flush all”, where the first “-s” is for the verbosity level and the second “-s” for
setting the option “neigh” for neighborhood, and delete a specific entry in the cache with a command
like “sudo arp -d 10.0.0.10” if you wanted to delete the entry for the IP address 10.0.0.10. After
flushing the entire cache or deleting a specific entry, the cache will be refilled with fresh entries as ARP
broadcasts are made in the LAN. People sometimes play with these commands when they
are unable to get one host to communicate directly with another host in the same LAN.
(Let’s say you are working at home and, using the rsync command, you want to transfer
a directory directly from one machine to another.) In most such cases, the problems are
28
Computer and Network Security by Avi Kak Lecture 23
caused more by something having gone awry in the operation of the router than in the
promisc’ where ethX stands for the name of the interface (it would be something like eth0, eth1,
wlan0, etc.). ]
29
Computer and Network Security by Avi Kak Lecture 23
urlsnarf : From the sniffed packets, this utility extracts the URL’s of all
the web sites that the network users are visiting.
mailsnarf: This utility can track all the emails that the network users
are receiving.
webspy : This utility can track a designated user’s web surfing pattern
in real-time.
30
Computer and Network Security by Avi Kak Lecture 23
• tcpdump uses the pcap API (in the form of the libpcap library)
for packet capturing. (The Windows equivalent of libpcap is
WinCap.)
31
Computer and Network Security by Avi Kak Lecture 23
– First create a file for dumping all of the information that will
be produced by tcpdump:
touch tcpdumpfile
chmod 600 tcpdumpfile
This will print out all the strings, meaning sequences of charac-
ters delimited by nonprintable characters, in the tcpdumpfile.
The function strings is in the binutils package.
32
Computer and Network Security by Avi Kak Lecture 23
• As you play with Wireshark, you will soon realize the importance
of a GUI based interface for understanding the packets and ana-
lyzing their content in your network. To cite just one example of
the ease made possible by the GUI frontend, suppose you have
located a suspicious packet and now you want to look at the rest
of the packets in just that TCP stream. With Wireshark, all you
have to do is to click on that packet and turn on “follow TCP
stream feature”. Subsequently, you will only see the packets in
that stream. The packets you will see will include resend packets
and ICMP error message packets relevant to that stream.
options. [If you are overwhelmed by the number of packets you see in
the main window, enter something like http in the “Filter” text window just
below the top-level icons. Subsequently, you will only see the http packets.
By filtering out the packets you do not wish to see, it is easier to make sense
of what is going on.]
34
Computer and Network Security by Avi Kak Lecture 23
– The lowest part of the GUI shows the hexdump for the packet.
• Note that wireshark will set the local Ethernet interface to promis-
cuous mode so that it can see all the Ethernet frames.
35
Computer and Network Security by Avi Kak Lecture 23
36
Computer and Network Security by Avi Kak Lecture 23
rules for, here are the names of the rule files in /etc/snort/rules di-
rectory on my Ubuntu machine:
backdoor.rules community-web-iis.rules pop2.rules
bad-traffic.rules community-web-misc.rules pop3.rules
chat.rules community-web-php.rules porn.rules
community-bot.rules ddos.rules rpc.rules
community-deleted.rules deleted.rules rservices.rules
community-dos.rules dns.rules scan.rules
community-exploit.rules dos.rules shellcode.rules
community-ftp.rules experimental.rules smtp.rules
community-game.rules exploit.rules snmp.rules
community-icmp.rules finger.rules sql.rules
community-imap.rules ftp.rules telnet.rules
community-inappropriate.rules icmp-info.rules tftp.rules
community-mail-client.rules icmp.rules virus.rules
community-misc.rules imap.rules web-attacks.rules
community-nntp.rules info.rules web-cgi.rules
community-oracle.rules local.rules web-client.rules
community-policy.rules misc.rules web-coldfusion.rules
community-sip.rules multimedia.rules web-frontpage.rules
community-smtp.rules mysql.rules web-iis.rules
community-sql-injection.rules netbios.rules web-misc.rules
community-virus.rules nntp.rules web-php.rules
community-web-attacks.rules oracle.rules x11.rules
community-web-cgi.rules other-ids.rules
community-web-client.rules p2p.rules
where the keyword alert is the action part of the rule, the
keyword tcp the protocol part, the string any any the source
address and the source port, the string -> the direction operator,
and the string 192.168.1.0/24 80 the destination address and
port . These five parts constitute the rule header. What comes
after that inside ’()’ is the rule body.
37
Computer and Network Security by Avi Kak Lecture 23
• It is the body of a rule that takes some time getting used to.
Remember, the body is whatever is between the parentheses ‘(’
and ‘)’.
• Some of the more commonly used keywords for the payload de-
tection option are: content that looks for a string of bytes in
the packet payload, nocase that makes payload detection case
38
Computer and Network Security by Avi Kak Lecture 23
• Some of the more commonly used keywords are for the metadata
option are: msg, reference, classtype, priority, sid, rev,
etc.
• In the rule example shown at the bottom of page 36, the body
contains two options: the payload detection option content and
the metadata option msg. Therefore, that rule will be triggered
by any TCP packet whose payload contains the byte sequence
A1 CC 35 87. When you are listing the bytes in hex, you are
supposed to place them between ‘|’ and ‘|’.
• You can also cause one rule to create conditions for triggering
another rule later on. This is done with the flowbits option.
An option declaration inside the rule body that looks like
39
Computer and Network Security by Avi Kak Lecture 23
flowbits:set, community_is_proto_irc;
means that you have set a tag named community is proto irc.
Now if there is another rule that contains the following option
declaration inside its body:
flowbits:isset, community_is_proto_irc;
this would then become a condition for the second rule to fire.
• With that very brief introduction to the rule syntax, let’s now
peek into some of the rule files that are used for intrusion detec-
tion.
quently called bot herders — who may use the botnet to spew out malware such as spam, spyware,
etc. It makes it more difficult to track down malware if it seems to emanate randomly from a large
]
A bot herder typically sets up an IRC (Internet
network of zombies.
and Java examples and Chapter 15 of my SwO book for Perl and Python examples), what makes IRC
different is that one IRC server can connect with other IRC servers to expand the IRC network. Ideally,
when inter-server hookups are allowed, the servers operate in a tree topology in which the messages
are routed only through the branches that are necessary to serve all the clients but with every server
40
Computer and Network Security by Avi Kak Lecture 23
aware of the state of the network. IRC also allows for private client-to-client messaging and for private
individual-to-group link-ups. That should explain why bot herders like IRC. Joining an IRC
chat does not require a log-in, but it does require a nickname (frequently abbreviated as just nick in
# The following rule merely looks for IRC traffic on any TCP port (by detecting NICK change
# events, which occur at the beginning of the session) and sets the is_proto_irc flowbit.
# It does not actually generate any alerts itself:
alert tcp any any -> any any (msg:"COMMUNITY BOT IRC Traffic Detected By Nick Change"; \
flow: to_server,established; content:"NICK "; nocase; offset: 0; depth: 5; flowbits:set,\
community_is_proto_irc; flowbits: noalert; classtype:misc-activity; sid:100000240; rev:3;)
# Using the aforementioned is_proto_irc flowbits, do some IRC checks. This one looks for
# IRC servers running on the $HOME_NET
alert tcp $EXTERNAL_NET any -> $HOME_NET any (msg:"COMMUNITY BOT Internal IRC server detected"; \
flow: to_server,established; flowbits:isset,community_is_proto_irc; classtype: policy-violation; \
sid:100000241; rev:2;)
alert tcp $HOME_NET any -> $EXTERNAL_NET any (msg:"COMMUNITY BOT Agobot/PhatBot bot.die command";
flow: established; flowbits:isset,community_is_proto_irc; content:"bot.die"; classtype:
trojan-activity; sid:100000243; rev:2;)
....
....
....
alert tcp $EXTERNAL_NET any -> $HOME_NET 5554 (msg:"COMMUNITY VIRUS Dabber PORT overflow \
attempt port 5554"; flow:to_server,established,no_stream; content:"PORT"; nocase; isdataat:100,\
relative; pcre:"/^PORT\s[^\n]{100}/smi"; reference:MCAFEE,125300; classtype:attempted-admin; \
sid:100000110; rev:1;)
41
Computer and Network Security by Avi Kak Lecture 23
alert tcp $EXTERNAL_NET any -> $HOME_NET 1023 (msg:"COMMUNITY VIRUS Dabber PORT overflow \
attempt port 1023"; flow:to_server,established,no_stream; content:"PORT"; nocase; isdataat:100,\
relative; pcre:"/^PORT\s[^\n]{100}/smi"; reference:MCAFEE,125300; classtype:attempted-admin; \
sid:100000111; rev:1;)
alert tcp $HOME_NET any -> 207.172.16.155 80 (msg:"COMMUNITY VIRUS Possible BlackWorm or \
Nymex infected host"; flow:to_server,established; uricontent:"/cgi-bin/Count.cgi?df=765247"; referen
Win32%2fMywife.E%40mm; reference:url,cme.mitre.org/data/list.html#24; reference:url,isc.\
sans.org/blackworm; classtype:trojan-activity; sid:100000226; rev:2;)
....
....
to see what the instructions are. As you will find out from these
README files, a full installation of snort requires that you also
install a database server like MySQL or PostgreSQL. But if you
want to just have fun with snort as you are becoming
familiar with the tool, it is not necessary to do so. You
42
Computer and Network Security by Avi Kak Lecture 23
just need to make sure that you delete the zero-content file named
db-pending-config from the /etc/snort/ directory.
• Once you get snort going, try the following command lines:
sudo snort -v -i wlan0 // will see the headers of ALL TCP
// packets visible to the wlan0
// wireless interface
43
Computer and Network Security by Avi Kak Lecture 23
• You can customize how snort works for each separate interface
by writing a config file specific to that interface. The naming con-
vention for such files is /etc/snort/snort.$INTERFACE.conf
44
Computer and Network Security by Avi Kak Lecture 23
45
Computer and Network Security by Avi Kak Lecture 23
• Let’s say you want to create a worm for the iPhone platform
but you don’t know how to program in Objective C, the primary
language for iPhone applications. Not to worry. With the Metas-
ploit Framework, all you have to do is to execute the command
msfpayload and give it the options that apply to the iPhone
platform, and, voila, you’ll have the executable of a worm for the
iPhone. Obviously you would still be faced with the problem of
delivering the worm you just created to its intended target. For
that you could try mounting a social engineering attack of the
type discussed in Lecture 30.
46
Computer and Network Security by Avi Kak Lecture 23
• To give the reader a sense of the syntax used for creating the
payloads, the command
msfpayload windows/shell_bind_tcp X > temp.exe
48
Computer and Network Security by Avi Kak Lecture 23
49
Computer and Network Security by Avi Kak Lecture 23
• Netcat has got to be one of the most versatile tools ever created
for troubleshooting networks. It is frequently referred to as the
Swiss Army knife for network diagnostics.
• I suppose the coolest thing about netcat is that you can create
TCP/UDP servers and clients without knowing a thing about
how to program up such things in any language.
50
Computer and Network Security by Avi Kak Lecture 23
• What I have said so far in this section is the good news. The
bad news is that you are likely to find two versions of netcat in
your Ubuntu install: nc.openbsd and nc.traditional. The
command nc is aliased to nc.openbsd. There are certain things
you can do with nc.traditional that you are not allowed to
with nc. Perhaps the most significant difference between nc and
nc.traditional is with regard to the ‘-e’ option. It is supported
in nc.traditional but not in nc. The ‘-e’ option can be used
to create shells and remote shells for the execution of commands.
You have a shell if the machine with the listener socket (the server socket) executes a shell command
like /bin/sh on Unix/Linux machines or like cmd.exe on Windows machines. Subsequently, a client
can send commands to the server, where they will be interpreted and executed by the shell. You have a
reverse shell if the client side creates a client socket and then executes a shell command locally (such as
by executing /bin/sh or cmd.exe) for the interpretation and execution of the commands received from
vulnerability.
• Let’s now look at some of the many modes in which you can use
netcat. I’ll assume that you have available to you two machines
that both support netcat. [If one of these machines is behind a wireless access point
at home and the other is out there somewhere in the internet, you’d need to ask your wireless router
to open the server-side port you will be using for the experiments I describe below — regardless of
which of the two machines you use for the server side. If you don’t know how to open specific ports
on your home router, for a typical home setting, you’ll need to point your browser at home to a URL
like http://192.168.1.1 and, for the case of LinkSys routers at least, go to a page like “Applications
and Gaming” to enter the port number and the local IP address of the machine for which you want
the router to do what’s known as port forwarding. When “playing” with netcat, most folks use port
51
Computer and Network Security by Avi Kak Lecture 23
1234 for the server side. So just allow port forwarding on port 1234. ]
nc -l 1234
The command-line option ‘-l’ (that is the letter ‘el’ and not the
number ‘one’) in the first command above creates a listening
socket on port 1234 at the moonshine end. The laptop end
creates a client socket that wants to connect to the service at
port 1234 of moonshine.ecn. purdue.edu. This establishes a
two-way TCP link between the two machines for the exchange
of one-line-at-a-time text. So anything you type at one end of
this link will appear at the other end. [This is obviously an example of a
rudimentary chat link.] You can obviously reverse the roles of the two
machines (provided, if you are at home behind a router, you have
enabled port-forwarding in the manner I described earlier).
52
Computer and Network Security by Avi Kak Lecture 23
53
Computer and Network Security by Avi Kak Lecture 23
Note the use of the ‘-e’ option, which is only available with
nc.traditional on Ubuntu machines. [If you are running the above
command on a Windows machine, replace /bin/sh by cmd.exe. Also, on Windows, you would call
nc and not nc.traditional. Running ‘-e’ option on Widows works only if you installed the version
of netcat that has ‘-e’ enabled. Note that an installation of the ‘-e’ enabled version of netcat on
Windows may set of anti-virus alarms. ] Subsequently, I will run on the laptop
the command
nc moonshine.ecn.purdue.edu 1234
the other end. And entering Ctrl-C on the server side means that you are sending the SIGINT signal
Now, on the laptop side, I’ll enter the following command line:
nc.traditional moonshine.ecn.purdue.edu 1234 -e /bin/sh
• You can also use netcat to carry out a rudimentary port scan
with a command like
nc -v -z -w 2 shay.ecn.purdue.edu 20-30
where the last argument, 20-30, means that we want the ports
20 to 30, both ends inclusive, to be scanned. The ‘-w 2’ sets the
timeout to 2 seconds for the response from each port. The option
‘-v’ is for the verbose mode. When used for port scanning, you
may not see any output if you make the call without the verbose
option. The option ‘-z’ ensures that no data will be sent the
55
Computer and Network Security by Avi Kak Lecture 23
• For the next example, I’ll show how you can use netcat to redi-
rect a port. [This is something that you can also do easily with iptables by inserting a
REDIRECT rule in the PREROUTING chain of the nat table of the firewall. See Chapter 18.] To
explain the idea with a simple example, as you know, the SSH
service is normally made available on port 22. Let’s say, just for
sake of making an example of port redirection, that you cannot
reach that port directly. Instead you are allowed to reach, say,
the port 2020. With netcat, you can relay your SSH connection
through the port 2020. To bring that about, you execute the fol-
lowing two commands in some directory (which could be ‘/tmp’
that all processes are allowed to write to)
mkfifo reverse
nc -l 2020 < reverse | nc localhost 22 > reverse
As to the reason for the first command above, note that a pipe
is a unidirectional connection. So if we use a pipe to route the
incoming traffic at the server on the listening port 2020 to another
instance of netcat acting as a client vis-a-vis the SSHD server
on port 22 of the same host, we also need to figure out how to
route the information returned by the SSHD server. That is,
when the SSHD server sends the TCP packets back to whosoever
made a connection request, those packets need to travel back on
the same relay path. This we do by first creating a standalone
pipe with a designated name with the mkfifo command. We
call this pipe reverse for obvious reasons. [In order to understand why nc
56
Computer and Network Security by Avi Kak Lecture 23
localhost 22 > reverse captures the return TCP packets emanating the SSHD server, go back to
the command ‘nc -l 2020’ write to the standard output get fed
into the standard input to ‘nc localhost 22’. Subsequently,
at the client site, you enter a command line like the following to
make an SSH connection with the remote host:
ssh [email protected] -p 2020
57
Computer and Network Security by Avi Kak Lecture 23
1. Nowadays even the hoi polloi talk about the ports on their home
computers being open or closed. But what exactly is meant by an
open port? And by a closed port? Say I buy a brand new laptop
with only the most basic software (word processor, browser, etc.)
installed on it. Should I assume that all the ports on the laptop
are open?
2. Let’s say your home router has a firewall in it that you can con-
figure with a web-based tool running on a computer behind the
router. Is the meaning of a port being open in the router firewall
the same as the meaning of a port being open in your laptop?
3. What are all the different things you can accomplish with the
nmap port scanner? Say that my laptop is only hosting the sshd
and httpd server daemons. Assuming a standard install for these
servers, which ports will be found to be open on my laptop by
the nmap port scanner?
4. Let’s say you have port scanned my laptop and found no ports to
be open. Should I leap to the conclusion that all the ports on my
58
Computer and Network Security by Avi Kak Lecture 23
5. What are the main differences between a port scanner like nmap
and a vulnerability scanner like nessus?
]
See Lecture 16 for the difference between routers, switches, hubs.
59
Computer and Network Security by Avi Kak Lecture 23
11. How does the Data Link Layer in the TCP/IP stack of a router
map the destination IP address in a packet received from the
internet to the MAC address of the destination machine in the
LAN controlled by the router?
60