avatarTeri Radichel

Summary

The website content provides guidance on how to end an active AWS CLI assumed role session, emphasizing the importance of clearing cached credentials and discussing the lack of a direct AWS CLI command to revoke an assumed role session.

Abstract

The article discusses the process of terminating an AWS CLI assumed role session, highlighting the necessity of deleting cached credentials stored in the .aws/cli/cache directory to ensure the session is properly ended. It points out that simply removing files or environment variables is insufficient, as the session may still be active elsewhere. The author, Teri Radichel, also expresses a desire for AWS to introduce a CLI command that can revoke assumed role sessions, similar to the existing functionality for terminating SSM and SSO sessions. The current method of revoking permissions by altering policies is noted to be risky and potentially disruptive to other active users of the role session. Additionally, the article touches on best practices for managing AWS credentials, such as avoiding the use of environment variables and ensuring periodic refresh of credentials when using role profiles. Radichel also advocates for a defense-in-depth approach to security, not relying solely on IAM, and suggests clearing out credentials and bash history after use to mitigate the risk of credential theft and abuse.

Opinions

  • The author believes that AWS should provide a direct CLI command to revoke assumed role sessions for better security and usability.
  • Teri Radichel prefers not to use environment variables for storing AWS credentials due to their potential exposure during penetration tests.
  • The article suggests that all methods of storing credentials have their drawbacks and that it's crucial to clear credentials after use to prevent unauthorized access.
  • The author emphasizes the importance of a multi-layered security approach, beyond just IAM, to protect against data breaches involving stolen or abused credentials.
  • Radichel recommends designing systems to automatically refresh credentials when they expire, especially when using role profiles.

How to End an Active AWS CLI Assumed Role Session

ACM.371 They may or may not be in the credentials file and deleting a file or environment variables is not enough

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⚙️ Check out my series on Automating Cybersecurity Metrics | Code.

🔒 Related Stories: AWS Security | Secure Code | Cybersecurity | IAM

💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the last post I wrote about restricting users so they can only change their own credentials.

While testing, I was looking around for how to end an active CLI session the other day because I forgot where they were so I am writing a post about it. I ended up digging around in my file system as some of the answers I found in a quick Google search were not applicable.

If you’re using the AWS CLI and you’ve assumed a role and wanted to end that session, you can delete the cached credentials. Initially I thought the cached credentials were in the credentials file. Depending on how you create the CLI profile and assume the role, sometimes they are.

But if you have assumed a role as I explained in this post they will be in another location.

What I wanted to do while testing the above post was clear my current MFA session so I could test that it still worked after making changes to an IAM policy. You also might want to clear these files when you’re done with a particular IAM session so someone cannot continue to use the session after you’re done with it.

I’m doing all my development work on an AWS EC2 instance. I can navigate to the .aws directory as shown below and you’ll see a cli directory. In that directory is a cache directory. In there you’ll find various json files depending on how many sessions you created.

If you look in those files you’ll find your credentials.

You can delete those files and then your local CLI and machine doesn’t have the credentials anymore — but note — the session is still active if someone else got ahold of those credentials in the meantime.

There’s not a good way to terminate sessions in AWS. They have a way to terminate SSM sessions and SSO sessions so why not CLI sessions? I really don’t understand. Please add an AWS CLI command to revoke an assumed role session. #awswishlist

As this documentation explains, you can terminate a session by essentially changing a policy to remove all permissions and that’s risky. First of all, you’re tampering with policies and might fat finger something. Secondly, if something or someone else is actively using a role session they will also be affected.

One other note.

You can obtain short term STS session credentials and use them in other ways. One would be to put them in the aws credentials file. You might also put them in environment variables where the AWS CLI can find them.

I’ve read posts about never doing one or the other. I personally don’t like using environment variables because they seem more easily accessible — on penetration tests, for example, if you don’t encrypt your Lambda environment variables and give me read access to your account to assess your security.

😊

I’m not one to say “Never” because it depends on your use case and where you are using the credentials. If you cannot write to a file system them you might have to use environment variables — which gets me thinking again about my problem using MFA in Lambda. Hmm. I’m still trying to see if there’s a way I can work this out.

I was able to do this with an EC2 instance.

The downside of putting your credentials in your credentials file or as a role profile is that you will have to design a way to refresh the credentials stored in your CLI credentials periodically when your session times out, as opposed to the method I used in the above post.

But to me, it doesn’t matter which one you use as all these methods are going to leave credentials hanging around on your system. If you put them in your environment variables you’ll have to change them every time you change your role as well.

Whatever method you choose, an attacker that gets on your system can get those credentials, so it’s a good idea to clear them out when you’re done using a session that requires MFA to assume and use. You can add the commands to clear those files out when you are done and clear your bash history as well. I’ve written about those things in other posts in this series:

If you want to read about all the different ways you can create short term credentials and assume roles and restrict privilege escalation and compromise check out my posts on IAM here:

Also, please make sure you are not relying on IAM alone. Stolen and abused credentials are involved in almost every data breach. Design your architecture with layers of defenses as I’ve demonstrated in the above series.

Follow for updates.

Teri Radichel | © 2nd Sight Lab 2023

About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab
Need Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presentation
Follow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
❤️ Sign Up my Medium Email List
❤️ Twitter: @teriradichel
❤️ LinkedIn: https://www.linkedin.com/in/teriradichel
❤️ Mastodon: @teriradichel@infosec.exchange
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
AWS
Iam
Assumed Role
Session
Cli
Recommended from ReadMedium