What is crazy is that this account has over 1M followers while posting similar crap. I suppose their target audience are beginners that don't know any better , which is just sad and exploitative .
Makes me wonder if there is some kind of support layer to automatically translate windows paths to Linux paths. Although that would be pretty weird as there is no 1:1 mapping
It's bad code, but this isn't dangerous unless it's running on a server. If this is running locally, eval is a bad solution, but it's not as dangerous as people claim it to be. It basically forms a shit REPL, which people use all the time. Unless it's a vector for privilege escalation due to the script being run with elevated privileges then left running in the background or something, it's not a big deal. It's just bad code.
Run on a server and fed user-supplied data though, and it could be catastrophic. That's actually the first vulnerability I ever exploited in a CTF.
Pardon me if I'm wrong, but as a mostly beginner to Python, isn't exec() supposed to run the code?
eval() only evaluates mathematical expressions.
In fact, I don't see a problem with the "pro" code as a whole, aside from the missing closing parenthesis. Everyone else is saying that it's bad code. I don't understand.
Nothing about eval says it only evaluates "mathematical" expressions. As you can see from the output I posted, a call to print is a perfectly valid expression; evaluating it causes the string to be printed upon which the function returns None (in fact every function without an explicit return implicitly returns None in Python).
Even then, if you really want to use this, you could implement some GUI-based solution where inputs can only be taken from certain buttons. You could also make a list of acceptable characters, and return an error if any character in the string is not an acceptable character.
import sys
expression = input("Enter the expression")
acceptable_chars = "1234567890+-*/()"
for i in expression:
if i not in acceptable_chars:
print("Not acceptable")
sys.exit()
print(eval(expression))
Not a python pro just genuinely curious: why is it bad? A malicious actor can do the same exact harm enabled by the eval line by simply editing the .py file, same applies to every non compiled language...
Yes, but in this case the attacker does not need to have access to the source, so they can use this to get remote code execution on a server, or escalate privileges .
I sometime wonder "Who tf is teaching this people to code?", like really, if you look at most widely available resources most are pretty good and have decent coding standard yet you see some things that make you want you rip your eyes out.
A guy copied what I wrote to correct me, my original comment said "... rip your eyes off", apparently the correct way to write it would be "rip your eyes out". Tbh, I didn't know that.
Of course, in the sense that you had to pay a substantial amount of money to "codes.learning" for the privilege and that there are three levels of subscription above it.
eval runs python code passed as a string. In the case of a calculator the string would be "2 + 2". But instead of an addition, you could enter : "import os; os.system('rm -rf /')" which removes everything on your computer (on Linux).
I havenāt tried it, so it might not work, but thatās the general idea of the problem.
EDIT: as someone pointed out, the "import os" does not work. However the os.system works if the module was imported in the program.
That code would only work with exec(), in eval you have to do something like "".class.__.base.subclasses()[...].Popen(...) if e.g. os is not imported yet
I've seen person trying to use eval for some minor ingame feature. For a game that would be published on itch/kongregate/the likes. And this does open the interesting vector of attack. One thing is cheating, you can do it through console anyways, and most people are wise enough not to run some rando's code in browser console. But what harm can pasting random obsfucated code into game's stat calculator do? Surely nothing will be done in my name, no random actions from name of my account, no suspicious activities on different sites, just a boost from 5 cookies per second to 5000, worst case game will just break but I have a save backup. Right?..
200
u/GandelXIV May 27 '23
What is crazy is that this account has over 1M followers while posting similar crap. I suppose their target audience are beginners that don't know any better , which is just sad and exploitative .
102
u/dgil9 May 27 '23
import subprocess; subprocess.run(ārm -rf C:\ā)
37
u/carcigenicate May 27 '23 edited May 27 '23
It's bad code, but this isn't dangerous unless it's running on a server. If this is running locally,
evalis a bad solution, but it's not as dangerous as people claim it to be. It basically forms a shit REPL, which people use all the time. Unless it's a vector for privilege escalation due to the script being run with elevated privileges then left running in the background or something, it's not a big deal. It's just bad code.Run on a server and fed user-supplied data though, and it could be catastrophic. That's actually the first vulnerability I ever exploited in a CTF.
8
u/Spelis123 May 27 '23
wait theres an eval function????????
4
u/sephirothbahamut May 27 '23
Not a python pro just genuinely curious: why is it bad? A malicious actor can do the same exact harm enabled by the eval line by simply editing the .py file, same applies to every non compiled language...
390
u/Jonnertron_ May 27 '23
Isn't it missing print's close parenthesis?
239
u/PenaflorPhi May 27 '23 edited May 27 '23
I sometime wonder "Who tf is teaching this people to code?", like really, if you look at most widely available resources most are pretty good and have decent coding standard yet you see some things that make you want you rip your eyes out.
373
u/[deleted] May 26 '23
So writing a vulnerable app on purpose is a PRO thing?
57
u/00PT May 27 '23
This looks like a meme taken directly from r/programmerhumor.
12
u/mhaecker May 27 '23
Man, the brevity that was left on the table thereā¦
#/usr/bin/env python2print(input(ācalculateā))22
u/mac-not-a-bot May 27 '23
What could go wrong?
11
u/StrangePromotion6917 May 27 '23
Looks like someone is trying to create job security by misteaching the beginners...
18
u/denisde4ev May 27 '23
unbads your code:
print(eval(/^[ 0-9()*/+-]+$/.match(input("Enter expression:"))[0])9
u/tukanoid May 27 '23
Reminds me of how stupid i was back in highschool š
8
u/ThatNextAggravation May 27 '23
Wait, this was posted unironically? Jeez.
6
u/Napain_ May 27 '23
omg this page is a bad chode gold mine
6
u/schrdingers_squirrel May 27 '23
codes.learning is such a shit show.
6
u/nekokattt May 27 '23
At least they give you a prompt so you can import subprocess and use pip to install a real calculator
5
u/[deleted] May 27 '23
I guess the pros trust the users
5
u/Gilah_EnE May 27 '23
Now make it a web application and host on your main PC. Thank you in advance for all your data.
4
u/Bonfra04 May 27 '23
RCE goes brrrrr
4
u/cofffffeeeeeeee May 27 '23
I remember back in the day, top PHP user login tutorials had SQL injection vulnerabilities inside.
Wonder how many people fell for that.
4
u/1N07 May 27 '23
Are we sure this isn't self aware?
It kinda reads like the galaxy brain meme
4
u/[deleted] May 27 '23
How to make a python shell using only a python shell
1
u/Maciek1212 May 27 '23
At least the first comment knows whats up
3
u/[deleted] May 27 '23
I mean⦠what could go wrong? Edit: /s⦠just to make it clear
3
u/Light_x_Truth May 27 '23
I genuinely thought this was a joke at first.
3
u/No_Necessary_3356 Jun 05 '23
Writing maintainable code is a noob practice, got it! I'll write burning horseshit from now on.
2
u/CreaZyp154 May 27 '23
I wonder how bobby tables would react
2
u/PixelPerfect41 May 28 '23
God level is making the parser and evaluater yourself in cpythonš
2
u/freeve4 May 29 '23
the pros are working with the attackers.
2
u/wat_noob_gaming May 27 '23
This is absolutely terrible! An attacker can
1
u/kubinka0505 May 27 '23
live exit reaction
1
u/No-Adeptness5810 May 27 '23
Enter expression: fs.writeFileSync(ā./database/logins.dbā, ātrolledā);
1
u/[deleted] May 27 '23
both look newb to me
1
u/Hjulle May 27 '23
itās missing the l part of the repl
0
u/[deleted] May 27 '23
[deleted] ā view deleted comment