brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Aug 28. 2023

37탄-1.  AWS- S3 보안1


목표

S3의 보안 취약점을 실습해보자.

IAM 분석기를 사용해 보자.



<1> 환경 구축

<2> S3 취약점 - S3 접근 통제 3가지

<3>  flaws.cloud  챌린지

<4> 동일 s3 실습환경을 만들어 테스트하자

<5> 분석기 - IAM Access Analyzer

<6> AWS CLI S3 명령 s3 및 s3 api 차이점    

<7> Public Bucket 





<1> 환경 구축 


Cloudformation 2개 실행

명령 EC2 2개를 만든다.  


1

Aws console login  

https://console.aws.amazon.com/console/home



2

Aws 인증 없는 EC2 1개 생성

키페어 1개 필요

Aws s3 ls  


3

Aws 인증받은 EC2 1개 생성

키페어 1개 필요.

Access-key, secret-key  필요.

Aws s3 ls   




<2>  S3 취약점 - S3 접근 통제 3가지 


1

객체  ACL , 버킷  ACL?

권장하지 않음.  


2

버킷 정책?

작업, 요청자, 리소스 및 요청의 여러 측면(예: IP 주소)을 비롯한 다양한 조건을 기반으로 버킷 및 객체에 대한 중앙 집중식 액세스 제어를 제공  


3

IAM 정책?

IAM을 사용하여 S3 리소스에 대한 액세스를 관리  




<3>  flaws.cloud  챌린지 


1

http://flaws.cloud/  


2

Aws 인증 없는 EC2 


nmap -A flaws.cloud -Pn

80/tcp open  http    Amazon S3 httpd



aws s3 ls s3://flaws.cloud  

Unable to locate credentials. You can configure credentials by running "aws configure".   



4

aws s3 ls s3://flaws.cloud --no-sign-request --recursive --human-readable --summarize 

2017-03-14 03:00:38    2.5 KiB hint1.html

2017-03-03 04:05:17    1.7 KiB hint2.html

2017-03-03 04:05:11    1.1 KiB hint3.html

2020-05-22 18:16:45    3.1 KiB index.html

2018-07-10 16:47:16   15.6 KiB logo.png

2017-02-27 01:59:28   46 Bytes robots.txt

2017-02-27 01:59:30    1.0 KiB secret-dd02c7c.html 

Total Objects: 7

   Total Size: 25.0 KiB  




[root@My-EC2 ~]# aws s3 cp s3://flaws.cloud/secret-dd02c7c.html . --no-sign-request

download: s3://flaws.cloud/secret-dd02c7c.html to./secret-dd02c7c.html 


[root@My-EC2 ~]# ls

secret-dd02c7c.html  


6

[root@My-EC2 ~]# file secret-dd02c7c.html

secret-dd02c7c.html: HTML document, ASCII text, with CRLF line terminators 


[root@My-EC2 ~]# more secret-dd02c7c.html 

Level 2 is at <a href="http://level2-c8b217a33 fcf1f839f6f1f73a00a9 ae7.flaws.cloud"> http://level2-c8b217a33 fcf1f839f6f1f73a00a9 ae7.flaws.cloud </a>     





<4> 동일 s3 실습환경을 만들어 테스트하자 


1

서울리전

<인증 받은서버 = 자격증명 설정된 서버에서 실행>

또는  AWS CloudShell 사용 가능 


# S3 버킷 생성

#aws s3 mb s3://버킷(유일한 이름) --region ap-northeast-2

NICKNAME=<자신의 닉네임> 


NICKNAME=topasvga1

aws s3 mb s3://ahss-$NICKNAME --region ap-northeast-2 


# S3 버킷 조회

aws s3 ls

2023-08-29 09:05:16 ahss-topasvga1

2023-07-02 10:31:07 cf-templates-18pmd1tc8r6q0-ap-northeast-2



aws s3api get-public-access-block --bucket ahss-$NICKNAME   



2

퍼블릭 access 관련 설정 내용을 보자~ 

aws s3api get-public-access-block --bucket ahss-$NICKNAME | jq  

