Skip to main content

Secure unknown python code execution

Project description

SecurePy

SecurePy is a project which aims to allow very secure unknown python code execution without worries.

Functionality

SecurePy has 2 ways of restricting your executions:

  • securepy.Restrictor this is a way to execute python code directly, using exec in a protected way. It tries to remove as many potentially harmful builtin classes and functions as it can, in order to run the given code safely.
  • securepy.Sandbox this is only available for Linux based devices and currently requires a privilege escalation (must be run as root). This is because it uses nsjail to create an isolated environment for the python script to run. This option is much safer but it's currently unfinished and it can't be used in production. (It's being developed in the nsjail branch)

Critical note

Even though this library is able to safely execute most unknown python source codes, you should still be very careful with granting someone access to random code execution as it is hard to cover everything.

You should also take note that this library is still relatively new and the development is still in progress, although the basic implementation is working and is relatively secure.

Usage

In order to use this library, you must first download it from PyPi: pip install securepy

Restrictor

import securepy

restrictor = securepy.Restrictor(max_exec_time=3, restriction_scope=2)
stdout, exc = restrictor.execute("""
[your python code here]
""")

max_exec_time parameter is a way to specify the maximum amount of seconds the code will be allowed to run for until interruption. restriction_scope parameter is a way to specify how restricted the code should be. These are the currently available scopes:

  • 0: No restriction (regular exec)
  • 1: Restricted globals (removed some unsafe globals)
  • 2 (RECOMMENDED): Secure globals (only using relatively safe globals)
  • 3: No globals (very limiting but quite safe)

stdout and exc variables will hold the outputs from your code. exc will hold the exception if there is some (otherwise it will be None). stdout will hold the simple standard output (print output).

Sandbox (NsJail)

This is only available in nsjail branch and it isn't production ready, only use it for testing purposes until it's finished.

You'll need to run your script as root user (or using sudo) because nsjail requires mounting privileges and some more file/floder management permissions, this is not a security issue as the script inside of nsjail is completely isolated and it can't touch the main system even with root privileges.

You'll also have to have nsjail installed and manually create a directory /securepy which will be used as the working directory for nsjail.

from pathlib import Path
import securepy

sandbox = securepy.Sandbox(
    nsjail_path=Path("/usr/sbin/nsjail"),  # This is path to nsjail binary (not necessary if left default)
    python_path=Path("/usr/bin/python"),  # This is path to python binary (not necessary if left default)
    allowed_memory=64_000_000,  # Maximum allowed memory: 64 MB
    max_stdout=1_000_000,  # Maximum of 1MB of stdout/stderr data
    read_chunk_size=10_000,  # Read from stdout/stderr by chunks of 10KB
    time_limit=6,  # Set maximum execution time to 6 seconds
)

Maximum time limiting

SecurePy has the ability to stop given function if it takes over certain given maximum execution time.

from time import sleep
import securepy

# Decorator
@securepy.TimedFunction(3)  # Limit this function to 3 seconds
def foo():
    sleep(5)  # Function will take 5 seconds (> 3s limit)
    return 2

foo()  # <-- this will raise `TimeoutError` after 3 second limit and the execution of the function will be automatically stopped.

# Wrapper
timed_func = securepy.TimedFunction(3)

def foo():
    sleep(1)
    return 2

timed_foo = timed_func(foo)

timed_foo()  # <-- this will return `2` after 1 second, since it didn't reach the given limit

# Exceptions
@securepy.TimedFunction(3)  # Limit this function to 3 seconds
def foo():
    raise TypeError("example exception")

foo()  # <-- this will raise `securepy.TimedFunctionError` and the original exception will be stored in `TimedFunctionError.inner_exception`

Important note: Running timed functions requires running them as separate processes in order to be able to terminate them after time limit was reached. This means that you might encounter some issues if you want to access/change certain variables because they'll exist in separate process. If you need to obtain some extra variables, the best approach would be to subclass TimedFunction and override _capture_return and _value_return functions to your needs.

STDOUT/STDERR Capturing

SecurePy has the ability to run a function in a STD capturing mode which will redirect STDOUT/STDERR and store it internally so that it can be accessed later on as a string.

import securepy

captured_std = securepy.StdCapture(auto_reset=True)

# Context Manager:
with captured_std:
    print("hello")

# Wrapper:
def foo(*args, **kwargs):
    print("hello")

captured_std.capture(foo, args=None, kwargs=None)

# Decorator:
@captured_std
def foo(*args, **kwargs):
    print("hello")

foo(*args, **kwargs)

# Getting STDOUT
captured_std.stdout  # <-- will contain the captured STDOUT (str): "hello\n"
# Getting STDERR
captured_std.stderr  # <-- will contain the captured STDERR (str): ""

auto_reset parameter passed into StdCapture is a bool which guides whether stored stdout should keep being added to or if it should reset itself once function ends. Default value is True. Note that if you set this to False you'll have to reset manually with StdCapture.reset().

Capturing STDOUT/STDERR with Time Limiting

If you need to capture STDOUT/STDERR for a function which you also want to time-limit, you can use CapturingTimedFunction which works very similarly to TimedFunction but apart from max time, it also takes the StdCapture class

import securepy

std_capture = securepy.StdCapture()

@securepy.TimedFunction(time_limit=2, std_capture=std_capture)
def foo(value):
    print("hello")
    return value

foo(2)  # <-- will return `value` (2)

std_capture.stdout  # <-- will hold the captured stdout string: "hello\n"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

securepy-0.5.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

securepy-0.5.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file securepy-0.5.0.tar.gz.

File metadata

  • Download URL: securepy-0.5.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for securepy-0.5.0.tar.gz
Algorithm Hash digest
SHA256 a38bf0b83b27eb3a363cdca4904c774cfb7ca99a0e40e6e8458821c4c2332717
MD5 7057e6e4136ce477916682c6e97fbf73
BLAKE2b-256 c726c5f42a6ca0c37f0e71dc5709a72202b36942a084eb1a80b22581c98a1f59

See more details on using hashes here.

File details

Details for the file securepy-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: securepy-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for securepy-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bdce46e0a70dba40fe8cd48c8b7a4f1ea3ec5f425e777c4419c2ab666af20b38
MD5 1b9e4d696b1cc0adab742b289f199969
BLAKE2b-256 693e5467522471f4e8b05c9b695460ede46611f5b75fa7e92e872d5bbc31f512

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page