Cloud

[스터디] S3 개별 권한 제어 및 액세스 분석기 사용

mokpolar 2023. 9. 2. 23:27
반응형

이번에 CloudNeta팀의 AWS Hacking & Security Study 에 참여하게 되었다.

다른 사람들은 회사에서 AWS의 보안을 어떻게 관리하는 지 알고 싶을 때가 있는데 이 스터디를 통해서 알게 되길 바란다.

 

1주차의 내용은 S3 취약점 및 보안이다. 

 

S3 버킷 생성해서 액세스 정보 확인하기

테스트를 위해서 버킷을 생성하고 정보를 확인해보자.

# S3 버킷 생성
NICKNAME=mokpolar
aws s3 mb s3://ahss-$NICKNAME --region us-west-2

# 해당 버킷의 퍼블릭 액세스 확인
aws s3api get-public-access-block --bucket ahss-$NICKNAME

 

보통은 아무 생각없이 콘솔에서 스토리지의 퍼블릭 액세스 차단 설정을 했었는데,

그러지 말아야겠다는 생각이 들었다.

콘솔에서 확인하면 실제로 잘 차단되어 있는 모습을 볼 수 있다.

또한 각각의 항목이 무엇을 말하는지 자세하게 알 수 있다.

# 테스트용 파일을 생성하고 업로드 해주자. 
tourch a b c d
aws s3 sync . s3://ahss-$NICKNAME

upload: ./b to s3://ahss-mokpolar/b
upload: ./d to s3://ahss-mokpolar/d
upload: ./c to s3://ahss-mokpolar/c
upload: ./a to s3://ahss-mokpolar/a

그리고 버킷에 올라간 파일들 각각의 정보를 확인할 수 있다.

aws s3api list-objects --bucket ahss-$NICKNAME | jq

{
  "Contents": [
    {
      "Key": "a",
      "LastModified": "2023-09-02T13:29:11+00:00",
      "ETag": "\\"d41d8cd98f00b204e9800998ecf8427e\\"",
      "Size": 0,
      "StorageClass": "STANDARD",
      "Owner": {
        "DisplayName": "*****",
        "ID": "****"
      }
    },
    {
      "Key": "b",
      "LastModified": "2023-09-02T13:29:10+00:00",
      "ETag": "\\"d41d8cd98f00b204e9800998ecf8427e\\"",
      "Size": 0,
      "StorageClass": "STANDARD",
      "Owner": {
        "DisplayName": "*****",
        "ID": "****"
      }
    },
    {
      "Key": "c",
      "LastModified": "2023-09-02T13:29:11+00:00",
      "ETag": "\\"d41d8cd98f00b204e9800998ecf8427e\\"",
      "Size": 0,
      "StorageClass": "STANDARD",
      "Owner": {
        "DisplayName": "*****",
        "ID": "****"
      }
    },
    {
      "Key": "d",
      "LastModified": "2023-09-02T13:29:11+00:00",
      "ETag": "\\"d41d8cd98f00b204e9800998ecf8427e\\"",
      "Size": 0,
      "StorageClass": "STANDARD",
      "Owner": {
        "DisplayName": "*****",
        "ID": "****"
      }
    }
  ]
}

 

권한 없이 액세스할 수 있도록 객체 소유권으로 제어하기

일반적인 권한으로 S3 버킷을 탐색하면 파일들을 볼 수가 있는데,

aws s3 ls s3://ahss-$NICKNAME --human-readable

2023-09-02 22:29:11    0 Bytes a
2023-09-02 22:29:10    0 Bytes b
2023-09-02 22:29:11    0 Bytes c
2023-09-02 22:29:11    0 Bytes d

이 과정에서 테스트용으로 현재 내 aws configure를 통한 인증을 거치지 않으면

Access Denied가 뜨게 된다.

aws s3 ls s3://ahss-$NICKNAME --human-readable --no-sign-request

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied

이제 AWS 콘솔에서 S3 버킷에 대한 권한 변경을 통해서 이 동작이 가능하도록 해보자.

