TOR with python stem (basic) - 'tor' not in PATH - python

I'm trying to get an .onion website's content to python, a little research showed that 'stem'
and as i am running this tutorial script, or more specifically, when i'm trying to use stem.process.launch_tor_with_config, i get this error:
'tor' isn't available on your system. Maybe it's not in your PATH?
I supposed to have some sort of tor process installed, I got the tor browser bundle and put the /Tor library (with the tor.exe) in it in my path, and it's not helping...
Obviously I'm missing something VERY BASIC, please advise....
Many thanks...

I had the same issue on my Mac. Try specifying exactly where the tor file is located to the tor_cmd parameter:
tor_process = stem.process.launch_tor_with_config(
tor_cmd = '/Applications/TorBrowser.app/Tor/tor.real',
config = { SocksPort': str(SOCKS_PORT),
'ExitNodes': '{ru}',},
init_msg_handler = print_bootstrap_lines,
)

Related

Python - how to write a window shared folder path (ex. \\192.168.1.19\cert\testcert.pfx) using pywinrm

I access to one remote machine to automate some ssl task. To accomplish this from the remote I need import a file from a shared network folder in windows. I´m using pywinrm then after a successful session started I run:
s.run_ps('''
$Password = ConvertTo-SecureString -AsPlainText -Force {0}
Import-PfxCertificate -FilePath {1} -CertStoreLocation Cert:\LocalMachine\WebHosting -
Password $Password
'''.format(secret, path)
using similar script with local folder paths I don't have any problem but typing kind of this \\192.168.1.19\cert\testcert.pfx I have the following error
std_error b'Import-PfxCertificate : The system cannot find the path specified. 0x80070003 \n(WIN32: 3 ERROR_PATH_NOT_FOUND)\nAt line:3 char:9\n+ Import-PfxCertificate -FilePath \\192.168.1.19\\Certificates1\\test ...\n+
I built this path like string "host"+"\\"+cert+"\\"+file where host = "\\192.168.1.19", cert = "Certificates1", and file = "testcert.pfx"
I have seen that in powershell I should type exactly PS C:\>\\192.168.1.19\cert\testcert.pfx and it works but pointing to the error the string that is passing python to powershell is \ and not escaping and passing only one backslash.
Any help or advice?
Thank you in advance.
the error the string that is passing python to powershell is \ and not escaping and passing only one backslash.
\\ = \ so you need to do host = "\\\\192.168.1.19"
The problem deals with the second hop issue, so the script can't access to another network shared file while remoting.
The solution was change authentication to CredSSP and allowing the second hop delegate the credentials.
After this, no errors referring to any path appears.

Finding file in windows path

I'm trying to get someone else's Python script to run on my computer and part of the script finds a file in the USERPROFILE. Here is that code:
for w in os.walk(os.getenv('USERPROFILE')):
if 'FilenName' in w[1]:
path = str(w[0]) + r'\FilenName\UsrData\Directory\Data'
However, in the above code, the program tries to search in the following directory:
C:\Users\User\AppData\Roaming\
When, in fact, the program should be looking in
C:\Users\User\AppData\Local\
If I replace the problem code with the following, it works, but I need it to run for all USERPROFILEs, not just mine:
path = r'C:\Users\Bill\AppData\Local\FilenName\UsrData\Directory\Data'
What is the solution to this?
Thanks.
I'm not on a windows machine, so this is a bit tricky, but could you find all user profiles using the env var ALLUSERSPROFILE?
Another option may be to replace "Roaming" with "Local" in the string. It's a bit hacky, but can be done:
for w in os.walk(os.getenv('USERPROFILE')):
if 'FilenName' in w[1]:
path = (str(w[0]) + r'\FilenName\UsrData\Directory\Data').replace('Roaming', 'Local')

Running python cgi script Interpreter results differ to browser

I was having difficulty converting a program I made to a cgi script. I suspected it was to do with os.walk so I made a smaller test script to test this.
(I noticed the single \ before the D in the variable loc and tried changing that to a double \ still no change)
Produces no errors cant tell why it doesn't run the for loop with os.walk in the browser.
I tried adding some data into s and run for loop printing of contents of it and that worked fine, but trying to do it on os.walk I can't seem to get it to work. I can't find anything relating to the issue on google or stackoverflow.
Below is the code:
import cgi,cgitb,os
loc = "C:\\Users\\wen\Desktop\\sample data\\old py stuff\\"
cgitb.enable(display=1,logdir=loc)
s = []
print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<body>")
print("<p>"+loc+"</p>")
for r,ds,fs in os.walk(loc):
print("<p>omgwtf</p>")
for f in fs:
s.append(f)
for i in s:
print("<p>"+i+"</p>")
print("</body>")
print("</html>")
Took a screenshot, the output in interpreter on the left and browser on right
i.imgur.com/136y1Yq.jpg
webserver is running iis7
I'm pretty sure I've solved the problem, I needed to give the folders permissions for 'Authenticated users'.

Read registry value with python problems

I feel like I am taking crazy pills. So for security on an api at work I am using, I have to read 2 things from the registry, that I then pass to suds. The problem is with reading the registry values. No matter what I do, I get "Error2 the system cannot find the file specified". I know that the registry file is there, yet it won't let me read it. I have tried the code below on 2 different 2008 r2 servers. On one windows 7 box, I am able to read the values...but only on one machine. Below is the code, with the actual directory I need changed(to protect anonymity)
from _winreg import *
key = OpenKey(HKEY_LOCAL_MACHINE, r"Software\a\b", 0, KEY_ALL_ACCESS)
devguid = QueryValueEx(key, "DeviceID")
devid = QueryValueEx(key, "DeviceGUID")
devnm = socket.gethostname()
If I change the directory to something other than \a\b, it works fine. I have verified that the permissions on these directories are the exact same as directories I can read from.
Also, I can run the following command from cmd and get the output I need:
reg query HKLM\software\a\b /v DeviceGUID
But when I run it from a python script, it says cannot find file specified.
import os
cmd = "reg query HKEY_LOCAL_MACHINE\software\a\b /v DeviceGUID"
a = os.system(cmd)
print a
Running my script as admin or anything doesn't help. For some reason, python is unable to try and ready registry....
First of all you do need to make sure that your backslashes are suitably escaped, or use raw strings as per the first code sample. I'm going to assume that you've done that.
The most likely explanation is that you use 32 bit Python on a 64 bit system. And so are subject to the registry redirector serving up the 32 bit view of the registry.
Either use 64 bit Python, or specifically open they key with a 64 bit view. Do the latter by specifying the KEY_WOW64_64KEY flag.

Installing a CGI Proxy on Tomcat

I want to setup a proxy for openlayers to use so I followed these steps:
Downloaded the proxy.cgi file from the OpenLayers web site:
http://trac.osgeo.org/openlayers/browser/trunk/openlayers/examples/proxy.cgi
Modify the proxy.cgi file to include my domain in the allowedHosts list:
allowedHosts = ['localhost:6901']
Copy the proxy.cgi file to the following folder:
$TOMCAT_PATH$/webapps/yourApp/WEB-INF/cgi/
Modify the file web.xml of your web app by adding the sections below. You find the file at
$TOMCAT_PATH$/webapps/yourApp/WEB-INF/web.xml
Comment: In case the web.xml file doesn’t exist for your webapp, just create it yourself or copy it from another webapp and modify it. (created!)
Comment: the “param-value” for the “executable” parameter has to contain the path to your Python installation. (it does!)
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value>c:\python25\python.exe</param-value>
</init-param>
<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
Modify the file context.xml of your web app by adding the element below. You find the file at $TOMCAT_PATH$/webapps/yourApp/META-INF/context.xml
Restart Tomcat
To use the proxy with OpenLayers, just include this single line into your code:
OpenLayers.ProxyHost = "/yourWebApp/cgi-bin/proxy.cgi?url=";
But when I try to use it like:
/webappname/cgi-bin/proxy.cgi?url=labs.metacarta.com
I get this error:
Some unexpected error occurred. Error text was: list index out of range
I think its related with os.environ["REQUEST_METHOD"] but I dont know how its related ..
You're asking for an environment variable that isn't defined.
You need to either catch and handle the exception or use os.environ.get:
try:
methodq = os.environ["REQUEST_METHOD"]
except KeyError:
methodq = "default value"
Or:
methodq = os.environ.get("REQUEST_METHOD", "default value")
You're submitting :
/webappname/cgi-bin/proxy.cgi?url=labs.metacarta.com
But the proxy.cgi script is trying to do this:
host = url.split("/")[2]
Try http://labs.metacarta.com for your url so proxy.cgi has some slashes to split on, or modify it to parse the url a little smarter.
The answer is: You don't install or use cgi proxy on Tomcat.
cgi is for apache server or IIS that are used as a front-end server. Tomcat may sit behind it. The configuration of Apache is detailed in: http://tomcat.apache.org/tomcat-6.0-doc/proxy-howto.html
Be warned that OpenLayers warns that its proxy.cgi is only an example and may not have good enough check to stop it from being exploited, i.e. it may run some malicious script.
If you are serving your OpenLayers client page on Tomcat alone and it contains layers from other GeoServer or Mapserver, you can use proxy servlet and specify it as:
OpenLayers.ProxyHost = "sevlet URL on the server that served this page";
http://wiki.apache.org/tomcat/ServletProxy
https://svn.openxdata.org/Experimental/openXmapper/trunk/gwt-openlayers-server/src/main/java/org/gwtopenmaps/openlayers/server/GwtOpenLayersProxyServlet.java
There are reverse proxy or rewrite sevlet solutions to it, too. Please Google on these.

Categories