# Custom Falco rules for the container runtime security lab
# Save as falco-rules/custom.yaml

- rule: Terminal Shell in Container
  desc: A shell was spawned in a container with an attached terminal
  condition: >
    spawned_process and container
    and proc.name in (bash, sh, zsh, fish, ksh, tcsh)
    and proc.tty != 0
  output: >
    Terminal shell spawned in container (user=%user.name container=%container.name image=%container.image.repository shell=%proc.name cmdline=%proc.cmdline)
  priority: WARNING
  tags: [container, shell, mitre_execution]

- rule: Suspicious Binary in Container
  desc: Execution of suspicious binaries commonly used for reconnaissance or exploitation
  condition: >
    spawned_process and container
    and proc.name in (nc, netcat, nmap, masscan, curl, wget, python3, perl, ruby, lua, node, php)
    and not proc.name in (python3)
  output: >
    Suspicious binary executed in container (user=%user.name container=%container.name binary=%proc.name cmdline=%proc.cmdline)
  priority: WARNING
  tags: [container, reconnaissance, mitre_discovery]

- rule: Write to Binary Directory
  desc: Writing to directories that should be immutable in containers
  condition: >
    open_write and container
    and (fd.name startswith /bin or fd.name startswith /sbin or fd.name startswith /usr/bin or fd.name startswith /usr/sbin or fd.name startswith /lib)
  output: >
    Write to binary directory in container (user=%user.name container=%container.name file=%fd.name proc=%proc.name)
  priority: ERROR
  tags: [container, persistence, mitre_persistence]

- rule: Mount Namespace Escape Attempt
  desc: Attempts to manipulate mount namespaces from within a container
  condition: >
    container and (evt.type in (mount, umount, unshare, setns, pivot_root))
  output: >
    Mount namespace manipulation detected (container=%container.name evt=%evt.type user=%user.name)
  priority: CRITICAL
  tags: [container, escape, mitre_privilege_escalation]

- rule: Access to Host Filesystem via Proc
  desc: Container accessing host filesystem through /proc
  condition: >
    open_read and container
    and fd.name startswith /proc/
    and fd.name contains /root/
  output: >
    Container accessing host filesystem via /proc (container=%container.name file=%fd.name proc=%proc.name)
  priority: WARNING
  tags: [container, escape, mitre_credential_access]

- rule: Kubernetes Service Account Token Access
  desc: Reading Kubernetes service account token from within a container
  condition: >
    open_read and container
    and fd.name = /var/run/secrets/kubernetes.io/serviceaccount/token
  output: >
    Service account token accessed (container=%container.name proc=%proc.name)
  priority: INFO
  tags: [container, kubernetes, credential_access]

- rule: Crypto Miner Binary Execution
  desc: Known cryptominer binary names executed in container
  condition: >
    spawned_process and container
    and proc.name in (xmrig, xmr-stak, cpuminer, minerd, cgminer, bfgminer, ethminer, claymore, phoenixminer, lolminer, teamredminer, nbminer, gminer, trex, cryptonight, monerod)
  output: >
    Cryptominer binary executed (container=%container.name binary=%proc.name cmdline=%proc.cmdline)
  priority: CRITICAL
  tags: [container, crypto_miner, mitre_resource_development]

- rule: Unexpected Outbound Connection
  desc: Container making outbound connection to non-standard port
  condition: >
    container and evt.type = connect and fd.net and fd.sport >= 1024
    and fd.dport not in (80, 443, 53, 5432, 3306, 6379, 27017, 9200, 5672, 1521, 1433)
  output: >
    Unexpected outbound connection (container=%container.name proc=%proc.name src=%fd.sip sport=%fd.sport dst=%fd.dip dport=%fd.dport)
  priority: WARNING
  tags: [container, network, mitre_command_and_control]