Esempio n. 1
0
def main():

  print 'Opening log file, further output will be there.'

  # redirect output to file, https://stackoverflow.com/questions/7152762
  f = file('TorBrowser/OnioNS/stem.log', 'w')
  sys.stdout = f

  # get current time of day
  now = datetime.datetime.now()

  try:
    # open main controller
    controller = Controller.from_port(port = 9151)
  except stem.SocketError:
    sys.exit("[err] The Tor Browser is not running. Cannot continue")

  controller.authenticate()
  controller.set_options({
    '__LeaveStreamsUnattached': '1'
  })

  print '[%d:%d | notice] Successfully connected to the Tor Browser.' % (now.minute, now.second)
  sys.stdout.flush()

  event_handler = functools.partial(handle_event, controller)
  controller.add_event_listener(event_handler, EventType.STREAM)

  print '[%d:%d | debug ] Now monitoring stream connections.' % (now.minute, now.second)
  sys.stdout.flush()

  try:
    time.sleep(60 * 60 * 24 * 365) #basically, wait indefinitely
  except KeyboardInterrupt:
    print ''
Esempio n. 2
0
def main():
  parser = argparse.ArgumentParser(description=
       "Parse the log_dir location for tor bw logs and generate graphs.")
  parser.add_argument("log_dir", help="Location of tor-log-bw.py output logs.")
  args = parser.parse_args()
  log_dir = args.log_dir

  """Create a log file and setup the event handler for BW events"""
  with Controller.from_port(port = 9051) as controller:
    controller.authenticate()

    """
    TODO This try except could be eliminated and just straight up call log_bw
    """
    log_file = os.path.join(log_dir, "tor_bw.log")

    create_timed_rotating_log(log_file)
    logger = logging.getLogger("Rotating Log")
    logger.info("Starting BW Event Logging")

    try:
      # This makes curses initialize and call draw_bandwidth_graph() with a
      # reference to the screen, followed by additional arguments (in this
      # case just the controller).

      log_bandwidth(controller)
    except KeyboardInterrupt:
      pass  # the user hit ctrl+c
Esempio n. 3
0
 def restartTor(self):
     """Get new tor session"""
     with Controller.from_port(port=9051) as controller:
         controller.authenticate("Den135790")
         controller.signal(Signal.NEWNYM)
     self.clean()
     self.logger.debug("success restart tor")
Esempio n. 4
0
def renew_connection():
    with Controller.from_port(port = 9151) as controller:
        controller.authenticate();
        print (controller.get_newnym_wait())
        time.sleep(controller.get_newnym_wait());
        controller.signal(Signal.NEWNYM);
    return;    
Esempio n. 5
0
def get_exit(is_running):
   """
   Get list of exit node from stem
   """
   if is_running:
      try:
         with Controller.from_port(port = 6969) as controller:
            controller.authenticate()
            exit = {'count': [], 'fingerprint': [], 'nickname': [], 'ipaddress': []}
            count = -1
            for circ in controller.get_circuits():
               if circ.status != stem.CircStatus.BUILT:
                  continue
               exit_fp, exit_nickname = circ.path[-1]
               exit_desc = controller.get_network_status(exit_fp, None)
               exit_address = exit_desc.address if exit_desc else 'unknown'
               count += 1
               exit['count'].append(count)
               exit['fingerprint'].append(exit_fp)
               exit['nickname'].append(exit_nickname)
               exit['ipaddress'].append(exit_address)
         return exit
      except stem.SocketError as exc:
         notify("TorTP", "[!] Unable to connect to port 6969 (%s)" % exc)
         sys.exit(1)
   else:
      notify("TorTP", "[!] Tor is not running")
      sys.exit(0)
Esempio n. 6
0
def change_ipadress(passphrase="your_TORsoftware_password", sleep=1) :
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate(passphrase)
        controller.signal(Signal.NEWNYM)  
    #we wait here but you can eventually skip this part or set it in place to
    #gain controle over time.
    time.sleep(sleep)
Esempio n. 7
0
def newId():
    global changeID, password
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate(password = password)
        controller.signal(Signal.NEWNYM)
    time.sleep(3)
    changeID = 0
