Sitemap
資安工作者的學習之路

業餘資安寫手,希望透過紀錄所學的知識來回饋於社群上,互相學習分享。個人介紹參考 https://portaly.cc/kurohuang

Running Custom Scripts on Windows WSL for AWS Resource Inventory: Addressing AWS SecurityAudit Policy Restrictions

4 min readNov 15, 2024

--

This article shares the process of running a custom script to perform specific asset inventory checks in an AWS subscription account under the IAM Managed Policy — SecurityAudit.

The link to the Traditional Chinese version of this article : 在 Windows 安裝WSL 執行Scirpt 以進行 AWS 資源盤點:解決 AWS SecurityAudit Policy限制

Objective

The objective is to inventory specific AWS resources within a subscription account, including EC2, ECS, EKS, and RDS. The user account is restricted by AWS’s SecurityAudit policy, and the computer operating environment is Windows.

Under severe constraints and a demanding environment, I could not execute from the cloud’s highest administrative account, nor did I have elevated privileges for regular subscription accounts. Without the necessary management tools and a limited workforce, we faced the challenge of extracting specific asset information statistics from many subscriptions within a tight timeframe.

By the way, you can only check information one account at a time. You can’t do anything from a higher-level admin account.

The initial script, designed to run on PowerShell in Windows, encountered some issues. So, the decision was made to switch to Ubuntu, and this article documents the steps to complete the script execution.

Reference Documents

WSL

AWS CLI

My Script

Installing WSL

To install WSL, open PowerShell and run:

wsl --install
Press enter or click to view image in full size
Press enter or click to view image in full size

After installation, a restart is required. For WSL setup details, refer to the official documentation.

Using Ubuntu in WSL (Windows Subsystem for Linux Mode)

  1. Install the JSON processing package, jq:

jq is a lightweight and flexible command-line JSON processor.

sudo apt install jq

2. Copy the script to the Ubuntu environment under WSL:

\\wsl.localhost\Ubuntu\home\[XXX]

3. Modify script permissions:

chmod +x [yourfile].sh

4. Installing AWS CLI

curl "[<https://awscli.amazonaws.co>](<https://awscli.amazonaws.co/>)m/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
./awscli-bundle/install -b ~/bin/aws

Running the Script

To set up environment variables or use a configuration file (AWS configuration) for the subscription account, we’ll demonstrate with environment variables here. The Access Key and SECRET in this example are from the official documentation:

export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_DEFAULT_REGION=your-region

Due to limitations in the SecurityAudit permissions, which prevent access to all Regions and CloudShell, I’m using a script to retrieve the information. The Regions are manually specified. I’ve provided another script to list the enabled Regions in the current account.

Security Warning: Storing Access Keys in plain text locally is a security risk. For production use, consider using AWS IAM roles and temporary credentials or IAM roles for EC2 if running within an EC2 instance. Avoid hard-coding sensitive information.

When mentioning the use of IAM roles, it’s recommended to also emphasize adhering to the Principle of Least Privilege to ensure that roles only have the permissions necessary for their tasks.

For external applications or services that need access to AWS resources, using AWS STS (Security Token Service) to generate temporary credentials is a more secure option.

Confirm the correct identity:

aws sts get-caller-identity 

Execute the Script

Run the script on your WSL envireonment ( Ubuntu )

./[yoursciprt].sh
Press enter or click to view image in full size

Finally, the script will display the summarized inventory results.

For source code security, I used Amazon CodeGuru Security, now available for free, to scan the script. No findings were detected.

Press enter or click to view image in full size

About Kuro

I’m excited to share my experiences and create educational content that inspires a community of knowledge-sharing! My hope is that others feel encouraged to write and share their own insights too. If you’re self-studying and have any questions about exam applications or preparation, don’t hesitate to contact me on LinkedIn or Facebook! I’m here to help as much as I can. Also, if you’d like to grab a coffee and chat, I’d love to connect with others in the industry — I’ve already had lots of fantastic conversations! Let’s inspire each other!

For more about me, my contact information, and to see all my posts (I recommend this link for a more comprehensive view):

--

--

資安工作者的學習之路
資安工作者的學習之路

Published in 資安工作者的學習之路

業餘資安寫手,希望透過紀錄所學的知識來回饋於社群上,互相學習分享。個人介紹參考 https://portaly.cc/kurohuang

Kuro Huang
Kuro Huang

Written by Kuro Huang

資安顧問、架構師與水肺潛水員。對教育充滿期待的資安從業者,現任ISC2台北分會理監事, AWS Community Builder。希望對資安社群盡一點心力。期望自己與各位夥伴能有所進步,歡迎喝咖啡聊資安。希望保持著定期參與資安社群活動。個人介紹 https://portaly.cc/kurohuang

No responses yet