-: In security, one of the most important things
is to ensure that you understand,
what openings you have created in your systems.
When it comes to computers and networks,
most of these openings are going to be created by ports.
Now, a port is simply a logical communication endpoint
that exists on your computer or your server.
For example, if you're running a web server,
you're going to have port 80 open
and listening for inbound requests
from your potential visitors.
Now, ports are classified as either
inbound or outbound ports.
An inbound port is used when your computer or server
is listening for a connection.
Just as in my earlier example,
the web server had port 80 open, that's an inbound port.
It's just waiting for somebody to come along
and connect to it.
An outbound port, on the other hand, is opened by a computer
whenever it wants to connect to a server.
If my computer is attempting to make a connection
to your web server over port 80,
well, then my computer is going to open up
a random high number port such as port 52363
and it's going to make an outbound request to that web server.
Now, what does all this look like in the real world?
Well, let's look at an example of how an inbound
and outbound port are used when my laptop
attempts to connect to a remote server over SSH.
First, we have a server at the top of the screen
and it has a public IP address assigned to it,
and it's listening on port 22,
so port 22 is the inbound port awaiting new connections.
And in this case, port 22 is open.
At the bottom of the screen,
I have my laptop that wants to make the connection.
Now, my laptop has a private IP address assigned
because my network is using NAT at the router
and that gives me some additional protections.
So, notice at this point
my laptop doesn't have any ports opened yet.
So now, my laptop wants to go
and establish the SSH connection.
It's going to open up an outbound port on itself,
which is going to be some random high number port like 51233
and it's going to send a request to the SSH server
over port 22, which is the server's inbound port
and destined for its IP address,
in this case, 46.124.63.13.
Now, once a server receives this request,
it has to respond to it.
So, it's going to send a packet of information back
to my laptop's IP in the outbound port that was open.
In this case, that's port 51233 and in reality,
it would be the public-facing IP address of my router,
but for our example, I'm going to use the private IP address
of 192.168.1.45.
Now that my laptop has made the request to the server
and the server answered that request,
we now have a session established
and both devices can communicate back and forth as needed.
Once that session is over,
the connection is going to be closed,
my laptop is going to close its outbound port
because it's no longer needed
and the server will keep that inbound port open
so they can receive requests from the next user
who wants to use it.
So, now that we showed how ports work in the real world,
let's talk a little bit more about the ports themselves.
In addition to being called inbound and outbound ports,
the ports are going to be assigned a number.
Now, the number can be anywhere between 0 and 65,535,
but this big range is actually divided into
three smaller groups.
The first group is called the Well-Known ports.
This is for any ports that are between 0 and 1023.
These are called Well-Known ports
because they are designated by IANA,
the Internet Assigned Numbers Authority,
and they are going to assign it to commonly-used
protocols and ports.
Now, for example, secure web browsing is one we all know.
It's https and it uses port 443.
Telnet is another well-known one, it's port 23.
Both of these are considered well-known ports
as well as hundreds of others.
Now, the second group we have is going to cover ports
from 1024 up to 49,151.
This range is called the registered ports
because they have to be used by vendors
for their own proprietary protocols
and each vendor is going to register them
with IANA prior to using them.
For example, Microsoft has an SQL server
and it uses port 1433 which is another registered port.
Another good example of this is remote desktop protocol
which is a proprietary Microsoft protocol
called RDP. It operates on port 3389.
The third and final group
is called the dynamic and private ports.
This uses the ports between 49,152
all the way up to 65,535.
These ports can be used by any application
at any time without having to be registered with IANA first.
Now, this range is usually used by your client
whenever it picks a random high number port
for its application.
Anytime it wants to have a temporary outbound connection,
this is the range that it's going to use.
This is also commonly used in gaming,
as well as instant message and chat.