<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Karol Havrillay</title>
    <description>The latest articles on Forem by Karol Havrillay (@chkso).</description>
    <link>https://forem.com/chkso</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F847041%2Fe1532461-0ab5-4a4c-a319-f1f071cf0d5e.png</url>
      <title>Forem: Karol Havrillay</title>
      <link>https://forem.com/chkso</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/chkso"/>
    <language>en</language>
    <item>
      <title>How Helpful and Secure can AI be? A Little Comparison of ChatGPT and Amazon Q Developer</title>
      <dc:creator>Karol Havrillay</dc:creator>
      <pubDate>Mon, 31 Mar 2025 20:16:18 +0000</pubDate>
      <link>https://forem.com/chkso/how-helpful-and-secure-can-ai-be-a-little-comparison-of-chatgpt-and-amazon-q-developer-1fao</link>
      <guid>https://forem.com/chkso/how-helpful-and-secure-can-ai-be-a-little-comparison-of-chatgpt-and-amazon-q-developer-1fao</guid>
      <description>&lt;p&gt;Every now and then there are these small tasks that end up on my table that are too tedious to write from scratch but there is too much overhead to delegate. So naturally I ask AI for help and hope to get it done in a manner of minutes instead of manually writing the required code. Since I mostly do infra stuff, the code in this case is Terraform. &lt;/p&gt;

&lt;p&gt;The task is quite simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create terraform code with an IAM user with permissions to upload to an S3 bucket. Create an S3 bucket using KMS encryption and the corresponding KMS Key.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  ChatGPT Response and Analysis
&lt;/h2&gt;

&lt;p&gt;Since its launch some time ago, ChatGPT earned its place on the bookmark bar in my browser. Just thanks to muscle memory it tends to be the tool of choice for similar tasks. And mostly it also does the job right, however this time I was a bit disappointed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider "aws" {
  region = "us-east-1"  # Specify the AWS region of your choice
}

# Create an S3 bucket with KMS encryption
resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-bucket-with-kms-encryption"  # Change to your desired bucket name

  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm     = "aws:kms"
        kms_master_key_id = aws_kms_key.s3_bucket_key.arn
      }
    }
  }
}

# Create the KMS key for S3 bucket encryption
resource "aws_kms_key" "s3_bucket_key" {
  description             = "KMS key for S3 bucket encryption"
  deletion_window_in_days = 30
  enable_key_rotation     = true

  policy = &amp;lt;&amp;lt;EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Enable IAM User Permissions",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "kms:*",
      "Resource": "*"
    },
    {
      "Sid": "Allow S3 to use the key",
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": [
        "kms:Encrypt",
        "kms:Decrypt",
        "kms:ReEncrypt*",
        "kms:GenerateDataKey*",
        "kms:DescribeKey"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "kms:EncryptionContext:aws:s3:arn": "${aws_s3_bucket.example_bucket.arn}"
        }
      }
    }
  ]
}
EOF
}

# Create an IAM user without console or programmatic access
resource "aws_iam_user" "upload_user" {
  name = "s3-upload-user"
}

# Attach a policy to allow the IAM user to upload to the S3 bucket
resource "aws_iam_policy" "s3_upload_policy" {
  name        = "S3UploadPolicy"
  description = "Policy for uploading objects to the S3 bucket"

  policy = jsonencode({
    Version = "2012-10-17",
    Statement = [
      {
        Effect = "Allow",
        Action = [
          "s3:PutObject",
          "s3:PutObjectAcl"
        ],
        Resource = "${aws_s3_bucket.example_bucket.arn}/*"
      }
    ]
  })
}

# Attach the policy to the IAM user
resource "aws_iam_user_policy_attachment" "upload_policy_attachment" {
  user       = aws_iam_user.upload_user.name
  policy_arn = aws_iam_policy.s3_upload_policy.arn
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dissect this step by step. &lt;/p&gt;

&lt;h4&gt;
  
  
  Deprecated Code
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;aws_s3_bucket_server_side_encryption_configuration&lt;/code&gt; argument in the s3 bucket resource definition is deprecated and should be replaced by a &lt;a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration" rel="noopener noreferrer"&gt;separate resource.&lt;/a&gt;. Doesn't break anything, perhaps the model in the backend is outdated, but nothing major. &lt;/p&gt;

&lt;h4&gt;
  
  
  Potential Ransomware Attack
&lt;/h4&gt;

&lt;p&gt;Here comes the interesting part. The actual KMS key policy and its statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
      "Sid": "Enable IAM User Permissions",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "kms:*",
      "Resource": "*"
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you ChatGPT, you've just enabled &lt;strong&gt;any&lt;/strong&gt; principal in &lt;strong&gt;any&lt;/strong&gt; AWS account to do &lt;strong&gt;anything&lt;/strong&gt; with my KMS key, including updating its key policy. That could lead to a potential ransomware attack where the attacker could make the key defunct and allow only himself to fix the key policy and thus release your data. AWS Support might be of help here too, but not everyone is aware of this option.&lt;/p&gt;

&lt;h4&gt;
  
  
  Wrong and Unnecessary Statement in the Key Policy
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
      "Sid": "Allow S3 to use the key",
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": [
        "kms:Encrypt",
        "kms:Decrypt",
        "kms:ReEncrypt*",
        "kms:GenerateDataKey*",
        "kms:DescribeKey"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "kms:EncryptionContext:aws:s3:arn": "${aws_s3_bucket.example_bucket.arn}"
        }
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not wrong per se, it just doesn't work like this. What ChatGPT wanted to achieve is to allow the key to be used only within a certain &lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingKMSEncryption.html#encryption-context" rel="noopener noreferrer"&gt;encryption context&lt;/a&gt;, meaning the KMS key can only be used for the given S3 bucket. This wasn't requested in the prompt. Furthermore, the S3 service itself doesn't really need the given permissions, the principal (e.g. an IAM user) does. Perhaps what ChatGPT wanted to do was to use the &lt;a href="https://docs.aws.amazon.com/kms/latest/developerguide/conditions-kms.html#conditions-kms-via-service" rel="noopener noreferrer"&gt;kms:ViaService&lt;/a&gt; condition to allow usage of the key only when when S3 in involved.&lt;/p&gt;

