

Escaping a Python sandbox (NdH 2013 quals writeup) - delroth
http://blog.delroth.net/2013/03/escaping-a-python-sandbox-ndh-2013-quals-writeup/

======
Erwin
I recommend Zope's Restricted Python:
<https://pypi.python.org/pypi/RestrictedPython> for that sort of thing (this
is not like Python's now-dead restricted mode).

Restricted Python will recompile your code from something like "x.y(42)" into
your_getattr(x, "y")(42). It will disallow access of any names starting with
_, a number of other unsafe constructs etc.

So you can set up your sandbox as restricted as you want. For example the
objects I expose have each an ACL so I can give users access to some system
object's specific properties while disallowing modifications of them.

------
justincormack
Tl;dr do not try to create a Python sandbox it is not a good language for it.

~~~
lucian1900
Or create a real sandbox, with interpreter support, like the PyPy one.

~~~
chubot
Hm how new is this? Any projects using it? It looks pretty good from the docs:

<http://pypy.readthedocs.org/en/latest/sandbox.html>

~~~
pekk
It's really not new at all. Pretty clear demonstration that Python as a
language is not incompatible with sandboxing

------
chrismorgan
Did you try grabbing the code of the ``auth`` function so that you could
discover what you were supposed to do?

~~~
kaoD
From the article:

> You can get the code object of a function using myfunc.func_code. This is
> forbidden in the restricted mode of the Python interpreter, so we can’t see
> the code of the auth function.

------
lvh
You can get access to the usual builtins using a lambda function's
func_globals.

------
pekk
Is it really news to concoct exploits against obscure, antiquated practices
which nobody uses because they have been known as insecure for years?

------
MostAwesomeDude
The regulars of #python have known about this for a while; here's a decent
writeup:
[http://nedbatchelder.com/blog/201206/eval_really_is_dangerou...](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html)

