Defencely Clarifies Python Object Injection Exploitation

 

Readers,

Welcome to Defencely Blog, This is Rony, part of the Red Teaming Operations Associate at Defencely Cloud Security Pvt. Ltd. & we are extremely delighted to present scenarios of exploitation of a recently conducted security operations for prestigious organizations in India & for Global Enterprises.

Today at Defencely Lab we are going to explain & demonstrate the Python Object Injection attack in minute details. The whole demonstration will be done with our coded intended vulnerable Application & Exploit which you can find at this Link –  Github – Python Object Injection

Contents – 

  • Introduction to Python Classes and Objects.
  • What is an Object Injection?
  • Detecting an Object Injection Attack.
  • Understanding the workflow of a Vulnerable Application.
  • Coding our own exploit against the intended Vulnerable Application.

Prerequisite – 

  • Basic Understanding on OOP Concepts.

Introduction to Python Classes and Objects.

 

What are Classes?

Class is basically a template where you store your variables & methods.

What are Objects?

Objects can be Anything, an instance of a class, a variable or a function in a class.

 

Lets go into the practical examples :-

Here you can see we have created an instance of the class named Test, and assigned the same to a variable named simpleapp passing the value of the variable rony to the Instance.

Output –

“ simpleapp = Test(rony) ”

When this particular code is executed python creates an object and then we are passing our value to the first parameter. Whenever python creates an object the __init__ function gets invoked. __init__ works like a constructor in python.

The random things which got printed with our output it’s because We are directly printing out the instance assigned variable to show how python is treating this as an object.

What is an Object Injection?

Object Injection is an Application Level Security Vulnerability that could allow an attacker to perform critical level attacks depending on the context.

Python specifically have its native module named as “Pickle” which is vulnerable to Object Injection on particular scenarios.

Python already lists pickle as risky module on their official documentation when user controlled data is passed.

We can compare the module “Pickle” with the serialize/unserialize() native functions in PHP which is also vulnerable to Object Injection when user inputs are supplied.

In Python we don’t need a magic methods as a condition to Inject into the Objects Unlike PHP.

Serializing and Deserializing in python is just Pickling and Unpickling of data.

Unpickling of data is NOT necessarily dangerous in Python until and unless user input data are passed to the process of Unpickling.

This is how Pickled and Unpickled data looks like in python –

 

Detecting an Object Injection Attack 

To achieve an Object injection, you have to do a white-box Pentest on a application. Because whenever you are pickling on complex Objects the serialized data in Python comes with the name of the class, variables & their values.

The Pickle module offers us four methods for easy and fast pickling/Unpickling.

  • dump()
  • dumps()
  • load()
  • loads()

You can find their respective functioning in Python’s Official Documentation.

As I already mentioned Unpickling of data is NOT necessarily dangerous, but If you are handling user inputs where in the backend you are pickling and unpickling the user inputted data that’s where the risk comes in. I quote – “Never Trust User Inputs”.

If the data supplied is user controlled it can obviously get tampered.

So, if you see pickled data is passing through any HTTP method, there might be a possibility of Object Injection.

 

Understanding the workflow of a Vulnerable Application

Filename: pickle.py

We will be studying the above code and tweak this accordingly to achieve an object injection.

Ignore everything else written on the code above let’s concentrate on the three things.

  • The user input which is the arg variable in this case.
  • The final_workout() method inside the class simpleApp which interestingly runs a python file.
  • The method which is called app.secureaApp() which is unpickling the inputted data.

Now lets dive deep and lets understand what role does these methods are playing.

The secureApp() method in the simpleApp() class

I am assuming that you probably have read the Python Official Documentation which i have already linked above & know the in’s and out’s of the methods used in this particular post by now.

Methods

  1. dump()
  2. dumps()
  3. load()
  4. loads()

What this secureApp() method is doing is taking a filename as an argument. Unpickling the data from the file using the load() method from the pickle module and assigning the value means the unpickled data to the variable workDone. Which is later on taken as an argument by the final_workout() method.

Let’s see what’s there on the final_workout() method.

The final_workout() method in the simpleApp() class –

This method creates a python file named code.py writes the unpickled data into the file and runs it. Simple right?

Let’s see how it looks like when we run our main vulnerable application pickle.py with the already generated serialized data.

As we can see it prints out the content from the serialized data and runs it successfully which prints out the string.

We will learn, crafting our own serialized data to do a successful object injection on the same application.

 

 

Coding our own Exploit

We now know that the pickle.py is working with the serialized data, so to serialize and making our own crafted payload we will be using the dumps() method to pickle our payloads.

And here is our final exploit to have our own serialized payload data which you can also found on the shared Github Link.

Filename:- exploit_pickle.py

 

We are going to serialize a piece of code which includes system commands with the exploit coded and test it if it works.

And we are now able to Successfully inject our own crafted codes.

I hope this will be a tremendous insight for such vulnerabilities which are well hidden in heap of crafted Enterprise Code & we are hoping to continue providing excellent security services for all kind of business houses & the enterprises in an absolute integrity.

This is Rony Das from Defencely Cloud Security Pvt. Ltd. & join you next week on Defencely Blog with more such highlights whereby our Defencely Lab exploitation goes beyond all measurements bypassing all kinds of security walls.

Thank you & have an excellent day ahead.