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.
Open a session. Confirm there is no inbound rule and the transcript was saved.
How it works
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 startReady
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 47 checks passed, and nine rules flagged, each with a written reason:
- Security group not attached
- S3 lifecycle configuration
- S3 event notifications
- Session Manager data encrypted with your own key
- S3 cross-region replication
- S3 encrypted with KMS
- Log group encrypted with KMS
- S3 server access logging
- S3 versioning on the SIEM failed-events bucket
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
StandardReady
vpc_id. VPC for the no-inbound security group.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# terraform.tfvars vpc_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 77 checks passed, and seven rules flagged, each with a written reason:
- Security group not attached
- S3 event notifications
- IAM permissions management without constraints
- IAM write access without constraints
- S3 cross-region replication
- S3 server access logging
- IAM resource of *
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
vpc_id. VPC for the no-inbound security group.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# terraform.tfvars vpc_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
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.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
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.
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.
Terraform. Azure Bastion, with every connection recorded
What you need first
- A subscription, and Network Contributor or Contributor on it.
- For Standard, an Entra group for the operators.
What it creates
- A virtual network with AzureBastionSubnet and a workloads subnet with no default internet access
- Azure Bastion Standard with native client support, reached by az network bastion ssh
- Connection records in a Log Analytics workspace, kept a year
- Premium with session recording, the network rules Microsoft documents for Bastion, and SSH and RDP accepted from Bastion only (Standard)
- Copy and paste and file transfer off, an operator group with Reader on Bastion and VM User Login on the workloads, and a NAT gateway so Entra ID sign-in works (Standard)
The code
Quick startReady
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: two checks passed, and one rule flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open main.tf as plain text
StandardReady
subscription_id. Subscription for the network and Bastion.operator_group_object_id. Entra group whose members may connect. They get Reader on Bastion and Virtual Machine User Login on the workloads resource group, and nothing else.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = "" operator_group_object_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 52 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
subscription_id. Subscription for the network and Bastion.operator_group_object_id. Entra group whose members may connect. They get Reader on Bastion and Virtual Machine User Login on the workloads resource group, and nothing else.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = "" operator_group_object_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 52 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Connect with az network bastion ssh, then query MicrosoftAzureBastionAuditLogs in the workspace. The session is there with your name. Standard prints more checks with terraform output verify.
Bastion needs a subnet named AzureBastionSubnet, /26 or larger. A network security group on it without every rule Microsoft documents stops Bastion working.
Session recording needs a storage container whose SAS URL is set on Bastion in the portal after apply. Until then nothing is recorded.
Signing in with Entra ID needs the AADSSHLoginForLinux extension on each Linux machine, outbound HTTPS to Entra ID and Microsoft's package servers, and the Virtual Machine User Login role. The quick start has no outbound access, so it uses SSH keys; Standard adds a NAT gateway.
Bastion Standard and Premium bill by the hour whether or not anyone connects.
How to undo it
terraform destroy removes Bastion, the network, and the workspace. Machines in the workloads subnet must be moved or deleted first.
What it costs
Bastion bills per hour by tier, Premium more than Standard, plus outbound data. Standard's NAT gateway bills by the hour and per GB. Log Analytics by GB.
Terraform. Identity-Aware Proxy TCP forwarding with audit logs
What you need first
- A project where you can create firewall rules and grant IAM roles.
- VMs with no external IP address, carrying the network tag the firewall rule names.
- The person or group allowed in, as user: or group:.
What it creates
- A firewall rule that lets only IAP's range reach port 22 on tagged VMs
- The IAP tunnel role and the OS Login role for the named person or group
- OS Login switched on for the project
- Tunnels limited to named ports by an IAM condition, and every tunnel recorded in the audit log (Standard)
The code
Quick startReady
project_id = "your-project-id"main.tf, line 18network = "default"main.tf, line 19operator = "user:you@example.com"main.tf, line 20
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 17 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
StandardReady
project_id. Project containing the VMs.network. VPC network the VMs use.operators. Who may open tunnels and sign in: user:, group:, or serviceAccount: members. Prefer one group.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = "" network = "" operators = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 17 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
project_id. Project containing the VMs.network. VPC network the VMs use.operators. Who may open tunnels and sign in: user:, group:, or serviceAccount: members. Prefer one group.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = "" network = "" operators = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 17 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Run gcloud compute ssh with --tunnel-through-iap and the session opens. The VM has no external IP, and the only rule allowing port 22 names 35.235.240.0/20.
IAP records who opened a tunnel, to which VM and port, and when. It does not record what was typed. For that, run auditd on the VM and ship its logs.
Tunnel records are Data Access logs, off by default. Standard switches them on for IAP.
roles/compute.osAdminLogin adds sudo on every VM in the project. Both levels grant osLogin only, so grant admin separately and to fewer people.
An existing firewall rule that allows port 22 from anywhere still works alongside this one. Find and delete it, or the VM stays reachable from the internet if it has an external IP.
How to undo it
terraform destroy removes the firewall rule and the role grants. OS Login stays switched on until you remove the enable-oslogin metadata.
What it costs
IAP TCP forwarding is free. Data Access logs are billed by volume beyond the free allowance.
Terraform. OCI Bastion with managed SSH sessions
What you need first
- A compartment, and permission to manage networks, bastions, and policies.
- Instances with the Bastion plugin of Oracle Cloud Agent enabled.
- For Standard, a group for the operators.
What it creates
- A network with a private subnet, a service gateway for Oracle Cloud Agent, and no route to the internet
- A bastion that accepts connections from one address range, with sessions of an hour at most
- Several address ranges, 30-minute sessions, and SSH accepted from the bastion's own address only (Standard)
- An operator policy limited to this bastion and one OS user, and an email for every session created (Standard)
See the one alert this creates
The code
Quick startReady
compartment_ocid = "ocid1.compartment.oc1..change-me"main.tf, line 23client_cidr = "203.0.113.0/24"main.tf, line 24
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: five checks passed, and none flagged.
Last run 2026-11-09 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
tenancy_ocid. The tenancy. The operator policy is created here.region. Region identifier, such as us-ashburn-1.compartment_ocid. Compartment for the network, the bastion, and the instances operators reach.client_cidrs. Where operators connect from, such as the office and VPN ranges. The bastion refuses everything else.operator_group. Group whose members may open sessions, as the OS user below, on instances in the compartment.alert_email. Address told about every session created. It must confirm the subscription before mail arrives.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" region = "" compartment_ocid = "" client_cidrs = "" operator_group = "" alert_email = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: four checks passed, and three rules flagged, each with a written reason:
- VCN has an inbound security list
- Security list restricts SSH from 0.0.0.0/0
- Network security group ingress rules are stateless
Last run 2026-11-09 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
tenancy_ocid. The tenancy. The operator policy is created here.region. Region identifier, such as us-ashburn-1.compartment_ocid. Compartment for the network, the bastion, and the instances operators reach.client_cidrs. Where operators connect from, such as the office and VPN ranges. The bastion refuses everything else.operator_group. Group whose members may open sessions, as the OS user below, on instances in the compartment.alert_email. Address told about every session created. It must confirm the subscription before mail arrives.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" region = "" compartment_ocid = "" client_cidrs = "" operator_group = "" alert_email = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: four checks passed, and none flagged.
Last run 2026-11-09 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Create a managed SSH session with oci bastion session create-managed-ssh and connect with the command it prints. Audit shows the CreateSession event under your name. Standard prints more checks with terraform output verify.
Bastion does not record what is typed. The record is who opened which session, to which instance, as which user, and when.
Managed SSH sessions need the Bastion plugin running on the instance, and the plugin needs to reach Oracle's services.
The quick start accepts SSH from anywhere in the subnet, so instances there can reach each other. Standard narrows it to the bastion's address.
When a session reaches its time limit, Bastion ends it, and the SSH connection through it ends too.
Deleting the notification topic or the connector's policy stops the session email, and nothing reports it. Hardened refuses both deletes, and its daily drift plan notices one made by hand.
How to undo it
terraform destroy removes the bastion, the network, and the policy. Instances in the subnet must be moved or deleted first.
What it costs
Nothing. OCI Bastion is free.
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.