Esempio n. 8
0
def _getController(controlAddr="127.0.0.1", controlPort=9051, passphrase=None, incorrectPasswordMsg="", printError=True):
  """
  Custom handler for establishing a stem connection (... needs an overhaul).
  """
  
  controller = None
  try:
    chroot = util.torTools.getConn().getPathPrefix()
    controller = Controller.from_port(controlAddr, controlPort)
    
    try:
      controller.authenticate(password = passphrase, chroot_path = chroot)
    except stem.connection.MissingPassword:
      try:
        passphrase = getpass.getpass("Controller password: "******"Unable to authenticate: password incorrect":
      # provide a warning that the provided password didn't work, then try
      # again prompting for the user to enter it
      print incorrectPasswordMsg
      return _getController(controlAddr, controlPort)
    elif printError:
      print exc
    
    return None
Esempio n. 9
0
def checkTor():
	if not controller.isAlive():
		print "tor controller was dead, now trying to restart it"
		try:
			controller = Controller.from_port()
			controller.authenticate()
		except Exception as e:
			print e
			print "tor is not letting us authenticate, is it configured correctly and running?" 
			exit()

		print "successfully opened control connection to tor"
#TODO here and elsewhere we should change from the get_conf/set_options
#interface to the create_ephemeral_hidden_service/
#remove_ephemeral_hidden_service/list_ephemeral_hidden_services
#that way we can also put the key and hostname in config and not have to
#worry about setting up /var/lib/tor/hidden_service

	if controller.get_conf("HiddenServiceDir") is None:
		print "tor was not configure to provide a hidden service, now configuring"
		try:
			controller.set_options([
			("HiddenServiceDir", get_hidden_svc_dir()),
			("HiddenServicePort", "80 %s:%s" % (host, str(port)))
			])
		except Exception as e:
			print "unable to create hidden service"
			print e
			quit()
Esempio n. 10
0
def enable_tordns():
   """
   Use Tor ControlPort for enable TorDNS
   """
   with Controller.from_port(port = 9051) as controller:
      controller.authenticate()
      controller.set_options({"DNSPort": "9053", "DNSListenAddress": "127.0.0.1", "AutomapHostsOnResolve": "1", "AutomapHostsSuffixes": ".exit,.onion"})
def handle_timeout(process, onion, identity_lock):
    # halt the main thread while we grab a new identity
    identity_lock.clear()

    # kill the onionscan process
    try:
        process.kill()
        print("[!!!] Killed the onionscan process.")
    except:
        pass

    # Now we switch TOR identities to make sure we have a good connection
    with Controller.from_port(port=9051) as torcontrol:

        # authenticate to our local TOR controller
        torcontrol.authenticate(PASSWD)

        # send the signal for a new identity
        torcontrol.signal(Signal.NEWNYM)

        # wait for the new identity to be initialized
        time.sleep(torcontrol.get_newnym_wait())

        print("[!!!] Switched TOR identities.")

    # push the onion back on to the list
    session_onions.append(onion)
    random.shuffle(session_onions)
    # allow the main thread to resume executing
    identity_lock.set()

    return
Esempio n. 12
0
def init_tor(socks_port=None, control_port=None):
    """ Initiates a tor connection.

    :param socks_port: local port socket where tor will listen to requests (configurable in tor.rc).
    :type socks_port: int
    :param control_port: local port where tor will listen to control requests (configurable in tor.rc).
    :type control_port: int
    :return: a tor process and a controller of the process.
    :rtype: process, controller
    """
    if socks_port is None:
        socks_port = SOCKS_PORT
    if control_port is None:
        control_port = CONTROL_PORT

    process = stem.process.launch_tor_with_config(
        config={
            'SocksPort': str(socks_port),
            'ControlPort': str(control_port)
        },
        init_msg_handler=print_bootstrap_lines, timeout=60, take_ownership=True)

    controller = Controller.from_port()
    controller.authenticate()

    return process, controller
Esempio n. 13
0
def launch_tor(country):
    print(term.format("Starting Tor with exit node in %s:" % (country), term.Attr.BOLD))
    try:
        tor_process = stem.process.launch_tor_with_config(
            config = {
                'SocksPort': str(SOCKS_PORT),
                'ControlPort': str(CONTROL_PORT),
                'ExitNodes': "{"+country+"}",
            },
            timeout = 30,
            # init_msg_handler = print_bootstrap_lines,
        )
    # finally:
    #     print("test")
    except OSError: 
        print("Timeout when trying to find relay....")
        return 0

            # Add listener
    with Controller.from_port(port = CONTROL_PORT) as controller:
        controller.authenticate()
        stream_listener = functools.partial(stream_event, controller)
        controller.add_event_listener(stream_listener, EventType.STREAM)

    return tor_process
Esempio n. 14
0
    def __init__(self, control_host = _CONTROL_HOST, 
                control_port = _CONTROL_PORT, sock = None, 
                authenticator = _AUTHENTICATOR):
        """Initialize the CtlUtil object, connect to Stem."""

        self.sock = sock

        if not sock:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.control_host = control_host
        self.control_port = control_port
        self.authenticator = authenticator

        # Try to connect 
        try:
            self.sock.connect((self.control_host, self.control_port))
        except:
            errormsg = "Could not connect to Tor control port.\n" + \
            "Is Tor running on %s with its control port opened on %s?" %\
            (control_host, control_port)

            logging.error(errormsg)
            raise

        
        self.control = Controller.from_port(port = self.control_port)

        # Authenticate connection
        self.control.authenticate(config.authenticator)
Esempio n. 15
0
 def connect(self):
     self.connected = False
     self.dir = os.path.join(settings.config_path, 'tor')
     connected = False
     for port in (9831, 9151):
         try:
             self.controller = Controller.from_port('127.0.0.1', port)
             connected = True
             break
         except stem.SocketError:
             pass
     if not connected:
         if not self.daemon:
             logger.debug("start own tor process")
             self.daemon = TorDaemon()
             logger.debug("daemon %s", self.daemon)
             return self.connect()
         logger.debug("Failed to connect to system or own tor process.")
         return False
     try:
         self.controller.authenticate()
     except stem.connection.MissingPassword:
         logger.debug("TOR requires a password")
         return False
     except stem.connection.PasswordAuthFailed:
         logger.debug("invalid tor password")
         return False
     self.controller.add_event_listener(self.event_listener)
     self.controller.add_status_listener(self.status_listener)
     self.connected = True
     self.socks_port = int(self.controller.get_conf('SocksPort').split(' ')[0])
     self.publish()
     state.online = self.is_online()
     return True
Esempio n. 16
0
def judgeActive(onion_addr):
    # conn = get_connection()
    proc = os.getpid()
    print('{0} doubled to {1} by process id: {2}'.format(
        onion_addr, '*', proc))
    with Controller.from_port(port=9053) as controller:
        controller.authenticate("111111")
        try:
            # cur = conn.cursor()######
            hs_descriptor = controller.get_hidden_service_descriptor(onion_addr,await_result = True)
            # print(hs_descriptor)
            sql = 'update ONION SET status = 1 WHERE onion_addr = "%s" ' % (onion_addr[:-6])
            # print(sql)
            # cur.execute(sql)#######
            print(onion_addr + " is alive from process " + str(proc) + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

        except stem.DescriptorUnavailable:
            # print("Descriptor not found, the hidden service may be offline.")
            sql = 'update ONION SET status = -1 WHERE onion_addr = "%s" ' % (onion_addr[:-6])
            # print(sql)
            # cur.execute(sql)#########
            print(onion_addr + " is died from process " + str(proc) + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
        except Exception,e:
            print '------------------------------------'
            print("timeout is found" + onion_addr + str(e) + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
            print ("-----------------------------")
        try:#This place is more important that using the try catch make the timeout come realize and useable,so why
            controller.close()
            # conn.commit()#######
            # cur.close()#########
            # conn.close()########
        except Exception,e:
            print "excepiton found" + str(e)
Esempio n. 17
0
def main():
    """
    The scanner's entry point.
    """

    stats = Statistics()
    args = parseCmdArgs()
    logger.debug("Command line arguments: %s" % str(args))

    torProc = bootstrapTor()
    torCtrl = Controller.from_port(port = const.TOR_CONTROL_PORT)
    stem.connection.authenticate_none(torCtrl)

    # Redirect Tor's logging to work around the following problem:
    # https://bugs.torproject.org/9862
    logger.debug("Redirecting Tor's logging to /dev/null.")
    torCtrl.set_conf("Log", "err file /dev/null")

    # We already have the current consensus, so we don't need additional
    # descriptors or the streams fetching them.
    torCtrl.set_conf("FetchServerDescriptors", "0")

    for moduleName in args.module:
        runModule(moduleName, args, torCtrl, stats)

    return 0
Esempio n. 18
0
    def determine_exits(self, state):
        global can_exit

        if options.exits is not None:
            exits = map(lambda l: in_consensus(state, l), options.exits)
            exits = filter(lambda r: r is not None, exits)
        else:
            exits = state.routers_by_hash.values()
        exits = filter(lambda r: "exit" in r.flags, exits)

        # get descriptors from stem
        con = Controller.from_port(port = self.options.control_port)
        con.authenticate()
        descriptors = {}
        for desc in con.get_server_descriptors():
            descriptors[desc.fingerprint] = desc
        con.close()

        can_exit_func = functools.partial(can_exit, descriptors,
                                     warn=not options.num_exits)
        exits = map(can_exit_func, exits)
        exits = filter(lambda t: t[0] is not None, exits)

        if options.num_exits is not None:
            if options.num_exits > len(exits):
                print "Not enough exits. Giving you all I've got."
            else:
                exits = random.sample(exits, options.num_exits)

        return exits
Esempio n. 19
0
def enable_torproxy():
   """
   Use Tor ControlPort for enable Tor Transparent Proxy
   """
   with Controller.from_port(port = 9051) as controller:
      controller.authenticate()
      controller.set_options({"VirtualAddrNetwork": "10.192.0.0/10", "TransPort": "9040", "TransListenAddress": "127.0.0.1","AvoidDiskWrites": "1", "WarnUnsafeSocks": "1"})
Esempio n. 20
0
    def renew(self):
        '''Signal TOR for a new connection

        thanks http://stackoverflow.com/questions/30286293/
            make-requests-using-python-over-tor
        and https://gist.github.com/
            KhepryQuixote/46cf4f3b999d7f658853'''
        SLEEPSECS = 1
        SESSION = self.Session()
        old_ip = check_ip(SESSION)
        # we don't want a CCC because it closes tor process by default :)
        with Controller.from_port(port=self.CONTROL_PORT) \
                as controller:
            controller.authenticate(password=self.PASSWORD)
            controller.signal(Signal.NEWNYM)
        new_ip = check_ip(SESSION)
        seconds = 0
        while old_ip == new_ip:
            # sleep this thread for the specified duration
            sleep(SLEEPSECS)
            # track the elapsed seconds
            seconds += SLEEPSECS
            # obtain the current IP address
            new_ip = check_ip(SESSION)
            # signal that the program is still awaiting a different IP address
            print("%d seconds elapsed awaiting a different IP address."
                  % seconds)
Esempio n. 21
0
    def __init__(self, transparent_torification=False):
        self.transparent_torification = transparent_torification

        # files and dirs to delete on shutdown
        self.cleanup_filenames = []
        self.service_id = None

        # connect to the tor controlport
        found_tor = False
        self.c = None
        ports = [9151, 9153, 9051]
        for port in ports:
            try:
                self.c = Controller.from_port(port=port)
                self.c.authenticate()
                found_tor = True
                break
            except SocketError:
                pass
        if not found_tor:
            raise NoTor(strings._("cant_connect_ctrlport").format(str(ports)))

        # do the versions of stem and tor that I'm using support ephemeral hidden services?
        tor_version = self.c.get_version().version_str
        list_ephemeral_hidden_services = getattr(self.c, "list_ephemeral_hidden_services", None)
        self.supports_ephemeral = callable(list_ephemeral_hidden_services) and tor_version >= '0.2.7.1'
Esempio n. 22
0
def start_hidden_service(port):
    # come up with a hidden service directory name
    hidserv_dir_rand = random_string(8)
    if get_platform() == "Windows":
        if 'Temp' in os.environ:
            temp = os.environ['Temp'].replace('\\', '/')
        else:
            temp = 'C:/tmp'
        hidserv_dir = "{0}/onionshare_{1}".format(temp, hidserv_dir_rand)
    else:
        hidserv_dir = "/tmp/onionshare_{0}".format(hidserv_dir_rand)

    # connect to the tor controlport
    controlports = [9051, 9151]
    controller = False
    for controlport in controlports:
        try:
            controller = Controller.from_port(port=controlport)
        except SocketError:
            pass
    if not controller:
        raise NoTor(translated("cant_connect_ctrlport").format(controlports))
    controller.authenticate()

    # set up hidden service
    controller.set_options([
        ('HiddenServiceDir', hidserv_dir),
        ('HiddenServicePort', '80 127.0.0.1:{0}'.format(port))
    ])

    # figure out the .onion hostname
    hostname_file = '{0}/hostname'.format(hidserv_dir)
    onion_host = open(hostname_file, 'r').read().strip()

    return onion_host
Esempio n. 23
0
File: web.py Progetto: sirfoga/hal
def renew_connection(password):
    """Renews TOR session

    :param password: new password
    """
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password=password)
        controller.signal(Signal.NEWNYM)
Esempio n. 24
0
    def tutorial_example():
      from stem.control import Controller

      with Controller.from_port(control_port = 9051) as controller:
        controller.authenticate()

        for desc in controller.get_network_statuses():
          print "found relay %s (%s)" % (desc.nickname, desc.fingerprint)
Esempio n. 25
0
	def renew_connection(self):
		with Controller.from_port(port = 9051) as controller:
	  		controller.authenticate('natalie')
	  		controller.signal(Signal.NEWNYM)

		self.logger.info('*'*50)
		self.logger.info('\t'*6+'Renew TOR IP: %s'%self.request(self.ip_url))
		self.logger.info('*'*50)
Esempio n. 26
0
 def _register_event_handler(self):
     self._control = Controller.from_port(address=self.control_address,
                                          port=self.control_port)
     if self.control_password:
         self._control.authenticate(password=self.control_password)
     self._control.add_event_listener(lambda e: self._handle_event(e),
                                      EventType.BW)
     self._handler_active = True
Esempio n. 27
0
def newId():
    global changeIDInPregress
    changeIDInPregress = True
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate(password = password)
        controller.signal(Signal.NEWNYM)
    time.sleep(3)
    changeIDInPregress = False
def renew(): #will turn off proxy temporarily to renew tor routing -not sure why this is necessary but it wasnt working through the proxy and easier to add here than to use proxy with indiv. requests. 
    #turn off proxy
    socket.socket = default_socket
    #reset tor routing
    with Controller.from_port(port=9051) as controller:
        controller.authenticate("account")
        controller.signal(Signal.NEWNYM)
    #turn proxy back on
    socket.socket = socks.socksocket
  def __init__(self, rate, lcd):

    self.controller = Controller.from_port()
    self.controller.authenticate()

    self.fingerprint = self.controller.get_info('fingerprint')
    self.rate = rate
    self.controller.set_conf('SocksPort', '9050')
    self.lcd = lcd
Esempio n. 30
0
def renew_connection(logger, settings):
    try:
        # TODO docker file with control port and password
        with Controller.from_port(port = 9051) as controller:
            pw = environ.get(TOR_CONTROL_PASSWORD, settings.get(TOR_CONTROL_PASSWORD, default='')),
            controller.authenticate(password = pw)
            controller.signal(Signal.NEWNYM)
    except SocketClosed as e:
        logger.error(e)
Esempio n. 31
0
def renew_connection():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password='******')
        controller.signal(Signal.NEWNYM)
        controller.close()
Esempio n. 32
0
import getpass
import sys

import stem
import stem.connection

from stem.control import Controller

if __name__ == '__main__':
    
    try:
        controller = Controller.from_port(address='127.0.0.1', port=9351)
    except stem.SocketError as exc:
        print("Unable to connect to tor on port 9351: %s" % exc)
        sys.exit(1)

    try:
        controller.authenticate()
    except stem.connection.MissingPassword:
        pw = ''
# 
#     try:
#         controller.authenticate(password = pw)
#     except stem.connection.PasswordAuthFailed:
#         print("Unable to authenticate, password is incorrect")
#         sys.exit(1)
#     except stem.connection.AuthenticationFailure as exc:
#         print("Unable to authenticate: %s" % exc)
#         sys.exit(1)

    print("Tor is running version %s" % controller.get_version())
Esempio n. 33
0
 def renew_connection(self):
     """ Restart sessione Tor con nuovo ip """
     with Controller.from_port(port=9051) as controller:
         controller.authenticate(password = self.tor_password)
         controller.signal(Signal.NEWNYM)
     self.session = self.create_session()
Esempio n. 34
0
    def run(self,
            do_onion=True,
            do_inet=True,
            tgen_model=None,
            tgen_client_conf=None,
            tgen_server_conf=None):
        '''
        only `tgen_server_conf.listen_port` are "public" and need to be opened on the firewall.
        if `tgen_client_conf.connect_port` != `tgen_server_conf.listen_port`, then you should have installed a forwarding rule in the firewall.
        all ports need to be unique though, and unique among multiple onionperf instances.

        here are some sane defaults:
        tgen_client_conf.listen_port=58888, tgen_client_conf.connect_port=8080, tgen_client_conf.tor_ctl_port=59050, tgen_client_conf.tor_socks_port=59000,
        tgen_server_conf.listen_port=8080, tgen_server_conf.tor_ctl_port=59051, tgen_server_conf.tor_socks_port=59001
        '''
        self.threads = []
        self.done_event = threading.Event()

        if tgen_client_conf is None:
            tgen_client_conf = TGenConf(listen_port=58888,
                                        connect_ip='0.0.0.0',
                                        connect_port=8080,
                                        tor_ctl_port=59050,
                                        tor_socks_port=59000)
        if tgen_server_conf is None:
            tgen_server_conf = TGenConf(listen_port=8080,
                                        tor_ctl_port=59051,
                                        tor_socks_port=59001)

        # if ctrl-c is pressed, shutdown child processes properly
        try:
            # make sure stem and Tor supports ephemeral HS (version >= 0.2.7.1-alpha)
            # and also the NEWNYM mode that clears descriptor cache (version >= 0.2.7.3-rc)
            if do_onion:
                try:
                    tor_version = get_system_tor_version(self.tor_bin_path)
                    if tor_version < Requirement.ADD_ONION or tor_version < Version(
                            '0.2.7.3-rc'):  # ADD_ONION is a stem 1.4.0 feature
                        logging.warning(
                            "OnionPerf in onion mode requires Tor version >= 0.2.7.3-rc, you have {0}, aborting"
                            .format(tor_version))
                        return
                except:
                    logging.warning(
                        "OnionPerf in onion mode requires stem version >= 1.4.0, you have {0}, aborting"
                        .format(stem_version))
                    return

            logging.info("Bootstrapping started...")
            logging.info(
                "Log files for the client and server processes will be placed in {0}"
                .format(self.datadir_path))

            general_writables = []
            tgen_client_writable, torctl_client_writable = None, None

            if do_onion or do_inet:
                tgen_model.port = tgen_server_conf.listen_port
                general_writables.append(self.__start_tgen_server(tgen_model))

            if do_onion:
                logging.info(
                    "Onion Service private keys will be placed in {0}".format(
                        self.privatedir_path))
                # one must not have an open socks port when running a single
                # onion service.  see tor's man page for more information.
                if self.single_onion:
                    tgen_server_conf.tor_socks_port = 0
                tor_writable, torctl_writable = self.__start_tor_server(
                    tgen_server_conf.tor_ctl_port,
                    tgen_server_conf.tor_socks_port, {
                        tgen_client_conf.connect_port:
                        tgen_server_conf.listen_port
                    })
                general_writables.append(tor_writable)
                general_writables.append(torctl_writable)

            if do_onion or do_inet:
                tor_writable, torctl_client_writable = self.__start_tor_client(
                    tgen_client_conf.tor_ctl_port,
                    tgen_client_conf.tor_socks_port)
                general_writables.append(tor_writable)

            server_urls = []
            if do_onion and self.hs_v3_service_id is not None:
                server_urls.append("{0}.onion:{1}".format(
                    self.hs_v3_service_id, tgen_client_conf.connect_port))
            if do_inet:
                connect_ip = tgen_client_conf.connect_ip if tgen_client_conf.connect_ip != '0.0.0.0' else util.get_ip_address(
                )
                server_urls.append("{0}:{1}".format(
                    connect_ip, tgen_client_conf.connect_port))
            tgen_model.servers = server_urls

            if do_onion or do_inet:
                assert len(server_urls) > 0

                tgen_model.port = tgen_client_conf.listen_port
                tgen_model.socks_port = tgen_client_conf.tor_socks_port
                tgen_client_writable = self.__start_tgen_client(tgen_model)

                self.__start_log_processors(general_writables,
                                            tgen_client_writable,
                                            torctl_client_writable)

                logging.info("Bootstrapping finished, entering heartbeat loop")
                time.sleep(1)
                while True:
                    if tgen_model.num_transfers:
                        # This function blocks until our TGen client process
                        # terminated on its own.
                        self.__wait_for_tgen_client()
                        break

                    if self.__is_alive():
                        logging.info(
                            "All helper processes seem to be alive :)")
                    else:
                        logging.warning(
                            "Some parallel components failed too many times or have died :("
                        )
                        logging.info(
                            "We are in a broken state, giving up and exiting now"
                        )
                        break

                    logging.info(
                        "Next main process heartbeat is in 1 hour (helper processes run on their own schedule)"
                    )
                    logging.info("press CTRL-C for graceful shutdown...")
                    time.sleep(3600)
            else:
                logging.info("No measurement mode set, nothing to do")

        except KeyboardInterrupt:
            logging.info(
                "Interrupt received, please wait for graceful shutdown")
            self.__is_alive()
        finally:
            logging.info("Cleaning up child processes now...")

            if self.hs_v3_service_id is not None:
                try:
                    with Controller.from_port(
                            port=self.hs_v3_control_port) as torctl:
                        torctl.authenticate()
                        torctl.remove_ephemeral_hidden_service(
                            self.hs_v3_service_id)
                except:
                    pass  # this fails to authenticate if tor proc is dead

#            logging.disable(logging.INFO)
            self.done_event.set()
            for t in self.threads:
                logging.info("Joining {0} thread...".format(t.getName()))
                t.join()
            time.sleep(1)
            #            logging.disable(logging.NOTSET)

            logging.info("Child processes terminated")
            logging.info("Child process cleanup complete!")
            logging.info("Exiting")
Esempio n. 35
0
    def connect(
        self,
        custom_settings=None,
        config=None,
        tor_status_update_func=None,
        connect_timeout=120,
        local_only=False,
    ):
        if local_only:
            self.common.log(
                "Onion", "connect", "--local-only, so skip trying to connect"
            )
            return

        self.common.log("Onion", "connect")

        # Either use settings that are passed in, or use them from common
        if custom_settings:
            self.settings = custom_settings
        elif config:
            self.common.load_settings(config)
            self.settings = self.common.settings
        else:
            self.common.load_settings()
            self.settings = self.common.settings

        # The Tor controller
        self.c = None

        if self.settings.get("connection_type") == "bundled":
            # Create a torrc for this session
            if self.use_tmp_dir:
                self.tor_data_directory = tempfile.TemporaryDirectory(
                    dir=self.common.build_tmp_dir()
                )
                self.tor_data_directory_name = self.tor_data_directory.name
            else:
                self.tor_data_directory_name = self.common.build_tor_dir()
            self.common.log(
                "Onion",
                "connect",
                f"tor_data_directory_name={self.tor_data_directory_name}",
            )

            # Create the torrc
            with open(self.common.get_resource_path("torrc_template")) as f:
                torrc_template = f.read()
            self.tor_cookie_auth_file = os.path.join(
                self.tor_data_directory_name, "cookie"
            )
            try:
                self.tor_socks_port = self.common.get_available_port(1000, 65535)
            except:
                print("OnionShare port not available")
                raise PortNotAvailable()
            self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")

            # If there is an existing OnionShare tor process, kill it
            for proc in psutil.process_iter(["pid", "name", "username"]):
                try:
                    cmdline = proc.cmdline()
                    if (
                        cmdline[0] == self.tor_path
                        and cmdline[1] == "-f"
                        and cmdline[2] == self.tor_torrc
                    ):
                        self.common.log(
                            "Onion",
                            "connect",
                            "found a stale tor process, killing it",
                        )
                        proc.terminate()
                        proc.wait()
                        break
                except:
                    pass

            if self.common.platform == "Windows" or self.common.platform == "Darwin":
                # Windows doesn't support unix sockets, so it must use a network port.
                # macOS can't use unix sockets either because socket filenames are limited to
                # 100 chars, and the macOS sandbox forces us to put the socket file in a place
                # with a really long path.
                torrc_template += "ControlPort {{control_port}}\n"
                try:
                    self.tor_control_port = self.common.get_available_port(1000, 65535)
                except:
                    print("OnionShare port not available")
                    raise PortNotAvailable()
                self.tor_control_socket = None
            else:
                # Linux and BSD can use unix sockets
                torrc_template += "ControlSocket {{control_socket}}\n"
                self.tor_control_port = None
                self.tor_control_socket = os.path.join(
                    self.tor_data_directory_name, "control_socket"
                )

            torrc_template = torrc_template.replace(
                "{{data_directory}}", self.tor_data_directory_name
            )
            torrc_template = torrc_template.replace(
                "{{control_port}}", str(self.tor_control_port)
            )
            torrc_template = torrc_template.replace(
                "{{control_socket}}", str(self.tor_control_socket)
            )
            torrc_template = torrc_template.replace(
                "{{cookie_auth_file}}", self.tor_cookie_auth_file
            )
            torrc_template = torrc_template.replace(
                "{{geo_ip_file}}", self.tor_geo_ip_file_path
            )
            torrc_template = torrc_template.replace(
                "{{geo_ipv6_file}}", self.tor_geo_ipv6_file_path
            )
            torrc_template = torrc_template.replace(
                "{{socks_port}}", str(self.tor_socks_port)
            )

            with open(self.tor_torrc, "w") as f:
                f.write(torrc_template)

                # Bridge support
                if self.settings.get("tor_bridges_use_obfs4"):
                    f.write(
                        f"ClientTransportPlugin obfs4 exec {self.obfs4proxy_file_path}\n"
                    )
                    with open(
                        self.common.get_resource_path("torrc_template-obfs4")
                    ) as o:
                        for line in o:
                            f.write(line)
                elif self.settings.get("tor_bridges_use_meek_lite_azure"):
                    f.write(
                        f"ClientTransportPlugin meek_lite exec {self.obfs4proxy_file_path}\n"
                    )
                    with open(
                        self.common.get_resource_path("torrc_template-meek_lite_azure")
                    ) as o:
                        for line in o:
                            f.write(line)

                if self.settings.get("tor_bridges_use_custom_bridges"):
                    if "obfs4" in self.settings.get("tor_bridges_use_custom_bridges"):
                        f.write(
                            f"ClientTransportPlugin obfs4 exec {self.obfs4proxy_file_path}\n"
                        )
                    elif "meek_lite" in self.settings.get(
                        "tor_bridges_use_custom_bridges"
                    ):
                        f.write(
                            f"ClientTransportPlugin meek_lite exec {self.obfs4proxy_file_path}\n"
                        )
                    f.write(self.settings.get("tor_bridges_use_custom_bridges"))
                    f.write("\nUseBridges 1")

            # Execute a tor subprocess
            start_ts = time.time()
            if self.common.platform == "Windows":
                # In Windows, hide console window when opening tor.exe subprocess
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                self.tor_proc = subprocess.Popen(
                    [self.tor_path, "-f", self.tor_torrc],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    startupinfo=startupinfo,
                )
            else:
                self.tor_proc = subprocess.Popen(
                    [self.tor_path, "-f", self.tor_torrc],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )

            # Wait for the tor controller to start
            time.sleep(2)

            # Connect to the controller
            try:
                if (
                    self.common.platform == "Windows"
                    or self.common.platform == "Darwin"
                ):
                    self.c = Controller.from_port(port=self.tor_control_port)
                    self.c.authenticate()
                else:
                    self.c = Controller.from_socket_file(path=self.tor_control_socket)
                    self.c.authenticate()
            except Exception as e:
                print("OnionShare could not connect to Tor:\n{}".format(e.args[0]))
                raise BundledTorBroken(e.args[0])

            while True:
                try:
                    res = self.c.get_info("status/bootstrap-phase")
                except SocketClosed:
                    raise BundledTorCanceled()

                res_parts = shlex.split(res)
                progress = res_parts[2].split("=")[1]
                summary = res_parts[4].split("=")[1]

                # "\033[K" clears the rest of the line
                print(
                    f"\rConnecting to the Tor network: {progress}% - {summary}\033[K",
                    end="",
                )

                if callable(tor_status_update_func):
                    if not tor_status_update_func(progress, summary):
                        # If the dialog was canceled, stop connecting to Tor
                        self.common.log(
                            "Onion",
                            "connect",
                            "tor_status_update_func returned false, canceling connecting to Tor",
                        )
                        print()
                        return False

                if summary == "Done":
                    print("")
                    break
                time.sleep(0.2)

                # If using bridges, it might take a bit longer to connect to Tor
                if (
                    self.settings.get("tor_bridges_use_custom_bridges")
                    or self.settings.get("tor_bridges_use_obfs4")
                    or self.settings.get("tor_bridges_use_meek_lite_azure")
                ):
                    # Only override timeout if a custom timeout has not been passed in
                    if connect_timeout == 120:
                        connect_timeout = 150
                if time.time() - start_ts > connect_timeout:
                    print("")
                    try:
                        self.tor_proc.terminate()
                        print(
                            "Taking too long to connect to Tor. Maybe you aren't connected to the Internet, or have an inaccurate system clock?"
                        )
                        raise BundledTorTimeout()
                    except FileNotFoundError:
                        pass

        elif self.settings.get("connection_type") == "automatic":
            # Automatically try to guess the right way to connect to Tor Browser
            automatic_error = "Could not connect to the Tor controller. Is Tor Browser (available from torproject.org) running in the background?"

            # Try connecting to control port
            found_tor = False

            # If the TOR_CONTROL_PORT environment variable is set, use that
            env_port = os.environ.get("TOR_CONTROL_PORT")
            if env_port:
                try:
                    self.c = Controller.from_port(port=int(env_port))
                    found_tor = True
                except:
                    pass

            else:
                # Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor
                try:
                    ports = [9151, 9153, 9051]
                    for port in ports:
                        self.c = Controller.from_port(port=port)
                        found_tor = True
                except:
                    pass

                # If this still didn't work, try guessing the default socket file path
                socket_file_path = ""
                if not found_tor:
                    try:
                        if self.common.platform == "Darwin":
                            socket_file_path = os.path.expanduser(
                                "~/Library/Application Support/TorBrowser-Data/Tor/control.socket"
                            )

                        self.c = Controller.from_socket_file(path=socket_file_path)
                        found_tor = True
                    except:
                        pass

            # If connecting to default control ports failed, so let's try
            # guessing the socket file name next
            if not found_tor:
                try:
                    if self.common.platform == "Linux" or self.common.platform == "BSD":
                        socket_file_path = (
                            f"/run/user/{os.geteuid()}/Tor/control.socket"
                        )
                    elif self.common.platform == "Darwin":
                        socket_file_path = (
                            f"/run/user/{os.geteuid()}/Tor/control.socket"
                        )
                    elif self.common.platform == "Windows":
                        # Windows doesn't support unix sockets
                        print(automatic_error)
                        raise TorErrorAutomatic()

                    self.c = Controller.from_socket_file(path=socket_file_path)

                except:
                    print(automatic_error)
                    raise TorErrorAutomatic()

            # Try authenticating
            try:
                self.c.authenticate()
            except:
                print(automatic_error)
                raise TorErrorAutomatic()

        else:
            # Use specific settings to connect to tor
            invalid_settings_error = "Can't connect to Tor controller because your settings don't make sense."

            # Try connecting
            try:
                if self.settings.get("connection_type") == "control_port":
                    self.c = Controller.from_port(
                        address=self.settings.get("control_port_address"),
                        port=self.settings.get("control_port_port"),
                    )
                elif self.settings.get("connection_type") == "socket_file":
                    self.c = Controller.from_socket_file(
                        path=self.settings.get("socket_file_path")
                    )
                else:
                    print(invalid_settings_error)
                    raise TorErrorInvalidSetting()

            except:
                if self.settings.get("connection_type") == "control_port":
                    print(
                        "Can't connect to the Tor controller at {}:{}.".format(
                            self.settings.get("control_port_address"),
                            self.settings.get("control_port_port"),
                        )
                    )
                    raise TorErrorSocketPort(
                        self.settings.get("control_port_address"),
                        self.settings.get("control_port_port"),
                    )
                else:
                    print(
                        "Can't connect to the Tor controller using socket file {}.".format(
                            self.settings.get("socket_file_path")
                        )
                    )
                    raise TorErrorSocketFile(self.settings.get("socket_file_path"))

            # Try authenticating
            try:
                if self.settings.get("auth_type") == "no_auth":
                    self.c.authenticate()
                elif self.settings.get("auth_type") == "password":
                    self.c.authenticate(self.settings.get("auth_password"))
                else:
                    print(invalid_settings_error)
                    raise TorErrorInvalidSetting()

            except MissingPassword:
                print(
                    "Connected to Tor controller, but it requires a password to authenticate."
                )
                raise TorErrorMissingPassword()
            except UnreadableCookieFile:
                print(
                    "Connected to the Tor controller, but password may be wrong, or your user is not permitted to read the cookie file."
                )
                raise TorErrorUnreadableCookieFile()
            except AuthenticationFailure:
                print(
                    "Connected to {}:{}, but can't authenticate. Maybe this isn't a Tor controller?".format(
                        self.settings.get("control_port_address"),
                        self.settings.get("control_port_port"),
                    )
                )
                raise TorErrorAuthError(
                    self.settings.get("control_port_address"),
                    self.settings.get("control_port_port"),
                )

        # If we made it this far, we should be connected to Tor
        self.connected_to_tor = True

        # Get the tor version
        self.tor_version = self.c.get_version().version_str
        self.common.log("Onion", "connect", f"Connected to tor {self.tor_version}")

        # Do the versions of stem and tor that I'm using support ephemeral onion services?
        list_ephemeral_hidden_services = getattr(
            self.c, "list_ephemeral_hidden_services", None
        )
        self.supports_ephemeral = (
            callable(list_ephemeral_hidden_services) and self.tor_version >= "0.2.7.1"
        )

        # Do the versions of stem and tor that I'm using support stealth onion services?
        try:
            res = self.c.create_ephemeral_hidden_service(
                {1: 1},
                basic_auth={"onionshare": None},
                await_publication=False,
                key_type="NEW",
                key_content="RSA1024",
            )
            tmp_service_id = res.service_id
            self.c.remove_ephemeral_hidden_service(tmp_service_id)
            self.supports_stealth = True
        except:
            # ephemeral stealth onion services are not supported
            self.supports_stealth = False

        # Does this version of Tor support next-gen ('v3') onions?
        # Note, this is the version of Tor where this bug was fixed:
        # https://trac.torproject.org/projects/tor/ticket/28619
        self.supports_v3_onions = self.tor_version >= Version("0.3.5.7")
Esempio n. 36
0
    namesplit = name.split()
    search = namesplit[0]
    for i in range(1, len(namesplit)):
        search += '+' + namesplit[i]
    URL = 'https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors={}&btnG='.format(
        search)

    # random time lag for requests
    random.seed(1)
    sleeptimerandom = 3 * random.random()
    if sleeptimerandom < 0.6:
        sleeptimerandom + 0.6
    time.sleep(sleeptimerandom)

    with Controller.from_port(port=9151) as c:
        c.authenticate()
        c.signal(Signal.NEWNYM)

    page = requests.get(URL, proxies=proxies, headers=headers)

    #page = requests.get(URL)
    soup = BeautifulSoup(page.content, 'html.parser')
    #print(soup.prettify())

    # Check if profile exists
    GSprofile = True
    div = soup.find('div', attrs={'id': 'gsc_sa_ccl'})
    try:
        textsearch = div.p.text
        GSprofile = False
Esempio n. 37
0
import gc, os, signal
import random
import time, argparse
import unicodedata
import pymysql
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from stem import Signal
from stem.control import Controller
import atexit, smtplib
from smtplib import SMTPException

# controller = Controller.from_port(port=9051)
controller = Controller.from_port(port=3306)

def releaseList(a):
    del a[:]
    del a

def SendMail(pidNumber):
    sender = '***********'
    reciever = ['************']
    message ="""
              From: Anil Pediredla<*******>
              To: Anil Pediredla<************>
              Subject: Failed Job

              From process running at host:<replace with host name> with Tor service<put the tor port here>
              terminated with start pid as
              """+pidNumber+" and ending pid as<pidNumberEnd>"
Esempio n. 38
0
 def reset_tor_ip(self):
     with Controller.from_port(port = 9051) as controller:
         controller.authenticate()
         controller.signal(Signal.NEWNYM)
Esempio n. 39
0
from stem.control import Controller
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template('camera.html')


print(' * Connecting to tor')

with Controller.from_port() as controller:
  print("Authenticating ...")
  controller.authenticate()

  # Create a hidden service where visitors of port 80 get redirected to local
  # port 5000 (this is where Flask runs by default).
  print("create ephemeral_hidden_service ...")
  response = controller.create_ephemeral_hidden_service({80: 5000}, await_publication = True)
  print(" * Our service is available at %s.onion, press ctrl+c to quit" % response.service_id)

  try:
    app.run()
  finally:
    print(" * Shutting down our hidden service")



Esempio n. 40
0
def switchIP():
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)
Esempio n. 41
0
from stem.control import Controller
import getpass
controlador = Controller.from_port(port=9151)
pw = getpass.getpass("Controller password: ")
controlador.authenticate(pw)
for ruta in controlador.get_network_statuses():
	print ruta
Esempio n. 42
0
    def run(self,
            do_onion=True,
            do_inet=True,
            client_tgen_listen_port=58888,
            client_tgen_connect_ip='0.0.0.0',
            client_tgen_connect_port=8080,
            client_tor_ctl_port=59050,
            client_tor_socks_port=59000,
            server_tgen_listen_port=8080,
            server_tor_ctl_port=59051,
            server_tor_socks_port=59001):
        '''
        only `server_tgen_listen_port` are "public" and need to be opened on the firewall.
        if `client_tgen_connect_port` != `server_tgen_listen_port`, then you should have installed a forwarding rule in the firewall.
        all ports need to be unique though, and unique among multiple onionperf instances.

        here are some sane defaults:
        client_tgen_listen_port=58888, client_tgen_connect_port=8080, client_tor_ctl_port=59050, client_tor_socks_port=59000,
        server_tgen_listen_port=8080, server_tor_ctl_port=59051, server_tor_socks_port=59001
        '''
        self.threads = []
        self.done_event = threading.Event()

        # if ctrl-c is pressed, shutdown child processes properly
        try:
            # make sure stem and Tor supports ephemeral HS (version >= 0.2.7.1-alpha)
            # and also the NEWNYM mode that clears descriptor cache (version >= 0.2.7.3-rc)
            if do_onion:
                try:
                    tor_version = get_system_tor_version(self.tor_bin_path)
                    if tor_version < Requirement.ADD_ONION or tor_version < Version(
                            '0.2.7.3-rc'):  # ADD_ONION is a stem 1.4.0 feature
                        logging.warning(
                            "OnionPerf in onion mode requires Tor version >= 0.2.7.3-rc, you have {0}, aborting"
                            .format(tor_version))
                        return
                except:
                    logging.warning(
                        "OnionPerf in onion mode requires stem version >= 1.4.0, you have {0}, aborting"
                        .format(stem_version))
                    return

            logging.info("Bootstrapping started...")
            logging.info(
                "Log files for the client and server processes will be placed in {0}"
                .format(self.datadir_path))

            general_writables = []
            tgen_client_writable, torctl_client_writable = None, None

            if do_onion or do_inet:
                general_writables.append(
                    self.__start_tgen_server(server_tgen_listen_port))

            if do_onion:
                logging.info(
                    "Onion Service private keys will be placed in {0}".format(
                        self.privatedir_path))
                tor_writable, torctl_writable = self.__start_tor_server(
                    server_tor_ctl_port, server_tor_socks_port,
                    {client_tgen_connect_port: server_tgen_listen_port})
                general_writables.append(tor_writable)
                general_writables.append(torctl_writable)

            if do_onion or do_inet:
                tor_writable, torctl_client_writable = self.__start_tor_client(
                    client_tor_ctl_port, client_tor_socks_port)
                general_writables.append(tor_writable)

            server_urls = []
            if do_onion and self.hs_v3_service_id is not None:
                server_urls.append("{0}.onion:{1}".format(
                    self.hs_v3_service_id, client_tgen_connect_port))
            if do_inet:
                connect_ip = client_tgen_connect_ip if client_tgen_connect_ip != '0.0.0.0' else util.get_ip_address(
                )
                server_urls.append("{0}:{1}".format(connect_ip,
                                                    client_tgen_connect_port))

            if do_onion or do_inet:
                assert len(server_urls) > 0

                tgen_client_writable = self.__start_tgen_client(
                    server_urls, client_tgen_listen_port,
                    client_tor_socks_port)

                self.__start_log_processors(general_writables,
                                            tgen_client_writable,
                                            torctl_client_writable)

                logging.info("Bootstrapping finished, entering heartbeat loop")
                time.sleep(1)
                if self.oneshot:
                    logging.info(
                        "Onionperf is running in Oneshot mode. It will download a 5M file and shut down gracefully..."
                    )
                while True:
                    # TODO add status update of some kind? maybe the number of files in the www directory?
                    # logging.info("Heartbeat: {0} downloads have completed successfully".format(self.__get_download_count(tgen_client_writable.filename)))
                    if self.oneshot:
                        downloads = 0
                        while True:
                            downloads = self.__get_download_count(
                                tgen_client_writable.filename)
                            if downloads >= 1:
                                logging.info(
                                    "Onionperf has downloaded a 5M file in oneshot mode, and will now shut down."
                                )
                                break
                        else:
                            continue
                        break

                    if self.__is_alive():
                        logging.info(
                            "All helper processes seem to be alive :)")
                    else:
                        logging.warning(
                            "Some parallel components failed too many times or have died :("
                        )
                        logging.info(
                            "We are in a broken state, giving up and exiting now"
                        )
                        break

                    logging.info(
                        "Next main process heartbeat is in 1 hour (helper processes run on their own schedule)"
                    )
                    logging.info("press CTRL-C for graceful shutdown...")
                    time.sleep(3600)
            else:
                logging.info("No measurement mode set, nothing to do")

        except KeyboardInterrupt:
            logging.info(
                "Interrupt received, please wait for graceful shutdown")
            self.__is_alive()
        finally:
            logging.info("Cleaning up child processes now...")

            if self.hs_v3_service_id is not None:
                try:
                    with Controller.from_port(
                            port=self.hs_v3_control_port) as torctl:
                        torctl.authenticate()
                        torctl.remove_ephemeral_hidden_service(
                            self.hs_v3_service_id)
                except:
                    pass  # this fails to authenticate if tor proc is dead

#            logging.disable(logging.INFO)
            self.done_event.set()
            for t in self.threads:
                logging.info("Joining {0} thread...".format(t.getName()))
                t.join()
            time.sleep(1)
            #            logging.disable(logging.NOTSET)

            logging.info("Child processes terminated")
            logging.info("Child process cleanup complete!")
            logging.info("Exiting")
Esempio n. 43
0
def renew_connection():
    """Change tor exit node. This will allow cycling of IP addresses."""
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password="******")
        controller.signal(Signal.NEWNYM)  # pylint: disable=E1101
