Supposing this working code :
import win_inet_pton
import socks
import socket
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, "localhost", 9050)
s.connect(("xmh57jrzrnw6insl.onion",80))
s.send("GET / HTTP/1.1\r\n\r\n")
print 'sended'
data=s.recv(1024)
print data
Tor service is indeed running at port 9050.
In normal condition, python will perform DNS resolution through the SOCKS5 proxy, which is connected to the Tor relay. However, Tor do not handle UDP packet(It handles the resolution directly from the hostname in the TCP packet) so DNS resolution will fail.
How is it possible that this code work? (the equivalent code in java for exemple will fail as the DNS resolution can't be made).
IT is explained in this link: Python requests fails when tryign to connect to .onion site
You simply have to use socks5h instead of socks5
Related
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
Im trying to open a simple webpage with python using Urlib2 in a TAILS system but I am unable to make it work, with error 111, connection refused. So that is obviously because TAILS refuses all non-Tor traffic,and the solution should be opening a proxy with:
proxy = urlib2.ProxyHandler({protocol:"127.0.0.1:{}".format(PORT)})
opener = urlib2.build_opener(proxy)
opener.open("https://jojeji")
I tried lots of protocols and ports, asumming that this choice is the only problem I have. Tails has a configuration file in etc/ferm/ferm.conf. In the output part it says:
# White-list access to Tor's SOCKSPort's
daddr 127.0.0.1 proto tcp syn dport 9050 {
mod owner uid-owner _apt ACCEPT;
mod owner uid-owner proxy ACCEPT;
mod owner uid-owner nobody ACCEPT;
}
daddr 127.0.0.1 proto tcp syn mod multiport
destination-ports (9050 9061 9062 9150) {
mod owner uid-owner $amnesia_uid ACCEPT;
}
There are white lists also for:
access to onionshare
access to Monkeysphere
access to CUPS
access to the accesibility daemon
access to system DNS and TOr's DNS PORT
access to Tor's TransPort
access to Tor control port filtrer
access to Tor ControlPort
So wich values should a TAILS user pass to the urlib2 proxy and, is there any better way to acces the internet trough urlib2?
Tails/Tor implements SOCKS5 proxy, not HTTP proxy. AFAIK, this is not directly supported by urlib2. You need to use the SocksiPy module as shown in this answer.
The socks proxy is 127.0.0.1 and the port is 9050.
Your other option is to use pycurl or even command-line curl (with --socks5-hostname=127.0.0.1:9050)
I am writing a crawler in Python that will run through Tor. I have Tor working and used code from this YouTube tutorial on how to route my Python requests to go through the Tor SOCKS proxy at 127.0.0.1:9050.
What I can't figure out is how to toggle this on/off within my script. Some requests I want to go through Tor and some I don't. Basically, I can't figure out the correct "close" or "shutdown" method in the socket objects I am using because I don't understand them.
Here's what happens now
import socket
import socks
import requests
def connect_to_socks():
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9050, True)
socket.socket = socks.socksocket
r = requests.get('http://wtfismyip.com/text')
print r.text #prints my ordinary IP address
connect_to_socks()
r = requests.get('http://wtfismyip.com/text')
print r.text #prints my Tor IP address
How do I turn off the socket routing to the SOCKS proxy so that it goes through my ordinary internet connection?
I'm hoping to use requests instead of urllib2 as it seems a lot easier but if I have to get into the guts of urllib2 or even httplib I will. But would prefer not to.
Figured it out by listening to this good YouTube tutorial.
Just need to call socket.setdefaultproxy() and it brings me back.
For Python 3 you can set back default socket by using this:
socks.setdefaultproxy(None)
socket.socket = socks.socksocket
I have a very basic piece of Python code:
import smtplib
server = smtplib.SMTP(host, port)
problems = server.sendmail(from_addr, to_addr, message)
Is there solution to run it behind an HTTP proxy? I am using Python 3.4.1 on Linux with the http_proxy variable set.
Now I am getting a timeout from SMTP, but if I run this code from a proxy-free network, it works OK.
Is there solution to run it behind an HTTP proxy?
No, HTTP is a different protocol than SMTP and the proxy is for HTTP only. If you are very lucky you might be able to create a tunnel using the CONNECT command to the outside SMTP server, but usually the ports used for CONNECT are restricted so that you will not be able to create a tunnel to an outside host port 25 (i.e. SMTP).
I'm using TOR to proxy connections but am having difficulty proxying DNS lookups via socket.gethostbyname("www.yahoo.com") -- I learned that it was not sending DNS traffic via proxy by sniffing traffic with wireshark. Here's a copy of the code I'm using
import StringIO
import socket
import socks # SocksiPy module
import stem.process
from stem.util import term
SOCKS_PORT = 7000
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
socket.gethostbyname("www.yahoo.com") <--- This line is not sending traffic via proxy
Any help is greatly appreciated!
You're calling gethostbyname in the socket module. It doesn't know anything about your SOCKS socket; it is simply interacting with your operating system's name resolution mechanisms. Setting socket.socket = socks.socksocket may affect network connections made through the socket module, but the module does not make direct connections to DNS servers to perform name resolution so replacing socket.socket has no impact on this behavior.
If you simply call the connect(...) method on a socks.socksocket object using a hostname, the proxy will perform name resolution via SOCKS:
s = socks.socksocket()
s.connect(('www.yahoo.com', 80))
If you actually want to perform raw DNS queries over your SOCKS connection, you'll need to find a Python DNS module to which you can provide your socksocket object.
If you resolve the DNS yourself with Socks5 you may leak information about your own computer. Instead try tunneling with Proxifier, then to Tor. Alternatively you can use SocksiPy's Socks4A extension. This will make sure information is not leaked.