-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add edge for shared process namespace attacks (#112)
Add the edge attack for the Shared PS Namespace attack path. Renamed the file to SHARE_PS_NAMESPACE to be consitent with the k8s api. The links processing between each container is O(n^2) (n = container in a pod), which is required to make the number of hop correct (we can't just do A -> B -> C, we need to do A ->B, A->C, B->C). The tests adds a new pods so we have 2 distinct pods to make sure we aren't linking them together.
- Loading branch information
Showing
8 changed files
with
286 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package edge | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/DataDog/KubeHound/pkg/kubehound/graph/adapter" | ||
"github.com/DataDog/KubeHound/pkg/kubehound/graph/types" | ||
"github.com/DataDog/KubeHound/pkg/kubehound/models/converter" | ||
"github.com/DataDog/KubeHound/pkg/kubehound/storage/cache" | ||
"github.com/DataDog/KubeHound/pkg/kubehound/storage/storedb" | ||
"github.com/DataDog/KubeHound/pkg/kubehound/store/collections" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
func init() { | ||
Register(&SharePSNamespace{}, RegisterDefault) | ||
} | ||
|
||
type SharePSNamespace struct { | ||
BaseEdge | ||
} | ||
|
||
type sharedPsNamespaceGroup struct { | ||
Containers []primitive.ObjectID `bson:"container_ids" json:"container_ids"` | ||
} | ||
type sharedPsNamespaceGroupPair struct { | ||
ContainerA primitive.ObjectID `bson:"container_a_id" json:"container_a"` | ||
ContainerB primitive.ObjectID `bson:"container_b_id" json:"container_b"` | ||
} | ||
|
||
func (e *SharePSNamespace) Label() string { | ||
return "SHARE_PS_NAMESPACE" | ||
} | ||
|
||
func (e *SharePSNamespace) Name() string { | ||
return "SharePSNamespace" | ||
} | ||
|
||
// Processor delegates the processing tasks to to the generic containerEscapeProcessor. | ||
func (e *SharePSNamespace) Processor(ctx context.Context, oic *converter.ObjectIDConverter, entry any) (any, error) { | ||
typed, ok := entry.(*sharedPsNamespaceGroupPair) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid type passed to processor: %T", entry) | ||
} | ||
|
||
return adapter.GremlinEdgeProcessor(ctx, oic, e.Label(), typed.ContainerA, typed.ContainerB) | ||
} | ||
|
||
func (e *SharePSNamespace) Stream(ctx context.Context, store storedb.Provider, _ cache.CacheReader, | ||
callback types.ProcessEntryCallback, complete types.CompleteQueryCallback) error { | ||
|
||
coll := adapter.MongoDB(store).Collection(collections.PodName) | ||
pipeline := bson.A{ | ||
bson.D{{"$match", bson.D{{"k8.spec.shareprocessnamespace", true}}}}, | ||
bson.D{ | ||
{"$lookup", | ||
bson.D{ | ||
{"from", "containers"}, | ||
{"localField", "_id"}, | ||
{"foreignField", "pod_id"}, | ||
{"as", "containers_with_shared_ns"}, | ||
}, | ||
}, | ||
}, | ||
bson.D{ | ||
{"$project", | ||
bson.D{ | ||
{"_id", 1}, | ||
{"containers_with_shared_ns", bson.D{{"_id", 1}}}, | ||
}, | ||
}, | ||
}, | ||
bson.D{ | ||
{"$project", | ||
bson.D{ | ||
{"_id", 0}, | ||
{"container_ids", "$containers_with_shared_ns._id"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
cur, err := coll.Aggregate(ctx, pipeline) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
defer cur.Close(ctx) | ||
|
||
for cur.Next(ctx) { | ||
var entry sharedPsNamespaceGroup | ||
err := cur.Decode(&entry) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, containerSrc := range entry.Containers { | ||
for _, containerDst := range entry.Containers { | ||
// No need to create a link with itself | ||
if containerSrc == containerDst { | ||
continue | ||
} | ||
err = callback(ctx, &sharedPsNamespaceGroupPair{ | ||
ContainerA: containerSrc, | ||
ContainerB: containerDst, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
return complete(ctx) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# SHARE_PS_NAMESPACE edge | ||
# There's 2 pods here so we can also test we don't mix both pods shared NS together. | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: sharedps-pod1 | ||
labels: | ||
app: kubehound-edge-test | ||
spec: | ||
shareProcessNamespace: true | ||
containers: | ||
- name: sharedps-pod1-a | ||
image: ubuntu | ||
command: [ "/bin/sh", "-c", "--" ] | ||
args: [ "while true; do sleep 30; done;" ] | ||
- name: sharedps-pod1-b | ||
image: ubuntu | ||
command: [ "/bin/sh", "-c", "--" ] | ||
args: [ "while true; do sleep 30; done;" ] | ||
- name: sharedps-pod1-c | ||
image: ubuntu | ||
command: [ "/bin/sh", "-c", "--" ] | ||
args: [ "while true; do sleep 30; done;" ] | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: sharedps-pod2 | ||
labels: | ||
app: kubehound-edge-test | ||
spec: | ||
shareProcessNamespace: true | ||
containers: | ||
- name: sharedps-pod2-a | ||
image: ubuntu | ||
command: [ "/bin/sh", "-c", "--" ] | ||
args: [ "while true; do sleep 30; done;" ] | ||
- name: sharedps-pod2-b | ||
image: ubuntu | ||
command: [ "/bin/sh", "-c", "--" ] | ||
args: [ "while true; do sleep 30; done;" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.