Confused about SSH into private EC2 instance from a public EC2 instance - amazon-web-services

I have two separate NACLs, one for my public subnet and one for my private subnet. I am able to SSH into my public EC2 instance but not able to SSH into the private one without adding an outbound rule for my private NACL that allows ALL TCP to be open. What I'm confused about is why SSH fails if I only specify port 22 to be open on my private NACL outbound and inbound rule.

NACLs are stateless and need both inbound and outbound rules to allow a specific type of communication.For example ,if you want to access an ec2 instance on using ssh (tcp port 22) ,then in the inbound direction you will need to allow tcp port 22 and for the outbound direction ,since the destination port can be anything in the range 1024-65535 (depending on the client os),you will need to have such kind of rule for the ssh access.
Inbound
Rule # Type Protocol Port Range Source Allow/Deny
101 SSH TCP 22 (CLIENT IP or 0.0.0.0/0) Allow
Outbound
Rule # Type Protocol Port Range Destination Allow/Deny
101 Custom TCP TCP 1024-65535 (CLIENT IP or 0.0.0.0/0) Allow
ref:https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html#custom-network-acl
Now specifically to your setup,rules need to be created depending on how you are trying to access your ec2 instance in the private subnet.If you are using a bastion host to connect to your ec2 instance in private subnet ,then you will need to allow that ip/port in the outbound nacl rule .If you are using VPN then the rules will need to be changed accordingly.Perhaps more details about the nacl rule in your public and private subnet and how you are accessing the instances should help us provide more details on the problem.

As a general rule, you should never modify NACLs. They can be used for specific purposes (such as creating a DMZ), but there is rarely a need to change them from their default "Allow All" settings.
Instead, you should be using Security Groups to manage network permissions.
The typical configuration for your scenario would be:
On the Public (Bastion) instance, add a Security Group (Bastion-SG) that permits Inbound connections on port 22 (SSH) from your IP address (it is a good idea to restrict such access to the smallest possible range of IP addresses to prevent unauthorized access)
On the Private instance, add a Security Group (Private-SG) that permits Inbound connections on port 22 from Bastion-SG
This way, the Private-SG specifically references Bastion-SG, which will allow connections from the Bastion. Access attempts from any other location will be denied.
Security Groups are stateful, which means that return traffic will be permitted. This works well with SSH, which could use a range of different port numbers for return traffic.

Related

Cannot connect to Amazon RDS database

