> For the complete documentation index, see [llms.txt](https://rodgar.gitbook.io/rodgar/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rodgar.gitbook.io/rodgar/othe/escalada-de-privilegios/path-hijacking.md).

# Path Hijacking

Cuando ejecutamos un comando en una terminal o un cmd, como sabe la shell que esa palabra que hemos escrito corresponde a un comando con X función. ¿Qué decide que un comando sea detectado y otro no?:

Todo esto es gracias al PATH. El path es una variable de entorno la cual contiene rutas del sistema. Cuando ejecutamos un comando, el sistema va buscando algún archivo con el nombre del comando que hemos escrito, en cada ruta del path.

<figure><img src="/files/CEUVvvKkGE2XBawrP26k" alt=""><figcaption></figcaption></figure>

Usando strings vemos un binario compilado en C donde whoami uno tiene la ruta absoluta el primero pero el segundo la ruta es relativa. Esto da para un Path Hijacking.

<figure><img src="/files/TKiBGuGDymX5cNAspFxQ" alt=""><figcaption></figcaption></figure>

Si primero hacemos un echo del PATH observamos que empieza en /usr/local/sbin/etc.

Pero comos segundo si hacemos un export del PATH para que inicie desde la ruta /tmp, luego vemos que si hacemos un echo $PATH ahora el PATH inicia en /tmp/ y no en /usr/local/sbin.

<figure><img src="/files/5eUhJR4MzMaMinI5gW6M" alt=""><figcaption></figcaption></figure>

Ahora creamos el binario whoami, y le damos permisos de ejecucion.

Lo que pasara es que este binario esta alojado en /tmp/ y recordar que el PATH lo cambiamos ahora inicia en esta ruta no en la ruta absoluta entonces tomara el nuestro en vez del original.

<figure><img src="/files/vtr7pwcZDlolld7OoGzz" alt=""><figcaption></figcaption></figure>

Se ejecuta y listo somos root.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://rodgar.gitbook.io/rodgar/othe/escalada-de-privilegios/path-hijacking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