Esempio n. 44
0
def renew_connection(tor_password):
    print(tor_password)
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password=tor_password)
        controller.signal(Signal.NEWNYM)
Esempio n. 45
0
    def start_hidden_service(self, gui=False, tails_root=False):
        if not self.port:
            self.choose_port()

        if helpers.get_platform() == 'Tails' and not tails_root:
            # in Tails, start the hidden service in a root process
            if gui:
                args = [
                    '/usr/bin/gksudo', '-D', 'OnionShare', '--',
                    '/usr/bin/onionshare'
                ]
            else:
                args = ['/usr/bin/sudo', '--', '/usr/bin/onionshare']
            print "Executing: {0}".format(args + [str(self.port)])
            p = subprocess.Popen(args + [str(self.port)],
                                 stderr=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            stdout = p.stdout.read(22)  # .onion URLs are 22 chars long

            if stdout:
                self.onion_host = stdout
                print 'Got onion_host: {0}'.format(self.onion_host)
            else:
                if p.poll() == -1:
                    raise TailsError(o.stderr.read())
                else:
                    raise TailsError(strings._("error_tails_unknown_root"))

        else:
            if self.local_only:
                self.onion_host = '127.0.0.1:{0}'.format(self.port)

            else:
                # come up with a hidden service directory name
                if helpers.get_platform() == 'Tails':
                    # need to create HS directory in /var/lib/tor because of AppArmor rules included in Tails
                    self.hidserv_dir = tempfile.mkdtemp(dir='/var/lib/tor')

                    # change owner to debian-tor
                    import pwd
                    import grp
                    uid = pwd.getpwnam("debian-tor").pw_uid
                    gid = grp.getgrnam("debian-tor").gr_gid
                    os.chown(self.hidserv_dir, uid, gid)
                else:
                    self.hidserv_dir = tempfile.mkdtemp()
                self.cleanup_filenames.append(self.hidserv_dir)

                # connect to the tor controlport
                self.controller = None
                tor_control_ports = [9051, 9151]
                for tor_control_port in tor_control_ports:
                    try:
                        self.controller = Controller.from_port(
                            port=tor_control_port)
                        break
                    except SocketError:
                        pass
                if not self.controller:
                    raise NoTor(
                        strings._("cant_connect_ctrlport").format(
                            tor_control_ports))
                self.controller.authenticate()

                # set up hidden service
                if helpers.get_platform() == 'Windows':
                    self.hidserv_dir = self.hidserv_dir.replace('\\', '/')
                hsdic = self.controller.get_conf_map(
                    'HiddenServiceOptions') or {
                        'HiddenServiceDir': [],
                        'HiddenServicePort': []
                    }
                if self.hidserv_dir in hsdic.get('HiddenServiceDir', []):
                    # Maybe a stale service with the wrong local port
                    dropme = hsdic['HiddenServiceDir'].index(self.hidserv_dir)
                    del hsdic['HiddenServiceDir'][dropme]
                    del hsdic['HiddenServicePort'][dropme]
                hsdic['HiddenServiceDir'] = hsdic.get('HiddenServiceDir',
                                                      []) + [self.hidserv_dir]
                hsdic['HiddenServicePort'] = hsdic.get(
                    'HiddenServicePort',
                    []) + ['80 127.0.0.1:{0}'.format(self.port)]

                self.controller.set_options(hsdic2list(hsdic))

                # figure out the .onion hostname
                hostname_file = '{0}/hostname'.format(self.hidserv_dir)
                self.onion_host = open(hostname_file, 'r').read().strip()
Esempio n. 46
0
import psycopg2
import pandas as pd
from io import StringIO
from lxml import etree
from time import sleep
from random import randint
from selenium import webdriver
from sqlalchemy import create_engine
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from stem import Signal
from stem.control import Controller

# First, we run TOR.
controller = Controller.from_port(port=9051)


def renew_tor():
    controller.authenticate('Behemoth3d')
    controller.signal(Signal.NEWNYM)


# def test_tor():
#     browser.get('http://www.whatsmyip.org/')


def google_pdf_parser(all_links, page, parser, error_appears, engine, browser,
                      a_keyword):
    dict_links = []
    _next_page_exist = True
Esempio n. 47
0
def renew_connection():
    with Controller.from_port(port = 1051) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)
        controller.close()