&lt;h4&gt;
  
  
  Insufficient Permissions
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      {
        Effect = "Allow",
        Action = [
          "s3:PutObject",
          "s3:PutObjectAcl"
        ],
        Resource = "${aws_s3_bucket.example_bucket.arn}/*"
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, the permissions given to the user are not sufficient to successfully upload files to the S3 bucket because there are no explicit &lt;code&gt;Allow&lt;/code&gt;s in the user's IAM policy or in the KMS key policy.&lt;/p&gt;

&lt;h4&gt;
  
  
  Result
&lt;/h4&gt;

&lt;p&gt;Result? Not only the code doesn't do what it's supposed to, it also opens the door into my AWS account and possibly exposes my data to attackers. There are some safety measures to alert you about this even if such code gets deployed, such as &lt;a href="https://dev.to/chkso/how-to-improve-your-security-posture-in-just-a-few-clicks-with-aws-iam-access-analyzer-4io8"&gt;AWS IAM Access Analyzer&lt;/a&gt;, provided it's configured correctly and the alerting works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Amazon Q Developer Response and Analysis
&lt;/h2&gt;

&lt;p&gt;The installation of Amazon Q Developer into your IDE is ridiculously quick and easy and requires just a &lt;a href="https://profile.aws.amazon.com/" rel="noopener noreferrer"&gt;Builder ID&lt;/a&gt;. Installing the &lt;a href="https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode" rel="noopener noreferrer"&gt;Amazon Q Developer VS Code Extension&lt;/a&gt; and logging in with the Builder ID took less than 5 minutes. Upon clicking on the Q logo in VS Code a chat window opens and lets you input your prompts. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xf8d3kt5izkuya0u1u2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xf8d3kt5izkuya0u1u2.png" alt="Amazon Q VS Code first steps" width="800" height="940"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's check the code provided by the Amazon Q Developer.&lt;br&gt;
Compared to ChatGPT, Q spat out a clean code with no deprecated parameters and included a few interesting pieces where it's obvious that it follows the AWS best practices also in terms of security. &lt;/p&gt;

&lt;p&gt;To improve readability I'm not including the code provided by the Q Developer, just the interesting bits.&lt;/p&gt;
&lt;h4&gt;
  
  
  Versioning Enabled
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/versioning-workflows.html" rel="noopener noreferrer"&gt;Versioning&lt;/a&gt; is enabled on the S3 bucket allowing you to restore accidentally deleted or overwritten data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_s3_bucket_versioning" "bucket_versioning" {
  bucket = aws_s3_bucket.encrypted_bucket.id
  versioning_configuration {
    status = "Enabled"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Public Access Blocked Preemptively
&lt;/h4&gt;

&lt;p&gt;Public access is explicitly blocked on the bucket preventing you from accidentally exposing your data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_s3_bucket_public_access_block" "bucket_public_access_block" {
  bucket = aws_s3_bucket.encrypted_bucket.id

  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Working KMS and IAM Policies
&lt;/h4&gt;

&lt;p&gt;Using a &lt;a href="https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html" rel="noopener noreferrer"&gt;default KMS key policy&lt;/a&gt; allows usage of the key only within the AWS account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      {
        Sid    = "Enable IAM User Permissions"
        Effect = "Allow"
        Principal = {
          AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
        }
        Action   = "kms:*"
        Resource = "*"
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The IAM permissions for the user allow him to encrypt uploaded data with the KMS key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      {
        Sid    = "AllowKMSAccess"
        Effect = "Allow"
        Action = [
          "kms:Decrypt",
          "kms:GenerateDataKey"
        ]
        Resource = [
          aws_kms_key.s3_key.arn
        ]
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Result
&lt;/h3&gt;

&lt;p&gt;I'm quite happy with the results and especially with the nice to have features and quirks such as public access block which is not really necessary at this stage but is a good safeguard measure if the bucket policy gets updated later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Humans are creatures of habit. Sometimes it's a good idea to reevaluate your habits and try something new especially in the fast-paced industry that IT is. Amazon Q Developer convinced me of it. An even more important message: make sure you understand &lt;strong&gt;every single line of code&lt;/strong&gt; before deploying. &lt;/p&gt;

</description>
      <category>aws</category>
      <category>amazonqdeveloper</category>
      <category>cloudsecurity</category>
      <category>ai</category>
    </item>
    <item>
      <title>How (not) to Expose Your Password in Plain Text When Using AWS Transfer Family SFTP</title>
      <dc:creator>Karol Havrillay</dc:creator>
      <pubDate>Sun, 16 Mar 2025 22:49:30 +0000</pubDate>
      <link>https://forem.com/chkso/how-not-to-expose-your-entra-id-password-in-plain-text-when-using-aws-transfer-family-sftp-3d9p</link>
      <guid>https://forem.com/chkso/how-not-to-expose-your-entra-id-password-in-plain-text-when-using-aws-transfer-family-sftp-3d9p</guid>
      <description>&lt;p&gt;Recently I was approached by one of my customers who wanted a storage service in AWS and wanted to use their existing corporate Entra ID credentials for authentication. &lt;/p&gt;

&lt;p&gt;Looking at the possible options to achieve this and &lt;a href="https://aws.amazon.com/blogs/storage/authenticating-to-aws-transfer-family-with-azure-active-directory-and-aws-lambda/" rel="noopener noreferrer"&gt;available guidance from AWS&lt;/a&gt;, the option with the custom Lambda IDP seemed to be a quick and easy way forward. &lt;/p&gt;

&lt;p&gt;(Please note &lt;a href="https://docs.aws.amazon.com/transfer/latest/userguide/azure-sftp.html" rel="noopener noreferrer"&gt;it is now possible&lt;/a&gt; to use Entra ID Domain Services for such use case but requires more effort to setup and doesn't illustrate the purpose of this article.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution at a Glance
&lt;/h2&gt;

&lt;p&gt;So how does a Lambda function as a custom IDP work in a high level? As depicted in the diagram below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First a user connects to the Transfer Family SFTP server. The server prompts for the user's username and password. The user types in their Entra ID credentials. &lt;/li&gt;
&lt;li&gt;The credentials  are then passed to the Lambda function that validates the credentials by contacting the Entra ID public API URL and using the supplied credentials to log in. &lt;/li&gt;
&lt;li&gt;If the login is successful, the Lambda returns an IAM role and a session policy to the SFTP server which enables the user to interact with the SFTP server (e.g. write files).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2lkio2tnahmbmqqcmjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2lkio2tnahmbmqqcmjn.png" alt="Architecture" width="692" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far so good, the workflow works as expected, the Lambda function even logs some part of the payload to CloudWatch for monitoring or debugging purposes. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd75qpti7ap91vxg1pbah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd75qpti7ap91vxg1pbah.png" alt="Logging Enabled" width="692" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An example output from logs can look as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbge8f5z5skwjiidixj0w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbge8f5z5skwjiidixj0w.png" alt="Sample Logs" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Opportunity Makes the Thief
&lt;/h2&gt;

&lt;p&gt;From the functional perspective there is nothing wrong with the setup above, the users are happy and can upload data to the SFTP server and enjoy the benefits of the highly durable S3 storage in the background. &lt;/p&gt;

&lt;p&gt;However let's assume that the AWS account where the SFTP server is running is used by multiple users and the Security team didn't spend too much time with IAM management, let alone enforce the principle of least privilege. So such a user with over-permissive privileges might go unnoticed for quite some time. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1yw42viqkzq88pzcfax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1yw42viqkzq88pzcfax.png" alt="Over-privileged User" width="692" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say this user is a developer or a DevOps engineer so he/she has some pretty common permissions, e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DevAccess",
            "Effect": "Allow",
            "Action": [
                "lambda:*",
                "dynamodb:*",
                "sqs:*",
                "sns:*"
                ],
            "Resource": "*"
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And furthermore, the user has the ReadOnlyAccess AWS managed job policy attached:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arn:aws:iam::aws:policy/ReadOnlyAccess
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this combination of access the user can browse through the source code of the IDP Lambda function and not only that. The user can also inject malicious code into the Lambda function and thus exfiltrate sensitive data into CloudWatch. It can go from this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnl21p3o3vhhzycxz4ika.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnl21p3o3vhhzycxz4ika.png" alt="Original source code" width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwwzc8fpxachqeltue1ri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwwzc8fpxachqeltue1ri.png" alt="Malicious source code" width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon looking into the corresponding CloudWatch log group, indeed the adversary (or just an overprivileged user) can indeed read passwords in &lt;strong&gt;plain text&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwjlvjc0mez9hljyotxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwjlvjc0mez9hljyotxf.png" alt="Plain text password in CloudWatch" width="800" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mitigation
&lt;/h2&gt;

&lt;p&gt;So, how to fix this? As stated in the beginning there are now better ways how to integrate the SFTP server with e.g. Entra ID or Entra ID Domain Services. &lt;br&gt;
However, if you end up in a situation where you need to use an IDP Lambda function such as above, following the below best practices should considerably improve the security posture of your environment. &lt;/p&gt;

&lt;h4&gt;
  
  
  1. Employ a multi-account strategy
&lt;/h4&gt;

&lt;p&gt;Isolate your workloads by deploying them into &lt;a href="https://docs.aws.amazon.com/whitepapers/latest/organizing-your-aws-environment/organizing-your-aws-environment.html" rel="noopener noreferrer"&gt;multiple AWS accounts&lt;/a&gt;. A rule of thumb is one account per application/workload per SDLC stage plus other AWS accounts for specific use cases. This limits the blast radius and makes sure the AWS accounts can't "talk" to each other unless you explicitly allow them to do so on either IAM (cross-account IAM roles) or networking (VPC Peering, Transit Gateway) layer. A good way to start with this approach is using AWS Control Tower which sets up the whole landing zone for you and allows you to enforce various controls and guardrails across AWS accounts.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Establish a proper IAM management
&lt;/h4&gt;

&lt;p&gt;Ideally manage the identities at one place (e.g. corporate Entra ID / Active Directory) and &lt;a href="https://aws.amazon.com/iam/identity-center/" rel="noopener noreferrer"&gt;federate with AWS Identity Center&lt;/a&gt;. This allows users to use their corporate credentials to access AWS, you don't need to duplicate your organisational structure in the cloud and allows you to make use of concepts such as ABAC and RBAC. &lt;br&gt;
The above approach provides an additional benefit where you get rid of any static long-lived credentials or forgotten over-privileges IAM users. &lt;/p&gt;

&lt;h4&gt;
  
  
  3. Enforce least privilege
&lt;/h4&gt;

&lt;p&gt;Use services such as AWS IAM Access Analyzer to help you fine tune the permissions of your IAM entities &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-policy-generation.html" rel="noopener noreferrer"&gt;based on their historical usage patterns&lt;/a&gt;. Or &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/what-is-access-analyzer.html#what-is-access-analyzer-unused-access-analysis" rel="noopener noreferrer"&gt;find excessive permissions which were not used&lt;/a&gt; in a given time frame. &lt;/p&gt;

&lt;h4&gt;
  
  
  4. Encrypt, encrypt, encrypt
&lt;/h4&gt;

&lt;p&gt;The password exfiltration scenario above wouldn't be successful if the CloudWatch log group was encrypted by a KMS key with a proper KMS policy. This is a quick win and a recommended approach for any use case.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
    </item>
    <item>
      <title>How Can AWS IAM Access Analyzer Help You Improve Security Checks in Your CI/CD Pipeline?</title>
      <dc:creator>Karol Havrillay</dc:creator>
      <pubDate>Sun, 31 Mar 2024 17:30:40 +0000</pubDate>
      <link>https://forem.com/chkso/how-can-aws-iam-access-analyzer-help-you-improve-security-checks-in-your-cicd-pipeline-50hc</link>
      <guid>https://forem.com/chkso/how-can-aws-iam-access-analyzer-help-you-improve-security-checks-in-your-cicd-pipeline-50hc</guid>
      <description>&lt;p&gt;Recently I was working on improving a client's CI/CD Pipeline security and one of the aspects I wanted to explore was how to use AWS native tools to identify over-permissive IAM policies or detect privilege escalation possibilities. In this article I would like to have a look at what the AWS IAM Access Analyzer has to offer with its policy validation feature. Apart from checking policy grammar, it can also raise a security finding if the "&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-policy-validation.html"&gt;policy provides access that AWS considers a security risk because the access is overly permissive&lt;/a&gt;". Or can it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Get the Basics Right
&lt;/h2&gt;

&lt;p&gt;Privilege escalation is the act of exploiting a flaw to gain elevated access to resources that normally shouldn't be accessible to you. In AWS it is done by (enumerating and) abusing permissions assigned to your principal or to the principal the attacker gained access to. The permissions are assigned to principals using IAM policies. In turn, the policies are made of one or more statements, each statement containing a tuple of action, resource, effect and optionally a condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does Privilege Escalation Work?
&lt;/h2&gt;

&lt;p&gt;In terms of privilege escalation, it boils down to the individual IAM actions in the statement or their combination. At the time of writing the article, AWS provided &lt;a href="https://www.awsiamactions.io/"&gt;whopping 16458&lt;/a&gt; individual IAM actions. The most critical area are the IAM actions related to the IAM service itself. Filtering only IAM actions, we still get &lt;a href="https://www.awsiamactions.io/?a=iam%3A"&gt;171&lt;/a&gt; actions that could be potentially dangerous. Obviously we can't deny all those IAM actions for everyone in our AWS account or AWS Organization e.g. by a Service Control Policy as that would render the AWS pretty unusable. For example you wouldn't be able to assign an EC2 instance an IAM role allowing it to put items in DynamoDB or read data from an S3 bucket or make any other services talk to each other. &lt;/p&gt;

&lt;p&gt;So, which actions really pose a threat? A great security researcher &lt;a href="https://frichetten.com/"&gt;Nick Frichette&lt;/a&gt; (worth following on social networks) and his project &lt;a href="https://hackingthe.cloud"&gt;https://hackingthe.cloud&lt;/a&gt; identified &lt;a href="https://hackingthe.cloud/aws/exploitation/iam_privilege_escalation/"&gt;37 ways&lt;/a&gt; how an IAM policy can be abused to gain more privileges. Some of them are quite surprising, e.g. DetachRolePolicy which sounds counterintuitive, but if the policy being detached contains a Deny statement, then detaching it can in fact allow you to perform more actions which is considered privilege escalation.&lt;/p&gt;

&lt;p&gt;Looking at the checks performed by the IAM Access Analyzer, we can see some resemblance with the list at &lt;a href="https://hackingthe.cloud"&gt;https://hackingthe.cloud&lt;/a&gt;, however both lists look at the problem from a slightly different perspective. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhj7xxk3ook9cupnzw7nw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhj7xxk3ook9cupnzw7nw.png" alt="Available Checks" width="800" height="1104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whereas the Hacking The Cloud approach leans more towards auditing and penetration testing by looking for real exploitation possibilities thanks to a combination of permissions for multiple AWS services, the Access Analyzer looks more at prevention and mostly misuse of the &lt;code&gt;iam:PassRole&lt;/code&gt; permission. A good explanation of why the iam:PassRole permission can be dangerous is explained in detail for example &lt;a href="https://ermetic.com/blog/aws/auditing-passrole-a-problematic-privilege-escalation-permission/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Can AWS IAM Access Analyzer Help?
&lt;/h2&gt;

&lt;p&gt;Based on the observations above, the best place for such checks would be a CI/CD pipeline where the Access Analyzer can check IAM policies before they are deployed to AWS and identify possible security issues. Usually, the IAM policies are part of Infrastructure as Code templates such as Cloud Formation or Terraform. That's where the first problem arises. The input for the &lt;code&gt;aws iam accessaanalyzer validate&lt;/code&gt; command is NOT a CloudFormation template or a Terraform plan file, but a JSON document containing the policy. It looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws accessanalyzer validate-policy
    --policy-document file://myfile.json
    --policy-type IDENTITY_POLICY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second problem is that CloudFormation templates and the IAM policies in the templates more often than not contain also intrinsic functions, such &lt;code&gt;Fn::Join&lt;/code&gt; or &lt;code&gt;Fn::Sub&lt;/code&gt; to join or substitute values which might not be known by simply parsing the template as a JSON or YAML file. This might not be the case though in case of scanning Terraform plan files where the variables are already replaced with the respective values. &lt;br&gt;
So, how to deal with a template looking like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AWSTemplateFormatVersion: '2010-09-09'
Resources:
  BobUser:
    Type: AWS::IAM::User
    Properties:
      UserName: Bob

  BobPolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: BobEC2Policy
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - "ec2:RunInstances"
              - !Join
                - ""
                - - "iam:"
                  - "PassRole"
            Resource: "*"
      Users:
        - Ref: BobUser

  BobPolicyAttachment:
    Type: AWS::IAM::UserPolicyAttachment
    Properties:
      UserName: !Ref BobUser
      PolicyArn: !Ref BobPolicy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The critical &lt;code&gt;iam:PassRole&lt;/code&gt; action is constructed here by joining the two parts of the action together using the &lt;code&gt;Fn::Join&lt;/code&gt; intrinsic function. &lt;/p&gt;

&lt;p&gt;Luckily, AWS provides a solution called &lt;a href="https://github.com/awslabs/aws-cloudformation-iam-policy-validator"&gt;cfn-policy-validator&lt;/a&gt; which can parse CloudFormation templates and uses IAM Access Analyzer in the background. (There is also &lt;a href="https://github.com/awslabs/terraform-iam-policy-validator"&gt;a version of the tool for Terraform&lt;/a&gt;). It can be installed by simply running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install cfn-policy-validator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the actual check of a CloudFormation template is performed by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cfn-policy-validator validate --template-path DangerousTemplate.yaml --region eu-central-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The command requires valid AWS credentials with permissions to run AWS IAM Access Analyzer checks.&lt;/p&gt;

&lt;p&gt;Running it against the template above, we get a JSON document as a result correctly identifying the dangerous action &lt;code&gt;iam:PassRole&lt;/code&gt; with &lt;code&gt;*&lt;/code&gt; as a Resource, meaning the user Bob could pass any IAM role and possibly escalate his privileges.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "BlockingFindings": [
        {
            "findingType": "SECURITY_WARNING",
            "code": "PASS_ROLE_WITH_STAR_IN_RESOURCE",
            "message": "Using the iam:PassRole action with wildcards (*) in the resource can be overly permissive because it allows iam:PassRole permissions on multiple resources. We recommend that you specify resource ARNs or add the iam:PassedToService condition key to your statement.",
            "resourceName": "Bob",
            "policyName": "BobEC2Policy",
            "details": {
                "findingDetails": "Using the iam:PassRole action with wildcards (*) in the resource can be overly permissive because it allows iam:PassRole permissions on multiple resources. We recommend that you specify resource ARNs or add the iam:PassedToService condition key to your statement.",
                "findingType": "SECURITY_WARNING",
                "issueCode": "PASS_ROLE_WITH_STAR_IN_RESOURCE",
                "learnMoreLink": "https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-reference-policy-checks.html#access-analyzer-reference-policy-checks-security-warning-pass-role-with-star-in-resource",
                "locations": [
                    {
                        "path": [
                            {
                                "value": "Statement"
                            },
                            {
                                "index": 0
                            },
                            {
                                "value": "Action"
                            },
                            {
                                "index": 1
                            }
                        ],
                        "span": {
                            "start": {
                                "line": 1,
                                "column": 91,
                                "offset": 91
                            },
                            "end": {
                                "line": 1,
                                "column": 105,
                                "offset": 105
                            }
                        }
                    },
                    {
                        "path": [
                            {
                                "value": "Statement"
                            },
                            {
                                "index": 0
                            },
                            {
                                "value": "Resource"
                            }
                        ],
                        "span": {
                            "start": {
                                "line": 1,
                                "column": 120,
                                "offset": 120
                            },
                            "end": {
                                "line": 1,
                                "column": 123,
                                "offset": 123
                            }
                        }
                    }
                ]
            }
        }
    ],
    "NonBlockingFindings": []
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checking the return code by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we get a non-zero code, 2 in this case. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The Validate feature of AWS IAM Access Analyzer is certainly not an almighty tool but I think it has its place in the CI/CD tooling and beside security findings it also provides other warnings and suggestions possibly pointing out an unintentional mistake in IAM policies. Besides IAM policies it also supports resource policies and service control policies. &lt;br&gt;
The checks are free of charge and together with the cfn-policy-validator it is surprisingly easy to integrate into CI/CD pipelines and provides an easy-to-parse output and decision-making. Furthermore, it has further capabilities how to treat findings, so you can treat certain findings or resources as non-blocking. Overall, it seems to be a versatile tool and was worth checking. &lt;/p&gt;

&lt;p&gt;How about privilege escalation though? Besides detecting some &lt;code&gt;iam:PassRole&lt;/code&gt; related dangerous policies, it doesn't provide much in this area, but then again it's not the main purpose of the service. When it comes to detecting overly permissive statements as stated in the feature description of the feature, the tool does the job just fine.&lt;br&gt;
If you are interested in finding or exploiting the privilege escalation possibilities, there are multiple great write-ups and tools, such as: &lt;a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/"&gt;https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/&lt;/a&gt; and their pentesting tool &lt;a href="https://rhinosecuritylabs.com/aws/pacu-open-source-aws-exploitation-framework/"&gt;pacu&lt;/a&gt;, or &lt;a href="https://bishopfox.com/blog/privilege-escalation-in-aws"&gt;https://bishopfox.com/blog/privilege-escalation-in-aws&lt;/a&gt; and their tool &lt;a href="https://github.com/BishopFox/iam-vulnerable"&gt;IAM Vulnerable&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>cicd</category>
      <category>privesc</category>
    </item>
    <item>
      <title>How to Improve Your Security Posture in Just a Few Clicks with AWS IAM Access Analyzer</title>
      <dc:creator>Karol Havrillay</dc:creator>
      <pubDate>Thu, 29 Feb 2024 22:48:14 +0000</pubDate>
      <link>https://forem.com/chkso/how-to-improve-your-security-posture-in-just-a-few-clicks-with-aws-iam-access-analyzer-4io8</link>
      <guid>https://forem.com/chkso/how-to-improve-your-security-posture-in-just-a-few-clicks-with-aws-iam-access-analyzer-4io8</guid>
      <description>&lt;p&gt;When talking about security on AWS, the first services that pop up are usually Security Hub, Cloud Trail, Guard Duty and so on. They are all important and have their place in your AWS environment, however there is one service which is often overlooked but it can provide a lot of value in almost no time. Yes, it is the &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/what-is-access-analyzer.html" rel="noopener noreferrer"&gt;AWS IAM Access Analyzer&lt;/a&gt; and its capability that finds AWS resources with external access. The best part is, this feature of AWS IAM Access Analyzer is free of charge!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics
&lt;/h2&gt;

&lt;p&gt;What exactly is meant by external access? It means that an entity (including unauthenticated anonymous users) outside of your zone of trust can access your resource, e.g. read files in your S3 bucket or access your EBS volumes snapshots. Under the hood, AWS IAM Access Analyzer uses automated reasoning powered by &lt;a href="https://aws.amazon.com/blogs/security/protect-sensitive-data-in-the-cloud-with-automated-reasoning-zelkova/" rel="noopener noreferrer"&gt;Zelkova&lt;/a&gt; to analyze a policy (e.g. an IAM policy or a bucket policy) and provide a definitive answer whether it allows (unintended) access to the resource from outside of your zone of trust. &lt;/p&gt;

&lt;p&gt;The zone of trust can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a single AWS Account&lt;/li&gt;
&lt;li&gt;the whole AWS Organization if you setup an organizational Analyzer from the management AWS account or from a delegated admin account.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important to note is that unlike IAM, the Access Analyzer is a regional service so it needs to be enabled in each region you are actively using. &lt;/p&gt;

&lt;h2&gt;
  
  
  Which Resources Are Supported?
&lt;/h2&gt;

&lt;p&gt;Since the time I wrote &lt;a href="https://dev.to/chkso/aws-iam-access-analyzer-demystified-578i"&gt;my first article&lt;/a&gt; on this topic the number of supported resources types has more than doubled. &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/what-is-access-analyzer.html#what-is-access-analyzer-resource-identification" rel="noopener noreferrer"&gt;The supported resources&lt;/a&gt; include those resources which you can make public by either attaching a resource policy, e.g. Secrets Manager, SQS Queue; create a grant, e.g. KMS key; or make them public explicitly, e.g. RDS Snapshots, EBS volumes snapshots.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does It Work?
&lt;/h2&gt;

&lt;p&gt;If you are working in a multi-account setup which should be the case if you run more than one workload in more than one SDLC stage, it is a best practice to use &lt;a href="https://aws.amazon.com/organizations/" rel="noopener noreferrer"&gt;AWS Organizations&lt;/a&gt; to govern and manage your AWS accounts. Going further into the best practices, it is a a recommendation to have a &lt;a href="https://docs.aws.amazon.com/whitepapers/latest/organizing-your-aws-environment/security-ou-and-accounts.html#security-tooling-accounts" rel="noopener noreferrer"&gt;separate Security or Audit AWS Account&lt;/a&gt; to manage your security services on the organizational scale. &lt;br&gt;
In that case, you need to first log in to your Administrative account and navigate to &lt;a href="https://eu-central-1.console.aws.amazon.com/access-analyzer/home?region=eu-central-1#/" rel="noopener noreferrer"&gt;AWS IAM Access Analyzer in the web UI&lt;/a&gt; and select &lt;a href="https://eu-central-1.console.aws.amazon.com/access-analyzer/home?region=eu-central-1#/settings" rel="noopener noreferrer"&gt;Analyzer Settings&lt;/a&gt; under the Access Analyzer section. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3cwyelqhgu4ky3gev7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3cwyelqhgu4ky3gev7x.png" alt="IAM Access Analyzer Settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking on the Add delegated administrator will allow you to specify the account ID of your Security account which you will use to manage the Access Analyzer. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65ctfq5n35kp4x1tsf4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65ctfq5n35kp4x1tsf4s.png" alt="Adding a delegated administrator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, you need to login to the Security or Audit account which you specified in the previous step as the Delegated administrator account and navigate to the IAM Access Analyzer section and hit the Create analyzer button. It is important to select the right type of Analyzer (external access), give it a meaningful name and define the scope or the zone of trust. Since we are in a multi-account setup we are going for the Organization as the zone of trust. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwi2gj32tsgv8yv24i1gb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwi2gj32tsgv8yv24i1gb.png" alt="Access Analyzer settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voilà, your Access Analyzer is up and running. It will take some time to populate the findings, but you just made your AWS environment much safer with almost zero effort. &lt;/p&gt;

&lt;h2&gt;
  
  
  Working with the Findings
&lt;/h2&gt;

&lt;p&gt;After observing the findings in an environment using &lt;a href="https://aws.amazon.com/iam/identity-center/" rel="noopener noreferrer"&gt;AWS IAM Identity Center (formerly AWS SSO)&lt;/a&gt; to manage identities and access, we can see that we have a lot of findings related to the IAM Identity Center roles and the SAML provider which the IAM IC creates in each account. The Access analyzer considers these SAML providers external to the Organization because theoretically you could federate with Identity Providers outside of your Organization or AWS. &lt;/p&gt;

&lt;p&gt;So, the easy way to get rid of the findings would be to just select all findings and archive them. However, if you create another AWS account in your enviornment, the IAM IC will again create a SAML provider in it together with multiple AWS roles. So the long term solution is to create archive rules. &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Archive Rules
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Archive AWS IAM IC findings
&lt;/h4&gt;

&lt;p&gt;By navigating to Analyzer Settings, selecting the Analyzer, and hitting the Create archive rule, we can create Archive rules. They are applied automatically when new findings are created and can help us, if crafted properly, eliminate false positives. &lt;/p&gt;

&lt;p&gt;So, to exclude (or auto-archive) the IAM roles related to AWS IAM Identity Center, the rule could look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvejvf8kcibtqba7vpnt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvejvf8kcibtqba7vpnt.png" alt="Auto archive AWS SSO Roles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure the rule matches the active findings and click the Create and archive active findings button. &lt;/p&gt;

&lt;h4&gt;
  
  
  Archive IAM Roles in Non-primary Regions
&lt;/h4&gt;

&lt;p&gt;If you are actively using multiple regions, you will see findings for IAM roles in each region since IAM roles are global resources. To prevent these duplicates, you should pick a primary region and create an archive rules in all other regions to auto archive all IAM Roles findings, i.e. Resource Type is IAM::Role. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38gd4tv6xtmv5mp9y0ek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38gd4tv6xtmv5mp9y0ek.png" alt="Auto Archive IAM Roles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Integration with Other Services
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Security Hub
&lt;/h3&gt;

&lt;p&gt;If you are using Security Hub to manage your security posture, you will find AWS IAM Access Analyzer findings there automatically. &lt;/p&gt;

&lt;h3&gt;
  
  
  Event Bridge
&lt;/h3&gt;

&lt;p&gt;The other option would be an SNS topic or virtually endless possibilities of integration via the Event Bridge. &lt;br&gt;
The corresponding Event rule would look like this: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
  "source": [
    "aws.access-analyzer"
  ],
  "detail-type": [
    "Access Analyzer Finding"
  ],
  "detail": {
    "status": [
      "ACTIVE"
    ]
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Currently it is not possible to drill down further in the event rule and to specify only External access type of findings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pricing
&lt;/h2&gt;

&lt;p&gt;The external access feature of Access Analyzer that detects resources with external access is free of charge!&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips and Recommendations
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Always create an organizational Analyzer if you run more than one AWS account.&lt;/li&gt;
&lt;li&gt;Create or use a delegated admin account for managing the organizational Analyzer to limit the needed operative tasks in the Administrative account. The delegated admin account would be usually your Audit or Security account. &lt;/li&gt;
&lt;li&gt;Don't forget that AWS IAM Access Analyzer is a regional service so enable it in all regions which you are not &lt;a href="https://docs.aws.amazon.com/controltower/latest/userguide/primary-region-deny-policy.html" rel="noopener noreferrer"&gt;actively blocking (e.g. by an SCP)&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;Related to the previous point, if you are using more than one region, pick a primary one where you want the Access Analyzer to scan all supported resources. In all other regions exclude the finding related to IAM roles otherwise you would see duplicit findings for the same roles in more regions. &lt;/li&gt;
&lt;li&gt;General advice, unless you are just evaluating and testing the service in a sandbox environment, always use Infrastructure as Code to set up and manage your AWS services and resources. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>accessanalyzer</category>
    </item>
    <item>
      <title>AWS IAM Access Analyzer Demystified</title>
      <dc:creator>Karol Havrillay</dc:creator>
      <pubDate>Wed, 13 Apr 2022 22:51:36 +0000</pubDate>
      <link>https://forem.com/chkso/aws-iam-access-analyzer-demystified-578i</link>
      <guid>https://forem.com/chkso/aws-iam-access-analyzer-demystified-578i</guid>
      <description>&lt;p&gt;Given how fast AWS release new products and features, it can be quite easy to lose track about what each individual service does or what features it provides. To make the matter even more complicated, AWS also likes to pack a broad range of functionalities under one umbrella. &lt;/p&gt;

&lt;p&gt;Once such example is AWS Systems Manager which consists of services with functionalities ranging from &lt;a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html"&gt;storing configuration data for your applications&lt;/a&gt; to &lt;a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html"&gt;securely accessing your EC2 instances via a shell session&lt;/a&gt; or &lt;a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-patch.html"&gt;patching them at scale&lt;/a&gt; just to name a few.&lt;/p&gt;

&lt;p&gt;Another example is &lt;a href="https://aws.amazon.com/iam/features/analyze-access/"&gt;AWS IAM Access Analyzer&lt;/a&gt; which provides a set of very interesting security related functionalities which do not exactly serve the same purpose, but they are covered by one service. Let’s have a look at them one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who Knows My Secrets?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/about-aws/whats-new/2019/12/introducing-aws-identity-and-access-management-access-analyzer/"&gt;Historically&lt;/a&gt;, this was the first functionality of Access Analyzer. It is backed by mathematical proofs, and it analyzes resource policies on subset of your AWS resources to determine whether they are shared outside of the defined boundaries. Btw, I recommend watching a &lt;a href="https://www.youtube.com/watch?v=i5apYXya2m0"&gt;great video&lt;/a&gt; on the internals of the service from one of the Re:Invents. &lt;/p&gt;

&lt;p&gt;The boundary can be either an AWS account or the whole AWS Organization and the service refers to it as the Zone of trust. In case you define as a boundary the whole AWS Organization, the Access Analyzer needs to be setup in the Management account (previously known as Master) of the Organization or from an account designated by the Management account. &lt;/p&gt;

&lt;p&gt;The list of supported resources is not particularly long but it covers some of the most critical ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IAM roles which can be assumed from outside of your zone of trust&lt;/li&gt;
&lt;li&gt;KMS keys&lt;/li&gt;
&lt;li&gt;Lambda functions and layers&lt;/li&gt;
&lt;li&gt;SQS queues&lt;/li&gt;
&lt;li&gt;Secrets Manager secrets&lt;/li&gt;
&lt;li&gt;S3 buckets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, in case you want to be notified if someone in your AWS account or an Organization opened up a way in through one of these services, this is the feature of AWS IAM Access Analyzer you should definitely enable and configure it accordingly.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VEocA7nb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egfxubpotyfgrotk5pq3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VEocA7nb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egfxubpotyfgrotk5pq3.png" alt="Findings of externally shared resources" width="880" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Is My Policy Going To Work As Intended?
&lt;/h2&gt;

&lt;p&gt;Another AWS IAM Access Analyzer functionality is policy validation. There is also an &lt;a href="https://docs.aws.amazon.com/access-analyzer/latest/APIReference/API_ValidatePolicy.html"&gt;API&lt;/a&gt; for that, but the functionality seems to be mostly useful in the web console when you are drafting or testing your policies before you deploy them finally via Infrastructure as Code, e.g. CloudFormation, Terraform or others. &lt;/p&gt;

&lt;p&gt;In the web console you can see a similar interface as depicted in the picture when working with IAM policies, Service Control Policies and S3 bucket policies. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZO_IKFQG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlwm2ope7uly8trgt4we.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZO_IKFQG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlwm2ope7uly8trgt4we.png" alt="IAM Policy validation pointing to a mismatch between the condition value and the operator used" width="880" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It comes with &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-reference-policy-checks.html"&gt;100+ checks&lt;/a&gt; and will warn you mostly about syntactical errors in your policy e.g., a missing mandatory element. But it will also raise a warning, a security warning or a suggestion e.g., if you use the condition operators incorrectly, or your policy might end up to be way more permissive that you might have intended, especially with the &lt;code&gt;iam:PassRole&lt;/code&gt; action which is commonly used in &lt;a href="https://hackingthe.cloud/aws/exploitation/iam_privilege_escalation/#iampassrole-ec2runinstances"&gt;privilege escalation techniques&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can I Get Fewer Permissions?
&lt;/h2&gt;

&lt;p&gt;This is the latest feature of AWS IAM Access Analyzer, and its purpose is to generate &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege"&gt;least privilege&lt;/a&gt; policies for an IAM principal (a user or a role) based on its previous activity. &lt;/p&gt;

&lt;p&gt;It was introduced approximately one year ago and as it often is the case with AWS, it was more of an &lt;a href="https://en.wikipedia.org/wiki/Minimum_viable_product"&gt;MVP&lt;/a&gt; with limited functionality. It was quite restricted in terms of number of supported services, on source data (you had to have your Cloud Trail in the same account as the IAM user or role for which you wanted to generate policies), and strict quotas in terms of number of generated policies per day. &lt;/p&gt;

&lt;p&gt;Nowadays it seems to be much more mature and returns fine-grained policies for dozens of services and less granular policies for the rest of the &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-policy-generation.html#access-analyzer-policy-generation-service-action"&gt;services&lt;/a&gt;. For the former, it uses CloudTrail and works fine also in enterprise environments with centrally stored CloudTrail logs in a separate account. For the latter, it uses the &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_access-advisor.html"&gt;“last accessed”&lt;/a&gt; information only to find out whether the service has been used or not without returning the individual actions. &lt;/p&gt;

&lt;p&gt;I’m quite sure that over time, the coverage of services where fine-grained policies can be generated will grow significantly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gw042-Mo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxyg7fqxuuw3aqceifci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gw042-Mo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxyg7fqxuuw3aqceifci.png" alt="You are prompted to fill in ARNs of the resources for actions that support resource-level permissions" width="880" height="738"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;AWS IAM Access Analyzer is a great tool, or better said, a great toolset, which can help you in many ways. Whether you are a software developer deploying code on AWS and wanting to check if your policies adhere to best practices, or a security engineer wanting to get an additional view on the security posture of your AWS Organization or if you are part of a central infrastructure or security team creating and finetuning least-privileged IAM roles and policies.&lt;/p&gt;

&lt;p&gt;I’ve spent some deploying and using the functionalities described above and I plan to do a deep dive about some of them in one of my next articles, so stay tuned!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>iam</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
