Example #1
0
 def addAllTool(self, command_name):
     """
     Add the appropriate tools (level check and wave's commands check) for this scope.
     Args:
         command_name: The command that we want to create all the tools for.
     """
     mongoInstance = MongoCalendar.getInstance()
     command = mongoInstance.findInDb("pollenisator", "commands",
                                      {"name": command_name}, False)
     if command["lvl"] == "network":
         newTool = Tool()
         newTool.initialize(command["name"], self.wave, self.scope, "", "",
                            "", "network")
         newTool.addInDb()
         return
     if command["lvl"] == "domain":
         if not Utils.isNetworkIp(self.scope):
             newTool = Tool()
             newTool.initialize(command["name"], self.wave, self.scope, "",
                                "", "", "domain")
             newTool.addInDb()
         return
     ips = self.getIpsFitting()
     for ip in ips:
         i = Ip(ip)
         i.addAllTool(command_name, self.wave, self.scope)
Example #2
0
 def getCommandToExecute(self, outputDirectory):
     """
     Get the tool bash command to execute.
     Replace the command's text's variables with tool's informations.
     Args:
         outputDirectory: the output directory for this tool (see getOutputDir)
     Return:
         Returns the bash command of this tool instance.
     """
     toolHasCommand = self.text
     if toolHasCommand is not None and toolHasCommand.strip() != "":
         command = self.text
         lvl = self.lvl
     else:
         comm = self.getCommand()
         command = comm["text"]
         lvl = comm["lvl"]
     mongoInstance = MongoCalendar.getInstance()
     command = command.replace("|outputDir|", outputDirectory)
     command = command.replace("|wave|", self.wave)
     if lvl == "network" or lvl == "domain":
         command = command.replace("|scope|", self.scope)
         if Utils.isNetworkIp(self.scope) == False:
             depths = self.scope.split(".")
             if len(depths) > 2:
                 topdomain = ".".join(depths[1:])
             else:
                 topdomain = ".".join(depths)
             command = command.replace("|parent_domain|", topdomain)
     if lvl == "ip":
         command = command.replace("|ip|", self.ip)
         ip_db = mongoInstance.find("ips", {"ip": self.ip}, False)
         ip_infos = ip_db.get("infos", {})
         for info in ip_infos:
             command = command.replace("|ip.infos." + str(info) + "|",
                                       command)
     if lvl == "port":
         command = command.replace("|ip|", self.ip)
         command = command.replace("|port|", self.port)
         command = command.replace("|port.proto|", self.proto)
         port_db = mongoInstance.find("ports", {
             "port": self.port,
             "proto": self.proto,
             "ip": self.ip
         }, False)
         command = command.replace("|port.service|", port_db["service"])
         command = command.replace("|port.product|", port_db["product"])
         port_infos = port_db.get("infos", {})
         for info in port_infos:
             # print("replacing "+"|port.infos."+str(info)+"|"+ "by "+str(info))
             command = command.replace("|port.infos." + str(info) + "|",
                                       str(port_infos[info]))
     return command
Example #3
0
    def doInsert(self, values):
        """
        Insert the Scope represented by this model in the database with the given values.

        Args:
            values: A dictionary crafted by MultipleScopeView or ScopeView containg all form fields values needed.

        Returns:
            {
                '_id': The mongo ObjectId _id of the inserted command document.
                'nbErrors': The number of objects that has not been inserted in database due to errors.
            }
        """
        # Only multi insert exists at the moment for Scope
        # Get form values
        wave = values["wave"]
        ret = []
        total = 0
        accepted = 0
        insert_setting = values["Settings"]
        split_range_setting = values.get("Split", False)
        for line in values["Scopes"].split("\n"):
            if line.strip() != "":
                # Insert in database
                scopeToAdd = line.strip()
                if Utils.isIp(scopeToAdd):
                    scopeToAdd += "/32"
                if Utils.isNetworkIp(scopeToAdd):
                    if split_range_setting:
                        network_ips = Utils.splitRange(scopeToAdd)
                        if len(network_ips) == 0:
                            model = Scope().initialize(wave, scopeToAdd, "")
                            inserted_res, iid = model.addInDb()
                        else:
                            for network_ip in network_ips:
                                model = Scope().initialize(wave,  str(network_ip), "")
                                inserted_res, iid = model.addInDb()
                                if inserted_res:
                                    accepted += 1
                                    ret.append(iid)
                                total += 1
                    else:
                        model = Scope().initialize(wave,  scopeToAdd, "")
                        inserted_res, iid = model.addInDb()
                else:
                    model = Scope().initialize(wave,  scopeToAdd, "")
                    inserted_res, iid = model.addDomainInDb(insert_setting)
                if inserted_res == 1:
                    accepted += 1
                    ret.append(iid)
                total += 1
        return ret, total-accepted  # nb errors = total - accepted
Example #4
0
    def checkDomainFit(cls, waveName, domain):
        """
        Check if a found domain belongs to one of the scope of the given wave.

        Args:
            waveName: The wave id (name) you want to search for a validating scope
            domain: The found domain.

        Returns:
            boolean
        """
        # Checking settings for domain check.
        settings = Settings()
        # get the domain ip so we can search for it in ipv4 range scopes.
        domainIp = Utils.performLookUp(domain)
        mongoInstance = MongoCalendar.getInstance()
        scopesOfWave = mongoInstance.find("scopes", {"wave": waveName})
        for scopeOfWave in scopesOfWave:
            scopeIsANetworkIp = Utils.isNetworkIp(scopeOfWave["scope"])
            if scopeIsANetworkIp:
                if settings.db_settings.get("include_domains_with_ip_in_scope",
                                            False):
                    if Ip.checkIpScope(scopeOfWave["scope"], domainIp):
                        return True
            else:  # If scope is domain
                # check if we include subdomains
                if settings.db_settings.get("include_all_domains", False):
                    return True
                else:
                    splitted_domain = domain.split(".")
                    # Assuring to check only if there is a domain before the tld (.com, .fr ... )
                    topDomainExists = len(splitted_domain) > 2
                    if topDomainExists:
                        if settings.db_settings.get(
                                "include_domains_with_topdomain_in_scope",
                                False):
                            if splitted_domain[1:] == scopeOfWave[
                                    "scope"].split("."):
                                return True
                    if settings.db_settings.get(
                            "include_domains_with_ip_in_scope", False):
                        inRangeDomainIp = Utils.performLookUp(
                            scopeOfWave["scope"])
                        if str(inRangeDomainIp) == str(domainIp):
                            return True
        return False
Example #5
0
 def isDomain(self):
     """Returns True if this scope is not a valid NetworkIP
     Returns:
         bool
     """
     return not Utils.isNetworkIp(self.scope)