Kubernetes Role-Based Access Control(RBAC)

htARTE (HackTricks AWS Red Team Expert)에서 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

Role-Based Access Control (RBAC)

Kubernetes에는 API 서버에 사용 권한을 설정하는 데 도움이 되는 **Role-Based Access Control(RBAC)**이라는 인가 모듈이 있습니다.

RBAC의 권한 모델은 세 가지 개별 부분으로 구성됩니다:

  1. Role\ClusterRole - 실제 권한입니다. 권한 집합을 나타내는 규칙을 포함합니다. 각 규칙에는 리소스동사가 포함됩니다. 동사는 리소스에 적용되는 동작입니다.

  2. Subject (User, Group or ServiceAccount) - 권한을 받을 객체입니다.

  3. RoleBinding\ClusterRoleBinding - Role\ClusterRole과 Subject 간의 연결입니다.

"Roles"과 "ClusterRoles"의 차이점은 역할이 적용되는 위치입니다. "Role"은 하나의 특정 네임스페이스에 대한 액세스를 허용하며, "ClusterRole"은 클러스터의 모든 네임스페이스에서 사용할 수 있습니다. 또한, ClusterRoles은 다음에도 액세스를 허용할 수 있습니다:

  • 클러스터 범위 리소스 (예: 노드).

  • 비 리소스 엔드포인트 (예: /healthz).

  • 네임스페이스 리소스 (예: Pods), 모든 네임스페이스에서.

Kubernetes 1.6 이상에서는 RBAC 정책이 기본적으로 활성화됩니다. 그러나 RBAC를 활성화하려면 다음과 같은 방법을 사용할 수 있습니다:

kube-apiserver --authorization-mode=Example,RBAC --other-options --more-options

템플릿

Role 또는 ClusterRole의 템플릿에서는 역할의 이름, 네임스페이스(roles에서), 그리고 apiGroups, resources, verbs를 지정해야 합니다:

  • apiGroups는 이 규칙이 적용되는 API 네임스페이스를 포함하는 배열입니다. 예를 들어, Pod 정의는 apiVersion: v1을 사용합니다. rbac.authorization.k8s.io 또는 [*]와 같은 값이 될 수 있습니다.

  • resources는 이 규칙이 적용되는 리소스를 정의하는 배열입니다. 모든 리소스를 찾으려면 다음을 사용할 수 있습니다: kubectl api-resources --namespaced=true

  • verbs허용된 동사를 포함하는 배열입니다. Kubernetes에서 동사는 리소스에 적용해야 하는 동작 유형을 정의합니다. 예를 들어, list 동사는 컬렉션에 대해 사용되고 "get"은 단일 리소스에 대해 사용됩니다.

규칙 동사

(이 정보는 문서 에서 가져왔습니다.)

HTTP 동사요청 동사

POST

create

GET, HEAD

get (개별 리소스에 대해), list (컬렉션에 대해, 전체 객체 내용 포함), watch (개별 리소스 또는 리소스 컬렉션을 감시하기 위해)

PUT

update

PATCH

patch

DELETE

delete (개별 리소스에 대해), deletecollection (컬렉션에 대해)

Kubernetes는 때때로 전문 동사를 사용하여 추가 권한에 대한 권한 부여를 확인합니다. 예를 들어:

  • policy API 그룹의 podsecuritypolicies 리소스에서 use 동사.

  • rbac.authorization.k8s.io API 그룹의 rolesclusterroles 리소스에서 bindescalate 동사.

  • 코어 API 그룹의 users, groups, serviceaccounts에서 impersonate 동사, 그리고 authentication.k8s.io API 그룹의 userextras에서.

kubectl api-resources --sort-by name -o wide를 실행하여 각 리소스가 지원하는 동사를 모두 찾을 수 있습니다.

예제

Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: defaultGreen
name: pod-and-pod-logs-reader
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]
ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
# "namespace" omitted since ClusterRoles are not namespaced
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]

예를 들어, 특정 사용자가 다음을 실행할 수 있도록 ClusterRole을 사용할 수 있습니다:

kubectl get pods --all-namespaces

RoleBinding과 ClusterRoleBinding

문서에서: RoleBinding은 역할에 정의된 권한을 사용자 또는 일련의 사용자에게 부여합니다. 이는 subjects(사용자, 그룹 또는 서비스 계정)의 목록과 부여되는 역할에 대한 참조를 가지고 있습니다. RoleBinding은 특정 네임스페이스 내에서 권한을 부여하고, ClusterRoleBinding은 해당 액세스를 클러스터 전체에 부여합니다.

piVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jane" to read pods in the "default" namespace.
# You need to already have a Role named "pod-reader" in that namespace.
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
# You can specify more than one "subject"
- kind: User
name: jane # "name" is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
# "roleRef" specifies the binding to a Role / ClusterRole
kind: Role #this must be Role or ClusterRole
name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
apiGroup: rbac.authorization.k8s.io
ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.
kind: ClusterRoleBinding
metadata:
name: read-secrets-global
subjects:
- kind: Group
name: manager # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.io

권한은 누적되므로 "list" 및 "delete" 비밀을 가진 clusterRole을 "get"으로 가진 Role과 함께 추가할 수 있습니다. 따라서 역할과 권한을 항상 테스트하고 허용된 것을 명시하십시오. 기본적으로 모든 것이 거부됩니다.

RBAC 열거

# Get current privileges
kubectl auth can-i --list
# use `--as=system:serviceaccount:<namespace>:<sa_name>` to impersonate a service account

# List Cluster Roles
kubectl get clusterroles
kubectl describe clusterroles

# List Cluster Roles Bindings
kubectl get clusterrolebindings
kubectl describe clusterrolebindings

# List Roles
kubectl get roles
kubectl describe roles

# List Roles Bindings
kubectl get rolebindings
kubectl describe rolebindings

권한 상승을 위한 역할/클러스터 역할 남용

pageAbusing Roles/ClusterRoles in Kubernetes
htARTE (HackTricks AWS Red Team Expert)를 통해 제로에서 영웅까지 AWS 해킹을 배워보세요!

HackTricks를 지원하는 다른 방법:

最終更新