html - Trying to automate Tor to do something on a site and change identity each time. Need some guidance -
i need automating tor on site (in case, check on poll) , restart tor new identity. have never done remotely close this. know html, css , js well.
now, sum up, want make loop repeatedly accesses site on tor, checks on site , restarts tor new identity.
if give me guidance , tell me can use, appreciated. have time , patience learn, works really.
here examples using php , python 3 accomplish want. they're simple starting points making requests on tor , changing identity on demand.
the php example uses torutils communicate controller , wrap curl through tor.
the python example uses stem communicate controller , requests sending requests on tor's socks proxy.
the examples assume have tor working , socksport
set 9050, , controlport
set 9051 cookie authentication working, or controller password of password
.
php
set up
- install composer install torutils package (you can download zipball , extract)
- once composer working, run
composer require dapphp/torutils
project directory download , install dependencies
code
<?php use dapphp\torutils\controlclient; use dapphp\torutils\torcurlwrapper; require_once 'vendor/autoload.php'; // composer autoloader // include torutils/src/controlclient.php , torutils/src/torcurlwrapper.php if using without composer $controller = new controlclient; // new controller object try { $controller->connect('127.0.0.1', 9051); // connect tor controller on localhost:9051 $controller->authenticate('password'); // attempt authenticate using "password" password } catch (\exception $ex) { die("failed open connection tor controller. reason: " . $ex->getmessage() . "\n"); } // issue 10 requests, changing identity after each request ($i = 0; $i < 10; ++$i) { try { $curl = new torcurlwrapper('127.0.0.1', 9050); // connect tor socks proxy on localhost:9050 $curl->httpget('https://drew-phillips.com/ip-info/'); // issue request $body = strip_tags($curl->getresponsebody()); if (preg_match('/using tor:\s*yes/i', $body)) { echo "you appear using tor successfully. "; } else { echo "proxy worked tor ip not known. "; } if (preg_match('/ip address:\s*(\d+\.\d+\.\d+\.\d+)/i', $body, $ip)) { echo "source ip = {$ip[1]}\n"; } else { echo "couldn't determine ip!\n"; } } catch (\exception $ex) { echo "http request failed! " . $ex->getmessage() . "\n"; } // todo: issue more requests needed here echo "\n"; sleep(10); try { // send signal controller request new identity (ip) $controller->signal(controlclient::signal_newnym); } catch (\exception $ex) { echo "failed issue newnym signal: " . $ex->getmessage() . "\n"; } }
python 3
set up
this example uses python 3 , assumes have python interpreter , running , have following packages installed: requests
, requests[socks]
, socks
, urllib3
, stem
.
on debian/ubuntu: sudo -h pip3 install requests requests[socks] socks urllib3 stem
code
#!/usr/bin/env python3 import requests stem.control import controller, signal import time import sys import re # specify tor's socks proxy http , https requests proxies = { 'http': 'socks5h://127.0.0.1:9050', 'https': 'socks5h://127.0.0.1:9050', } try: controller = controller.from_port(9051) # try connect controller @ localhost:9051 except stem.socketerror exc: print("unable connect tor on port 9051: %s" % exc) sys.exit(1) try: controller.authenticate('password') # try authenticate password "password" except stem.connection.passwordauthfailed: print("unable authenticate, password incorrect") sys.exit(1) # issue 10 requests, changing identity after each request in range(1,10): # issue request, passing proxies request r = requests.get('https://drew-phillips.com/ip-info/', proxies=proxies) #print(r.text) m = re.search('<dt>using tor:</dt><dd><span[^>]*>yes', r.text) if m: print("you appear using tor successfully. ", end="") else: print("proxy worked tor ip not known. ", end="") m = re.search('<dt>ip address:</dt><dd>(\d+\.\d+\.\d+\.\d+)</dd>', r.text) if m: print("source ip = %s" % m.groups(1)) else: print("failed scrape ip page") try: # send signal controller request new identity (ip) controller.signal(signal.newnym) except exception ex: print("newnym failed: %s" % ex) time.sleep(10)
Comments
Post a Comment