Nmap Tutorial: Mastering Network Scanning for Cybersecurity
Welcome to our comprehensive Nmap tutorial, designed to take you from a beginner to an advanced level in network scanning and vulnerability assessment. Whether you're new to Nmap or looking to enhance your skills, this video is packed with valuable information and practical demonstrations.
In this tutorial, we'll start by introducing you to Nmap, a powerful open-source network scanning tool used by professionals in the cybersecurity field. We'll explain how Nmap works, its capabilities, and the role it plays in identifying potential vulnerabilities in computer networks.
As we progress, we'll guide you through a step-by-step exploration of Nmap's functionalities. From basic scanning techniques to advanced port scans, we'll cover it all. You'll learn how to use Nmap for network discovery, port scanning, service identification, operating system detection, and more.
We'll also demonstrate how Nmap integrates with popular security tools like Kali Linux, Wireshark, and Metasploit, showcasing the synergy between these tools for comprehensive network analysis and penetration testing. You'll gain hands-on experience with real-world scenarios and understand how Nmap fits into the broader cybersecurity landscape.
Throughout this tutorial, we'll provide you with a wealth of practical tips, best practices, and command-line examples. You'll learn how to leverage Nmap's extensive range of commands to customize your scans, optimize performance, and obtain accurate results.
We'll also discuss advanced Nmap scanning techniques, allowing you to take your skills to the next level. From anonymous scans to bug bounty hunting, we'll explore various strategies and methodologies employed by seasoned professionals.
Whether you're using Nmap on Windows, Linux, or even Android, we've got you covered. Our tutorial encompasses different platforms, ensuring that you can follow along regardless of your operating system.
So, if you're ready to dive into the world of Nmap and enhance your network scanning skills, hit the play button and join us on this exciting journey. Discover the ins and outs of Nmap, understand its role in cyber security, and empower yourself with the knowledge to identify and mitigate potential vulnerabilities.
In this course you will learn about Nmap. Nmap lets you investigate a network so you know which
computers are available or online in the network or on the internet and it also lets you scan for
open ports and the ports like a connection to the to the outside world you can connect to.
So Nmap is a tool that should be in any hackers toolbox. In this course I'll use
this Linux distribution so the default Kali Linux distro. However you can install Nmap on any Linux
computer. If you're on Debian or Ubuntu you might run the command install Nmap and otherwise
whichever package manager you use. Now you can also, it's again my Linux distribution that I'm
using in this course, you can also just type Nmap and go to the website and you can then download
the security scanner on the download page. So if you're using another system there's a Windows
version of it, some more information about Linux version, a Mac version. So that's Nmap.org
if you want to manually download it and not install it using your package manager. So Nmap is the
program we'll use in this program. It's a command line program, so a terminal program. For example I
can type nmap-h and it will show you all of the things it can do. It shows you the version number,
it can discover which computers are on the network, it can do different types of scans
and by scanning it checks which services are online in the computer. That might be a web server,
it might be a mail server or different kind of services that the computer offers to the outside
world. You can specify which port it should scan, it's like the connection to the
outside world. For example the web might be going on port 80 and the file transfer might
be happening on port 21. So all of the internet traffic happens on different ports and it can scan
which ports are open. You'll see it can run scripts, we'll get more into that later. It can
detect which operating system is running and it can do some other stuff. So for example let me
give you a demo. I'll do nmap-o which scans for operating system. You'll see os-detection-o
and then type your target's computer address and it requires to run as administrator
and you'll see it will detect which computer system is running. So in this case it's running
a linux system and it tells some more information about it and also which services are online.
So this computer is opening different services to me or anyone in the network. MySQL which is
a web server and we'll get more into this in the course. But the main thing for this lecture
is to install nmap. So either using your package manager, either type whatever you use for example
apt or apt-get install nmap. If you don't use linux you can go to nmap.org and download it
from nmap.org. So if you go to the download page
you can download whichever version you need. So a windows version or a linux version or a
mac version. Alternatively you can go and download the the hacking linux distribution
from the internet. So on kelly.org you can go to downloads or installation
and download it from from this website. I'm in the documents page. You can click download when
you go to this website and install it and then nmap is available by default. But you don't
necessarily have to install it. It's just a nice little tool that hackers use because it has a lot
of hacking tools available. It's a specific linux distribution for hacking. So you can click
download on this website and nmap is included. Or you can manually download nmap from nmap.org
or alternatively just start your terminal and use your package manager to install nmap.
Your computer has a unique internet address. It can have more than one.
And if you open up a terminal and type the command ip space a you'll see that
your network devices. I have two devices eth0 which is a regular network device and a wireless
device. And by these network devices you connect through your network and the internet. And you'll
see a flag called inet which is short for internet address. So this is your local network address.
Now if you have a wired connection you'll see the same but you'll see inet somewhere here
on this line. In my case I have the wireless device connected so we have the internet
address here. And when it comes to hacking knowledge is power. So you want to know as much
information as possible about your potential target. And the target can be a single computer
or single device. But usually you want information about the entire network because you want to
exploit it. So there can be vulnerabilities in for example a printer, in a samba server or
ftp server. There can be different ways into the network. And so you want to have a complete
picture of the network. The more knowledge you have about the target system or network the more
options you have available. So how do you enumerate which computers are in the network?
Well you can do a scan with nmap. And so in order to do that you can start up a terminal.
So we have already a terminal open here. And we can type the command nmap. Now I'm using Kaili
Linux so nmap is always installed. However if it's not installed you can use your package manager to
install nmap. Then we type nmap sm. That will do a ping sweep. So what it will do is to find all
the networks or all the computers and devices on the same network. And the network we're interested
in is the same network as we're connected. So we want this network and you want to scan the entire
subnet. So 255 possible computers. And using this command you can scan which net which computers or
devices are online. So it might take a second or so to load. But it will return which devices are
online on the same computer network. And that's particularly useful because when you know the
addresses these are all possible ways to gain more access into the company. Except the last one.
So we can scan these computers or devices for ports. Ports are ways that applications
communicate with the outside world. Or you can also view it like how the computer connects with
the outside world. So it can be running a web server which would be on port 80. It can be running an
FTP server which would be running on 21. It can be running an SSH server, a printer server, all kind
of things could be running. And all these kind of all these applications they can have vulnerabilities.
So that vulnerability or weakness lets you into the system. That's why you want to scan
the devices. So we can scan for example with nmap and then scan the device. For example we can scan
this computer. And let's say you want to know if the web server port is open.
So you can type "-p 80". And you'll see the web service is closed. Now you can scan multiple ports.
So let's say you want to check if an FTP server is open. 21. You'll see FTP server is closed.
And of course you can manually define all your ports. So you can see a 403 is open which is
encrypted web server. And you can manually scan those. So this is on the one computer.
You can also do that for the other computers we scanned. So we scanned the computers in the
network using this command. And then we can check out this computer or this device what
ports are open to the outside world. So we check the five computer and see that okay on this computer
or this device the ports we specified are all closed. So that's some of the things you can do.
You can also scan a range of ports. So let's say we don't know exactly how many ports are open or
which. But you can say scan from port 1 to port 1000 and see if anything is open. So in this case
it shows you immediately everything is closed. If you have a large network the scanning can
take a while. The other computer was port 3. And you'll see it takes a little longer
to scan all the ports. But if a port is open it will show up using this command. And when the
port is open that means you can connect to it and also potentially exploit it. So you see on this
I really closed down everything in my network. But they show up here. I have another device.
Probably everything's closed as well. But the ports show up here and then you can
connect to it and potentially exploit it. So let's do a ping swipe again. We want to know
a complete picture of all the devices in the network. So we need this command nmap-sn
and then your IP address and the subnet size. If you run that it will show you the potential
targets on the same network. And then once you have that list of targets you can scan a specific
device or system which ports are open for the outside world. Now depending it can take a little
while to to show up. So you'll see I have several devices here on the network. We have a device
here. Another device. Another device. And you can scan a specific device. If you want to scan a
device for all open ports you can use dash p dash and then the IP address. So let's say we want to
scan 0.4. Then it will scan every port for that device and it will show you all of the ports
of the open ports. So you'll see it has SSH open which lets you remotely control the computer.
It has a web server open on port 80. There's a database running here and there are some other
ports and services open as well. Now this is really great because you can use these
these open ports to connect to the system and potentially get into these computers. For example
in the case of SSH they always ask for using a password to log in. So if I try to connect to it
with SSH you'll see it asks for a password. So we can try to brute force it. And that's the case
with all of these services or most of these services. Like you want a complete list and then
you can try to brute force that once you know which ports are open. Other things you can do
is running exploits. Nmap comes with a set of exploits. You can run an exploit using the script
command and exploits can have different categories. So if you go to the nmap directory which is
sorry nmap you'll see there's a directory called scripts. So the nmap directory is user share nmap
and in the scripts directory you'll find all of the exploit scripts available for nmap.
So it can abuse really a lot of weaknesses in systems and this is just a list of
weaknesses you can find. For example there can be an anonymous login allowed into the system
which is definitely not what you want. But it could be that it has that vulnerability or weakness
and lets you in without any credentials at all. It might be it's using pro FTP server which has
a backdoor. You'll see that all of these files or all of these scripts have the .nse extension
and you can open these files with any editor. So whether you use nano, vim or whatever you use to
edit files. So let's say we open one of the scripts and you'll see a lot of programming
code inside here. So this is all Lua code programming language called Lua. It doesn't
really matter but it's in this directory user share nmap scripts. You can find all of the
scripts exploit scripts and to nmap with it you use the flag
dash dash scripts. There are three basic scan types with nmap. So when you're port scanning
you can do different things. When you use a computer and network there are two main protocols
that are always used. Either the TCP protocol or the UDP protocol. Whenever you're using
data that needs to be correct you're using the TCP protocol and the UDP protocol is not
connection oriented. So with the UDP protocol the one computer is just continuously sending their
data like video and it doesn't check if the data sent and received is actually correct.
With the TCP protocol the data sent and received is checked and correct.
So you can do those types of scan. If you want to do a TCP scan you can type st and
if you want to do a UDP scan you can type nmap minus su. Now most of the times you'll be using
a TCP scan because most of the applications running on a computer or a server are always
like FTP, web services, SSH. They're all connection oriented. That's why it's using
TCP. So most of the times you'll use the st flag. Now you can also have open flags
which is like a little trick but it's the same as the TCP scan. However they are harder to detect.
Now that's because, let me remove this, with connection oriented or TCP it sends a message
to the server it sends a message back and it returns again a message to the server.
Now when you have a SYN or half open scan it sends a message to the server and the server
sends a message and then it just stops. So that's why it's harder to detect.
So the flags are st, ss and su. Those are the possible scans you can do.
Now in addition there also are some other scans you can do. You can do a TCP null scan,
TCP fin scan and a Christmas scan. They are some additional scans but they are less common.
The command nmap-st will do a TCP connection oriented scan which is the default in all the
web and most of the applications is the TCP protocol. It's a connection oriented protocol
and this protocol has a handshake, a three-way handshake. So it talks to the other computer,
sends a SYN message like hi, sends hi back and then the computer says hi, I have received it.
So it does that using SYN and SYNACK messages but that's the process.
So that's how it knows a port is opened. It sends the hi message or the SYN message,
it sends SYNACK back and then it returns ACK. Now with a closed port it also sends the SYN package
but then it sends RESET or ATTACK back. So that's how the scanner knows that the port is closed.
So that's the TCP handshake. It's always three messages sent back and forth or two if the port
is closed. TCP IP is one of the fundamentals of networks or networking besides the UDP protocol.
So the main way NMAP knows a port is closed is because it gets a RESET message instead of
SYNACK message back. So it can be that a message, a port is open but hidden behind a firewall.
In that case the NMAP cannot, doesn't know that. Now if you want you can read about the
TCP protocol but the main thing to know is that if a port is closed it sends the RESET package back.
You can also do a UDP scan. UDP is a connectionless protocol so whenever two computers
use UDP they just send message through the line and they don't check if the data is correct or
valid. If bits are lost they just send it and keep sending it all the time. That means that a handshake
like we had in TCP is not really necessary all right. It just starts sending data and it doesn't
care if the data is received or not so it doesn't do any handshake. So in order to do a UDP port scan
you type "-s uppercase U", with the IP address and maybe the port range that you want to scan.
And in order to do a UDP scan you need to have root privileges so you can do that by typing
sudo and then it will start doing the scan. So just like a TCP scan now it will check
for UDP ports that are open and you can specify certain ports if you want to. It might take a while
so if a UDP port doesn't respond it will return open filtered
and yeah basically the su flex checks for UDP ports that are opened. Now of course just like
TCP this might take a while but it's the main difference is that TCP is what you'll use most
of the time because people don't want their data to be lost. However there are for hacking it might
be that some video thing is open some video service or other kind of service which doesn't
really require the data to be valid all the time so then the UDP protocol or a UDP port could be
open as well. So that's how you would check it with the "-su flag". Now when you run nmap
you have the "-st flag for scanning over the TCP protocol". However this might be too obvious
for the firewall so there are three different types of scans you can use which is a null scan
a fin scan and a Christmas scan. All of these are TCP port scan but they're less common and stealthier
so the firewall may not detect that kind of scan which is great in case you don't want to
be detected. So to run a null scan you type "-SNN". That sends a TCP request with
no flags at all. You can run a fin scan with "-SF". This is almost identical but instead it sends an
empty packet and finally you have the Christmas scan in which you can just use another flag
"-SX", as you might have expected. So that runs the Christmas scan and it sends some malformed
TCP packets and then based on the response it knows if a port is open or closed. So all of
these flags are similar to the "-st flag". Functionally they do the same however they
are harder to detect for a firewall so if you don't want to be detected when scanning you can use
any of those flags. They might be a bit slower than just a regular scan
but the advantage is that they are harder to detect so you can evade the
firewall. Now Nmap has the scripting engine the Nmap scripting engine or NSE for short
and any script or scripts category we can run and check on a system. For example we can run
Nmap and select the category vulnerabilities so it checks the system for vulnerabilities
and then the IP address you want to scan. So let's do this IP address and run it. So now it will
run all the scripts of the category vulnerability and it does so on the target system.
Now there can be quite a lot of scripts so it can take a while.
You can also check for a specific script so let me break this and for example let's say we want
to scan for let's say we want to check this script ftp for an anonymous login so we can say
ftp add-on with the target IP address and it will check that vulnerability on that
device. You can use dash dash dash vv for verbose output if you want. So if you do that you'll see
the NSA scripting engine starts so that's dash dash dash vv that will give you verbose output
which is always a good idea it shows more information and we run a specific script
which checks if there's a vulnerability in ftp in order to log in and you should see in the logs that
if that is open or closed and let me see there there is one script running for scanning
start the script completed the script and it said connection refused
so that that's not allowed but you can use a specific script with the script flag
and you'll see there's lots of scripts here you can just select one or you can choose by category
for example the vulnerabilities category and then run it and then it will check the
system for vulnerabilities on the system so it scans the system for vulnerabilities
now you can also use the exploit flag to exploit the vulnerability or you can do a safe you can
run only the safe scripts to gain more information about the system so you can run all kind of
different scripts but the key takeaway is that you can use the dash dash scripts
parameter to run scripts with nmap whether that is an exploit or vulnerability scan or just
gathering information or brute force but that's the main idea of the script flag
so let's scan the target for vulnerabilities we type nmap dash dash script is vulnerable
so that runs all the vulnerabilities scripts that are available we type the target ip address
and we want for both outputs to show all of the possible information so this will run all of the
scripts of the category vulnerabilities and scan for vulnerabilities or scan for weaknesses on
the target system and once we know weaknesses we can of course exploit them so if we start to run
this it will automatically use the nmap scripting engine and run all of the scripts
and it will tell you if it found any weaknesses in the target system so once you know about
weaknesses you can exploit them now it might take a while because of course it has to run all of the
scripts but once it's completed and found vulnerabilities then it shows you that
those vulnerabilities in the terminal so the main thing to remember is dash dash scripts
will run a script and if you just want to run a category you can type for example vulnerabilities
for running vulnerabilities some scripts might actually need root privileges so you can run
it as root if you want and it's almost done and once it's done it will show you which or if any
vulnerabilities exist that nmap has found so that's the dash dash script flag that you need
to keep in mind and it's done and you'll see yes it did found vulnerabilities for example it found
a vulnerability in apache and then this vulnerability allows to do a denial of service attack
and it found some other vulnerabilities for example the mysql database it can do
synec attacks and also for some other protocols so there are vulnerabilities in this system that
you can exploit now nmap also comes with oh there's some more there's htp which also has
these weaknesses that you can exploit now if wordpress is found it might also be able to
exploit that depending on which wordpress version is installed so yeah that's you can
scan for vulnerabilities on a target system in that way if you do a port scan it shows you the
available ports for targeting for example we can do a port scan for let's say all of the ports
like this so all of the open tcp ports on the target computer and it can scan that
nmap is also able to brute force now ssh is a protocol you can use to connect
into a computer and it gives you full control over the computer so that's always very interesting
the ssh protocol and nmap comes with different brute force scripts so if i search for brute
force scripts like this in the directory ussr nmap scripts you'll find that for a lot of protocols
nmap can try to brute force and you'll see it can do that for ftp it can do that for htp
mysql oracle and also you'll see ssh there so nmap can also try to brute force ssh
ports and by brute force it means it tries different usernames and passwords to get access
so you can do that by typing the by running the script so and this is the script name ssh brute
so you type nmap dash dash script is ssh brute your target and you want verbose output
and if you run that command so the highlighted command it will run the script and try to
brute force the target system so if i run it you'll see it will try different username and
passwords in order to try to gain brute force access into the system and it will try with
different common usernames and passwords to try and find different ways into the system
now it might be these usernames and passwords exist but it can also be of course that usernames
and passwords don't exist so it's a bit of a lucky shot but that's the idea of brute force that
trying every username password and you can do brute forcing for different protocols as i've
showed you using the commands i've shown you before so using this command and pick any of
the brute force scripts you like for the open ports so for the ports that are shown you can
pick any like ssh or mysql and then try to run any of the brute force scripts to see if you gain
access to the system and this is the command to run a specific brute force script in this case ssh
with your target ip and you want verbose output in order to get some more data