Adding inbound rules to aws ec2 instance - amazon-web-services

What I really need to ask is that if multiple rules in a security group bound to an instance which accept traffic from port 22, 80 and 443 i.e ssh, http and https is the same thing as defining a single rule i.e custom-tcp which accepts traffic from port 0-60000. Will implementing the latter provision the same kind of access as the former?
In my system I'm trying to add rules to the default security group with node SDK/API and I thought just adding one rule to accept all kinds of traffic would be better than defining them separately but I'm not sure about that.
Image 1: Access with multiple rules on separate ports:
Image 2: Access with a single rule for all ports:

Will implementing the latter provision the same kind of access as the former?
Sadly, they are not equivalent, though technically they will both allow 22, 80 and 443 ports.
The 0-60000 will allow incoming connections on all ports. This does not agree well with the security best practice of granting least privileged permissions only.
In contrast, 22, 80 and 443 will allow only the three ports. Any access on other ports will be denied. If you don't need another ports, this option is better from the security perspective.

Related

What is 'Custom TCP' in AWS security group?

When I set a inbound rule in a security group for an AWS EC2 like this:
So source type says that only traffic from my computer is to be allowed and only port 8080 in EC2 instance is to be made accessible.
What does 'Type' field here means when I set it as 'Custom TCP'? I see other values for this field, like HTTP,HTTPS,NFS,RDS,Postgres,SSH etc which basically define protocol type(if I am not wrong). What does Custom TCP define?
Custom TCP just means you can manually type in a port number to open in the AWS firewall. Use this when you need to open a port that doesn't correspond to a commonly used service. For example, ssh typically uses TCP port 22, and ssh is a program/protocol most AWS system administrators will use, so there is a dropdown option for ssh as a convenience that automatically enters TCP as the protocol and 22 as the port to open for data to flow to your EC2 instance.
You could alternately select "Custom TCP (port)" and manually enter 22, but then you might give it a name in one security group like "Forward 22 for SSH" and in another security group "open ssh"... Using the dropdown's SSH option provides consistency/readability in your security group rules.
IANA maintains a port number registry for common ports, but this list is much larger than AWS is likely to use for their dropdown menu. The options AWS presents are just enough to provide convenience for the most commonly used protocols when you're setting up forwarding rules.

AWS security group: Port range?

While configuring security groups in AWS, few parameters are to be selected, like
Type, Protocol, Port range, Source
What exactly Port range mean? Does it represent port where client process runs or port of the AWS resource?
Does it represent port where client process runs or port of the AWS resource?
It represents ingress or egress ports (e.g. 22-80) that are allowed through or out of your SG. Your application nor instance may not use these ports at all.
The example range 22-80 for ingress would mean that incoming traffic on ports from 22 to 80 is allowed to pass through the SG. Your instance may not not listen to these ports at all. From SG perspective it does not matter.
Obviously its a bad security practice to open more ingress ports that your instance actually uses. So if you only want to access your instance through port 22 (ssh) and 80 (http), then only allow these to particular ports, not entire range.

AWS Security Group Meaning of Port 0 in Custom TCP Rule

I have a Custom TCP rule in Outbound Security Configuration with Port Range as 0. Does this 0 means it is open to all ports to the ip range provided in the rule?. I have faced issues with this 0 as port when I changed this to correct port(5432) number after which it works fine.
I assume you are talking about the inbound rules (ingress) and outbound rules (egress) of a security group. A security group being a firewall around an AWS component - in your case, as your using port 5432, a PostgreSQL RDS database.
I just tested it on my side. I had a working inbound rule for accessing an EC2 server. When I changed the port to 0 - I could no longer access it. If you were to allow inbound\outbound traffic to all the ports you would specify: 0-65535
So as far as I can tell port 0 specified on its own doesn't seem to give any access.
Besides the 0-65535 port range, there is another valid port value when working with Cloudformation: -1.
This is used for the ICMP and ICMPv6 protocols. Some mention of it here.
You can not specify -1 as a port number when working with the AWS GUI.
If you are worried about security and just want to create an inbound rule for yourself - make sure to specify your /32 CIDR as a suffix to the IP address as per the previous image where TCP access is given across all ports to someone at the 1.2.3.4 IP address. Using a CIDR other than /32 will allow access to other IP addresses O_O
If you are new to working with security groups also please note the following:
Security groups are stateful - if you send a request from your
instance, the response traffic for that request is allowed to flow in
regardless of inbound security group rules. Responses to allowed
inbound traffic are allowed to flow out, regardless of outbound rules.
In other words, don't create a possible security risk by unnecessarily duplicating inbound rules to outbound rules (or vica verca) if you mistakenly thought that you had to create a inbound\outbound rule to handle the responses to the allowed inbound\outbound traffic.

Why is my AWS NACL only allowing HTTP access with 'All Traffic' or 'All TCP' inbound rules?

