# Container Runtime Security Lab

A hands-on lab for learning runtime threat detection in containers using Falco.

## Overview

This lab sets up a minimal environment to experiment with Falco, a CNCF graduated runtime security tool. You'll learn to:

- Deploy Falco with eBPF instrumentation
- Write custom detection rules
- Simulate container attacks and observe alerts
- Understand what runtime detection catches (and misses)

## Prerequisites

- Linux machine or VM (Falco requires Linux kernel access)
- Docker and Docker Compose v2+
- Root/sudo access
- ~2 GB free disk space

## Quick Start

```bash
# Clone or download this directory
cd container-runtime-lab

# Start the lab
docker compose up -d

# Follow Falco logs (in another terminal)
docker compose logs -f falco
```

## Lab Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                        Host Machine                          │
│  ┌─────────────┐  ┌──────────────────┐  ┌────────────────┐  │
│  │   Falco     │  │   target-app     │  │    attacker    │  │
│  │ (monitor)   │  │  (hardened app)  │  │  (attack sim)  │  │
│  │             │  │                  │  │                │  │
│  │ - eBPF      │  │ - python:3.11    │  │ - ubuntu:22.04 │  │
│  │ - custom    │  │ - non-root       │  │ - privileged   │  │
│  │   rules     │  │ - read-only fs   │  │ - host pid     │  │
│  │ - JSON out  │  │ - dropped caps   │  │                │  │
│  └──────┬──────┘  └──────────────────┘  └───────┬────────┘  │
│         │                                        │            │
│         ▼                                        ▼            │
│  ┌─────────────────────────────────────────────────────────┐  │
│  │              Kernel Syscall Stream                       │  │
│  └─────────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘
```

## Running the Exercises

Open a terminal for the attacker container and run the test commands:

### 1. Shell Spawn Detection
```bash
docker exec -it attacker bash
bash
exit
exit
```

### 2. Suspicious Binary Execution
```bash
docker exec -it attacker bash
nc -h
python3 -c "print('hello')"
curl --version
exit
exit
```

### 3. Write to Binary Directory
```bash
docker exec -it attacker bash
echo "malicious" > /bin/evil
exit
exit
```

### 4. Mount Namespace Escape Attempt
```bash
docker exec -it attacker bash
mount --bind /tmp /mnt
unshare -m
exit
exit
```

### 5. Host Filesystem Access via /proc
```bash
docker exec -it attacker bash
cat /proc/1/root/etc/passwd
cat /proc/1/root/etc/shadow 2>/dev/null || echo "No access to shadow"
exit
exit
```

### 6. Cryptominer Binary Simulation
```bash
docker exec -it attacker bash
echo '#!/bin/bash\necho "mining..."' > xmrig
chmod +x xmrig
./xmrig
exit
exit
```

### 7. Unexpected Outbound Connection
```bash
docker exec -it attacker bash
nc -zv 8.8.8.8 4444 2>/dev/null || echo "Connection failed (expected)"
python3 -c "import socket; s=socket.socket(); s.connect(('8.8.8.8', 9999))" 2>/dev/null || echo "Failed"
exit
exit
```

## Expected Alerts

Each test triggers specific Falco rules. Look for these in the Falco logs:

| Test | Rule Triggered | Priority |
|------|---------------|----------|
| Shell spawn | Terminal Shell in Container | WARNING |
| nc/curl/python | Suspicious Binary in Container | WARNING |
| Write to /bin | Write to Binary Directory | ERROR |
| mount/unshare | Mount Namespace Escape Attempt | CRITICAL |
| /proc/1/root/ | Access to Host Filesystem via Proc | WARNING |
| xmrig binary | Crypto Miner Binary Execution | CRITICAL |
| nc to port 4444 | Unexpected Outbound Connection | WARNING |

## Understanding Falco Output

Falco outputs JSON. Key fields to examine:

```json
{
  "output": "Terminal shell spawned in container...",
  "priority": "WARNING",
  "rule": "Terminal Shell in Container",
  "time": "2026-11-16T10:30:45.123456789Z",
  "output_fields": {
    "container.id": "abc123...",
    "container.name": "attacker",
    "container.image.repository": "ubuntu",
    "proc.name": "bash",
    "proc.cmdline": "bash",
    "proc.pid": 1234,
    "proc.ppid": 1000,
    "user.name": "root",
    "user.uid": 0
  },
  "tags": ["container", "shell", "mitre_execution"]
}
```

Important fields:
- `container.name` / `container.image.repository` - Identifies the workload
- `proc.name` / `proc.cmdline` - What was executed
- `proc.pid` / `proc.ppid` - Process tree context
- `user.name` / `user.uid` - User inside container
- `tags` - MITRE ATT&CK mappings

## Customizing Rules

The `falco-rules/custom.yaml` file contains lab rules. To add your own:

1. Edit `falco-rules/custom.yaml`
2. Restart Falco: `docker compose restart falco`
3. Test your new rule

Rule writing tips:
- Use `container` macro to scope to containers only
- Leverage `proc.aname` (ancestor name) for context
- Tag with MITRE ATT&CK: `mitre_execution`, `mitre_persistence`, etc.
- Start with high-priority rules, tune down noise later

## Cleanup

```bash
# Stop and remove containers, networks, volumes
docker compose down -v

# Remove generated logs
rm -rf falco-output
```

## Troubleshooting

### Falco fails to start
- Ensure you're on Linux (not macOS/Windows without VM)
- Check kernel version: `uname -r` (needs 4.14+ for eBPF)
- Try kernel module mode: remove `--modern-bpf` from command

### No alerts appearing
- Verify Falco is running: `docker compose logs falco`
- Check rule syntax: `docker exec falco falco --validate`
- Ensure attacker container is privileged and using host PID namespace

### Permission denied on /proc
- The attacker container needs `privileged: true` and `pid: host`
- Target app is intentionally hardened (read-only, no caps)

## Next Steps

After completing the lab:

1. **Deploy to Kubernetes**: Use Falco Helm chart as DaemonSet
2. **Add Falcosidekick**: Forward alerts to Slack, Elasticsearch, Loki
3. **Write production rules**: Baseline your apps, then alert on deviations
4. **Explore Tetragon**: Cilium's eBPF-based runtime enforcement
5. **Integrate with SIEM**: Correlate Falco alerts with network and host logs

## References

- Falco Documentation: https://falco.org/docs/
- Falco Rules: https://falco.org/docs/rules/
- Falcosidekick: https://github.com/falcosecurity/falcosidekick
- MITRE ATT&CK for Containers: https://attack.mitre.org/matrices/enterprise/cloud/
- NIST SP 800-190: https://csrc.nist.gov/publications/detail/sp/800-190/final