버킷 상단의 탭에서 [권한]을 클릭하면 아래에 객체 소유권 항목이 있다.

그리고 객체 소유권을 편집해주자.

 

그리고 ACL 편집 항목에 들어와서

아래 퍼블릭 액세스에 체크를 할 수 있다.

그러면 굉장히 무서운 경고사항이 아래에 뜬다.

그리고 다시 —no-sign-request 옵션으로 접근하면 확인이 가능하다.

aws s3 ls s3://ahss-$NICKNAME --human-readable --no-sign-request

2023-09-02 22:29:11    0 Bytes a
2023-09-02 22:29:10    0 Bytes b
2023-09-02 22:29:11    0 Bytes c
2023-09-02 22:29:11    0 Bytes d

하지만 새로 파일을 생성해서 복사하면?

실패할 것이다.

위에서 보이는 권한 목록에서는 나열만 가능하기 때문이다.

touch e
aws s3 cp e s3://ahss-$NICKNAME --no-sign-request

upload failed: ./e to s3://ahss-mokpolar/e An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

 

 

인증된 사용자 그룹에 권한주고 액세스 해보기 

이제 퍼블릭 액세스 말고 다른 권한을 편집해보자.

퍼블릭애서의 권한을 지우고, 인증된 사용자 그룹에서의 권한을 추가하자.

“AWS 계정이 있는 모든 사용자” 니까 누구나 가입만 하면 인증된 사용자이다.

그러면 무슨 쓸모가 있지? 라는 의문이 들었다.

이제 테스트를 위해 test-man 이라는 아무 권한없는 깡통 사용자를 하나 만들었다.

나열이 되는지를 확인해보면, 잘 되는 것 같다.

aws s3 ls s3://ahss-$NICKNAME --human-readable --profile test-man

2023-09-02 22:29:11    0 Bytes a
2023-09-02 22:29:10    0 Bytes b
2023-09-02 22:29:11    0 Bytes c
2023-09-02 22:29:11    0 Bytes d

aws s3 ls s3://ahss-$NICKNAME --human-readable --no-sign-request

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied

 

IAM Access Analyzer 액세스 분석기 사용하기

IAM Access Analyzer라는 도구가 있다. 

참고 : 

https://docs.aws.amazon.com/cli/latest/reference/accessanalyzer/

https://aws.amazon.com/ko/blogs/security/how-to-prioritize-iam-access-analyzer-findings/

 

아래와 같은 역할을 하는 것 같다. 

 

Access Analyzer를 생성하자. 

ANA_ARN=$(aws accessanalyzer create-analyzer --analyzer-name firstanalyzer --type ACCOUNT --output text --query arn)
echo $ANA_ARN
arn:aws:access-analyzer:us-west-2:******:analyzer/firstanalyzer

 

스캔을 하면 

aws accessanalyzer start-resource-scan --analyzer-arn $ANA_ARN --resource-arn arn:aws:s3:::ahss-$NICKNAME

 

아래와 같이 분석 결과를 확인할 수 있다. 

aws accessanalyzer get-analyzed-resource --analyzer-arn $ANA_ARN --resource-arn arn:aws:s3:::ahss-$NICKNAME | jq
{
  "resource": {
    "resourceArn": "arn:aws:s3:::ahss-mokpolar",
    "resourceType": "AWS::S3::Bucket",
    "createdAt": "2023-09-02T14:21:48.862000+00:00",
    "analyzedAt": "2023-09-02T14:21:48.862000+00:00",
    "updatedAt": "2023-09-02T14:21:48.862000+00:00",
    "isPublic": true,
    "actions": [
      "s3:GetBucketAcl",
      "s3:ListBucket",
      "s3:ListBucketMultipartUploads",
      "s3:ListBucketVersions"
    ],
    "sharedVia": [
      "BUCKET_ACL"
    ],
    "status": "ACTIVE",
    "resourceOwnerAccount": "****"
  }
}

 

AWS 콘솔에 들어가면 액세스 분석기 항목이 있다. 

들어가보면 방금 생성한 분석기를 확인할 수 있다. 

반응형