This question is similar to Exploitable PHP Functions.
Tainted data comes from the user, or more specifically an attacker. When a tainted variable reaches a sink function, then you have a vulnerability. For instance a function that executes a sql query is a sink, and GET/POST variables are sources of taint.
What are all of the sink functions in Python? I am looking for functions that introduce a vulnerability or software weakness. I am particularly interested in Remote Code Execution vulnerabilities. Are there whole classes/modules that contain dangerous functionally? Do you have any examples of interesting Python vulnerabilities?
The
input
function, which evaluates the given string and returns the result, has some restrictions, but still may be exploitable.right from the pickle documentation:
I tend toward the paranoid when looking for this kind of thing. More so because I tend to do alot of metaprogramming.
open
,tarfile
,zipfile
, ...)urllib2
,socket
, ...)pickle
,shelve
, ...)subprocess
,os.fork
,os.kill
, ...)getattr
setattr
delattr
eval
exec
execfile
__import__
And probably others I'm forgetting. I'm also wary of user input going through functions where I'm modifying sys.path, sys.modules, etc.
The subprocess module contains nasty functionally which deprecated these ways of executing commands/processes:
There is also exec which will execute python code and eval which will "evaluate" an expression and can be used to manipulate variables.
eval
andexec
are the classics. However,open
andfile
can be abused too:Then there are the
os
,sys
,subprocess
, anddircache
modules. Pretty much anything that touches the filesystem or can be used to turn data into executable code (likeos.system
) is going to be on the list.As S. Lott pointed out in the comments, writing to the filesystem and executing arbitrary external programs aren't Python-specific. However, they are worth security auditors' consideration. Most of these functions can be safely used without too much concern for security.
eval
andexec
, on the other hand, are great big red flags. Using them safely requires meticulous care.