好吧,我有一个必须在shell=True模式下执行的命令.
os.system 要么 subprocess.Popen(..., shell=True)
此命令包含字符串替换,如: cmd = "some_secret_command {0}".format(string_from_user)
我想要逃避string_from_user变量来防止任何注射.
简单的错误答案:
shlex.quote- 不正确print(shlex.quote('file.txxt; &ls . #'))- > 'file.txxt; &ls . #'(注射)
例:
> python -c "import sys; print(sys.argv[1])" 'file.txxt; &ls . #'
secret.txt
secret2.txt
Run Code Online (Sandbox Code Playgroud)
^- 不正确 例:
import os
CMD = '''string with spaces'''.replace('', '^').replace('^"', '')
os.system('python -c "import sys; print(sys.argv[1])" {0}'.format(CMD))
Run Code Online (Sandbox Code Playgroud)
现在我可以使用 (space) and inject more then one argument.
^和"或'- 不正确 …