AppArmor Profile Erstellen
AppArmor Profile erstellen und verwalten – Ubuntu Security
Was ist AppArmor?
AppArmor ist ein Mandatory Access Control (MAC) System, das vor allem auf Ubuntu, Debian und Suse zum Standard geworden ist. Es beschränkt die Zugriffe von Anwendungen auf Dateisystem, Netzwerk und andere Ressourcen basierend auf definierten Sicherheitsprofilen.
AppArmor ist im Vergleich zu SELinux:
- Einfacher zu verstehen: Path-basierte Regeln statt Kontexttypen
- Einfacher zu konfigurieren: Automatische Profil-Generierung möglich
- Standard auf Ubuntu: Vorinstalliert und aktiviert
- Weniger ressourcenintensiv: Optimiert für Desktop und Server
AppArmor Status überprüfen
AppArmor ist aktiv?
$ sudo aa-status apparmor module is loaded. 62 profiles are loaded. 60 profiles are in enforce mode. /snap/snapd/20092/usr/lib/snapd/snap-confine /usr/sbin/cupsd /usr/sbin/named /usr/sbin/sshd 2 profiles are in complain mode. /usr/sbin/man profile-demo 0 processes are unconfined but have a profile defined.
Nur geladen Profile anzeigen
$ sudo aa-status | grep "profiles are"
Spezifisches Profil prüfen
$ sudo aa-status | grep "sshd" /usr/sbin/sshd
AppArmor Modi
| Modus | Beschreibung | Verhalten |
|---|---|---|
| Enforce | Profil ist aktiv und blockiert Zugriffe | Verstöße werden blockiert und geloggt |
| Complain | Profil ist im Diagnose-Modus | Verstöße werden nur geloggt, nicht blockiert |
| Disabled | Profil ist deaktiviert | Keine Einschränkungen |
Profile verwalten
Alle Profile auflisten
$ sudo aa-status --json | jq '.profiles | keys' [ "/snap/snapd/20092/usr/lib/snapd/snap-confine", "/usr/lib/snapd/snap-confine", "/usr/sbin/cupsd", "/usr/sbin/named", "/usr/sbin/sshd" ]
Profil in Enforce-Modus setzen
$ sudo aa-enforce /usr/sbin/sshd Setting /usr/sbin/sshd to enforce mode.
Profil in Complain-Modus setzen
$ sudo aa-complain /usr/sbin/sshd Setting /usr/sbin/sshd to complain mode.
Profil deaktivieren
$ sudo aa-disable /usr/sbin/sshd Disabling /usr/sbin/sshd.
Profil wieder aktivieren
$ sudo aa-enforce /usr/sbin/sshd
Bestehende AppArmor Profile verstehen
Profil-Verzeichnis
AppArmor Profile befinden sich in:
$ ls -la /etc/apparmor.d/ total 256 drwxr-xr-x 7 root root 4096 Mar 5 12:34 . drwxr-xr-x 38 root root 4096 Mar 5 12:33 .. -rw-r--r-- 1 root root 1234 Jan 15 08:23 sshd drwxr-xr-x 2 root root 4096 Jan 15 08:23 abstractions drwxr-xr-x 2 root root 4096 Jan 15 08:23 local
Beispiel-Profil untersuchen
$ cat /etc/apparmor.d/sshd
#include <tunables/global>
/usr/sbin/sshd {
#include <abstractions/base>
#include <abstractions/nameservice>
#include <abstractions/openssl>
capability chown,
capability kill,
capability setuid,
capability setgid,
capability net_bind_service,
/etc/ssh/sshd_config r,
/var/log/auth.log w,
/var/run/sshd.pid w,
/dev/urandom r,
/lib/x86_64-linux-gnu/** mr,
}
Profil-Syntax erklärt
Capabilities: Linux-Kernel-Fähigkeiten (setuid, net_bind_service, etc.)
File Rules:
r= readw= writex= executem= memory map executablek= lock*= wildcard (ein Verzeichnis-Level)**= recursive wildcard (alle Verzeichnis-Level)
Abstractions: Wiederverwendbare Profile-Schnipsel
$ ls /etc/apparmor.d/abstractions/ base nameservice ssl_keys apache-common python ruby perl openssl
Custom AppArmor Profile erstellen
Szenario: Python-Skript schützen
Wir erstellen ein Profil für ein einfaches Python-Skript /opt/myapp/app.py.
Schritt 1: Automatische Profil-Generierung starten
$ sudo aa-genprof /opt/myapp/app.py Profiling your application. Keep interacting with the application until you see the prompt for saving the profile. Make sure you exercise all the actions you expect to perform with the application.
Das Skript wird jetzt ausgeführt:
$ python3 /opt/myapp/app.py
Interagiere mit der Anwendung, um alle Operationen zu erfassen.
Schritt 2: Profil-Vorschläge bestätigen
Nach Beendigung der Anwendung zeigt aa-genprof alle erkannten Zugriffe:
(I)nherit, (P)rofile new child, (N)ew subprofile, (X) (D)eny, (A)llow, (AA) AllowAudit, (L)earned (E)xit, (S)ave
Bestätige jeden Zugriff mit A (Allow) oder D (Deny).
Schritt 3: Profil speichern
(S)ave
Das Profil wird gespeichert in:
$ cat /etc/apparmor.d/opt.myapp.app
#include <tunables/global>
/opt/myapp/app {
#include <abstractions/base>
#include <abstractions/python>
/opt/myapp/app r,
/opt/myapp/config.json r,
/tmp/* rw,
/var/log/myapp.log w,
}
Profil manuell bearbeiten
Für feinere Kontrolle kannst du das Profil direkt bearbeiten:
$ sudo nano /etc/apparmor.d/opt.myapp.app
Profile laden und neu laden
Profil laden (neu oder Update)
$ sudo apparmor_parser -r /etc/apparmor.d/opt.myapp.app Loading profile for /opt/myapp/app.
Alle Profile neu laden
$ sudo systemctl reload apparmor
Syntax vor dem Laden prüfen
$ sudo apparmor_parser -T -d /etc/apparmor.d/opt.myapp.app
Fehlerbehandlung und Debugging
Denials im Syslog finden
Wenn AppArmor eine Aktion blockiert, wird dies geloggt:
$ sudo tail -f /var/log/syslog | grep apparmor Mar 5 14:32:10 ubuntu kernel: [12345.678] apparmor="DENIED" operation="open" profile="/opt/myapp/app" name="/etc/shadow" pid=5678 comm="python3" requested_mask="r" denied_mask="r" fsuid=1000 ouid=0
Mit aa-logprof interaktiv Denials beheben
aa-logprof zeigt alle Denials und fragt, ob sie erlaubt werden sollen:
$ sudo aa-logprof Reading log entries from /var/log/syslog... The following denial was not previously seen: [1 - /opt/myapp/app] deny /etc/shadow r, (A)llow, (D)eny, (I)gnore, (O)ther (Nested) Profile? A
Troubleshooting Checkliste
| Problem | Diagnose | Lösung |
|---|---|---|
| Anwendung kann Datei nicht lesen | grep apparmor /var/log/syslog zeigt DENIED |
aa-logprof und Allow hinzufügen |
| Netzwerk-Zugriff blockiert | ausearch zeigt network_denied |
Capability network inet hinzufügen |
| Profil lädt nicht | apparmor_parser -d zeigt Fehler |
Syntax überprüfen, geschweifte Klammern |
Profile Zusammenfassung
| Befehl | Beschreibung |
|---|---|
sudo aa-status |
AppArmor und alle Profile anzeigen |
sudo aa-enforce PATH |
Profil in Enforce-Modus |
sudo aa-complain PATH |
Profil in Complain-Modus (Diagnose) |
sudo aa-disable PATH |
Profil deaktivieren |
sudo aa-genprof PATH |
Neues Profil generieren |
sudo aa-logprof |
Denials interaktiv beheben |
sudo apparmor_parser -r |
Profil neu laden |
sudo systemctl reload apparmor |
Alle Profile neu laden |