So I am attempting to call the Windows command type on some arbitrary file. Unfortunately, whenever I convert my cmd into from a shell command into a non-shell one it fails. As such, I cannot use the recommended method for ensuring that my python script cannot be exploited. Here is an example.
import subprocess
cmd = "type" + '"' + "some_file_with_no_spaces_or_other_things_wrong" + '"'
p = subprocess.pOpen(cmd, shell = True)
but when I try:
#Assume cmd split is done properly. Even when I manually put in the
#array with properly escaped quotes it does not work
subprocess.pOpen(cmd.split(), shell = False)
It fails, and I don't know how to solve this issue. I would like to be able to call this command securely by having the shell by false, but whenever I do so I get the following error.
Traceback (most recent call last):
File "C:\Users\Skylion\git\LVDOWin\Bin\Osiris.py", line 72, in openFileDialog
stderr = STDOUT, shell = False, bufsize = 0, universal_newlines=True)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Note that even running:
subprocess.Popen(['type'])
Will throw the error. My issue is how do I sanitize the filename so I can either run the filename with shell=True, or get shell=False properly working.
Any help on how to properly open a file in this way would be greatly appreciated.