Esempio n. 48
0
	element.send_keys(generated_value)
	button = driver.get_element_by_xpath("//input[@type='submit' and @value='create account']")
	button.click()
	print driver.page_source

while (True):
        cur_ip = requests.get("http://icanhazip.com/", proxies={"http": "127.0.0.1:8118"}).text.strip()
        if cur_ip not in ip_list:
                ip_list.append(cur_ip)
                firefox()
                print cur_ip + " | " + generated_value
                print "\n\n"
                print result.text.strip()
                print "\n\n"
        sleep(1)
        with Controller.from_port(port=9051) as controller:
                sleep(1.5)
                controller.authenticate(password="******")
                sleep(1.5)
                controller.signal(Signal.NEWNYM)
        sleep(1)

# Keyboard Interrupt

print ("We ended up with " + str(len(ip_list)) + " IP Addresses")
print (ip_list)

def firefox_get(url):
	driver.get(url)
	heading_element = driver.find_element_by_xpath("//*[@id='heading-breadcrumbs']")
	if heading_element:
Esempio n. 49
0
    def __start_tor(self,
                    name,
                    control_port,
                    socks_port,
                    hs_port_mapping=None):
        logging.info(
            "Starting Tor {0} process with ControlPort={1}, SocksPort={2}...".
            format(name, control_port, socks_port))

        tor_datadir = "{0}/tor-{1}".format(self.datadir_path, name)
        if not os.path.exists(tor_datadir): os.makedirs(tor_datadir)

        tor_config_template = "ORPort 0\nDirPort 0\nControlPort {0}\nSocksPort {1}\nSocksListenAddress 127.0.0.1\nClientOnly 1\n\
WarnUnsafeSocks 0\nSafeLogging 0\nMaxCircuitDirtiness 60 seconds\nUseEntryGuards 0\nDataDirectory {2}\nLog INFO stdout\n"

        tor_config = tor_config_template.format(control_port, socks_port,
                                                tor_datadir)

        tor_logpath = "{0}/onionperf.tor.log".format(tor_datadir)
        tor_writable = util.FileWritable(tor_logpath)
        logging.info("Logging Tor {0} process output to {1}".format(
            name, tor_logpath))

        # from stem.process import launch_tor_with_config
        # tor_subp = launch_tor_with_config(tor_config, tor_cmd=self.tor_bin_path, completion_percent=100, init_msg_handler=None, timeout=None, take_ownership=False)
        tor_cmd = "{0} -f -".format(self.tor_bin_path)
        tor_stdin_bytes = str_tools._to_bytes(tor_config)
        tor_ready_str = "Bootstrapped 100"
        tor_ready_ev = threading.Event()
        tor_args = (tor_cmd, tor_datadir, tor_writable, self.done_event,
                    tor_stdin_bytes, tor_ready_str, tor_ready_ev)
        tor_watchdog = threading.Thread(target=watchdog_thread_task,
                                        name="tor_{0}_watchdog".format(name),
                                        args=tor_args)
        tor_watchdog.start()
        self.threads.append(tor_watchdog)

        # wait until Tor finishes bootstrapping
        tor_ready_ev.wait()

        torctl_logpath = "{0}/onionperf.torctl.log".format(tor_datadir)
        torctl_writable = util.FileWritable(torctl_logpath)
        logging.info(
            "Logging Tor {0} control port monitor output to {1}".format(
                name, torctl_logpath))

        # give a few seconds to make sure Tor had time to start listening on the control port
        time.sleep(3)

        torctl_events = [
            e for e in monitor.get_supported_torctl_events()
            if e not in ['DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERR']
        ]
        newnym_interval_seconds = 300
        torctl_args = (control_port, torctl_writable, torctl_events,
                       newnym_interval_seconds, self.done_event)
        torctl_helper = threading.Thread(target=monitor.tor_monitor_run,
                                         name="torctl_{0}_helper".format(name),
                                         args=torctl_args)
        torctl_helper.start()
        self.threads.append(torctl_helper)

        if hs_port_mapping is not None:
            logging.info("Creating ephemeral hidden service...")
            with Controller.from_port(port=control_port) as torctl:
                torctl.authenticate()
                response = torctl.create_ephemeral_hidden_service(
                    hs_port_mapping, detached=True, await_publication=True)
                self.hs_service_id = response.service_id
                self.hs_control_port = control_port
                logging.info(
                    "Ephemeral hidden service is available at {0}.onion".
                    format(response.service_id))

        return tor_writable, torctl_writable
