Writable /etc/hosts DNS Hijack
A /etc/hosts writable by an unprivileged user looks like nothing much: it grants no execution and reads nothing. What it grants is control over where a name points, and that is enough when something running as root resolves that name.
The full scenario is a periodic root task that talks to a host by name. While the name does not resolve, the task does nothing and nobody looks at it. If the attacker can write /etc/hosts, the name starts resolving to their own machine and the task starts talking to them, with the privileges of whoever runs it.
The mechanism. It is a chain of three pieces, and the point is that none of the three is a vulnerability on its own:
- A
roottask (cron, asystemdtimer, a boot script) that uses a hostname, not an IP. - An
/etc/hostswritable by the user we already control. You check it like any other sensitive file, andlinpeasor a plainls -lgives it away. - The task doing something useful when the name answers: connecting, downloading, mounting or, in this corpus's case, firing a reverse shell at that host.
The first move is to see the task, not guess it: pspy shows what root runs without reading /etc/crontab, and then you read the script to understand what it does with the name.
ls -l /etc/hosts
cat /opt/taskWith that clear, the hijack is one line:
echo '<attacker ip> <name.that.resolves>' > /etc/hostsFrom there all that is left is listening on whatever port the task uses and waiting for cron to fire. The shell that lands is root's, because root is who runs the task.
Requirements. Write access to /etc/hosts (or to another point of name resolution, such as /etc/resolv.conf or /etc/nsswitch.conf) and a privileged task resolving a name under our control. You also need the machine to be able to reach the IP you write in the file: in a chained lab that may mean a port forward.
How to detect it. The change to /etc/hosts is the event to watch and it is trivial to audit (auditd with a -w /etc/hosts -p wa rule, or any agent's file-integrity check): that file does not change on a stable machine. The second clue is network-side: a periodic task that had gone months without talking to anyone starts resolving and connecting to an internal IP that is not its own, in the same minute as its cron entry. Reviewing the permissions of /etc files that should not be writable is a preventive control cheaper than the detection.
Defence. /etc/hosts belongs to root with 644: anything else is a misconfiguration, not a decision. And privileged tasks should use fixed addresses or validate the destination, because the name is resolved by the system and the system is exactly what the attacker has just touched. It shares its root cause with the cron over a writable script: in both cases what fails is write permission over something root is going to use.
Machines where it's used
- Vulnerable Lab (1 · 4 · 5)VulnHub