본 내용은 gasida님의 AHSS 1기 스터디에서 진행된 내용입니다.
IAM은 Identity & Access Management 약자이다. AWS에서는 리소스에대한 엑세를 안전하게 제어할 수 있는 Web Service이다. AWS 리소스에 대한 엑세스를 중앙에 권한을 가지고 안전하게 제어할 수 있다. AWS Management Console, AWS CLI, AWS SDK 그리고 IAM 쿼리 API로 접근 및 작업이 가능하다.
본격적인 IAM 실습을 진행하기 전에 실습환경을 생성한다. 실습은 Webserver EC2와 Attacker EC2로 진행이 된다. 실습을 통하여 IAM User의 기본 사용방법을 알아보고 취약점을 살펴본다.
#사전 준비사항
# s3 bucket에서 curl로 download 가능
curl -0 https://s3.ap-northeast-2.amazonaws.com/cloudformation.cloudneta.net/security/ahss-ec2_2ea.yaml
# ahss-ec2_2ea.yaml
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
LatestAmiId:
Description: (DO NOT CHANGE)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AllowedValues:
- /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
# IAM Role & Instance Profile
IAMLabInstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: IAMLabInstanceRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
IAMRoleForInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: IAMLabRoleForInstances
Path: /
Roles:
- !Ref IAMLabInstanceRole
# VPC
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Tags:
- Key: Name
Value: My-VPC
MyIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: My-IGW
MyIGWAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref MyIGW
VpcId: !Ref MyVPC
MyPublicRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: My-Public-RT
DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: MyIGWAttachment
Properties:
RouteTableId: !Ref MyPublicRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref MyIGW
MyPublicSN:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: My-Public-SN
MyPublicSN2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: !Select [ 2, !GetAZs '' ]
CidrBlock: 10.0.1.0/24
Tags:
- Key: Name
Value: My-Public-SN-2
MyPublicSNRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref MyPublicRT
SubnetId: !Ref MyPublicSN
MyPublicSNRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref MyPublicRT
SubnetId: !Ref MyPublicSN2
MySG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: WEB Security Group
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: My-SG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
MYEC2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
IamInstanceProfile: IAMLabRoleForInstances
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: WebServer
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref MyPublicSN
GroupSet:
- !Ref MySG
AssociatePublicIpAddress: true
PrivateIpAddress: 10.0.0.10
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp3
VolumeSize: 30
DeleteOnTermination: true
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
hostnamectl --static set-hostname WebServer
echo "sudo su -" >> /home/ec2-user/.bashrc
# Change Timezone
sed -i "s/UTC/Asia\/Seoul/g" /etc/sysconfig/clock
ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
# Install Packages
yum install -y httpd php tree git htop jq
# Install httpd & WebShell
systemctl start httpd && systemctl enable httpd
curl -o /var/www/html/index.php https://raw.githubusercontent.com/Arrexel/phpbash/master/phpbash.php
# Install aws cli v2
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip >/dev/null 2>&1
sudo ./aws/install
complete -C '/usr/local/bin/aws_completer' aws
echo 'export AWS_PAGER=""' >>/etc/profile
export AWS_DEFAULT_REGION=${AWS::Region}
echo "export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION" >> /etc/profile
# Install Docker
cat <<EOF | sudo tee /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
amazon-linux-extras install docker -y
systemctl start docker && systemctl enable docker
# Setting SSH
sed -i "s/^PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
sed -i "s/^#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
systemctl restart sshd
MYEC22:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: Attacker
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref MyPublicSN
GroupSet:
- !Ref MySG
AssociatePublicIpAddress: true
PrivateIpAddress: 10.0.0.20
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp3
VolumeSize: 30
DeleteOnTermination: true
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
hostnamectl --static set-hostname Attacker
echo "sudo su -" >> /home/ec2-user/.bashrc
# Change Timezone
sed -i "s/UTC/Asia\/Seoul/g" /etc/sysconfig/clock
ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
# Install Packages
yum install -y tree git htop jq nc lynx
# Install aws cli v2
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip >/dev/null 2>&1
sudo ./aws/install
complete -C '/usr/local/bin/aws_completer' aws
echo 'export AWS_PAGER=""' >>/etc/profile
export AWS_DEFAULT_REGION=${AWS::Region}
echo "export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION" >> /etc/profile
# Install Docker
cat <<EOF | sudo tee /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
amazon-linux-extras install docker epel -y
systemctl start docker && systemctl enable docker
# Install Password
yum install -y hydra gcc
cd /root
wget https://raw.githubusercontent.com/Mebus/cupp/master/cupp.py
wget https://raw.githubusercontent.com/Mebus/cupp/master/cupp.cfg
wget https://github.com/praetorian-inc/Hob0Rules/raw/master/wordlists/rockyou.txt.gz
gzip -d rockyou.txt.gz
wget https://sourceforge.net/projects/crunch-wordlist/files/latest/download
tar -xvf download && rm -rf download && cd crunch-3.6/
make && make install
Outputs:
WebServerIP:
Value: !GetAtt MYEC2.PublicIp
AttackerIP:
Value: !GetAtt MYEC22.PublicIp
#CloudFormation Stack 배포
#배포 완료 후 EC2 2대 IP 출력
생성된 EC2의 IP를 메모한다...
43.201.100.242
13.125.24.217
#각각 EC2 SSH 접속 및 정보확인
date & aws version
docker engine 설치 확인
볼륨 Size 확인 & 네트워크 인터페이스 IP 확인
4.[WebServer] Apache Web 서비스 확인
IAM User 생성 및 정책 연동을 진행한다.
계정 및 정책 생성을 위하여 환경변수를 지정한다.
#sts = Security Token Service
#aws iam creat-user command를 활용하여 user1, user2를 생성한다.
#user1,2 사용자의 AWS Web Console 로그인 profile을 생성한다.
#iam 사용자 리스트를 확인한다.
#사용자에게 프로그래밍 방시 엑세스 권한을 부여한다.
#사용자에 정책을 추가한다. User1은 AdministratorsAccess, User2는 ReadOnlyAccess의 권한을 주는 것을 확인할 수 있다.
#크롬 확장 프로그램 Multilogin 설치후
Arn, username, 비밀번호를 입력하여 login하여 각각 계정에서 시도해본다. EC2생성 및 재부팅을 시도하여본다. user1의 권한이 Administrator user2는 ReadOnlyAccess이기에 user1과 달리 user2는 제한이 있을 것 같다.
#user1 login
#user2 login
#EC2 생성 시도
user1 성공
user2 성공
#EC2 재부팅 시도
user1 성공
user2 성공
#첫번째 시도 시, credential 실패하는 것을 확인할 수 있다.
#자격증명 profile 생성
전에 만들었던 계정정보
user1, user2 profile 생성 후 자격 증명 정보가 저장되는 credential 파일 확인
#caller id 확인 시 2.0에서 만들었던 계정 정보 동일함을 확인 할 수 있다.
#버킷생성을 해본다.
#버킷이 생성되는 것을 확인. user2 는 콘솔에서의 실험과 마찬가지로 생성되지 않는 것을 확인할 수 있다.
access key id 보호에 신경써야한다는 것을 배웠다.