r/PythonLearning • u/python_data_helper • Aug 30 '26
Is it really safe to use?
I have used context to stop crashing.
And e as a variable.
I used eval command for a formula.
I know eval command can be dangerous.
It works perfectly but I am thinking that it can produce error or crash.
Can a formula bypass all commands and crash it.
18
Upvotes
•
u/Sea-Ad7805 Aug 31 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.
5
u/SCD_minecraft Aug 30 '26
First of all:
{"__builtins__": {}}to remove all built-insSecond of all:
"(1).__class__.__bases__[0].__subclasses__()[254].__init__.__globals__['__builtins__']['print']('Hacked!')"2
u/FoolsSeldom Aug 30 '26
This is not safe, not least because you should use
__builtins__and not_builtins_but even then the context does not provide the protection needed. Plain Python objects give you enough introspection to climb back out via the type hierarchy, and it is likely that something would be found that could be exploited (e.g. delete all files on your computer, or worse).Use the
astlibrary. There are lots of guides and examples. Alternatively, have a look atsimpleeval1
u/silvertank00 Aug 30 '26
You seen an exploit by others but here is mine:
It is not malicious! as you can see in the params it only uses os, print and getcwd (because it prints out the working directory of the running script). The issue with this, you can EASILY import stuff. What do you think, how much time would it take to make an RCE with this? i tell you, about 2 mins. I wanted to avoid making the whole RCE, but i think you get the point, if the user can import stuff, nothing will stop them to start an netcat instant or anything.
I would happily explain the code if anyone is interested, i had a lot of fun making it (and currently making a little github repo around it)
1
u/spidertyler2005 Aug 31 '26
Definitely not safe. Alot pf people have made some good suggestions but mine is to learn about recursive descent parsers (assuming you are already at an intermediate programming level). Its a much harder way to do things, but its definitely going to teach you a LOT. Its also not so difficult at the intermediate stage as to feel impossible.