Security Engineering

A Python automation script builds a ping command by concatenating a hostname from a ticket field and runs it with shell=True. Which change best prevents command injection?

Show the answer

Correct answerA. Pass the command as an argument list to subprocess without a shell

AWhy this is right: An argument list with no shell passes the hostname as a single argument, so metacharacters are not interpreted.

BWhy not this: Running with more privilege makes an injected command more damaging, not safer.

CWhy not this: os.system also invokes a shell and does not escape metacharacters.

DWhy not this: try/except handles exceptions but does not stop the injected command from executing.

Passing arguments as a list to subprocess without a shell means the hostname is treated as data, not parsed as shell syntax, so injected metacharacters have no effect. try/except only handles errors after the command has already run and does not stop injection.