Subprocess access denied error when running nmap as System User - python

I'm trying to run a python script when the system boots on a Windows 10 workstation. My python script runs fine when I kick it off in a different directory as a user with administrative privileges but when I run it from a bat file in the C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup folder on system boot I receive the following error in my logs.
2018-11-13 04:43:58,913 - VP - Level 55 - nmap args: ['"C:\\Program Files (x86)\\Nmap\\nmap.exe"', '-oX', 'C:\\\\nmap\\NmapResults\\scan-192.168.1.5-20181113044358.xml', '-sS', '-n', '-p-', '--allports', '--scan-delay', '1ms', '-sU', '-sV', '--version-all', '192.168.1.5']
Traceback (most recent call last):
File "secnmap\nmap_scanner.py", line 596, in <module>
exit_code = main()
File "secnmap\nmap_scanner.py", line 248, in main
run_nmap(args, scan_options, host_data_list, use_service_name_check)
File "secnmap\nmap_scanner.py", line 268, in run_nmap
nmap_proc = subprocess.Popen(nmap_cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
Here is the content of the .bat file.
cd C:\\nmap_startup
python nmap_startup.py
I have tried using the full nmap.exe path as shown in the code snippet as well as plain old nmap. I get the access denied error either way. I didn't see much in subprocess.py that would lead me to believe this to be an error with the python code either so I am leaning on it being an issue with System user running the script. If someone thinks otherwise or has any ideas, I'd love to hear them. Thanks
EDIT: I chose to run from a bat file because my python version is 32bit and I was having issues running from the C:\windows\system32 directory

It turns out that the path to nmap, C:\Program Files (x86)\Nmap, was not included in the system environment variables. When the script ran as a System user, it did not recognize the location of nmap

Related

Python: cannot run command line commands

I try to run several simple command line commands on my system and although the code run i cannot see my window opening.
For example:
command = 'cmd'
os.system(command)
Why i cannot see my cmd window ? all i can see in the console window (i am using pycharm) is this:
Microsoft Windows [Version 10.0.18362.449]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\blabla\tmp>
I also try to open appium server using command line and i have .bat file that call appium (this is what i put into the .bat file and this work fine manually):
This is the path to my .bat file:
C:\tmp\scripts\start_appium.bat
Command
p = subprocess.Popen("start_appium.bat", cwd=r"C:\tmp\scripts")
stdout, stderr = p.communicate()
And i received this error:
Traceback (most recent call last): File
"C:\Python37\lib\subprocess.py", line 775, in init
restore_signals, start_new_session) File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo) File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.4\helpers\pydev_pydev_bundle\pydev_monkey.py", line
536, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args) FileNotFoundError: [WinError 2]
The system cannot find the file specified
Python documentation clearly mentions cwd param is not the directory to search the executives.
If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.
You can refer the documentation here.
https://docs.python.org/2/library/subprocess.html#popen-constructor
Also, try something like this
import subprocess
p = subprocess.Popen("C:\tmp\scripts\start_appium.bat")

Packaging/Deploying Maven from Python

