Abuse Elevation Control Mechanism: Setuid and Setgid

An adversary may perform shell escapes or exploit vulnerabilities in an application with the setsuid or setgid bits to get code running in a different user’s context. On Linux or macOS, when the setuid or setgid bits are set for an application, the application will run with the privileges of the owning user or group respectively. [1]. Normally an application is run in the current user’s context, regardless of which user or group owns the application. However, there are instances where programs need to be executed in an elevated context to function properly, but the user running them doesn’t need the elevated privileges.

Instead of creating an entry in the sudoers file, which must be done by root, any user can specify the setuid or setgid flag to be set for their own applications. These bits are indicated with an "s" instead of an "x" when viewing a file's attributes via ls -l. The chmod program can set these bits with via bitmasking, chmod 4777 [file] or via shorthand naming, chmod u+s [file].

Adversaries can use this mechanism on their own malware to make sure they're able to execute in elevated contexts in the future.[2].

Ein Angreifer kann Shell-Escapes durchführen oder Schwachstellen in einer Anwendung mit den Bits setuid oder setgid ausnutzen, um Code in einem anderen Benutzerkontext laufen zu lassen. Wenn unter Linux oder macOS die Bits setuid oder setgid für eine Anwendung gesetzt sind, wird die Anwendung mit den Rechten des besitzenden Benutzers bzw. der besitzenden Gruppe ausgeführt. (Zitat: setuid man page). Normalerweise wird eine Anwendung im Kontext des aktuellen Benutzers ausgeführt, unabhängig davon, welcher Benutzer oder welche Gruppe Eigentümer der Anwendung ist. Es gibt jedoch Fälle, in denen Programme in einem erweiterten Kontext ausgeführt werden müssen, um ordnungsgemäss zu funktionieren, aber der Benutzer, der sie ausführt, die erweiterten Rechte nicht benötigt.

Anstatt einen Eintrag in der Datei sudoers zu erstellen, der von root vorgenommen werden muss, kann jeder Benutzer das setuid- oder setgid-Flag angeben, das für seine eigenen Anwendungen gesetzt werden soll. Diese Bits werden mit einem "s" anstelle eines "x" angezeigt, wenn Sie sich die Attribute einer Datei über ls -l ansehen. Das Programm chmod kann diese Bits über Bitmaskierung setzen, chmod 4777 [Datei] oder über die Kurzbezeichnung, chmod u+s [Datei].

Angreifer können diesen Mechanismus für ihre eigene Malware nutzen, um sicherzustellen, dass sie in Zukunft in erhöhten Kontexten ausgeführt werden kann.(Zitat: OSX Keydnap Malware).

Un adversaire peut effectuer des évasions de shell ou exploiter des vulnérabilités dans une application avec les bits setuid ou setgid pour obtenir du code s'exécutant dans le contexte d'un utilisateur différent. Sous Linux ou macOS, lorsque les bits setuid ou setgid sont définis pour une application, celle-ci s'exécutera avec les privilèges de l'utilisateur ou du groupe propriétaire respectivement. (Citation : page de manuel setuid). Normalement, une application est exécutée dans le contexte de l'utilisateur actuel, quel que soit l'utilisateur ou le groupe propriétaire de l'application. Cependant, il existe des cas où les programmes doivent être exécutés dans un contexte élevé pour fonctionner correctement, mais où l'utilisateur qui les exécute n'a pas besoin des privilèges élevés.

Au lieu de créer une entrée dans le fichier sudoers, ce qui doit être fait par root, tout utilisateur peut spécifier le drapeau setuid ou setgid à définir pour ses propres applications. Ces bits sont indiqués par un "s" au lieu d'un "x" lors de la visualisation des attributs d'un fichier via ls -l. Le programme chmod peut définir ces bits avec via un masquage de bits, chmod 4777 [file] ou via une dénomination abrégée, chmod u+s [file].

Les adversaires peuvent utiliser ce mécanisme sur leurs propres logiciels malveillants pour s'assurer qu'ils seront capables de s'exécuter dans des contextes élevés à l'avenir.(Citation : OSX Keydnap malware).

Un avversario può eseguire escape di shell o sfruttare vulnerabilità in un'applicazione con i bit setsuid o setgid per ottenere codice in esecuzione nel contesto di un utente diverso. Su Linux o macOS, quando i bit setuid o setgid sono impostati per un'applicazione, l'applicazione verrà eseguita con i privilegi dell'utente o del gruppo proprietario rispettivamente. (Citazione: pagina man di setuid). Normalmente un'applicazione viene eseguita nel contesto dell'utente corrente, indipendentemente da quale utente o gruppo possiede l'applicazione. Tuttavia ci sono casi in cui i programmi devono essere eseguiti in un contesto elevato per funzionare correttamente, ma l'utente che li esegue non ha bisogno dei privilegi elevati.

Invece di creare una voce nel file sudoers, cosa che deve essere fatta da root, qualsiasi utente può specificare il flag setuid o setgid da impostare per le proprie applicazioni. Questi bit sono indicati con una "s" invece di una "x" quando si visualizzano gli attributi di un file tramite ls -l. Il programma chmod può impostare questi bit con via bitmasking, chmod 4777 [file] o tramite denominazione stenografica, chmod u+s [file].

Gli avversari possono usare questo meccanismo sul proprio malware per assicurarsi di poter eseguire in contesti elevati in futuro.(Citazione: OSX Keydnap malware).

Login
ID: T1548.001
Sub-technique of:  T1548
Platforms: Linux, macOS
Permissions Required: User
Version: 1.0
Created: 30 January 2020
Last Modified: 27 March 2020
Translations:  DE FR IT EN
Provided by LAYER 8

Procedure Examples

ID Name Description
S0401 Exaramel for Linux

Exaramel for Linux can execute commands with high privileges via a specific binary with setuid functionality.[3]

S0276 Keydnap

Keydnap adds the setuid flag to a binary so it can easily elevate in the future.[2]

Mitigations

ID Mitigation Description
M1028 Operating System Configuration

Applications with known vulnerabilities or known shell escapes should not have the setuid or setgid bits set to reduce potential damage if an application is compromised. Additionally, the number of programs with setuid or setgid bits set should be minimized across a system.

Detection

ID Data Source Data Component
DS0017 Command Command Execution
DS0022 File File Metadata
File Modification

Monitor the file system for files that have the setuid or setgid bits set. Monitor for execution of utilities, like chmod, and their command-line arguments to look for setuid or setguid bits being set.

Überwachen Sie das Dateisystem auf Dateien, die die Bits setuid oder setgid gesetzt haben. Überwachen Sie die Ausführung von Dienstprogrammen wie chmod und deren Befehlszeilenargumente, um zu sehen, ob die Bits setuid oder setguid gesetzt wurden.

Surveillez le système de fichiers à la recherche de fichiers dont les bits setuid ou setgid sont activés. Surveillez l'exécution des utilitaires, comme chmod, et leurs arguments de ligne de commande pour rechercher les bits setuid ou setguid activés.

Monitorare il file system per i file che hanno i bit setuid o setgid impostati. Monitorare l'esecuzione di utility, come chmod, e i loro argomenti della linea di comando per cercare i bit setuid o setguid impostati.

References