Closing port 22 with Session Manager: five things that still bite after the SSH key is gone
Session Manager removes the open SSH port and the key file, but sudo by default, missing logs, broad StartSession rights and SendCommand can quietly hand back the access you meant to take away.
TL;DR: Moving from SSH to Session Manager closes port 22 and kills the key file problem. It does not limit who gets a root shell. Check sudo, logging, who can start sessions, and who can run SendCommand before you call it hardened.
The Securing Your Servers assignment in Cloud Quest Security had me manage and patch instances without SSH, using Systems Manager. The security group ended up with no inbound rules at all, and I could still get a shell. That feels like the job is done. From a SOC point of view it is only half done, because the attack surface moved from a network port to an IAM permission, and IAM is where most real AWS incidents start.
Gotcha 1: no session until the instance can reach Systems Manager
The instance needs the SSM Agent, an instance profile with AmazonSSMManagedInstanceCore, and outbound HTTPS to the Systems Manager endpoints. In a private subnet with no NAT gateway, that means interface VPC endpoints for ssm, ssmmessages and ec2messages. If the instance never shows up as Online in Fleet Manager, it is almost always one of these three, not the security group.
Gotcha 2: the default user can sudo
On Linux, sessions start as ssm-user, and that account is added to sudoers by default. So anyone allowed to start a session effectively has root. If that is not what you want, remove ssm-user from sudoers or turn on Run As support so sessions open as a specific OS user tied to the IAM identity.
Gotcha 3: nothing is logged unless you turn it on
CloudTrail records that a session started and who started it. It does not record what they typed. Session output logging to S3 or CloudWatch Logs is off until you enable it in Session Manager preferences. Even then, port forwarding and SSH-over-Session-Manager sessions do not get their content logged, so those should be allowed only where you really need them.
Gotcha 4: StartSession on every instance is the new open port
An IAM policy with ssm:StartSession on Resource * gives a shell on every managed instance in the account. Scope it with a tag condition such as ssm:resourceTag/Team, and let people terminate only their own sessions by restricting ssm:TerminateSession to session IDs that start with their own user ID.
Gotcha 5: SendCommand is a shell too
ssm:SendCommand with the AWS-RunShellScript document runs commands as root on the target, with no interactive session and outside the session logging above, since Run Command has its own output settings. Attackers know this. Treat SendCommand as admin access and alert on it.
Checks worth running
These show where SSH is still open and which instances Session Manager can reach.
# security groups that still allow SSH from anywhereaws ec2 describe-security-groups \ --filters Name=ip-permission.from-port,Values=22 Name=ip-permission.cidr,Values=0.0.0.0/0 \ --query "SecurityGroups[].[GroupId,GroupName]" --output text
# instances registered with Systems Manager and their agent statusaws ssm describe-instance-information \ --query "InstanceInformationList[].[InstanceId,PingStatus,AgentVersion]" --output table
# open a shell with no inbound portaws ssm start-session --target i-0123456789abcdef0If the first command returns anything, Session Manager was added next to SSH instead of replacing it.
What I would do in a real account
- Remove the port 22 rules, then add an AWS Config rule so they cannot come back quietly.
- Enable session logging to an S3 bucket in a separate log account, encrypted with KMS.
- Scope ssm:StartSession by tag and keep ssm:SendCommand to a small automation role.
- Turn on Run As or drop ssm-user from sudoers.
- Alert on StartSession and SendCommand events from people who do not normally use them.
The lab showed me that I could close the port. The real work is making sure the IAM permission that replaced it is as tight as the firewall rule used to be.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article