Big picture is I'm trying to automate my deployment process of building with maven and deploying to a web logic server. Little picture is I'm using subprocess to see if I can call maven from within python. When I attempt this subprocess mistakes mvn for a file.
Here is my code so far:
import subprocess
def main():
print(subprocess.check_output(["mvn", "-v"]))
if __name__ == '__main__':
main()
And here's my error:
C:\pythondev\python.exe "C:/pythondev/development/deployment scripts/redploy-to-localhost.py"
Traceback (most recent call last):
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 9, in <module>
main()
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 5, in main
subprocess.check_output(["a"])
File "C:\pythondev\lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "C:\pythondev\lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\pythondev\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\pythondev\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 1
Although my issue is with subprocess I'm open to answers that suggest a better alternative.
I ran into the same issue and was hesistant to use shell=True, because the internet tells me this is evil.
When I run where mvn in my cmd.exe, I can see that there are two matches:
mvn, which is a Unix shell-script (it starts with #!/bin/sh),
mvn.cmd, which is a Windows batch file.
I think what happens when you execute mvn something -something in cmd.exe is the following: Windows tries finding an executable called mvn. It finds the mvn file, but realizes that this file is not executable. It then tries finding files like mvn.com, mvn.exe, ... (see the %PATHEXT% system variable). When it finds mvn.cmd, it executes that and everyone is happy.
As far as I understand it, the problem with subprocess.check_output (and subprocess.run, and so on) is that the path-"expansion" via %PATHEXT% is not being performed. So the solution is that you have to give the extension manually and run your command as
print(subprocess.check_output(["mvn.cmd", "-v"]))
Try this it worked for me.
print(subprocess.check_output(["mvn", "-v"], shell=True))

error trying to load exiftool

I have installed exiftool (https://smarnach.github.io/pyexiftool/) and I am able to import the library, but I get the following error when trying to run the test data just to see if it works.
ERROR: test_get_metadata (__main__.TestExifTool)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Program Files\Python36\Lib\site-
packages\pyexiftool\test\test_exiftool.py", line 66, in test_get_metadata
with self.et:
File "C:\Program Files\Python36\lib\site-packages\exiftool.py", line 191, in __enter__
self.start()
File "C:\Program Files\Python36\lib\site-packages\exiftool.py", line 174, in start
stderr=devnull)
File "C:\Program Files\Python36\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python36\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I also did run the setup code that is in the exiftool folder and still no luck. I think it might be a library issue or path or the (init.py) file, but I've tried several ways and so I'm here to ask if anyone else has a solution or ideas for me to try and fix it.
I'm running Python 3.6.6 and have tried other versions.
(I can run exiftool in command line, but I have encoded BASE64 images that exiftool doesnt work in command line to fully decode.)
Thank you StarGeek! The problem was that I didn't have the exiftool command tool (the separate application of exiftool) in the right PATH env variable. Once I added the application to the PATH env variables i got it to work. Also in the python exiftool code at line 70 it says that you have to have it in the path or direct it to the executable, that I had missed. Thanks again!

Running Linux Commands from Python on Windows Computer

I am trying to learn how to run command line commands from Python. I am able to do this with DOS:
import subprocess
subprocess.call("dir",shell=True)
This is fine, but I need to be able to do this for linux commands because my company uses linux servers. I am using Mobaxterm to run a local linux session. When I try this:
import subprocess
subprocess.call("ls",shell=True)
I get this error from the terminal:
'ls' is not recognized as an internal or external command, operable program or batch file.
Which sounds ridiculous to me, because ls is clearly a linux command.
If I don't include shell=True I get this error:
Traceback (most recent call last):
File "ConsolePractice.py", line 4, in <module>
subprocess.call("ls")
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 560, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
So what am I doing wrong? How can I get this to work?
EDIT: Thanks for the comments. Python for Windows won't work for what I'm trying to do. I connected to a linux server and did:
cat > ConsoleCmd.py
import subprocess
subprocess.call("ls",shell=True)
^C
python ConsoleCmd.py
... and ls ran. Found the issue. I'll just have to get my scripts on to the server and run them from there. Thanks everyone
I was able to resolve this issue by opening an SFTP Session within Mobaxterm, transferring the python file to the server, then running the file directly on the server (python was already installed on the server).

Running python scripts with subprocess in windows. Python code checker wrappers from the emacswiki yield the same error

So i'm trying to setup the python code checkers suggested in the emacs wiki. However, I'm unable to run those scripts in my command shell let alone emacs.
The section is found here:
http://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc7
And I tried the script located here and here
In both cases I changed the first line from #!usr/bin python with the full path of my python executable and when I run the scripts via
python pylint_etc_wrappers.py someModule.py
or
python pycheckers.py soemModule.py
both boil down to the same error, most likely because they try to open a subprocess. Here's the trace:
Traceback (most recent call last):
File "pycheckers.py", line 254, in <module>
runner.run(source_file)
File "pycheckers.py", line 91, in run
process = Popen(args, stdout=PIPE, stderr=PIPE)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The second script suggests to change the first line to the path of the interpreter (which I did) and to change the path in the main function which looks something like :
os.environ['PATH'] = \
path.dirname(sys.executable) + ':' + os.environ['PATH']
which was a bit unclear to me. Any ideas?
I have pylint 0.25.1, installed using easy_install (Python 2.7, Win XP). Both pylint and pylint.bat were installed in Python27/Scripts (this directory is in my PATH).
I too get the "The system cannot find the file specified" error when running the pylint_etc_wrapper.py script unchanged.
Running pylint from the script does work if
command = 'pylint'
is changed to
command = 'pylint.bat'
Another way to make it work is to add shell=True to the Popen() call.
I can't really explain all this, but there is an unresolved Python bug that looks like it might be relevant: http://bugs.python.org/issue8557.

Categories