SQL Query to get list of S3 buckets exposed to internet

SQL Query to get list of S3 buckets exposed to internet

  1. WITH nested_acl_grants as (
  2.   select
  3.     *,
  4.     contains(
  5.       transform(
  6.         CAST(acl_grants AS ARRAY < JSON >),
  7.         a -> json_extract_scalar(a, '$.Grantee.URI') in (
  8.           'http://acs.amazonaws.com/groups/global/AllUsers',
  9.           'http://acs.amazonaws.com/groups/global/AuthenticatedUsers'
  10.         )
  11.       ),
  12.       true
  13.     ) as acl_flag
  14.   from
  15.     aws_s3_bucket_current
  16. ),
  17. unnested_acl_grants as(
  18.   select
  19.     *,
  20.     CASE WHEN ignore_public_acls = true
  21.     and restrict_public_buckets = true THEN false WHEN (
  22.       restrict_public_buckets = false
  23.       or restrict_public_buckets is null
  24.     )
  25.     and is_bucket_policy_status_public = true THEN true WHEN (
  26.       ignore_public_acls is null
  27.       or ignore_public_acls = false
  28.     )
  29.     and acl_flag = true
  30.     and acl_flag is not null THEN true else false end as public_access
  31.   from
  32.     nested_acl_grants
  33. ),
  34. s3 AS (
  35.   select
  36.     a.region_code AS regionCode,
  37.     a.bucket_name AS bucketName,
  38.     a.customer_id AS customerId,
  39.     a.acl_grants AS aclGrants,
  40.     a.acl_owner_display_name AS aclOwnerDisplayName,
  41.     a.acl_owner_id AS aclOwnerId,
  42.     a.block_public_acls AS blockPublicAcls,
  43.     a.block_public_policy AS blockPublicPolicy,
  44.     a.bucket_accelerate_status AS bucketAccelerateStatus,
  45.     a.bucket_cors_enabled AS bucketCorsEnabled,
  46.     a.bucket_creation_date AS bucketCreationDate,
  47.     a.bucket_logging_enabled AS bucketLoggingEnabled,
  48.     a.bucket_logging_target_bucket AS bucketLoggingTargetBucket,
  49.     a.bucket_logging_target_grants AS bucketLoggingTargetGrants,
  50.     a.bucket_logging_target_prefix AS bucketLoggingTargetPrefix,
  51.     a.bucket_policy_serverside_encryption_enabled AS bucketPolicyServersideEncryptionEnabled,
  52.     a.bucket_public_access_block_enabled AS bucketPublicAccessBlockEnabled,
  53.     a.bucket_region AS bucketRegion,
  54.     a.bucket_request_payer AS bucketRequestPayer,
  55.     a.bucket_ssl_access_only AS bucketSslAccessOnly,
  56.     a.bucket_versioning_status_enabled AS bucketVersioningStatusEnabled,
  57.     a.cors_rules AS corsRules,
  58.     a.ignore_public_acls AS ignorePublicAcls,
  59.     a.is_bucket_policy_status_public AS isBucketPolicyStatusPublic,
  60.     a.mfa_delete_status_enabled AS mfaDeleteStatusEnabled,
  61.     a.metrics_configuration_list AS metricsConfigurationList,
  62.     a.notification_config_event_bridge_config AS notificationConfigEventBridgeConfig,
  63.     a.notification_config_lambda_function_configs AS notificationConfigLambdaFunctionConfigs,
  64.     a.notification_config_queue_configs AS notificationConfigQueueConfigs,
  65.     a.notification_config_topic_configs AS notificationConfigTopicConfigs,
  66.     a.object_lock_configuration_rule AS objectLockConfigurationRule,
  67.     a.object_lock_enabled AS objectLockEnabled,
  68.     a.object_ownership_control_rules AS objectOwnershipControlRules,
  69.     a.public_access_block_enabled AS publicAccessBlockEnabled,
  70.     a.restrict_public_buckets AS restrictPublicBuckets,
  71.     a.server_side_encryption_rules AS serverSideEncryptionRules,
  72.     a.serverside_encryption_enabled AS serversideEncryptionEnabled,
  73.     a.tags AS tags,
  74.     a.website_enabled AS websiteEnabled,
  75.     a.account_id AS accountId,
  76.     a.region AS region,
  77.     b.public_access AS isBucketPublic
  78.   FROM
  79.     aws_s3_bucket_current a
  80.     INNER JOIN unnested_acl_grants b ON a.bucket_name = b.bucket_name
  81. )
  82. SELECT
  83.     *
  84. FROM
  85.     s3
  86. WHERE
  87.   
  88.   isBucketPublic=true