1

I wrote a script that changes the IP with stem, but it seems like it does not work. Here is a shortened version of the script:

from stem import Signal
from stem.control import Controller
from stem.connection import connect

def changeIP():
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)

def printIP():
    my_ip = urlopen('http://ip.42.pl/raw').read()
    print("IP -> %s" % my_ip)

#Some of my other codes

while(true):
    j+=1
    if j == 2:
        changeIP()
        j = 0
        printIP()

And it just prints my public IP again and again. It should print the same IP 2 times and then change, but it doesn't. My torrc config is correctly configured.

ControlPort = 9051
HashedControlPassword 16:AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B
cookie authentication 1

I even tried putting the hashed Control password in the control.authenticate(password='AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B'), but it still did not work, and I also want my script to not use it. I have been searching for this weeks now, and I found out that I can use SocksiPy module, but I can't do it.

Please recode my script, Thank you very much.

  • You are connecting to the Controller to issue commands, but are not using Tor's SOCKS proxy to issue actual HTTP requests. Check out [this answer](https://stackoverflow.com/questions/43682909/connect-to-onion-websites-on-tor-using-python/43823166#43823166) – drew010 Jan 05 '18 at 22:13
  • @drew010 I don't really get it. So what am I supposed to do? Do the proxies, then what? Where do I put that? –  Jan 05 '18 at 22:26
  • The code from that answer would go in printIP(). The control port is just to talk to the Tor daemon to control Tor. To make requests with it, you have to connect to the SOCKS proxy (usually 127.0.0.1:9050 or 9150 with Tor Browser Bundle). SockiPy is not considered a great idea anymore since that uses Monkey Patching to set the proxy and affects *all* socket connections. Instead, I suggest using python requests and the proxies option. – drew010 Jan 05 '18 at 22:31
  • @drew010 Sorry, It doesn't work..... I get the error : Unable to determine SOCKS version from socks5h://127.0.0.1:9050 –  Jan 07 '18 at 19:58

1 Answers1

2

I am assuming you are working on windows. I am not sure if this will work on Linux.

I would suggest you to download a fresh copy of Tor without altering the torrc config. Personally, I didn't configure the hashcontrolpassword but the following python code still works for me. Try the step 1 listed here : https://stackoverflow.com/a/48144473/9183144

Following step 1, You should be able to see 127.0.0.1:9150 and 127.0.0.1:9151 when you run netstat -an in your terminal.

After that copy the following code and try running it (change the directory to your Tor folder).

# library to launch and kill Tor process
import os
import subprocess

# library for Tor connection
import socket
import socks
import http.client
import time
import requests
from stem import Signal
from stem.control import Controller

def launchTor():
    # start Tor (wait 30 sec for Tor to load)
    sproc = subprocess.Popen(r'.../Tor Browser/Browser/firefox.exe')
    time.sleep(30)
    return sproc

def killTor(sproc):
    sproc.kill()

def connectTor():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150, True)
    socket.socket = socks.socksocket
    print("Connected to Tor")

def set_new_ip():
    # disable socks server and enabling again
    socks.setdefaultproxy()
    """Change IP using TOR"""
    with Controller.from_port(port=9151) as controller:
        controller.authenticate()
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150, True)
        socket.socket = socks.socksocket
        controller.signal(Signal.NEWNYM)

def checkIP():
    conn = http.client.HTTPConnection("icanhazip.com")
    conn.request("GET", "/")
    time.sleep(3)
    response = conn.getresponse()
    print('current ip address :', response.read())

# Launch Tor and connect to Tor network
sproc = launchTor()
connectTor()

# Check current IP
checkIP()

# Set new IP
set_new_IP()

# Check newly assigned IP
time.sleep(10)
checkIP()

# remember to kill process 
killTor(sproc)
KittyBot
  • 41
  • 2