Python Tor sharing socket port with local network - python

Hi I'm trying to figure out how to share my tor connection with another pc in my network.I create Tor using expert bundle and torrc file looks like this.
# Where data will be stored?
DataDirectory C:\Users\someuser\Tor\TorData\data\10002
# Countdown time before exit
ShutdownWaitLength 5
# Where to write PID
PidFile C:\Users\someuser\Tor\TorData\data\10002\pid
# Communication ports
SocksPort xx.xxx.xxx.xx:10002
ControlPort xx.xxx.xxx.xx:10003
# Authentication of Tor
CookieAuthentication 1
# GeoIP file paths?
GeoIPFile C:\Users\someuser\Tor\Data\Tor\geoip
GeoIPv6File C:\Users\someuser\Tor\Data\Tor\geoip6
SocksListenAddress xx.xxx.xxx.xx
SocksPolicy accept xx.xxx.xxx.xx/24
So now I'm trying to use that same tor connection from another pc like this.
import requests
ses = requests.session()
ses.proxies = {'http': '{0}{1}:{2}'.format('socks5://','xx.xxx.xxx.xx', 10002),
'https': '{0}{1}:{2}'.format('socks5://','xx.xxx.xxx.xx', 10002)}
r = ses.get('some http webpage')
print r.content
I did check firewall settings that is ok. Over stem I can connect to control port. But dunno why SockPort doesn't pass.
error that im geting no matter the veriso of interpeter is
File "C:\Python27\lib\site-packages\requests\packages\urllib3\contrib\socks.py", line 73, in _new_conn
conn = socks.create_connection(
AttributeError: 'module' object has no attribute 'create_connection'
Any ideas what am I missing?

I found out that is is because of differance in python interperter after all. It must run in python 3 both sides and it will work like a charm.
I hope someone will help torrc configuration.

Related

Is there any way to test the weblogic admin connecting URL (t3/t3s) before connecting to it

I'm using following command to connect to weblgic using WLST,
java weblogic.wlst core.py
inside core.py I'm calling following command to connect to the weblogic admin. but some times the service url becomes unresponsive And my script hangs occasionally due to this. Is there any way to give a timeout to this connect() method or any other method to implement a timeout functionality?. Appreciate if someone can shed some light on this. Thanks.
connect(username,password,t3://:)
in earlier WebLogic versions they have provided following functionality(to ping), but they have removed it after 12.2*
java weblogic.Admin -url t3://localhost:7001 -username weblogic -password weblog
ic ping 3 100
This is a very common situation, where you can use Python's socket module to check that the Admin port is opened or not with the following function.
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AdminIP = '192.168.33.10'
result = sock.connect_ex((AdminIP,7001))
if result == 0:
print "AdminPort is open you can connect"
else:
print "Admin Port is not yet open"
sock.close()
add your logic accordingly, HTH!

What may be the cause of the following error on python OPC UA server with SCADA client?

Here is the code for setting up OPC UA Server:
import sys
from opcua import ua, Server
server = Server()
server.set_endpoint("opc.tcp://127.0.0.1:4808/")
server.set_server_name("ServerOPC")
uri = "http://server"
idx = server.register_namespace(uri)
objects = server.get_objects_node()
Object_1 =objects.add_object(idx,'MyFirstObject')
Object_2 =objects.add_object(idx,'MySecondObject')
Object_3 =objects.add_object(idx,'MyThirdObject')
Discret_1 = Object_1.add_variable(idx,'Discret_1',[0,0,0,0,0,0,0,0],varianttype=ua.VariantType.Int16)
Discret_2 = Object_2.add_variable(idx,'Discret_2',[0,0,0,0,0,0,0,0],varianttype=ua.VariantType.Int16)
Analog_3 = Object_3.add_variable(idx,'Analog_3',[10,20,30,40,50],varianttype=ua.VariantType.Int16)
Discret_1.set_writable()
Discret_2.set_writable()
Analog_3.set_writable()
server.allow_remote_admin(allow=True)
server.start()
I can succesfully read and write the variables with UaExpert. But i have it failed with SCADA-client (WinCC). I have connection established and can browse the nodes (variables). The types are compatible. But when I start runtime mode my server gives the following error.
Does anyone have an idea how to fix this problem?
Thanks in advance!
Finally, i found out that problem was not about the SCADA-client or the server, but about virtual machine. I don't know what exactly it was, but on another VM everything is going well.

Cannot connect to my tcp tor hidden service in Python

I have a fully functional tor hidden service with his v3 url, created with stem and the controller class. The server is redirecting the traffic into a local tcp server running on port 5000 created with socket. But now I want to connect to this hidden service using python. So I tried socks with SOCKS5 proxies, no way of making it work. I also tried the torpy library with the example provided on the offical doc but, again, no way to make it works:
from torpy import TorClient
url = 'myv3torurl.onion'
tor = TorClient()
with tor.create_circuit() as circuit:
print('circuit done')
with circuit.create_stream(('http://' + url, 5000)) as client:
print('sending')
client.send(b'yeah yeah im there')
print(client.recv(1024))
Also tried changing the url adding http:// or https://, or tried to change the port with 80, 9050 or 9051. The torpy logs are always the same and you can find them here.
At this point I do not really know what to try. I just want a tcp connection hosted by the onion website. And by the way, I'm sure it's not a problem of the server because if I try to copy and paste the url on the tor browser it is fully reachable. Thank you for any help.
Noticed that torpy page says it is for v2 services and yours is v3.

Make a python 3 request using proxy

I want to make an anonymous web request using python 3.
I've tried few suggestions such as: Make requests using Python over Tor
I've managed to get a fake ip using this snippet:
Installation
pip install requests requests[socks]
Basic usage
import requests
def get_tor_session():
session = requests.session()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5://127.0.0.1:9150',
'https': 'socks5://127.0.0.1:9150'}
return session
# Make a request through the Tor connection
# IP visible through Tor
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
# Above should print an IP different than your public IP
# Following prints your normal public IP
print(requests.get("http://httpbin.org/ip").text)
But that works only on port 9150 and when the tor web browser works.
I want to make a request without the tor browser, as i want to Dockerize the whole thing.
I've read about Socks5, and as you can see i've installed it, but when i make a request on port 9050 on the same snippet i get:
requests.exceptions.ConnectionError:
SOCKSHTTPConnectionPool(host='httpbin.org', port=80): Max retries
exceeded with url: /ip (Caused by
NewConnectionError(': Failed to establish a new connection: [WinError
10061] No connection could be made because the target machine actively
refused it',))
How can i solve it?
Thanks!
10061 is 'connection refused'
That means there was nothing listening on that port you tried to connect to, no service is up and running (no open port) or firewall on target IP blocks it
you can test that port with telnet
telnet `IP` `PORT`
And also check this port issue on Windows: here
I was also facing this issue, in my case my tor service was not running, actually I was using kalitorify which is a transparent proxy, and whenever I was using this I was not be able to use normal sites such as google search or similar, so to use these sites I was turning off my kalitorify service which also turns off your tor service
So if you're also using that then also check it once

Changing identity using Tor and Stem

I'm following the tutorial To Russia With Love, and as part of this I want to change the identity everytime I run the code. I've looked at multiple questions and tutorials and from the looks of it my torrc-defaults file is correct as it has:
ControlPort 9151
CookieAuthentication 1
I wanted to edit the torrc file first but if i touch it Tor won't run anymore, so i read somewhere to look at torrc instead. The torrc file alone only has:
# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1 or similar, and Tor will ignore it
I don't want to set a password so my understanding from reading the stem documents is that setting the Authentication is enough. I have the following code:
import socks # SocksiPy module
import socket
import requests
SOCKS_PORT = 9150
# Set socks proxy and wrap the urllib module
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
url = "http://google.com"
print requests.get(url).elapsed.total_seconds()
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9151) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
But when I run it i get the error:
socks.SOCKS5Error: 0x01: General SOCKS server failure
I'm not sure what to do, i've been reading about this for hours and haven't been able to solve the problem. Any hints would be great.
EDIT:
I've also read through This Post, but it doesn't seem like there's a solution.
I also tried putting the Controller statement before setting the sockets, but in that case the IP remains the same and doesn't change.
I ended up solving this problem by moving the controller statement before setting the sockets. Initially it looked like the IP remains the same but turns out if you wait an extra 3 or 4 seconds the IP changes, so I just added a time delay and it runs fine.

Categories