- Ned Batchelder starts with a demonstration how dangerous
eval()really is;eval()is often used to execute Python expressions; as a primitive and naive sandbox for one-liners.Ned Batchelder starts with a demonstration how dangerous
eval()really is;eval()is often used to execute Python expressions; as a primitive and naive sandbox for one-liners.He then continued to try and apply the same principles to Python 3, eventually succeeding to break out with some helpful pointers.
He then continued to try and apply the same principles to Python 3, eventually succeeding to break out with some helpful pointers.
- Pierre Bourdon uses similar techniques to hack a python system at a hack-a-thon
Pierre Bourdon uses similar techniques to hack a python system at a hack-a-thon
- Strictly control the byte compilation of the Python code, or at least post-process the bytecode to remove any access to names starting with underscores.
Strictly control the byte compilation of the Python code, or at least post-process the bytecode to remove any access to names starting with underscores.
This requires intimate knowledge of how the Python interpreter works and how Python bytecode is structured. Code objects are nested; a module's bytecode only covers the top level of statements, each function and class consists of their own bytecode sequence plus metadata, containing other bytecode objects for nested functions and classes, for example.
This requires intimate knowledge of how the Python interpreter works and how Python bytecode is structured. Code objects are nested; a module's bytecode only covers the top level of statements, each function and class consists of their own bytecode sequence plus metadata, containing other bytecode objects for nested functions and classes, for example.
- You need to whitelist modules that can be used. Carefully.
You need to whitelist modules that can be used. Carefully.
A python module contains references to other modules. If you import
os, there is a local nameosin your module namespace that refers to theosmodule. This can lead a determined attacker to modules that can help them break out of the sandbox. Thepicklemodule, for example, lets you load arbitrary code objects for example, so if any path through whitelisted modules leads to thepicklemodule, you have a problem still.
A python module contains references to other modules. If you import os, there is a local name os in your module namespace that refers to the os module. This can lead a determined attacker to modules that can help them break out of the sandbox. The pickle module, for example, lets you load arbitrary code objects for example, so if any path through whitelisted modules leads to the pickle module, you have a problem still.
- You need to strictly limit the time quotas. Even the most neutered code can still attempt to run forever, tying up your resources.
You need to strictly limit the time quotas. Even the most neutered code can still attempt to run forever, tying up your resources.