0
votes

I have this simple script to connect via ssh to a server and it works if I run the script from shell, but it doesn't when I call the script from cgi-bin (http://localhost/cgi-bin/test.py)

#!C:\Python27\python

print "Content-type: text/html\n\n"

import spur
shell = spur.SshShell(hostname="192.168.1.5", username="user", password="pass", missing_host_key=spur.ssh.MissingHostKey.accept)
result = shell.run(["pwd"])
print result.output

I'm using XAMPP on Windows and I edited the httpd.conf to accept .py files from cgi-bin, infact a simple

#!C:\Python27\python

print "Content-type: text/html\n\n"
print "Test"

works in browser.


The error found in my Apache logs is as follows:

Traceback (most recent call last):
  File "C:/xampp/cgi-bin/smart.py", line 4, in <module>
    import spur
  File "C:\\Python27\\lib\\site-packages\\spur\\__init__.py", line 2, in <module>
    from spur.ssh import SshShell
  File "C:\\Python27\\lib\\site-packages\\spur\\ssh.py", line 15, in <module>
    import paramiko
  File "build\\bdist.win32\\egg\\paramiko\\__init__.py", line 30, in <module>
  File "build\\bdist.win32\\egg\\paramiko\\transport.py", line 49, in <module>
  File "build\\bdist.win32\\egg\\paramiko\\dsskey.py", line 26, in <module>
  File "build\\bdist.win32\\egg\\Crypto\\PublicKey\\DSA.py", line 89, in <module>
  File "build\\bdist.win32\\egg\\Crypto\\Random\\__init__.py", line 28, in <module>
  File "build\\bdist.win32\\egg\\Crypto\\Random\\OSRNG\\__init__.py", line 34, in <module>
  File "build\\bdist.win32\\egg\\Crypto\\Random\\OSRNG\\nt.py", line 28, in <module>
  File "build\\bdist.win32\\egg\\Crypto\\Random\\OSRNG\\winrandom.py", line 7, in <module>
  File "build\\bdist.win32\\egg\\Crypto\\Random\\OSRNG\\winrandom.py", line 4, in __bootstrap__
  File "C:\\Python27\\lib\\site-packages\\pkg_resources\\__init__.py", line 1155, in resource_filename
    self, resource_name
  File "C:\\Python27\\lib\\site-packages\\pkg_resources\\__init__.py", line 1851, in get_resource_filename
    self._extract_resource(manager, self._eager_to_zip(name))
  File "C:\\Python27\\lib\\site-packages\\pkg_resources\\__init__.py", line 1881, in _extract_resource
    self.egg_name, self._parts(zip_path)
  File "C:\\Python27\\lib\\site-packages\\pkg_resources\\__init__.py", line 1221, in get_cache_path
    self.extraction_error()
  File "C:\\Python27\\lib\\site-packages\\pkg_resources\\__init__.py", line 1201, in extraction_error
    raise err
pkg_resources.ExtractionError: Can't extract file(s) to egg cache

The following error occurred while trying to extract file(s) to the Python egg
cache:

  [Error 5] Accesso negato: 'C:\\\\Windows\\\\Application Data'

The Python egg cache directory is currently set to:

  C:\\Windows\\Application Data\\Python-Eggs

Perhaps your account does not have write access to this directory?  You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.

Thanks in advance

1
"Doesn't work" is an utterly useless problem description; you'd need to get an error message from your web server's logs and put it in the question to even allow a start at a good diagnosis. - Charles Duffy
ok, any clue on how can I log the errors? - z3d0
Depends entirely on your web server. In Apache, I'd expect them to be in the error_log already (assuming a reasonable default configuration). - Charles Duffy
here's the log pastebin.com/FVVtBiK5, I set the PYTHON_EGG_CACHE to C:\tmp with write permissions in the script with os.environ['PYTHON_EGG_CACHE'] = 'C:\tmp' but nothing changed, I also created the directory C:\Windows\Application Data\Python-Eggs with write permissions but I'm still getting this error - z3d0
I'm assuming you use adblock and don't know this, but pastebin.com links are full of ads. Please use a different one (like gist.github.com), if you can't just cut things down enough to edit into your question. - Charles Duffy

1 Answers

0
votes

A place to start would be installing paramiko (and other dependencies, should this error recur) directly into your existing Python installation, such that there's no need for it to be downloaded and extracted as an egg (and thus no need for file permissions appropriate for that task). For instance:

c:\Python\Scripts\easy_install.exe --always-unzip paramiko

Allowing your web server to write to a location where code is stored (and Python eggs are code!) is very bad security practice (on compromise, an attacker could put their own content there, and have it always run when paramiko or other libraries are loaded).