{

  "PublicAccessBlockConfiguration": {

    "BlockPublicAcls": true,

    "IgnorePublicAcls": true,

    "BlockPublicPolicy": true,

    "RestrictPublicBuckets": true  

전부 true로 퍼블릭 접근은 차단되어 있다. 



콘솔에서 보자.

디폴트로 버킷을 만들면 퍼블릭 접근 차단이 기본이다.   



3

콘솔에서 퍼블릭 접근 차단을 해지하자.  



4

<인증 받은 서버에서 작업>

파일을 만들어  s3에 올리자.  


# 파일 생성

echo "memo1" > memo1.txt

echo "memo2" > memo2.txt

echo "memo3" > memo3.txt 


# S3로 업로드

aws s3 cp memo1.txt s3://ahss-$NICKNAME

aws s3 cp memo2.txt s3://ahss-$NICKNAME

aws s3 cp memo3.txt s3://ahss-$NICKNAME 


# 파일 확인

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


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



5

<인증 없는 서버에서 버킷 조회>


# 출력 결과의 원인을 유추해보자!

NICKNAME=<자신의닉네임>


NICKNAME=topasvga1

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

Unable to locate credentials. You can configure credentials by running "aws configure".

안됨. 


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

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

안됨



6

취약한 버킷 설정으로 만들기 ?

AWS Console 에서

s3> 버킷 > 권한 > 객체 소유권 편집 > ACL 활성화 - ACL이 복원된다는 것을 확인합니다. >  변경사항 저장.  

권한 > ACL(액세스 제어 목록)  편집  > 모든 사람(퍼블릭 액세스)  나열  > 저장  



7

<다시 인증없는 서버에서 조회>


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

2023-08-28 02:53:34    6 Bytes memo1.txt

2023-08-28 02:53:35    6 Bytes memo2.txt

2023-08-28 02:53:37    6 Bytes memo3.txt  



8

복사?

안됨.

aws s3 cp s3://ahss-$NICKNAME/memo1.txt . --no-sign-request

fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden  



9

AWS 콘솔에서 파일단위로 권한을 변경해 테스트해보자.


Memo1.txt

모든사람(퍼블릭액세스) 체크 


Mome2.txt

인증된 사용자 그룹(AWS이 있는 모든 사용자)

// AWS 자격증명이 있는 모두. 내 계정이 아니어도 된다.!!!! 주의!!  



10

다운로드 시도?

aws s3 cp s3://ahss-$NICKNAME/memo1.txt . --no-sign-request 

인증안된 서버에서 시도 - 된다. 


aws s3 cp s3://ahss-$NICKNAME/memo2.txt . --no-sign-request 

인증안된 서버에서 시도 - 안된다.   

인증된 서버만 파일을 가져가도록 하였다.


인증된 서버에서 시도 한다. 된다.

aws s3 cp s3://ahss-$NICKNAME/memo2.txt . --no-sign-request   



11

Memo3.txt 는 모두 안된다.

aws s3 cp s3://ahss-$NICKNAME/memo3.txt . --no-sign-request  



12

Memo3.txt 파일에 대해 테스트해보자.


객체가 아닌 버킷 정책으로 통제가 가능하다.  


{

    "Version": "2012-10-17",

    "Statement": [

        {

            "Sid": "PublicReadGetObject",

            "Effect": "Allow",

            "Principal": "*",

            "Action": "s3:GetObject",

            "Resource": "arn:aws:s3:::ahss-topasvga1/*"

        }

    ]

}   


인증 안받은 서버에서도 된다!!!

root@My-EC2 ~]# aws s3 cp s3://ahss-$NICKNAME/memo3.txt . --no-sign-request

download: s3://ahss-topasvga/memo3.txt to ./memo3.txt    



13

권장은 하지 않는다.

하지만 필요한 경우가 있다.

https://dev.classmethod.jp/articles/s3-acl-enable-usecase/  


   


<5> 분석기 - IAM Access Analyzer 



1

자격증명 서버에서 실행 



# 액세스 분석기 생성

#aws accessanalyzer create-analyzer --analyzer-name firstanalyzer --type ACCOUNT --output text --query arn


ANA_ARN=$(aws accessanalyzer create-analyzer --analyzer-name firstanalyzer --type ACCOUNT --output text --query arn)


echo $ANA_ARN 


AWS console > 서울 리전에서 확인

IAM > 액세스 분석기


# S3 버킷 스캔 수행

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



# S3 버킷 스캔 결과 확인

aws accessanalyzer get-analyzed-resource --analyzer-arn $ANA_ARN --resource-arn arn:aws:s3:::ahss-$NICKNAME | jq  


{

  "resource": {

    "resourceArn": "arn:aws:s3:::ahss-topasvga1",

    "resourceType": "AWS::S3::Bucket",

    "createdAt": "2023-08-29T00:56:11.172000+00:00",

    "analyzedAt": "2023-08-29T01:10:21.479000+00:00",

    "updatedAt": "2023-08-29T01:10:21.479000+00:00",

    "isPublic": true,

    "actions": [

      "s3:GetObject",

      "s3:ListBucket",

      "s3:ListBucketMultipartUploads",

      "s3:ListBucketVersions"

    ],

    "sharedVia": [

      "BUCKET_ACL",

      "POLICY"

    ],

    "status": "ACTIVE",

    "resourceOwnerAccount": "451032684083"

  }

}



3

콘솔 IAM  에서 확인한다.  

서울 리전





4

조치?


4가지 퍼블릭 옵션을 차단한다!!!


aws s3api put-public-access-block --bucket ahss-$NICKNAME --public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"  


콘솔에서 확인해도 된다. 서울리전  




스캔결과 재확인


aws accessanalyzer get-analyzed-resource --analyzer-arn $ANA_ARN --resource-arn arn:aws:s3:::ahss-$NICKNAME | jq   


콘솔에서 확인

해결됨



5

분석기 삭제

aws accessanalyzer delete-analyzer  --analyzer-name firstanalyzer  


분석기 확인

aws accessanalyzer list-analyzers



6

# 버킷에 객체 모두 삭제

aws s3 rm s3://ahss-$NICKNAME --recursive

객체 파일을 모두 삭제해야 버킷 삭제가 가능하다.


# 실습에 사용한 S3 버킷 삭제

aws s3 rb s3://ahss-$NICKNAME 




# 확인

aws s3 ls   



7

다음 과정을 하지 않으면 Cloudformation 2개 삭제 =  ec2 2개 삭제  




<6> AWS CLI S3 명령 s3 및 s3api 차이점  



https://dev.classmethod.jp/articles/cli-command-s3-s3-and-s3api-i-tried-to-find-out-what-i-can-do/   




<7> Public Bucket 


https://buckets.grayhatwarfare.com/


- **Free Visitor 제한** : Search Results Listing Limit(500), Bucket Files Listing Limit(500)… - [Packages](https://grayhatwarfare.com/packages)


- **키워드 검색** : **packtpub.com** 입력 후 Search 클릭 → 하단 출력 파일 클릭 확인





다음 자료 보기

https://brunch.co.kr/@topasvga/3417



같이 볼만한 자료

https://brunch.co.kr/@topasvga/1791




몰아보기

https://brunch.co.kr/@topasvga/3430



다음은 주말 CloudNet  스터디 내용 참고하여  정리한 부분입니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863  




감사합니다.

매거진의 이전글 AWS 마켓 플레이스 사용하기
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari