Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): add new k8s rule to detect port-forwarding into containers (RBAC) #5266

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "38fa11ef-dbcc-4da8-9680-7e1fd855b6fb",
"queryName": "RBAC Roles with Port-Forwarding Permission",
"severity": "MEDIUM",
"category": "Access Control",
"descriptionText": "Roles or ClusterRoles with RBAC permissions to port-forward into pods can open socket-level communication channels to containers. In case of compromise, attackers may abuse this for direct communication that bypasses network security restrictions",
"descriptionUrl": "https://kubernetes.io/docs/reference/access-authn-authz/rbac/",
"platform": "Kubernetes",
"descriptionID": "38fa11ef"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {
document := input.document[i]
metadata := document.metadata

kinds := {"Role", "ClusterRole"}
document.kind == kinds[_]

verbs := {"update", "patch", "create", "*"}
document.rules[j].resources[_] == "pods/portforward"
document.rules[j].verbs[_] == verbs[_]

result := {
"documentId": document.id,
"searchKey": sprintf("metadata.name={{%s}}.rules", [metadata.name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("metadata.name={{%s}}.rules[%d].resources should not include the 'pods/portforward' resource", [metadata.name, j]),
"keyActualValue": sprintf("metadata.name={{%s}}.rules[%d].resources includes the 'pods/portforward' resource", [metadata.name, j]),
"searchLine": common_lib.build_search_line(["rules", j], ["resources"])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: my-namespace
name: allow-port-forward-neg
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: allow-port-forward-neg
namespace: my-namespace
subjects:
- kind: User
name: bob
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: allow-port-forward-neg
apiGroup: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: my-namespace
name: allow-port-forward
rules:
- apiGroups: [""]
resources: ["pods", "pods/portforward"]
verbs: ["get", "list", "create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: allow-port-forward
namespace: my-namespace
subjects:
- kind: User
name: bob
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: allow-port-forward
apiGroup: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"queryName": "RBAC Roles with Port-Forwarding Permission",
"severity": "MEDIUM",
"line": 8
}
]