🎉 Pythoholic is Creating visual content for all tech and programming on Youtube! You can support by buying a coffee ☕️ here — https://ko-fi.com/pythoholic
🍀 If you wish to support me please choose the links below: INSTAMOJO: https://instamojo.com/@pythoholic PAYPAL: paypal.me/pythoholic GADGETS I USE: www.amazon.in/shop/pythoholic
Disclaimer: The content provided in the channel is not affiliated in any way to the organization. We provide information here on the channel based on the knowledge we have on the topic. We advise our viewers to please do their own research and read more about them from the source provided by the organization to get a better outlook on the topic that has been covered. These videos are just to provide you a platform to learn, and there can be mistakes and we are always trying to improve based on your feedback. we recommend viewers have an open mind. Please support the channel to get more content like these in the future.
But before moving forward to NACLs or Network Access Control Lists,
let's understand the problem statement. I know this cannot be termed as a problem, but
it's more of an enhanced security measure. When it comes to security groups, let's check the case
one, where we have no access restrictions in the security group. For the inbound traffic,
we have port 80 and 22, and it's allowed for all IPv4 addresses. And the same goes for outbound
traffic, where it has all traffic allowed as well. So the instances in our subnet with the
security group as my security group can be accessed, and it's a free flow of request and response.
Now let's take the case two for security groups, and let us remove the access for all IPs from the
outgoing traffic and the outbound rules. The general logic here dictates that it should prevent
the connectivity to the instances with this type of security group configurations, but that's not
the case, isn't it? Because the security groups are stateful, and because of the property of
connection tracking, if the inbound rule allows all traffics to access the network or the instance,
by default, the outbound rule holds no value. And that is the reason why if we block the outbound
rules, it still works. Now let's bring in our special task force and let's place it in front
of our VPC subnet, just like a fireball, and configure it to block all traffics from the CIDR
192.168.0.1 slash 28. And let's see what happens. And yes, it doesn't even allow the traffic to
enter or reach the security group itself, even if the inbound and outbound rules for security groups
allow this IP set. That is the enhanced level of security we needed, isn't it? Let's suppose we
have a target and we want to restrict it from a list of six subnets, and that could be more than
25 security groups in that. So how will you restrict all that in one shot? Yes, by using a
network access control list. So let's understand more about that. So what is NACL or NACL or
network access control list? If I say NACL or NACL, please don't get confused. I'll be using
these terms as and when it comes to my mouth. So please forgive me for that. So NACL or network
access control list. So it's an optional layer of security for your VPC that acts as a firewall
for controlling traffic in and out of one or more subnets. So we need to understand clearly that
NACL or NACL is the optional layer which works for controlling the traffic with the subnet.
And security group works at the instance level and not the subnet level. So with this analogy,
tell me which takes higher precedence? Yes, you're right. It's NACL. And there are a few
rules and basic concepts that we need to understand before we can use NACLs properly.
So the first point is the default VPC automatically comes with a modifiable
default network ACL. And by default, it allows all inbound and outbound IPv4 traffic.
And the next point is you can create a custom network ACL and associate it with a subnet.
With the default one that you have, if you don't want to use it, or if you want to use a specific
target measure, then you can create your own NACLs as well and attach that to your subnets. So that's
a very good thing. And each subnet in your VPC must be associated with a network ACL, even if
it allows all traffic. And yes, for the fourth point, it's yes that you can associate a network
ACL with multiple subnets, but a subnet can be associated with only one network ACL at a time.
So you can associate a network ACL with multiple subnets, but a subnet can be associated with
only one network ACL at a time. Just as security groups have inbound rules and outbound rules,
and they work on the principle of ciders or IP address and the port we can route,
here the network ACLs has its numbered list of rules. And these rules are evaluated in order
of the number of the rule. And the highest number that you can use for a rule is 32766.
And what AWS recommends is like you can create like 100, 150, 200, 250, or you can have it like
100, 110, 120, 130 in that particular order. And it's ordered with the lowest number first.
And network ACL has separate inbound and outbound rules, and each rule can either
allow or deny traffic. And this is one of the most important differences as well with security
groups. The network ACLs has separate inbound and outbound rules, and each rule can either allow
or deny traffic. And network ACLs are stateless. And I know you might want to understand what that
means. We will understand that, don't worry. And you might ask me like how many network ACLs we can
have? What is the quota? So per VPC, you can have up to 200 NACLs, and each network ACL can have up
to a maximum of 20 rules. And this quota can be increased up to a maximum of 40 rules. And this
is the one way quota for a single network ACL. So per VPC, you can have a maximum of 200 network
ACLs. So a network ACL rule contains a pool of resources that we have to add to create a network
ACL. So let's understand that. The first one is rule number. As you can see the table here,
all the terms that we have in the columns will be discussed here. So don't worry about that.
So starting off with the rule number, the rule are evaluated starting with the lowest number rule,
which means if there is a rule, let's suppose 150 and it denies 443, but you have a rule with
the lesser number, for example, 100, which allows it, then the network ACL would allow it considering
the lowest number rule allows the request made for the 443 port. And the next is type. So this is
the type of traffic we expect like SSH or HTTPS or HTTP or ICMP. And you can also specify all
traffic or a custom range. So next is protocol. This is quite simple. We can specify the protocol
type such as TCP. And the port range, we can specify the listening port or port range like
443, 2280 or HTTPS, SSH and HTTP respectively. And source, which is for inbound rules only,
the source of the incoming traffic, you can specify the IP or the CIDR range.
The same goes for the destination, which is for the outbound rules only, the destination for the
outgoing traffic. So that can be a CIDR block as well. And allow and deny. If you
wish to allow a rule, you can specify allow, else you can deny that. Now that we know what are the
parts of the rules, let's check how does a default rule look like. So this is a default
network ACL that you get when you create a new network ACL. Just like the security groups,
we have inbound and outbound rules for network ACLs. If we reiterate this once again, network
ACL is like a firewall or security enhancement for your subnets. And security groups are meant for
your instances. So when you create a new network ACL, the default network ACL is configured to
allow all traffic to flow in and out of the subnet. So now let's understand how we can read
the network ACL rule set. So here, for both inbound and outbound, check the entries for the
type of the request. Here, rule number 100 that you see allows all type of traffic in and out of
the subnet for all protocols and port ranges. If the source is 0.0.0.0 slash 0, which covers all
the IPv4 addresses, remember to check both entries of inbound and outbound. But this rule allows all
the IPs. But in case you have IP ranges that are specified, then there might be a situation that a
set of IP may not match any rule set that is defined. For the same reason, the asterisk or
the star that you see here ensures that if a packet does not match any of the ordered numbers
or the other numbered rules, it actually gets denied. And unlike security groups, if you have
a rule set that allows inbound traffic and denies outbound traffic, then it's not going to work
because network ACLs are stateless. And that is the most important thing that we wanted to
understand, isn't it? So unlike security groups, if you have a rule set that allows inbound traffic
and denies the outbound traffic, then it is not going to work. It should be allowed both the ways,
in and out, then only the network ACLs work. Otherwise, they don't work. So that is why they
are termed as stateless. And along with the default NACL or network ACLs, you can also
customize the main network ACL as well, or we can create our own network ACL for our subnets.
And we have to understand this point very clearly that the rule numbers in the custom network ACLs
are really important and we have to understand them thoroughly. So when you see the custom
network ACL inbound rule, you see the column of rule numbers, isn't it? Where you see a list of
numbers like 100, 110, 120, 130, 140. The only rule when adding rule numbers is that the highest
number that you can use for a rule is 32766. And if you start from 100, AWS tells that it's
advisable to increment it by 10 and add your rules. And the rules will be evaluated from the lowest
order. So what does it mean? So we start from the first number, then we make our way to the bottom
to evaluate if the request is allowed or denied starting from the lowest number. So it's a bit
tricky, isn't it? But we have to understand this. So let's take an example here. So we have a user
here that you can see who wants to access our instances over HTTPS with 443. So let's see what
are the inbound rules and outbound rules for 443. So we have an entry here in the inbound rule for
443 that's on the second row, which allows all the IPv4 addresses to enter the subnet with
rule number 110. Now let's see the outbound rule set. We have the rule 110 here, which allows
outgoing traffic for HTTPS 443 from the subnet. So now let's see what happens when the request
comes from. So it tries to match the rule number 100, which is HTTP 80. So we don't have a match
here. Then it moves to the next higher number 110, which has HTTPS 443 allow rule set. So we
have a match. Now that it has a match, it will check for the ephemeral ports. And then if it
allows the set of ephemeral IP block, it will move on to the outbound traffic. So the rule number 100
has no match for HTTPS in the outbound traffic. Then it moves to the next one. Rule number 110
has a match. And then it checks for the ephemeral IPs for its permission levels. And if it is allowed,
then you have a successful connection. But I'm sure that you might be asking a lot of questions
to yourself right now. And we have learned about the flow of information and the packets. But
you might be thinking, what is ephemeral port? Don't worry, I haven't missed that out.
But before that, I want you to carefully look at the rule set and observe that we don't have
an allow all traffic for all IPv4 addresses. And just for a moment, rethink why we have allowed
custom TCP for these ephemeral ports. That is 32768 to 65535. And if I tell you that if I
remove the rule number 120 from the outbound traffic, your ports 80 and port 443 won't work.
So if I say this, will you agree with me on this one? Of course you should not, isn't it? Until and
unless you watch it from your own eyes. So let's understand the importance of ephemeral ports.
So what are ephemeral ports? Ephemeral in English means short-lived. And here as well,
an ephemeral port is a short-lived transport protocol port for IP communication. It looks
very short and simple, but it's not that simple. And we have to ask that if the definition tells
us that it is a short-lived transport protocol port for IP communication, why is it short-lived?
So these ports are short-lived because ephemeral ports are assigned on a temporary basis
for making or handling requests by the operating system, that is the host. For the same reason,
the client that initiates the request chooses the ephemeral port range and depending on the client's
operating system, whether it is Unix or Windows or Linux or whatever it is, it assigns an IP from
its ephemeral IP range. So here as it is already mentioned for Unix or Linux or for that matter
Amazon Linux kernels, use ports 32768 to 61000. And requests which are originating from the
Elastic Load Balancing uses ports 1024 to 65535. And for the Windows operating system,
through Windows Server 2003 uses 1025 to 5000. And for Windows Server 2008 and later versions,
they use 4915 to 65535. And a NAT gateway uses ports from 1024 to 65535. The same way actually
Lambda functions also use ports from 1024 to 65535. So these are their ephemeral port ranges
and whenever they initiate a request, they will choose one of these ports from the port ranges
and they will make the request. So based on your operating system, there will be an auto assignment
of ephemeral ports and that's the port number that will act as a source port for the packet header.
Yes, that's true. I know it sounds very strange but let's see this example of what happened when
a client sends an HTTPS request. So when a client makes a request with 443, the destination IP
is 421210 and the destination port is 443. Remember this very carefully that the destination port
is 443. Next, the source IP is 32.12.22.11 obviously because it has come from the source
but the source port is 32770. It's the ephemeral port, not 443. Remember that. And the most
important thing that you need to understand is that when you make a 443 request, your source
port will not be 443. Your destination port will be 443. Your communication board will be the
ephemeral port. Similarly, the way the response header has the source port as 443 because it is
a response. So the source port will be 443. The source IP will be 42.1.2.10 because it has come
from the source. Now as a part of the response, now the destination port will be 32770 that is
ephemeral port and the destination IP is the one that made the request that is 32.12.22.11 that is
our client. And that's the same reason why if we don't have a rule set in our inbound or outbound
rule with all the traffic allow, we need to have a rule set for the ephemeral ports. I know this
might be a bit confusing but don't worry we will get the clarification in the demo. But you have
to remember that the most important thing that you need to understand is that when you make a 443
request, your source port will not be 443 but your destination port will be 443. Your communication
port will be the ephemeral port.