如何在Windows上安全地转义cmd.exe shell的命令行参数?

前端之家收集整理的这篇文章主要介绍了如何在Windows上安全地转义cmd.exe shell的命令行参数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我有一个必须在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. #'(注射)

例:

  1. > python -c "import sys; print(sys.argv[1])" 'file.txxt; &ls . #'
  2. secret.txt
  3. secret2.txt

>使用转义^ – 不正确

例:

  1. import os
  2.  
  3. CMD = '''string with spaces'''.replace('','^').replace('^"','')
  4. os.system('python -c "import sys; print(sys.argv[1])" {0}'.format(CMD))

现在我可以使用(空格)并注入多个参数.

>使用^和“或” – 不正确

例:

  1. import os
  2.  
  3. CMD = '''some arg with spaces'''.replace('','')
  4. os.system('python -c "import sys; print(sys.argv[1])" "{0}"'.format(CMD))

打印^ s ^ o ^ m ^ e ^ ^ a ^ r ^ g ^ ^ w ^ i ^ t ^ h ^ ^ s ^ p ^ a ^ c ^ e ^ s ^

而如果 ‘

  1. import os
  2.  
  3. CMD = '''some spaces'''.replace('','^').replace('^\'','')
  4. os.system('python -c "import sys; print(sys.argv[1])" \'{0}\''.format(CMD))

打印’一些

我现在关于shell = False,但这对我来说是不正确的.

引用Windows命令行的问题在于有两个分层的解析引擎受到引号的影响.首先,有一个Shell(例如cmd.exe)可以解释一些特殊字符.然后,有一个被调用的程序解析命令行.这通常发生在Windows提供的CommandLineToArgvW函数中,但并非总是如此.

也就是说,对于一般情况,例如,使用cmd.exe和一个用CommandLineToArgvW解析其命令行的程序,你可以使用Daniel Colascione在Everyone quotes command line arguments the wrong way中描述的技术.我原本试图将它改编为Ruby,现在尝试将其转换为python.

  1. import re
  2.  
  3. def escape_argument(arg):
  4. # Escape the argument for the cmd.exe shell.
  5. # See http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
  6. #
  7. # First we escape the quote chars to produce a argument suitable for
  8. # CommandLineToArgvW. We don't need to do this for simple arguments.
  9.  
  10. if not arg or re.search(r'(["\s])',arg):
  11. arg = '"' + arg.replace('"',r'\"') + '"'
  12.  
  13. return escape_for_cmd_exe(arg)
  14.  
  15. def escape_for_cmd_exe(arg):
  16. # Escape an argument string to be suitable to be passed to
  17. # cmd.exe on Windows
  18. #
  19. # This method takes an argument that is expected to already be properly
  20. # escaped for the receiving program to be properly parsed. This argument
  21. # will be further escaped to pass the interpolation performed by cmd.exe
  22. # unchanged.
  23. #
  24. # Any Meta-characters will be escaped,removing the ability to e.g. use
  25. # redirects or variables.
  26. #
  27. # @param arg [String] a single command line argument to escape for cmd.exe
  28. # @return [String] an escaped string suitable to be passed as a program
  29. # argument to cmd.exe
  30.  
  31. Meta_chars = '()%!^"<>&|'
  32. Meta_re = re.compile('(' + '|'.join(re.escape(char) for char in list(Meta_chars)) + ')')
  33. Meta_map = { char: "^%s" % char for char in Meta_chars }
  34.  
  35. def escape_Meta_chars(m):
  36. char = m.group(1)
  37. return Meta_map[char]
  38.  
  39. return Meta_re.sub(escape_Meta_chars,arg)

应用此代码,您应该能够成功转义cmd.exe shell的参数.

  1. print escape_argument('''some arg with spaces''')
  2. # ^"some arg with spaces^"

请注意,该方法应引用一个完整的参数.如果你从多个源收集你的参数,通过构建一串python代码传递给python命令,你必须在将它传递给escape_argument之前组装它.

  1. import os
  2.  
  3. CMD = '''string with spaces and &weird^ charcters!'''
  4. os.system('python -c "import sys; print(sys.argv[1])" {0}'.format(escape_argument(CMD)))
  5. # string with spaces and &weird^ charcters!

猜你在找的Windows相关文章