Esempio n. 50
0
def get_new_ip():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)
        time.sleep(controller.get_newnym_wait())
Esempio n. 51
0
import logging

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

from stem.control import Controller

controller = Controller.from_port(address="127.0.0.1", port=9051)
try:
    controller.authenticate(password="******")
    logger.info(f'authenticated')
except Exception as e:
    logger.info(f'failed to authenticate: {e}')
Esempio n. 52
0
    def connect(self,
                settings=False,
                config=False,
                tor_status_update_func=None):
        common.log('Onion', 'connect')

        # Either use settings that are passed in, or load them from disk
        if settings:
            self.settings = settings
        else:
            self.settings = Settings(config)
            self.settings.load()

        # The Tor controller
        self.c = None

        if self.settings.get('connection_type') == 'bundled':
            if not self.bundle_tor_supported:
                raise BundledTorNotSupported(
                    strings._('settings_error_bundled_tor_not_supported'))

            # Create a torrc for this session
            self.tor_data_directory = tempfile.TemporaryDirectory()

            if self.system == 'Windows':
                # Windows needs to use network ports, doesn't support unix sockets
                torrc_template = open(
                    common.get_resource_path('torrc_template-windows')).read()
                try:
                    self.tor_control_port = common.get_available_port(
                        1000, 65535)
                except:
                    raise OSError(strings._('no_available_port'))
                self.tor_control_socket = None
                self.tor_cookie_auth_file = os.path.join(
                    self.tor_data_directory.name, 'cookie')
                try:
                    self.tor_socks_port = common.get_available_port(
                        1000, 65535)
                except:
                    raise OSError(strings._('no_available_port'))
                self.tor_torrc = os.path.join(self.tor_data_directory.name,
                                              'torrc')
            else:
                # Linux, Mac and BSD can use unix sockets
                with open(common.get_resource_path('torrc_template')) as f:
                    torrc_template = f.read()
                self.tor_control_port = None
                self.tor_control_socket = os.path.join(
                    self.tor_data_directory.name, 'control_socket')
                self.tor_cookie_auth_file = os.path.join(
                    self.tor_data_directory.name, 'cookie')
                try:
                    self.tor_socks_port = common.get_available_port(
                        1000, 65535)
                except:
                    raise OSError(strings._('no_available_port'))
                self.tor_torrc = os.path.join(self.tor_data_directory.name,
                                              'torrc')

            torrc_template = torrc_template.replace(
                '{{data_directory}}', self.tor_data_directory.name)
            torrc_template = torrc_template.replace('{{control_port}}',
                                                    str(self.tor_control_port))
            torrc_template = torrc_template.replace(
                '{{control_socket}}', str(self.tor_control_socket))
            torrc_template = torrc_template.replace('{{cookie_auth_file}}',
                                                    self.tor_cookie_auth_file)
            torrc_template = torrc_template.replace('{{geo_ip_file}}',
                                                    self.tor_geo_ip_file_path)
            torrc_template = torrc_template.replace(
                '{{geo_ipv6_file}}', self.tor_geo_ipv6_file_path)
            torrc_template = torrc_template.replace('{{socks_port}}',
                                                    str(self.tor_socks_port))
            with open(self.tor_torrc, 'w') as f:
                f.write(torrc_template)

                # Bridge support
                if self.settings.get('tor_bridges_use_obfs4'):
                    f.write('ClientTransportPlugin obfs4 exec {}\n'.format(
                        self.obfs4proxy_file_path))
                    with open(common.get_resource_path(
                            'torrc_template-obfs4')) as o:
                        for line in o:
                            f.write(line)
                elif self.settings.get('tor_bridges_use_meek_lite_amazon'):
                    f.write('ClientTransportPlugin meek_lite exec {}\n'.format(
                        self.obfs4proxy_file_path))
                    with open(
                            common.get_resource_path(
                                'torrc_template-meek_lite_amazon')) as o:
                        for line in o:
                            f.write(line)
                elif self.settings.get('tor_bridges_use_meek_lite_azure'):
                    f.write('ClientTransportPlugin meek_lite exec {}\n'.format(
                        self.obfs4proxy_file_path))
                    with open(
                            common.get_resource_path(
                                'torrc_template-meek_lite_azure')) as o:
                        for line in o:
                            f.write(line)

                if self.settings.get('tor_bridges_use_custom_bridges'):
                    if 'obfs4' in self.settings.get(
                            'tor_bridges_use_custom_bridges'):
                        f.write('ClientTransportPlugin obfs4 exec {}\n'.format(
                            self.obfs4proxy_file_path))
                    elif 'meek_lite' in self.settings.get(
                            'tor_bridges_use_custom_bridges'):
                        f.write(
                            'ClientTransportPlugin meek_lite exec {}\n'.format(
                                self.obfs4proxy_file_path))
                    f.write(
                        self.settings.get('tor_bridges_use_custom_bridges'))
                    f.write('\nUseBridges 1')

            # Execute a tor subprocess
            start_ts = time.time()
            if self.system == 'Windows':
                # In Windows, hide console window when opening tor.exe subprocess
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                self.tor_proc = subprocess.Popen(
                    [self.tor_path, '-f', self.tor_torrc],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    startupinfo=startupinfo)
            else:
                self.tor_proc = subprocess.Popen(
                    [self.tor_path, '-f', self.tor_torrc],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)

            # Wait for the tor controller to start
            time.sleep(2)

            # Connect to the controller
            try:
                if self.system == 'Windows':
                    self.c = Controller.from_port(port=self.tor_control_port)
                    self.c.authenticate()
                else:
                    self.c = Controller.from_socket_file(
                        path=self.tor_control_socket)
                    self.c.authenticate()
            except Exception as e:
                raise BundledTorBroken(
                    strings._('settings_error_bundled_tor_broken',
                              True).format(e.args[0]))

            while True:
                try:
                    res = self.c.get_info("status/bootstrap-phase")
                except SocketClosed:
                    raise BundledTorCanceled()

                res_parts = shlex.split(res)
                progress = res_parts[2].split('=')[1]
                summary = res_parts[4].split('=')[1]

                # "\033[K" clears the rest of the line
                print("{}: {}% - {}{}".format(strings._('connecting_to_tor'),
                                              progress, summary, "\033[K"),
                      end="\r")

                if callable(tor_status_update_func):
                    if not tor_status_update_func(progress, summary):
                        # If the dialog was canceled, stop connecting to Tor
                        common.log(
                            'Onion', 'connect',
                            'tor_status_update_func returned false, canceling connecting to Tor'
                        )
                        print()
                        return False

                if summary == 'Done':
                    print("")
                    break
                time.sleep(0.2)

                # If using bridges, it might take a bit longer to connect to Tor
                if self.settings.get('tor_bridges_use_custom_bridges') or \
                   self.settings.get('tor_bridges_use_obfs4') or \
                   self.settings.get('tor_bridges_use_meek_lite_amazon') or \
                   self.settings.get('tor_bridges_use_meek_lite_azure'):
                    connect_timeout = 150
                else:
                    # Timeout after 120 seconds
                    connect_timeout = 120
                if time.time() - start_ts > connect_timeout:
                    print("")
                    self.tor_proc.terminate()
                    raise BundledTorTimeout(
                        strings._('settings_error_bundled_tor_timeout'))

        elif self.settings.get('connection_type') == 'automatic':
            # Automatically try to guess the right way to connect to Tor Browser

            # Try connecting to control port
            found_tor = False

            # If the TOR_CONTROL_PORT environment variable is set, use that
            env_port = os.environ.get('TOR_CONTROL_PORT')
            if env_port:
                try:
                    self.c = Controller.from_port(port=int(env_port))
                    found_tor = True
                except:
                    pass

            else:
                # Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor
                try:
                    ports = [9151, 9153, 9051]
                    for port in ports:
                        self.c = Controller.from_port(port=port)
                        found_tor = True
                except:
                    pass

                # If this still didn't work, try guessing the default socket file path
                socket_file_path = ''
                if not found_tor:
                    try:
                        if self.system == 'Darwin':
                            socket_file_path = os.path.expanduser(
                                '~/Library/Application Support/TorBrowser-Data/Tor/control.socket'
                            )

                        self.c = Controller.from_socket_file(
                            path=socket_file_path)
                        found_tor = True
                    except:
                        pass

            # If connecting to default control ports failed, so let's try
            # guessing the socket file name next
            if not found_tor:
                try:
                    if self.system == 'Linux' or self.system == 'BSD':
                        socket_file_path = '/run/user/{}/Tor/control.socket'.format(
                            os.geteuid())
                    elif self.system == 'Darwin':
                        socket_file_path = '/run/user/{}/Tor/control.socket'.format(
                            os.geteuid())
                    elif self.system == 'Windows':
                        # Windows doesn't support unix sockets
                        raise TorErrorAutomatic(
                            strings._('settings_error_automatic'))

                    self.c = Controller.from_socket_file(path=socket_file_path)

                except:
                    raise TorErrorAutomatic(
                        strings._('settings_error_automatic'))

            # Try authenticating
            try:
                self.c.authenticate()
            except:
                raise TorErrorAutomatic(strings._('settings_error_automatic'))

        else:
            # Use specific settings to connect to tor

            # Try connecting
            try:
                if self.settings.get('connection_type') == 'control_port':
                    self.c = Controller.from_port(
                        address=self.settings.get('control_port_address'),
                        port=self.settings.get('control_port_port'))
                elif self.settings.get('connection_type') == 'socket_file':
                    self.c = Controller.from_socket_file(
                        path=self.settings.get('socket_file_path'))
                else:
                    raise TorErrorInvalidSetting(
                        strings._("settings_error_unknown"))

            except:
                if self.settings.get('connection_type') == 'control_port':
                    raise TorErrorSocketPort(
                        strings._("settings_error_socket_port").format(
                            self.settings.get('control_port_address'),
                            self.settings.get('control_port_port')))
                else:
                    raise TorErrorSocketFile(
                        strings._("settings_error_socket_file").format(
                            self.settings.get('socket_file_path')))

            # Try authenticating
            try:
                if self.settings.get('auth_type') == 'no_auth':
                    self.c.authenticate()
                elif self.settings.get('auth_type') == 'password':
                    self.c.authenticate(self.settings.get('auth_password'))
                else:
                    raise TorErrorInvalidSetting(
                        strings._("settings_error_unknown"))

            except MissingPassword:
                raise TorErrorMissingPassword(
                    strings._('settings_error_missing_password'))
            except UnreadableCookieFile:
                raise TorErrorUnreadableCookieFile(
                    strings._('settings_error_unreadable_cookie_file'))
            except AuthenticationFailure:
                raise TorErrorAuthError(
                    strings._('settings_error_auth').format(
                        self.settings.get('control_port_address'),
                        self.settings.get('control_port_port')))

        # If we made it this far, we should be connected to Tor
        self.connected_to_tor = True

        # Get the tor version
        self.tor_version = self.c.get_version().version_str

        # Do the versions of stem and tor that I'm using support ephemeral onion services?
        list_ephemeral_hidden_services = getattr(
            self.c, "list_ephemeral_hidden_services", None)
        self.supports_ephemeral = callable(
            list_ephemeral_hidden_services) and self.tor_version >= '0.2.7.1'

        # Do the versions of stem and tor that I'm using support stealth onion services?
        try:
            res = self.c.create_ephemeral_hidden_service(
                {1: 1},
                basic_auth={'onionshare': None},
                await_publication=False)
            tmp_service_id = res.service_id
            self.c.remove_ephemeral_hidden_service(tmp_service_id)
            self.supports_stealth = True
        except:
            # ephemeral stealth onion services are not supported
            self.supports_stealth = False
