Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple explanation of security issues related to input() vs raw_input() [duplicate]

I was reading this Python 2.7 tutorial and they're going over raw_input(), and it mentions that:

The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it.

I tried Googling some explanations for this, but still a bit unclear to me; what's a simple explanation of the alleged inherent security issues with input() vs raw_input() ?

like image 613
AdjunctProfessorFalcon Avatar asked Feb 09 '23 16:02

AdjunctProfessorFalcon


1 Answers

The input() function in Python 2.x evaluates things before returning.

So as an example you can take a look at this -

>>> input("Enter Something : ")
Enter Something : exit()

This would cause the program to exit (as it would evaluate exit()).

Another example -

>>> input("Enter something else :")
Enter something else :__import__("os").listdir('.')
['.gtkrc-1.2-gnome2', ...]

This would list out the contents of current directory , you can also use functions such as os.chdir() , os.remove() , os.removedirs() , os.rmdir()

like image 97
Anand S Kumar Avatar answered Feb 12 '23 06:02

Anand S Kumar