I've got an AWS VPC set up with 3 subnets - 1 public subnet and 2 private. I have an EC2 instance with an associated Elastic Block Store (the EBS contains my website) running in the public subnet, and a MySQL database in the private subnets. The security group attached to the EC2 instance allows inbound HTTP access from any source, and SSH access from my IP address only. The outbound security rule allows all traffic to all destinations. The security group associated with the database allows MySQL/Aurora access only for both inbound and outbound traffic, with the source and destination being the public access security group.
This has all been working perfectly well, but when I came to setting up the NACLs for the subnets I ran into a snag that I can't figure out. If I change the inbound rule on the public subnet's NACL to anything other than 'All Traffic' or 'All TCP', I get an error response from my website: Unable to connect to the database: Connection timed out. 2002. I've tried using every option available and always get this result. I'm also getting an unexpected result from the NACL attached to the private subnets: If I deny all access (i.e. delete all rules other than the default 'deny all' rule) for both inbound and outbound traffic, the website continues to function correctly (provided the inbound rule on the public subnet's NACL is set to 'All Traffic' or 'All TCP').
A similar question has been asked here but the answer was essentially to not bother using NACLs, rather than an explanation of how to use them correctly. I'm studying for an AWS Solutions Architect certification so obviously need to understand their usage and in my real-world example, none of AWS' recommended NACL settings work.
I know this is super late but I found the answer to this because I keep running into the same issue and always try to solve it with the ALL TRAFFIC rule. However, no need to do that anymore; it's answered here. The Stack Overflow answer provides the link to an AWS primary source that actually answers your question.
Briefly, you need to add a Custom TCP Rule to your outbound NACL and add the port range 1024 - 65535. This will allow the clients requesting access through the various ports to receive the data requested. If you do not add this rule, the outbound traffic will not reach the requesting clients. I tested this through ICMP (ping), ssh (22) http (80) and https (443).
Why do the ports need to be added? Apparently, AWS sends out traffic through one of the ports between 1024 and 63535. Specifically, "When a client connects to a server, a random port from the ephemeral port range (1024-63535) becomes the client's source port." (See second link.)
The general convention around ACLs is that because they are stateless, incoming traffic is sent back out through the mandatory corresponding port, which is why most newbies (or non hands on practitioners like me) may miss the "ephemeral ports" part of building custom VPCs.
For what it's worth, I went on to remove all the outgoing ports and left just the ephemeral port range. No outgoing traffic was allowed. It seems like either the ACL still needs those ports listed so it can send traffic requested through those ports. Perhaps the outgoing data, first goes through the appropriate outgoing port and then is routed to the specific ephemeral port to which the client is connected. To verify that the incoming rules still worked, I was able to ssh into an EC2 within a public subnet in the VPC, but was not able ping google.com from same.
The alternative working theory for why outgoing traffic was not allowed is because the incoming and matching outgoing ports are all below 1024-63535. Perhaps that's why the outgoing data is not picked up by that range. I will get around to configuring the various protocol (ssh, http/s, imcp) to higher port numbers,, within the range of the ephemeral ports, to continue to verify this second point.
====== [Edited to add findings ======
As a follow up, I worked on the alternate theory and it is likely that the outgoing traffic was not sent through the ephemeral ports because the enabled ports (22, 80 and 443) do not overlap with the ephemeral port range (1024-63535).
I verified this by reconfiguring my ssh protocol to login through port 2222 by editing my sshd_config file on the EC2 (instructions here. I also reconfigured my http protocol to provide access through port 1888. You also need to edit the config file of your chosen webserver, which in my case was apache thus httpd. (You can extrapolate from this link). For newbies, the config files will be generally found in the etc folder. Be sure to restart each service on the EC2 ([link][8] <-- use convention to restart ssh)
Both of these reconfigured port choices was to ensure overlap with the ephemeral ports. Once I made the changes on the EC2, I then changed the security group inbound rule, removed 22, 80 and 443 and added 1888 and 2222. I then went to the NACL and removed the inbound rules 22, 80 and 443 and added 1888 and 2222. [![inbound][9]][9]For the NACL, I removed the outbound rules 22, 80 and 443 and just left the custom TCP rule and add the ephemeral ports 1024-63535.[![ephemeral onnly][10]][10]
I can ssh using - p 2222 and access the web server through 1888, both of which overlap with ephemeral ports.[![p 1888][11]][11][![p2222][12]][12]
[8]: https://(https://hoststud.com/resources/how-to-start-stop-or-restart-apache-server-on-centos-linux-server.191/
[9]: https://i.stack.imgur.com/65tHH.png
[10]: https://i.stack.imgur.com/GrNHI.png
[11]: https://i.stack.imgur.com/CWIkk.png
[12]: https://i.stack.imgur.com/WnK6f.png

Security group https disappears from list

I am trying to enable https on my aws ec2 instance. I edited security group and alloved 443 port for ip4 and ip6. But after adding https when I save and check list of inbount rule, I cant see it
Add rule
After adding rule
Why https does not appear in list?
This is the default behavior of Security group.
If one or more rule specify the same port ,AWS will apply only most permissive rule for action.
If you specify the same port number for more rules with same IP type(IPv4 or IPv6),You will be getting error as "the same permission must not appear multiple times". As per your snap you have selected different IP address type in rule ,that why your rules are created successfully .
Note:Both rules will perform the same action(Customs TCP rule :443 & HTTPS: 443)
If there is more than one rule for a specific port, we apply the most
permissive rule. For example, if you have a rule that allows access to
TCP port 22 (SSH) from IP address 203.0.113.1 and another rule that
allows access to TCP port 22 from everyone, everyone has access to TCP
port 22.
Ref:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html?icmpid=docs_ec2_console#security-group-rules