I've been trying to create and connect to a new DB on AWS for days. I made it publicly accessible.
I've done this but it can't connect:
HOST: vepo-qa-database.xxxxxxx.ap-southeast-2.rds.amazonaws.com
URL: jdbc:postgresql://vepo-qa-database.xxxxxxx.ap-southeast-2.rds.amazonaws.com:5432/
Why will it not connect?
Your Security Group inbound rule configuration is:
Allow Inbound traffic on any port as long as it comes from sg-1fefe550 (itself!)
This means that only resources associated with that same security group can communicate with the database. Therefore, it is blocking access from your own computer on the Internet.
To enable access to the database, you should add another Inbound rule that permits inbound access on port 5432 (PostgreSQL) from your own IP address.
(I'm also not sure why you have configured the security group to permit access from 'itself'. This is not a normal way to configure security groups.)
There is not enough information in your question to be certain, but here are two suggestions:
1 - verify that the subnets for the database actually are public and that there is a route to the IGW
2 - add a rule to the inbound security group rules to allow you to connect to port 5431. Generally you'd want to restrict this to just your IP address for a public instance.
Also, I'm not sure why you have 192.31.0.0 as part of your VPC range - that is a public IP range.

Providing internet access to private Ec2 instance using NAT instance

I was able to connect my private Ec2 instance to the internet through NAT instance.
wget google.com was successful.
But when I attached a NACL to my private subnet.I wasn't able to connect to the internet.Can anyone please tell me what is wrong with my NACL.
10.0.1.0/24 and 10.0.2.0/24 are CIDRs of public subnet(which has the NAT instance)
private NACL inbound rule
private NACL outbound rule
Also, when I add rule to accept all traffic from internet.It works. Is it correct practice to add this rule ?
Based on the comments.
The issue was due to blocking inbound internet traffic (0.0.0.0/0). From the Instances cannot access the internet in AWS docs, to enable internet access of the instance using NAT, the following must be ensured:
Ensure that the network ACLs that are associated with the private subnet and public subnets do not have rules that block inbound or outbound internet traffic. For the ping command to work, the rules must also allow inbound and outbound ICMP traffic.
Question from comments:
But just one question how will whitelisting work in this case. If I only want to download certain libraries using the internet and not allow any api requests ?
If you know IP ranges of the public servers that these libraries are going to be downloaded from, then you could limit the internet traffic to only these ranges. But if the range is not fixed or unknown you have to allow all (0.0.0.0/0) internet traffic in the rules.
Generally, security groups are used as primary way of controlling network traffic. NACLs are good due to their their denying ability. Security groups can't explicitly deny traffic, thus NACLs are helpful in cases where you want to explicitly deny some IPs. But in your case, you are allow only traffic. Thus I think the use of default NACLs would be sufficient and could be considered.

Cannot access EC2 via HTTP/HTTPS

I recently had to completely rebuild my AWS EC2 environment, because I accidentally deleted the SSH key, thinking it was something else. Unfortunately, I cannot access my Tomcat instance which I have confirmed is running on that EC2 instance.
I have added the following security groups for inbound HTTP traffic:
Type. Protocol Port Range Source
HTTP TCP 80 0.0.0.0/0
HTTP TCP 80 ::/0
Custom TCP Rule TCP 8080 0.0.0.0/0
Custom TCP Rule TCP 8080 ::/0
SSH TCP 22 (my IP address)
I have three security groups, and the above rules were added to the group named default:
rds-launch-wizard
launch-wizard-1
**default
I purchased a domain via AWS which I have pointed to this EC2 instance using Route 53. Previously, the DNS was available, but now it is not. However, there is a bigger fundamental problem here because I can't even ping the public IP of my EC2 instance.
I am in fact able to access my EC2 instance via SSH on port 22, which is why I was able to setup Java and Tomcat (both of which I have confirmed are running).
I suspect that some state from my previous configuration is responsible for this problem, but I don't even know where to begin looking for something.
Any help would be appreciated.
To have inbound/outbound internet access to your EC2, you need to look for three things:
Are you able to SSH from outside or inside AWS via an internet gateway (IGW)? If you can SSH from outside then you already have IGW setup properly. Otherwise, make sure your subnet's route table points to IGW by having a route like below:
0.0.0.0/0 igw-efxxxxxxx Active No
Network ACL: Go to your EC2's subnet and find its associated NACL
Create both inbound and outbound rules to ALLOW traffic to the above NACL by adding a rule like below:
100 ALL Traffic ALL ALL 0.0.0.0/0 ALLOW
Security Groups: Your security groups look good. No changes required there. However, based on the comments you made below it appears that, while you did define a security group with the proper inbound rules, for whatever reason you did not associate that security group with your EC2 instance. As a result, the inbound rules you defined were not being applied. To fix this, from the EC2 instance tag access the following:
Actions -> Networking -> Change Security Groups
Then, associate your security group with your instance by checking the appropriate box. After making these changes, your inbound rules should take effect, and you should be able to hit your Tomcat instance running on EC2.

Does a curl/wget request respond on a random ephemeral port?

When I was setting up VPC in aws, I had created an instance in public subnet. The instance was not able to ping to google and was giving timeout when connecting to yum repository.
The security groups were open with required ports.
When I edited the ACL to add ICMP from 0.0.0.0/0 in inbound the instance was able to ping to google. But the yum repository was still was giving timeout. All the curl/wget/telnet commands were returning error. Only ping was working.
When I added the following port range for inbound in ACL 1024-65535 from all 0.0.0.0/0 that is when the yum repository was reachable. Why is that?
The outbound traffic was allow all in ACL. Why do we need to allow inbound from these ports to connect to any site?
In AWS, NACLs are attached to subnets. Security Groups are attached to instances (actually the network interface of an instance).
You must have deleted NACL Inbound Rule 100, which then uses Rule *, which blocks ALL incoming traffic. Unless you have specific reasons, I would use the default rules in your NACL. Control access using Security Groups which are "stateful". NACLs are "stateless".
The default Inbound rules for NACLs:
Rule 100 "ALL Traffic" ALL ALL 0.0.0.0/0 ALLOW
Rule * "ALL Traffic" ALL ALL 0.0.0.0/0 DENY
Your Outbound rules should look like this:
Rule 100 "ALL Traffic" ALL ALL 0.0.0.0/0 ALLOW
Rule * "ALL Traffic" ALL ALL 0.0.0.0/0 DENY
When your EC2 instance connects outbound to another system, the return traffic will usually be between ports 1024 to 65534. Ports 1 - 1023 are considered privileged ports and are reserved for specific services such as HTTP (80), HTTPS (443), SMPT (25, 465, 587), etc. A Security Group will remember the connection attempt and automatically open the required return port.

AWS network ACL: why do I need to allow outbound port 80 to be able to browse Internet?

I am very new to AWS and networking. I have been playing with network ACL. I realized that if I don't allow outbound port 443 (HTTPS) on the network ACL, I wouldn't be able to use a browser to go to https://www.google.com from within the EC2 instance in the subnet associated with this ACL.
Likewise, if I don't allow outbound port 80, I wouldn't be able to go to http://www.cnn.com.
This confused me. When I allow port 80 outbound on the ACL, am I allowing the EC2 to talk to port 80 on the CNN server, from an ephemeral port on my EC2, or am I allowing the EC2 to initiate a connection from port 80 on the EC2?
use the security groups at instance level to have security at instance level.And unlike security group, ACL is stateless and works at subnet level i.e if you want your instances to communicate over port 80(http) then you have add an inbound and outbound rule allowing port 80.
You don't have to add any rules. The default network ACL is configured to allow all traffic to flow in and out of the subnets to which it is associated. Each network ACL also includes a rule whose rule number is an asterisk. This rule ensures that if a packet doesn't match any of the other numbered rules, it's denied. You can't modify or remove this rule.
Rules allow all IPv6 traffic to flow in and out of your subnet. We also add rules whose rule numbers are an asterisk (Catch All) that ensures that a packet is denied if it doesn't match any of the other defined numbered rules.
A network ACL has separate inbound and outbound rules (Stateless), and each rule can either allow or deny traffic.
Out Bound Rules allow outbound traffic from the subnet to the Internet. In other words it is matching the traffic flow with the defined rules in the ACLs list and apply it (ALLOW/DENY).
If you have private instances that should not be accessible by public users in the internet it is best practice from security point of view to place these instances in a private subnet and use NAT instance in a public subnet and make all traffic flow goes through this NAT instance to make patch updates and get the public access.
For more information, Check Amazon Docs