GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing linuxsecurity.com — Linux AWS Credentials - Replace Static Keys With EC2 Roles Critical WordPress Flaw Exploited in the Wild: Learn how to protect your site and check your core update window.× In this article...
AWS Credentials on Linux: Moving to Short-Lived Access
8 - 15 min read
Most cloud access keys don’t persist on a Linux server intentionally. Key pairs can simply get pasted into a file, and forgotten once the job runs. If they are not caught in review, keys might sit there for years with permissions that still work. The alternative is short-lived credentials: tokens issued by the cloud provider that expire on their own and renew automatically. Below, we’ll look at where keys tend to hide on Linux hosts, what temporary credentials protect against, and walk through a real-world example of backup migration from a static AWS key to an EC2 instance role. Where Are the Keys Actually Kept?The AWS CLI stores keys in Then, there are environment variables. These became a popular way to pass configuration into applications, and secrets followed closely behind. An environment can be read through The systemd project’s own documentation on service credentials flags environment variables as a problem for secrets because they’re passed down the process tree by default. It doesn’t help that unit files in The next location is Shell history. You can simply type Finally, cloud credentials regularly turn up in configuration management. That includes places like Ansible variable files that were supposed to be vaulted, Terraform .tfvars files, Docker Compose .env files, and Kubernetes secrets that are base64-encoded rather than encrypted (for Kubernetes, encryption at rest can be configured separately). The Problem with Static CredentialsStatic key copies will keep working until someone rotates, deactivates, or deletes the key. These copies can multiply. Whenever the home directory gets backed up, the key goes in the backup. A dotfile gets committed to the wrong Git repository. A support bundle is sent to a vendor with the application’s config tucked inside it. Access controls for each copy are usually weaker than the server’s. Keys are also easy to spot. AWS gives long-term access key IDs the prefix AKIA and temporary ones the prefix ASIA, and secret scanners, whether they’re run by GitHub or by attackers, look for exactly that pattern. Test keys deliberately leaked to public repositories have repeatedly been picked up within minutes. Attackers actively look for exposed keys. When nobody is sure which jobs still depend on a particular key, they hesitate to delete keys. On older systems, that creates a collection of credentials that nobody can account for. The risks of long-lived cloud credentials extend beyond configuration files: application memory dumps can expose keys that remain usable after the original leak. Temporary Credentials: Better, But Not BulletproofIn AWS STS, temporary credentials are made up of three parts – an access key ID, a secret, and a session token. Once their expiry time passes, all three are useless. However, while temporary credentials are valid, anyone holding all three parts can use them from another machine, with every permission the role grants. The best-known example is the 2019 Capital One breach, where a server-side request forgery (SSRF) through a misconfigured web application firewall was used to ask the EC2 metadata service for role credentials. Those credentials were perfectly legitimate and perfectly temporary, and they were still used to copy data out of S3. Shorter-lived doesn’t mean harmless; it just gives the attacker less time. AWS responded later that year with version 2 of the Instance Metadata Service (IMDSv2). That reduces certain SSRF risks, but it isn’t complete protection: an attacker who can run code on the instance, or exploit a flaw that lets them send arbitrary requests, can still fetch a token just like any legitimate process. AWS also added condition keys specific to EC2 instance credentials, which allow a policy to check where a request using those credentials came from. However, those conditions only work if you explicitly write them into your IAM or resource policies, and if your workloads reach AWS through the network paths the policy expects (such as VPC endpoints). Without changing the default configuration, credentials lifted off the server can still be used from somewhere else. If something does leak, administrators can revoke a role’s active sessions. That attaches a deny policy to every credential the role issued before a chosen point in time. The deny policy doesn’t distinguish between the attacker’s copy and the legitimate workload’s copy, so every process using that role loses access until it obtains credentials issued after the revocation. A Practical Example: Moving a Backup Job to an Instance RoleAn Ubuntu server on EC2 runs PostgreSQL, and a systemd timer starts Before changing anything, the first step is to find out what the key actually does. On the host, CloudTrail can fill in the gaps, with one caveat: Keep the Permissions TightNext, you’ll need an IAM role. It needs a trust policy that allows ec2.amazonaws.com to assume it, and a permissions policy that allows exactly what the job does and nothing more. In this case, that means The AWS CLI switches to multipart once a file goes over its default threshold of 8 MB, which a database dump almost certainly will, and the individual multipart operations are covered by There’s no Attach the role with Getting (and Renewing) the CredentialsThe metadata service lives at 169.254.169.254, an address in the link-local range reserved for traffic that never leaves the local network segment. With IMDSv2, a client first requests a token by sending a PUT to Running the backup under a dedicated backup user is good practice, but it doesn’t, by itself, isolate the role’s credentials. The metadata service doesn’t check which Linux user is asking, so any process on the instance that can reach 169.254.169.254, whether that’s a compromised web application, a stray script, or another user’s cron job, can request exactly the same credentials. AWS’s documentation describes ways to limit this with local firewall rules (for example, an iptables rule that only lets specific user IDs reach the metadata service), but the more fundamental point is that an instance role is shared by everything running on that instance, so it’s best not to mix untrusted workloads with a privileged role on the same machine. According to AWS’s documentation on retrieving instance credentials, new credentials are made available at least five minutes before the old ones expire. AWS CLI and SDKs handle retrieval and refresh. The AWS CLI and SDKs work through a chain of credential providers, and which one wins depends on the active configuration: environment variables, a named profile, and settings in the shared config and credentials files are typically checked before the instance metadata service. So if the service’s environment still points the CLI at the old static key, whether through a profile, a variable, or the files in Then, check who the job thinks it is, and do it from inside the backup service’s actual execution environment rather than your own shell. A Testing Credential Renewal and Handling FailuresStart the service manually with systemctl start A test only proves that one set of credentials works. The real test comes after the first credential expires and the AWS CLI has to retrieve another. Let the job run on its normal schedule for at least a week, checking that each backup completes and the expected objects appear in S3. If renewal breaks, the job may only continue working until credentials expire. On Linux, the usual causes are:
If you have a staging instance, test renewal by adding a temporary nftables rule that blocks traffic to 169.254.169.254, wait for the current credentials to expire, and run the backup again. The job should fail with an error that your monitoring can detect. Remove the rule afterwards and confirm that the AWS CLI can retrieve credentials again. Also check the system clock when authentication starts failing. AWS API requests are signed with timestamps, and a clock that drifts too far from AWS's time can produce errors such as Run Backup failure might not draw attention until someone needs to restore a database. An Logs provide another useful signal. Search for The most reliable check watches the backup itself rather than just the service. A dead man's switch can send a heartbeat to a monitoring service after each successful run, or a scheduled check can verify that the newest object under That catches failures regardless of their cause. Expired credentials, blocked metadata requests, a drifting clock, a full disk, or an unavailable database can all leave the bucket without a fresh backup. If the expected backup doesn't appear, someone needs to know before the next restore. Retiring the Old KeyOnce the role has proven itself, it’s time to retire the old key. Do this in two stages, because only one of them can be undone. First, deactivate it with After that, leave the key inactive through at least one full cycle of every schedule that might plausibly use it, keeping an eye on CloudTrail and your application logs for authentication failures. If nothing unexpected happens, then it’s okay to delete the inactive key with Search News & UpdatesRelated Articles
Could not load related articles.
We use cookies to provide and improve our services. By using our site, you consent to our Cookie Policy. You are now being logged in using your Facebook credentials |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |