RBAC、IMDSの保護、監査ログでAmazon EKSのセキュリティを強化
Kamil Potrec
2021年7月7日
20 分で読めますInfrastructure as Code
この
Amazon Elastic Kubernetes Serviceとは?
Amazon Elastic Kubernetes Serviceは、
Amazon EKSは、
追加コスト以外に
EKSをすばやくデプロイする
Amazon EKSは、ウェブコンソール、
ここでは
EKSクラスターは
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = local.cluster_name
cidr = var.vpc_cidr
azs = var.vpc_azs
private_subnets = var.private_subnet_cidrs
public_subnets = var.public_subnet_cidrs
enable_nat_gateway = var.enable_nat_gateway
single_nat_gateway = var.single_nat_gateway
one_nat_gateway_per_az = var.one_nat_gateway_per_az
}デモ環境では、

EKSクラスターには、
module "cluster" {
source = "terraform-aws-modules/eks/aws"
cluster_name = local.cluster_name
cluster_version = var.cluster_version
subnets = module.vpc.private_subnets
vpc_id = module.vpc.vpc_id
worker_groups = [
{
instance_type = var.wg_instance_type
asg_max_size = var.wg_asg_max_size
}
]
}Terraform Cloudでは、backend設定を
terraform {
backend "remote" {
hostname = "app.terraform.io"
# TODO: update with your own organization
organization = "your-unique-organization-name"
workspaces {
name = "eks-demo-deployments"
}
}
}また、
初回の
dev@pwnbox:adversarialengineering/eks-demo-deployments/terraform/default$ terraform plan
Running plan in the remote backend. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.
Preparing the remote plan...
The remote workspace is configured to work with configuration at
terraform/default/ relative to the target repository.
<OMITTED>
+ tags = {
+ "Name" = "eks-threat-modelling-5FZtd82l"
}
+ tags_all = {
+ "Name" = "eks-threat-modelling-5FZtd82l"
}
}
Plan: 44 to add, 0 to change, 0 to destroy.これで

環境を
dev@pwnbox:~$ aws eks --region eu-west-1 update-kubeconfig --name eks-threat-modelling-5FZtd82l
Added new context arn:aws:eks:eu-west-1:123456789012:cluster/eks-threat-modelling-5FZtd82l to /home/dev/.kube/config
dev@pwnbox:~$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.20.0.1 <none> 443/TCP 15m
dev@pwnbox:~$この
Kubernetes APIへのアクセスを制限する
EKSサービスでは、サービスエンドポイントを
FLAG: --authentication-token-webhook-cache-ttl="7m0s"
FLAG: --authentication-token-webhook-config-file="/etc/kubernetes/authenticator/apiserver-webhook-kubeconfig.yaml"
FLAG: --authentication-token-webhook-version="v1beta1"
FLAG: --authorization-mode="[Node,RBAC]"
FLAG: --authorization-policy-file=""
FLAG: --authorization-webhook-cache-authorized-ttl="5m0s"
FLAG: --authorization-webhook-cache-unauthorized-ttl="30s"
FLAG: --authorization-webhook-config-file=""
FLAG: --authorization-webhook-version="v1beta1"ログからは、
さらにkubectlには
dev@pwnbox:$ kubectl get pod -v 9
<OMITTED>
I0620 13:50:56.340980 19 round_trippers.go:425] curl -k -v -XGET -H "Accept: application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/json" -H "User-Agent: kubectl/v1.20.1 (linux/amd64) kubernetes/c4d7527" 'https://DC83F13246D9B2E468D2CAAD692EAF22.yl4.eu-west-1.eks.amazonaws.com/api/v1/namespaces/default/pods?limit=500'
I0620 13:50:56.435282 19 round_trippers.go:445] GET https://DC83F13246D9B2E468D2CAAD692EAF22.yl4.eu-west-1.eks.amazonaws.com/api/v1/namespaces/default/pods?limit=500 200 OK in 94 milliseconds
I0620 13:50:56.435317 19 round_trippers.go:451] Response Headers:
I0620 13:50:56.435326 19 round_trippers.go:454] Audit-Id: 4814a650-c35b-4c98-bc0c-ba22fb3d58ce
I0620 13:50:56.435334 19 round_trippers.go:454] Cache-Control: no-cache, private
I0620 13:50:56.435342 19 round_trippers.go:454] Content-Type: application/json
I0620 13:50:56.435349 19 round_trippers.go:454] Content-Length: 2926
I0620 13:50:56.435356 19 round_trippers.go:454] Date: Sun, 20 Jun 2021 13:50:56 GMT
I0620 13:50:56.436467 19 request.go:1107] Response Body: {"kind":"Table","apiVersion":"meta.k8s.io/v1","metadata":{"selfLink":"/api/v1/namespaces/default/pods","resourceVersion":"12794"},"columnDefinitions"正しい
dev@pwnbox:$ curl -k -v -XGET https://DC83F13246D9B2E468D2CAAD692EAF22.yl4.eu-west-1.eks.amazonaws.com/api/v1/namespaces/default/pods?limit=500'
<OMITTED>
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "pods is forbidden: User \"system:anonymous\" cannot list resource \"pods\" in API group \"\" in the namespace \"default\"",
"reason": "Forbidden",
"details": {
"kind": "pods"
},
"code": 403デフォルトでは
この
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-only
subjects:
- kind: User
name: system:anonymous
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.ioこの
dev@pwnbox:$ curl -k -v -XGET https://DC83F13246D9B2E468D2CAAD692EAF22.yl4.eu-west-1.eks.amazonaws.com/api/v1/namespaces/default/pods?limit=500'
<OMITTED>
{
"kind": "PodList",
"apiVersion": "v1",
"metadata": {
"selfLink": "/api/v1/namespaces/default/pods",
"resourceVersion": "15018"
},
"items": []Kubernetesのsystem:anonymousユーザーとsystem:unauthenticatedグループは、subjects属性に*などのnameを*に
Kubernetes APIサーバーへの
アクセス可能なcluster_endpoint_public_access_cidrs属性を
もうcluster_endpoint_private_access属性とcluster_endpoint_public_access属性で
ただし、

このaws-auth ConfigMapを
パブリックエンドポイントを
開発者の
インスタンスメタデータサービスへのアクセスを制限する
EKSはインスタンスプロファイルを
apiVersion: v1
kind: Pod
metadata:
name: aws
namespace: default
spec:
containers:
- name: aws
image: amazon/aws-cli:latest
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
restartPolicy: Always
dev@pwnbox:$ kubectl exec -it aws -- /bin/bash
bash-4.2# aws sts get-caller-identity
{
"UserId": "AROAYREY3WYOLQYZ5IGZJ:i-0f52c37fc0a7a4a44",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/eks-threat-modelling-5FZtd82l20210620220209389400000009/i-0f52c37fc0a7a4a44"
}
bash-4.2#上記の
この
AWS標準のmetadata_http_tokens属性をrequiredに1に1に
dev@pwnbox:$ kubectl exec -it aws -- aws sts get-caller-identity
Unable to locate credentials. You can configure credentials by running "aws configure".
command terminated with exit code 253Amazon EKS用の
このhostNetworking属性が
apiVersion: v1
kind: Pod
metadata:
name: aws-node
namespace: default
spec:
hostNetwork: true
containers:
- name: aws-node
image: amazon/aws-cli:latest
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
restartPolicy: Always
dev@pwnbox:$ kubectl exec -it aws-node -- aws sts get-caller-identity
{
"UserId": "AROAYREY3WYOLQYZ5IGZJ:i-0e0ecce7af113398b",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/eks-threat-modelling-5FZtd82l20210620220209389400000009/i-0e0ecce7af113398b"
}
Kubernetes標準のhostNetworkがtrueに1に
apiVersion: crd.projectcalico.org/v1
kind: GlobalNetworkPolicy
metadata:
name: block-metadata-egress-only
spec:
selector: all()
egress:
- action: Deny
protocol: TCP
destination:
nets:
- 169.254.169.254/32
- action: Allow
destination:
nets:
- 0.0.0.0/0
dev@pwnbox:$ kubectl exec -it aws -- aws sts get-caller-identity
{
"UserId": "AROAYREY3WYOLQYZ5IGZJ:i-06a05d6ddfff5b2bd",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/eks-threat-modelling-5FZtd82l20210620220209389400000009/i-06a05d6ddfff5b2bd"
}
dev@pwnbox:$ kubectl apply -f tests/disable-imds-network-access/network-policy.yaml
globalnetworkpolicy.crd.projectcalico.org/allow-all-egress-except-ec2-metadata created
dev@pwnbox:$ kubectl exec -it aws -- aws sts get-caller-identity
Unable to locate credentials. You can configure credentials by running "aws configure".
command terminated with exit code 253
dev@pwnbox:$ログを有効にする
EKSクラスターがcluster_enabled_log_types属性を
EKSでは、auditログとauthenticatorログが
監査ログをauditとauthenticatorに
たとえば、aws-configマップへのaws-authと
{ ($.verb = "patch") && ($.objectRef.name = "aws-auth") }監査イベントには、
{
"kind": "Event",
"apiVersion": "audit.k8s.io/v1",
"level": "RequestResponse",
"auditID": "844b26da-3ce9-4062-b7aa-3c0a869d9e45",
"stage": "ResponseComplete",
"requestURI": "/api/v1/namespaces/kube-system/configmaps/aws-auth?fieldManager=kubectl-edit",
"verb": "patch",
"user": {
"username": "kubernetes-admin",
"uid": "heptio-authenticator-aws:123456789012:AROAYREY3WYOBFHU7VIRM",
"groups": [
"system:masters",
"system:authenticated"
],
"extra": {
"accessKeyId": [
"ASIAYREY3WYOETXWXAOG"
]
}
},
"sourceIPs": [
"12.34.256.27"
],
"userAgent": "kubectl/v1.20.1 (linux/amd64) kubernetes/c4d7527",
"objectRef": {
"resource": "configmaps",
"namespace": "kube-system",
"name": "aws-auth",
"apiVersion": "v1"
},
"responseStatus": {
"metadata": {},
"code": 200
},
<OMITTED>また、
{ ($.user.username = *anonymous) }
{
"kind": "Event",
"apiVersion": "audit.k8s.io/v1",
"level": "Metadata",
"auditID": "68f24024-080b-4ae7-a97b-fd5fd9792e31",
"stage": "ResponseComplete",
"requestURI": "/api/v1/namespaces/kube-system/configmaps/aws-auth",
"verb": "get",
"user": {
"username": "system:anonymous",
"groups": [
"system:unauthenticated"
]
},
"sourceIPs": [
"12.34.256.27"
],
"userAgent": "kubectl/v1.20.1 (linux/amd64) kubernetes/c4d7527",
"objectRef": {
"resource": "configmaps",
"namespace": "kube-system",
"name": "aws-auth",
"apiVersion": "v1"
},
"responseStatus": {
"metadata": {},
"status": "Failure",
"reason": "Forbidden",
"code": 403
},
"requestReceivedTimestamp": "2021-06-21T17:08:02.384514Z",
"stageTimestamp": "2021-06-21T17:08:02.384693Z",
"annotations": {
"authorization.k8s.io/decision": "forbid",
"authorization.k8s.io/reason": ""
}
}一方、AROAYREY3WYOBFHU7VIRMの
ime="2021-06-21T16:32:01Z" level=info msg="access granted" arn="arn:aws:iam::123456789012:role/ci" client="127.0.0.1:39092" groups="[system:masters]" method=POST path=/authenticate sts=sts.eu-west-1.amazonaws.com uid="heptio-authenticator-aws:123456789012:AROAYREY3WYOBFHU7VIRM" username=kubernetes-admin次回:Amazon EKSの高度なハードニング
今回の
Snyk Infrastructure as Code (Snyk IaC)の
ソースコードの 段階から インフラを 保護
Snykは



