0

I am wondering what attacks would be theoretically possible against a Python script that executes a subprocess involving sudo. I know that asking for the sudo password using input() or getpass(), then passing it to the subprocess's stdin could be exploited since the password string would enter the Python script's memory. I also know about injection attacks, but let's consider a non-parameterized example like sudo whoami.

But what about the following, where the Python script and the subprocess share the stdin file handle:

p = subprocess.Popen("sudo whoami", shell=True, text=True)
stdout, stderr = p.communicate()

In this case the stdin of the Python process is passed to the child, and sudo would read the password from it directly. Is there a way to grab the password - assuming the attacker has some access to the Python process's memory?

Attack scenarios:

  1. The attacker has compromised the target user through some other process, gained shell access, but does not know their sudo password. Then the target logs in and executes this Python script. Can the attacker fish the password out?
  2. Let's say that the script is a long running interactive app, like a custom command interpreter (similar to meterpreter). There is some vulnerability in another part of the script, like an unsafe eval(), which the attacker can control. What should the attacker do to get the password?
3
  • 1
    I'm not sure who is the attacker in your case. It cannot be the user executing the script - because they could of course do sudo stuff even without the script. It cannot be a compromised script either - because this could of course do anything. It can also not be the attacker executing the script, since they have no more privileges using the script than they would have without. Commented Dec 1 at 11:49
  • @SteffenUllrich I updated my question to cover two cases where the attacker has control outside or inside the running Python process. Commented Dec 1 at 12:16
  • 1
    If the attacker can influence what the user is executing (like when using a vulnerability in the script) the attacker does not need to catch the sudo password but could simply execute its own sudo commend because as long as the sudo session is not timed out. Commented Dec 1 at 16:12

0

You must log in to answer this question.

Browse other questions tagged .