def add_service(torconfig, service, port=None): """ Create a new hiddenservice and adds it to the `hiddenservices` list. : param service : the service to be run. """ port = port or config.custom.base_port + len(apaf.hiddenservices) service.hs = txtorcon.HiddenService( torconfig, os.path.join(config.tor_data, service.name), ['%d 127.0.0.1:%d' % (service.port, port)]) apaf.hiddenservices.append(service)
def add_service(torconfig, service, port=None): """ Create a new hiddenservice and adds it to the `hiddenservices` list. :param service: the service to be run. """ # picks a random port until it finds one avaible. while not service.tcp: port = port or new_port() try: service.tcp = reactor.listenTCP(port, service.factory) except error.CannotListenError: pass service.hs = txtorcon.HiddenService( torconfig, os.path.join(config.tor_data, service.name), ['%d 127.0.0.1:%d' % (service.port, port)]) apaf.hiddenservices.append(service)
## register something to clean up our tempdir reactor.addSystemEventTrigger( 'before', 'shutdown', functools.partial(txtorcon.util.delete_file_or_tree, hs_temp)) ## configure the hidden service we want. ## obviously, we'd want a more-persistent place to keep the hidden ## service directory for a "real" setup. If the directory is empty at ## startup as here, Tor creates new keys etcetera (which IS the .onion ## address). That is, every time you run this script you get a new ## hidden service URI, which is probably not what you want. ## The launch_tor method adds other needed config directives to give ## us a minimal config. config = txtorcon.TorConfig() config.HiddenServices = [ txtorcon.HiddenService( config, hs_temp, [str(hs_public_port) + " 127.0.0.1:" + str(hs_port)]) ] config.save() ## next we set up our service to listen on hs_port which is forwarded ## (via the HiddenService options) from the hidden service address on ## port hs_public_port site = server.Site(Simple()) hs_endpoint = TCP4ServerEndpoint(reactor, hs_port) hs_endpoint.listen(site) ## we've got our Twisted service listening locally and our options ## ready to go, so we now launch Tor. Once it's done (see above ## callbacks) we print out the .onion URI and then do "nothing" ## (i.e. let the Web server do its thing). Note that the way we've set ## up the slave Tor process, when we close the connection to it tor
def startHTTPHiddenService(self, serviceDir, servicePort=8080, hiddenserviceDir=None, hiddenservicePort=80, serviceInterface='127.0.0.1', socksPort=9152, orPort=9000): if is_valid_ipv4_address( serviceInterface) == False and is_valid_ipv6_address( serviceInterface) == False: pluginException = PluginException( message= 'The Service Interface is invalid. Try to use the default value without specify the parameter "serviceInterface" ', trace= "startHTTPHiddenService with args serviceDir=%s , servicePort=%s , hiddenserviceDir=%s , hiddenservicePort=%s , serviceInterface=%s , socksPort=%s , orPort=%s" % (serviceDir, str(servicePort), hiddenserviceDir, str(hiddenservicePort), serviceInterface, str(socksPort), str(orPort)), plugin="maliciousHiddenServicePlugin", method="startHTTPHiddenService") if self.runFromInterpreter: showTrace(pluginException) return else: print '[-] The Service Interface is invalid. Try to use the default value without specify the parameter "serviceInterface". ' raise pluginException if is_valid_port(hiddenservicePort) == False: pluginException = PluginException( message='The hidden service port is invalid.', trace= "startHTTPHiddenService with args serviceDir=%s , servicePort=%s , hiddenserviceDir=%s , hiddenservicePort=%s , serviceInterface=%s , socksPort=%s , orPort=%s" % (serviceDir, str(servicePort), hiddenserviceDir, str(hiddenservicePort), serviceInterface, str(socksPort), str(orPort)), plugin="maliciousHiddenServicePlugin", method="startHTTPHiddenService") if self.runFromInterpreter: showTrace(pluginException) return else: print "[-] The hidden service port is invalid. " raise pluginException if is_valid_port(socksPort) == False: pluginException = PluginException( message='The socks port is invalid.', trace= "startHTTPHiddenService with args serviceDir=%s , servicePort=%s , hiddenserviceDir=%s , hiddenservicePort=%s , serviceInterface=%s , socksPort=%s , orPort=%s" % (serviceDir, str(servicePort), hiddenserviceDir, str(hiddenservicePort), serviceInterface, str(socksPort), str(orPort)), plugin="maliciousHiddenServicePlugin", method="startHTTPHiddenService") if self.runFromInterpreter: showTrace(pluginException) return else: print "[-] The socks port is invalid. " raise pluginException if is_valid_port(orPort) == False: pluginException = PluginException( message='The OR port is invalid.', trace= "startHTTPHiddenService with args serviceDir=%s , servicePort=%s , hiddenserviceDir=%s , hiddenservicePort=%s , serviceInterface=%s , socksPort=%s , orPort=%s" % (serviceDir, str(servicePort), hiddenserviceDir, str(hiddenservicePort), serviceInterface, str(socksPort), str(orPort)), plugin="maliciousHiddenServicePlugin", method="startHTTPHiddenService") if self.runFromInterpreter: showTrace(pluginException) return else: print "[-] The OR port is invalid. " raise pluginException if is_valid_port(servicePort) == False: pluginException = PluginException( message='The Service port is invalid.', trace= "startHTTPHiddenService with args serviceDir=%s , servicePort=%s , hiddenserviceDir=%s , hiddenservicePort=%s , serviceInterface=%s , socksPort=%s , orPort=%s" % (serviceDir, str(servicePort), hiddenserviceDir, str(hiddenservicePort), serviceInterface, str(socksPort), str(orPort)), plugin="maliciousHiddenServicePlugin", method="startHTTPHiddenService") if self.runFromInterpreter: showTrace(pluginException) return else: print "[-] The Service port is invalid. " raise pluginException self.hiddenservicePort = hiddenservicePort config = txtorcon.TorConfig() config.SOCKSPort = socksPort config.ORPort = orPort if hiddenserviceDir is None: print "[+] HiddenServiceDir not specified... Generating a temporal file." hiddenserviceDir = self.__createTemporal() if os.path.exists(hiddenserviceDir) == False: print "[+] The HiddenServiceDir specified does not exists... Generating a temporal file." hiddenserviceDir = self.__createTemporal() if serviceDir is None or os.path.exists(serviceDir) == False: pluginException = PluginException( "The specified Server directory is not valid.", trace= "startHTTPHiddenService with args serviceDir=%s , servicePort=%s , hiddenserviceDir=%s , hiddenservicePort=%s , serviceInterface=%s , socksPort=%s , orPort=%s" % (serviceDir, str(servicePort), hiddenserviceDir, str(hiddenservicePort), serviceInterface, str(socksPort), str(orPort)), plugin="maliciousHiddenServicePlugin", method="startHTTPHiddenService") if self.runFromInterpreter: showTrace(pluginException) return else: print "[-] The specified Server directory is not valid. You must specify a valid directory where resources like HTML pages, images, CSS and stuff like that are located. The directory will be used to start a simple HTTP Server." raise pluginException config.HiddenServices = [ txtorcon.HiddenService(config, hiddenserviceDir, [ "%s %s:%s" % (str(hiddenservicePort), serviceInterface, str(servicePort)) ]) ] config.save() root = static.File(serviceDir) root.putChild("gatherUserInfo", GatherInformation()) site = server.Site(root) hs_endpoint = TCP4ServerEndpoint(reactor, servicePort, interface=serviceInterface) hs_endpoint.listen(site) try: d = txtorcon.launch_tor( config, reactor, progress_updates=self.__updates, ) except txtorcon.TorNotFound as torBinaryNotFound: print "[-] Tor binary not found in the system path. Using the property 'torExecutablePath' located in the config/config.py file." d = txtorcon.launch_tor( config, reactor, tor_binary=tortazoConfig.torExecutablePath, progress_updates=self.__updates, ) d.addCallback(functools.partial(self.__setup_complete, config)) d.addErrback(self.__setup_failed) reactor.run() return True
def main(): # Parse the commandline-options try: opts, args = getopt.getopt(sys.argv[1:], 'hd:p:') except getopt.GetoptError as excp: print(str(excp)) print_help() return 1 serve_directory = '.' # The default directory to serve files from hs_public_port = 8011 # The port the hidden service is available on web_port = 4711 # The real server's local port web_host = '127.0.0.1' # The real server is bound to localhost for o, a in opts: if o == '-d': serve_directory = a elif o == '-p': hs_public_port = int(a) elif o == '-h': print_help() return else: print('Unknown option "%s"' % (o, )) return 1 # Sanitize path and set working directory there (for SimpleHTTPServer) serve_directory = os.path.abspath(serve_directory) if not os.path.exists(serve_directory): print('Path "%s" does not exists, can\'t serve from there...' % \ (serve_directory, )) return 1 os.chdir(serve_directory) # Create a new SimpleHTTPServer and serve it from another thread. # We create a callback to Twisted to shut it down when we exit. print('Serving "%s" on %s:%i' % (serve_directory, web_host, web_port)) httpd = SocketServer.TCPServer((web_host, web_port), SimpleHTTPServer.SimpleHTTPRequestHandler) start_httpd(httpd) reactor.addSystemEventTrigger( 'before', 'shutdown', stop_httpd, httpd=httpd, ) # Create a directory to hold our hidden service. Twisted will unlink it # when we exit. hs_temp = tempfile.mkdtemp(prefix='torhiddenservice') reactor.addSystemEventTrigger( 'before', 'shutdown', functools.partial(txtorcon.util.delete_file_or_tree, hs_temp)) # Add the hidden service to a blank configuration config = txtorcon.TorConfig() config.SOCKSPort = 0 config.ORPort = 9089 config.HiddenServices = [ txtorcon.HiddenService( config, hs_temp, ports=['%i %s:%i' % (hs_public_port, web_host, web_port)]) ] config.save() # Now launch tor # Notice that we use a partial function as a callback so we have a # reference to the config object when tor is fully running. tordeferred = txtorcon.launch_tor(config, reactor, progress_updates=print_tor_updates) tordeferred.addCallback( functools.partial(setup_complete, config, hs_public_port)) tordeferred.addErrback(setup_failed) reactor.run()
'before', 'shutdown', functools.partial(txtorcon.util.delete_file_or_tree, hs_temp)) # configure the hidden service we want. # obviously, we'd want a more-persistent place to keep the hidden # service directory for a "real" setup. If the directory is empty at # startup as here, Tor creates new keys etcetera (which IS the .onion # address). That is, every time you run this script you get a new # hidden service URI, which is probably not what you want. # The launch_tor method adds other needed config directives to give # us a minimal config. config = txtorcon.TorConfig() config.SOCKSPort = 0 config.ORPort = 9089 config.HiddenServices = [ txtorcon.HiddenService(config, hs_temp, ["%d 127.0.0.1:%d" % (hs_public_port, hs_port)]) ] config.save() # next we set up our service to listen on hs_port which is forwarded # (via the HiddenService options) from the hidden service address on # port hs_public_port site = server.Site(Simple()) hs_endpoint = TCP4ServerEndpoint(reactor, hs_port, interface='127.0.0.1') hs_endpoint.listen(site) # we've got our Twisted service listening locally and our options # ready to go, so we now launch Tor. Once it's done (see above # callbacks) we print out the .onion URI and then do "nothing" # (i.e. let the Web server do its thing). Note that the way we've set # up the slave Tor process, when we close the connection to it tor