iac.htora.dev · security templates

Home/Templates/Shell access without opening a port

Shell access without opening a port

Reach a server for troubleshooting with nothing listening on the internet, and a recording of the session.

Terraform

Why bother

A jump box with SSH open to the world is something an attacker can reach and try passwords against. A broker keeps the port shut, checks who you are first, and writes down what you typed.

How you know it worked

Open a session. Confirm there is no inbound rule and the transcript was saved.

How it works

1You ask for a shellfrom the command line2The broker checks who you arethe server has no open port3The session runs through the brokeron an encrypted channel4A transcript is savedsomewhere you can audit it
Step by step, from the thing that happens to the result you want.

Set it up

Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.

Terraform. SSM Session Manager logging to S3 and CloudWatch

What you need first

  • An account where you can create IAM roles, S3 buckets, and Systems Manager documents.
  • Instances with the Systems Manager agent, which Amazon Linux and the AWS Windows images include, and outbound HTTPS to Systems Manager.
  • For Standard, the VPC for the security group, and a tag to mark instances operators may reach.

What it creates

  • Account-wide Session Manager preferences that record every session to S3 and CloudWatch Logs
  • An instance profile that lets the agent run and write transcripts, and a security group with no inbound rules
  • Each session encrypted under its own key, time limits, and transcripts kept for a set period (Standard)
  • An operator policy that reaches tagged instances only, through the recorded shell only (Standard)

The code

Quick startReadyone file, 163 lines
One file. Recorded sessions account-wide, an instance profile, and a security group with no inbound rules.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform destroy
    When you have finished. If it refuses, How to undo it below explains why.
Download main.tf
Checks

Formatting passedValid passedLint passed

Security scan: 47 checks passed, and nine rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/brokered-shell-access/aws/t0
main.tf
Open main.tf as plain text
StandardReadyfour files, 326 lines
Adds each session encrypted under its own key, encrypted transcripts with set retention, idle and maximum time limits, and an operator policy limited to tagged instances and the recorded shell.
Change these first
  • vpc_id. VPC for the no-inbound security group.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    vpc_id = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all four files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 77 checks passed, and seven rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/brokered-shell-access/aws/t1
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text
HardenedReadysix files, 237 lines
Standard as a module. Plan rules refuse session encryption or logging off, and operators able to start sessions anywhere, forward ports, or skip the tag condition. They also refuse inbound rules on the empty group, public access allowed, and key rotation off. A daily plan reports drift, and the scan fails on any unexplained finding.
Change these first
  • vpc_id. VPC for the no-inbound security group.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    vpc_id = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all six files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 71 checks passed, and none flagged.

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/brokered-shell-access/aws/t2
.checkov.yml
Open .checkov.yml as plain text
guard.yml
Open guard.yml as plain text
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text

How to check it worked

Open a session with aws ssm start-session, run a command, and exit. The transcript is in the bucket and the log group under your name, and the security group has no inbound rules.

What catches people

The preferences live in one document per account and region, named SSM-SessionManagerRunShell. If someone saved preferences in the console, it exists already, and apply fails until you import it.

Port forwarding sessions are not recorded, because the agent passes the traffic through without reading it. The Standard operator policy leaves them out for that reason.

The instance writes its own transcript with its own role. Without write access to the bucket and log group, the session is refused.

Asking for encrypted CloudWatch transcripts on a log group with no key makes every session fail. The quick start leaves that off, and Standard gives the group its key.

How to undo it

terraform destroy removes the preferences, bucket, log group, role, and security group. Instances still using the security group must be moved off it first.

What it costs

Session Manager is free for EC2. You pay for transcript storage in S3 and CloudWatch Logs.

Good to know

  • Nothing listens on the internet. Session Manager's agent and IAP's forwarder both connect out, so a port scan finds no SSH to attack.
  • Every session is tied to a cloud identity, so access ends when that identity is disabled, with no SSH keys left behind on the machines.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.