Esempio n. 53
0
def main():
    load_strings()

    # validate filename
    if len(sys.argv) != 2:
        sys.exit('Usage: {0} [filename]'.format(sys.argv[0]))
    filename = sys.argv[1]
    if not os.path.isfile(filename):
        sys.exit(strings["not_a_file"].format(filename))

    # calculate filehash, file size
    print strings["calculating_sha1"]
    BLOCKSIZE = 65536
    hasher = hashlib.sha1()
    with open(filename, 'rb') as f:
        buf = f.read(BLOCKSIZE)
        while len(buf) > 0:
            hasher.update(buf)
            buf = f.read(BLOCKSIZE)
    filehash = hasher.hexdigest()
    filesize = os.path.getsize(filename)

    # let the OS choose a port
    tmpsock = socket.socket()
    tmpsock.bind(("127.0.0.1", 0))
    port = tmpsock.getsockname()[1]
    tmpsock.close()

    # connect to the tor controlport
    print strings["connecting_ctrlport"].format(port)
    controlports = [9051, 9151]
    controller = False
    for controlport in controlports:
        try:
            controller = Controller.from_port(port=controlport)
        except SocketError:
            pass
    if not controller:
        sys.exit(strings["cant_connect_ctrlport"].format(controlports))
    controller.authenticate()

    # set up hidden service
    controller.set_options([('HiddenServiceDir', get_hidden_service_dir(port)),
                            ('HiddenServicePort',
                             '80 127.0.0.1:{0}'.format(port))])
    onion_host = get_hidden_service_hostname(port)

    # punch a hole in the firewall
    tails_open_port(port)

    # instructions
    print '\n' + strings["give_this_url"]
    print 'http://{0}/{1}'.format(onion_host, slug)
    print ''
    print strings["ctrlc_to_stop"]

    # start the web server
    app.run(port=port)
    print '\n'

    # shutdown
    tails_close_port(port)
Esempio n. 54
0
 def _setTorController(self):
     """Initialize a Controller with the control port."""
     try:
         self.tor_controller = Controller.from_port(port=self.ctrl_port)
     except Exception as err:
         raise EnvironmentError(err)
Esempio n. 55
0
def change_proxy():
    with Controller.from_port(port=9151) as controller:
        controller.authenticate(password="******")
        controller.signal(Signal.NEWNYM)
Esempio n. 56
0
def change_ip():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password='******')
        controller.signal(Signal.NEWNYM)
    logging.debug('changed ip')
Esempio n. 57
0
    def __init__(self, port, password=None):
        self.__passwd = password
        self._port = port

        self._ctrl = Controller.from_port(address='127.0.0.1', port=self._port)
Esempio n. 58
0
def new_identity():
    with Controller.from_port(port=9051) as contr:
        contr.authenticate(password='******')
        contr.signal(Signal.NEWNYM)  # Works despite the error
        print("New Tor connection")
Esempio n. 59
0
)

print(term.format("\nChecking our endpoint:\n", term.Attr.BOLD))

# Через requests
# print(term.format(query("https://www.atagar.com/echo.php"), term.Color.BLUE))
# print(query("http://ya.ru"))
# proxies = {
#     'http': 'socks5://127.0.0.1:{}'.format(SOCKS_PORT),
#     'https': 'socks5://127.0.0.1:{}'.format(SOCKS_PORT)
# }
# resp = requests.get('http://icanhazip.com/', proxies=proxies)
# print(resp.headers)
# print(resp.content)

