Guide to using AWS Tokens effectively
Introduction
Current issues
In previous articles, I have provided instructions on using AWS access tokens and secret tokens to utilize Amazon services, but there is an issue where these tokens do not have an expiration time. Thus, if your tokens are somehow leaked, an attacker can use them for as long as they want until you can detect and delete these tokens. This is a user-side issue, but there are still ways to limit the impact of this by granting temporary tokens during use. Naturally, these temporary tokens will have a short lifespan (about a few hours, or you can change the duration to suit your security level). Therefore, even if this token is leaked, an attacker only has a limited amount of time to use it before the token expires.
If you are a member created by IAM Identity Center, you are already supported with permission management and integrated security measures during use. However, if you are using a personal account or have the rights to create an IAM User, there are still ways to configure your own account to require active MFA before the tokens can be used.
Detail
First, use AWS CDK to create lib/user-stack.ts
- userName: you can change to a different username
- The information created includes
- Policy group: has full permissions for AWS S3 (AmazonS3FullAccess)
- Policy statement: Deny every action if there is no MFA session
- User: create AccessKeyId and SecretAccessKey (this information is only shown once, so please save it for use)
- Then add the Policy Group to the Policy statement, and add the User to the Policy Group
Update file bin/aws-cdk.ts
After deployment, the results are as follows:
The User has been created in IAM User
Then click Assign MFA device to add Multi-factor authentication
Once MFA is successfully created, get the Identifier information to add to the ~/.aws/config file as follows (please change the values accordingly):
Next, use this command to configure the token:
After success, the ~/.aws/credentials file will have the following content:
You can check the user currently in use like this
Next, install awsume, which is a popular tool for AWS Devs to automatically retrieve temporary tokens and set them into the environment for use
At this point, the preparation steps are complete and you can begin use. You might wonder why we have the aws_access_key_id and aws_secret_access_key but do not use them directly and instead must add many other steps; this is because, in the AWS CDK config above, we added a policy that only works when the session has MFA. Therefore, if you use the aws_access_key_id and aws_secret_access_key directly, they will not work (this is the security mechanism I mentioned at the beginning of the article; even if these tokens are leaked, there is no impact)
To verify, trying to use an AWS service will report the following error:
Next, you must use awsume to retrieve a temporary token to be able to use it
After successfully getting the session, the token information will be stored here ~/.awsume/cache/aws-credentials
Then you can use it as normal
To exit the current session, use the unset command as follows:
Happy coding!
Comments
Post a Comment