Last active
November 12, 2018 15:52
-
-
Save frytoli/5654d170868104e29615d6b5de70b1c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from selenium.webdriver.firefox.options import Options | |
from pyvirtualdisplay import Display | |
from configparser import ConfigParser | |
from stem.control import Controller | |
from selenium import webdriver | |
from datetime import datetime | |
from bs4 import BeautifulSoup | |
from stem import Signal | |
import random | |
import string | |
import json | |
import time | |
import sys | |
import os | |
def New_Identity(tor_pw): | |
with Controller.from_port(port = 9051) as controller: | |
controller.authenticate(password=tor_pw) | |
controller.signal(Signal.NEWNYM) | |
time.sleep(5) | |
def Initialize_Selenium(Download_Folder): | |
# Instantiate Firefox Profile | |
profile = webdriver.FirefoxProfile() | |
# Security Preferences | |
profile.set_preference('places.history.enabled', False) | |
profile.set_preference('privacy.clearOnShutdown.offlineApps', True) | |
profile.set_preference('privacy.clearOnShutdown.passwords', True) | |
profile.set_preference('privacy.clearOnShutdown.siteSettings', True) | |
profile.set_preference('privacy.sanitize.sanitizeOnShutdown', True) | |
profile.set_preference('signon.rememberSignons', False) | |
profile.set_preference('network.cookie.lifetimePolicy', 2) | |
profile.set_preference('network.dns.disablePrefetch', True) | |
profile.set_preference('network.http.sendRefererHeader', 0) | |
profile.set_preference('javascript.enabled', False) | |
profile.set_preference('permissions.default.image', 2) | |
#profile.set_preference('acceptInsecureCerts', True) | |
#profile.set_preference('assumeUntrustedIssuer', True) | |
#profile.set_preference('browser.safebrowsing.enabled', False) | |
#profile.set_preference('browser.safebrowsing.malware.enabled', False) | |
#profile.set_preference('browser.safebrowsing.phishing.enabled', False) | |
# Set proxy to Tor client | |
profile.set_preference('network.proxy.type', 1 ) | |
profile.set_preference('network.proxy.socks_version', 5 ) | |
profile.set_preference('network.proxy.socks', '127.0.0.1' ) | |
profile.set_preference('network.proxy.socks_port', 9050 ) | |
profile.set_preference('network.proxy.socks_remote_dns', True ) | |
# Download Preferences | |
profile.set_preference('browser.download.dir',Download_Folder) | |
profile.set_preference('browser.download.folderList',2) | |
profile.set_preference('broswer.download.manager.showWhenStarting', False) | |
profile.set_preference('browser.helperApps.neverAsk.openFile','application/octet-stream, application/binary, binary/octet-stream, text/plain, text/csv, application/sql, application/x-sqlite3, application/x-paradox, application/gzip, application/zip, application/x-tar, applicaton/x-gtar, multipart/x-tar, application/x-compress, application/x-compressed') | |
profile.set_preference('browser.helperApps.neverAsk.saveToDisk','application/octet-stream, application/binary, binary/octet-stream, text/plain, text/csv, application/sql, application/x-sqlite3, application/x-paradox, application/gzip, application/zip, application/x-tar, applicaton/x-gtar, multipart/x-tar, application/x-compress, application/x-compressed') | |
profile.update_preferences() | |
# Make headless | |
options = Options() | |
options.set_headless(headless=True) | |
# Initialize Webdriver | |
driver = webdriver.Firefox(firefox_options=options, firefox_profile=profile) | |
return(driver) | |
def Main(): | |
# Get tor password from secret config | |
config = ConfigParser() | |
config.read('secret.ini') | |
tor_pw = config.get('Tokens','tor_password') | |
# Initalize directory for automatic file download | |
Download_Folder = os.getcwd() + '/Downloads/' | |
if not os.path.exists(Download_Folder): | |
os.mkdir(Download_Folder) | |
# Set display for FF driver | |
display = Display(visible=0, size=(1024, 768)) | |
display.start() | |
# Initialize random interval variables for new identity requests | |
count = 0 | |
maxcount = random.randint(3,9) | |
# Initialize FF driver | |
ffdriver = Initialize_Selenium(Download_Folder) | |
# Test and Find IP | |
ipsource = ffdriver.get('http://icanhazip.com/') | |
ipsource = ffdriver.page_source | |
soup = BeautifulSoup(ipsource, 'html.parser') | |
ip = (soup.find('pre').contents[0]).strip() | |
print('[*] Selenium Firefox driver with custom preferances configured successfully. Connecting to {} with current public IP: {}.'.format('icanhazip.com',ip)) | |
# Infinte loop! | |
while True: | |
# Request new identity after a random number of requests | |
if count == maxcount: | |
maxcount = random.randint(3,9) | |
count = 0 | |
# Test and Find IP | |
try: | |
New_Identity(tor_pw) | |
ipsource = ffdriver.get('http://icanhazip.com/') | |
ipsource = ffdriver.page_source | |
soup = BeautifulSoup(ipsource, 'html.parser') | |
ip = (soup.find('pre').contents[0]).strip() | |
print('\n[*] New Identity established. Current Public IP: {}.'.format(ip)) | |
except: | |
print('\n[*] Error establishing New Identity.') | |
ffdriver.get('https://check.torproject.org/') | |
source = ffdriver.page_source | |
soup = BeautifulSoup(source, 'html.parser') | |
if 'Sorry. You are not using Tor.' in soup.get_text(): | |
print(' [!] Tor configurations do not seem to be working.') | |
elif 'Congratulations. This browser is configured to use Tor.' in soup.get_text(): | |
print(' [*] Tor configurations seem to be working. Yay!') | |
else: | |
print(' [!] Something seems off. Check the following output:') | |
print(soup.get_text()) | |
# After request, sleep for a random time between 1 and 5 seconds | |
secs = random.randint(1,5) | |
time.sleep(secs) | |
count += 1 | |
# Stop display for Firefox | |
display.stop() | |
if __name__ == '__main__': | |
Main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TPS.py : Tor as a Proxy with Selenium
The above is a scratch example of successfully configuring a Firefox driver with Selenium in Python3 and using Tor as a SOCKS proxy. See a more detailed explanation of the code here: secchur.ro/projects/tps
Versions:
Selenium v 3.141.0
Firefox-ESR 52.9.0 for Debian
Geckodriver 0.16.1
Python 3.5.0
Dependencies:
tor
xvfb
firefox-esr (version 52.9.0)
Python Requirements:
selenium
bs4
stem
pyvirtualdisplay