# Через Grab
g = Grab(proxy='127.0.0.1:{}'.format(SOCKS_PORT),
         proxy_type='socks5',
         timeout=90,
         connect_timeout=30)
c = Controller.from_port()
c.authenticate()
for i in range(10):
    g.go('http://icanhazip.com/')
    print(i)
    print(g.doc.head)
    print(g.doc.body)
    print()
    c.signal(Signal.NEWNYM)

tor_process.kill()  # stops tor
Esempio n. 60
0
        while len(buf) > 0:
            hasher.update(buf)
            buf = f.read(BLOCKSIZE)
    filehash = hasher.hexdigest()
    filesize = os.path.getsize(filename)

    # choose a port
    port = randint(1025, 65535)

    # connect to the tor controlport
    print strings["connecting_ctrlport"].format(port)
    controlports = [9051, 9151]
    controller = False
    for controlport in controlports:
        try:
            controller = Controller.from_port(port=controlport)
        except SocketError:
            pass
    if not controller:
        sys.exit(strings["cant_connect_ctrlport"].format(controlports))
    controller.authenticate()

    # set up hidden service
    controller.set_options([('HiddenServiceDir', get_hidden_service_dir(port)),
                            ('HiddenServicePort',
                             '80 127.0.0.1:{0}'.format(port))])
    onion_host = get_hidden_service_hostname(port)

    # punch a hole in the firewall
    tails_open_port(port)