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

rule(Change thread namespace): Modify condition to detect suspicious container activity #974

Merged
merged 2 commits into from
Mar 12, 2020

Conversation

rung
Copy link
Contributor

@rung rung commented Dec 16, 2019

Signed-off-by: Hiroki Suezawa suezawa@gmail.com

What type of PR is this?
/kind rule-update

Any specific area of the project related to this PR?
/area rules

What this PR does / why we need it:

  • What this PR does

    • Modify condition to detect some process names within a container.
    • Add evt.dir to avoid detection of same syscall
  • Why we need it

    • The current rule exclude some process names used on host
      • i.e. k8s_binaries, nsenter
    • But nsenter could be used to do privilege escalation
    • So I would like to change condition to detect suspicious container activity.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

rule(Change thread namespace): Modify condition to detect suspicious container activity

@rung
Copy link
Contributor Author

rung commented Dec 16, 2019

Procedure Step

  • Start a container
kubectl run malicious-container --restart=Never -ti --rm --image lol --overrides '{"spec":{"hostPID": true, "containers":[{"name":"1","image":"alpine","command":["sh"],"stdin": true,"tty":true,"securityContext":{"privileged":true}}]}}'
  • Change namespace to host
nsenter --mount=/proc/1/ns/mnt -- /bin/bash

Sample Output

{"output":"16:46:42.320531675: Notice Namespace change (setns) by unexpected program (user=root command=nsenter --mount=/proc/1/ns/mnt -- /bin/bash parent=sh k8s.ns=default k8s.pod=malicious-container container=3172964a195b container_id=3172964a195b image=alpine:latest) k8s.ns=default k8s.pod=malicious-container container=3172964a195b k8s.ns=default k8s.pod=malicious-container container=3172964a195b","priority":"Notice","rule":"Change thread namespace","time":"2019-12-16T16:46:42.320531675Z", "output_fields": {"container.id":"3172964a195b","container.image.repository":"alpine","container.image.tag":"latest","evt.time":1576514802320531675,"k8s.ns.name":"default","k8s.pod.name":"malicious-container","proc.cmdline":"nsenter --mount=/proc/1/ns/mnt -- /bin/bash","proc.pname":"sh","user.name":"root"}}

evt.type = setns
and not proc.name in (docker_binaries, k8s_binaries, lxd_binaries, sysdigcloud_binaries,
sysdig, nsenter, calico, oci-umount, network_plugin_binaries)
evt.type=setns and evt.dir=<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change.evt.dir=<

@Kaizhe
Copy link
Contributor

Kaizhe commented Dec 16, 2019

/lgtm

@poiana
Copy link

poiana commented Dec 16, 2019

LGTM label has been added.

Git tree hash: c19ce5e70645012684c11623453c3f717782b2ef

@rung
Copy link
Contributor Author

rung commented Dec 18, 2019

I'll check why CI failed.

leodido
leodido previously approved these changes Dec 18, 2019
Copy link
Member

@leodido leodido left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚒

@rung
Copy link
Contributor Author

rung commented Dec 27, 2019

(hmm, I tried finding the reason to get error, but I haven't solved it yet)

@Kaizhe
Copy link
Contributor

Kaizhe commented Dec 27, 2019

@mstemm can you please help? The rule changes might break the existing tests. It would be great that you can point us to the right place. Thanks!

@fntlnz fntlnz changed the base branch from dev to master February 3, 2020 15:14
@fntlnz
Copy link
Contributor

fntlnz commented Feb 25, 2020

Closing and reopening this to let this test against the new CI

@fntlnz fntlnz closed this Feb 25, 2020
@fntlnz fntlnz reopened this Feb 25, 2020
@rung
Copy link
Contributor Author

rung commented Feb 25, 2020

oh, very sorry. I thought this was merged 🙇🙇.
I'll fix the conflict.

…container activity

Signed-off-by: Hiroki Suezawa <suezawa@gmail.com>
sysdig, nsenter, calico, oci-umount, cilium-cni, network_plugin_binaries)
evt.type=setns and evt.dir=<
and not (container.id=host and proc.name in (docker_binaries, k8s_binaries, lxd_binaries, nsenter))
and not proc.name in (sysdigcloud_binaries, sysdig, calico, oci-umount, cilium-cni, network_plugin_binaries)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was conflicted because of the PR (48a0f51), and I fixed the conflict.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you rebase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, after I got needs-rebase label from poiana. The point I changed in first commit is to add cilium-cni.

@poiana poiana removed the approved label Feb 26, 2020
Signed-off-by: Hiroki Suezawa <suezawa@gmail.com>
@rung
Copy link
Contributor Author

rung commented Feb 26, 2020

Report on deep dive (why regression tests failed).

Reason 1

Reason 2

  • Error Message
15:30:21 ERROR| FAIL 55-/source/falco/test/falco_test.py:FalcoTest.test;no-kube-demo-47e1 -> TestFail: Detected 188 events when should have detected none
  • traces-negative/kube-demo.scap has many nsenter processes

    • Old rule bypassed every nsenter, but my rule detects every nsenter on container. so the regression test failed.
    • it seems kubelet(or hyperkube kubelet) use nsenter binary, (I think kubelet runs on host basically though, the regression test runs it on container)
  • Sample

DEBUG| [stdout] {"output":"04:46:52.887250174: Notice Namespace change (setns) by unexpected program (user=root command=nsenter --mount=/rootfs/proc/1/ns/mnt -- /bin/findmnt -o target --noheadings --target /var/lib/kubelet/pods/352aff16-2f8d-11e6-8bd9-0800270a6574/volumes/kubernetes.io~secret/default-token-pb8gr parent=hyperkube k8s-kubelet (id=af911d183773) container_id=af911d183773 image=<NA>:<NA>)","priority":"Notice","rule":"Change thread namespace","time":"2016-06-11T04:46:52.887250174Z", "output_fields": {"container.id":"af911d183773","container.image.repository":null,"container.image.tag":null,"container.name":"k8s-kubelet","evt.time":1465620412887250174,"proc.cmdline":"nsenter --mount=/rootfs/proc/1/ns/mnt -- /bin/findmnt -o target --noheadings --target /var/lib/kubelet/pods/352aff16-2f8d-11e6-8bd9-0800270a6574/volumes/kubernetes.io~secret/default-token-pb8gr","proc.pname":"hyperkube","user.name":"root"}}
  • How to fix
    • add this line
not proc.pname in (hyperkube, kubelet)

@rung
Copy link
Contributor Author

rung commented Feb 26, 2020

@leodido @Kaizhe Could you review this PR when you have time?
CI passed finally.
Thanks.

@rung
Copy link
Contributor Author

rung commented Mar 6, 2020

@leodido @Kaizhe How about this PR?
Thank you! 👍

@poiana
Copy link

poiana commented Mar 12, 2020

LGTM label has been added.

Git tree hash: 17f1406746d56ff07ee0721850368ffb50565b69

@fntlnz
Copy link
Contributor

fntlnz commented Mar 12, 2020

Good job as always @rung !

Copy link
Member

@leodido leodido left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@poiana
Copy link

poiana commented Mar 12, 2020

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fntlnz, Kaizhe, leodido

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit 3067af5 into falcosecurity:master Mar 12, 2020
@leodido
Copy link
Member

leodido commented Mar 12, 2020

/milestone 0.21.0

@poiana poiana added this to the 0.21.0 milestone Mar 12, 2020
@rung rung deleted the rule-mod-setns branch March 12, 2020 16:30
@rung
Copy link
Contributor Author

rung commented Mar 12, 2020

@fntlnz @leodido Thank you for your review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants