Skip to content

Instantly share code, notes, and snippets.

@csobankesmarki
Forked from rootVIII/sec_browse.py
Created May 17, 2020 21:47
Show Gist options
  • Save csobankesmarki/caa244246a4ea87442c9c00dec7de921 to your computer and use it in GitHub Desktop.
Save csobankesmarki/caa244246a4ea87442c9c00dec7de921 to your computer and use it in GitHub Desktop.
sec_browse.py - Auto-configures Firefox network settings and opens a secure Tor/Firefox browser session for the # time specified by -t
#! /usr/bin/python3
from os import popen, remove, getcwd
from selenium import webdriver
from subprocess import call
from sys import exit
from time import sleep
from argparse import ArgumentParser
from threading import Thread
# rootVIII
# sec_browse.py - Auto-configures Firefox network settings
# and opens a secure Tor/Firefox browser session for the
# time specified by -t
#
# Firefox Network settings are only modified while the
# script is running and will return to their defaults
#
# USAGE:
# python3 sec_browse.py -t <seconds>
# intended for Debian Linux Distros
def check_tor_srvc():
for s in popen('service --status-all'):
yield s.split()
def get_agent():
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
user_agent += "AppleWebKit/537.36 (KHTML, like Gecko) "
user_agent += "Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"
return user_agent
def set_profile(agent):
fp = webdriver.FirefoxProfile()
fp.set_preference('network.proxy.type', 1)
fp.set_preference('network.proxy.socks', '127.0.0.1')
fp.set_preference('network.proxy.socks_port', 9050)
fp.set_preference('general.useragent.override', agent)
fp.set_preference('http.response.timeout', 10)
fp.set_preference('dom.max_script_run_time', 10)
fp.update_preferences()
return fp
def session_length():
description = 'Usage: python sec_browse -t <time in seconds>'
parser = ArgumentParser(description=description)
parser.add_argument('-t', '--time', required=True, help='time', type=int)
return parser.parse_args().time
def browse(session_time):
for service in check_tor_srvc():
if 'tor' in service and '+' in service:
print('tor service already running')
break
else:
print('starting tor service')
call(['service', 'tor', 'start'])
sleep(10)
browser = webdriver.Firefox(firefox_profile=set_profile(get_agent()))
try:
browser.get('https://www.duckduckgo.com')
assert 'DuckDuckGo — Privacy, simplified.' in browser.title
sleep(session_time)
except Exception as error:
print(str(error))
finally:
browser.quit()
if __name__ == '__main__':
try:
thread = Thread(target=browse, args=[session_length()])
thread.daemon = True
thread.start()
thread.join()
except KeyboardInterrupt:
print('exiting...')
try:
remove(getcwd() + '/geckodriver.log')
except Exception:
print('Unable to remove geckodriver.log')
finally:
print('stopping Tor service')
call(['service', 'tor', 'stop'])
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment