Beispiel #1
0
 def app_create_port_list(self, name, port_range, comment=None):
     """
     Create a port list from range(s) of ports
     :param name: Name of the port range
     :param port_range: Port range (comma separated values, e.g. T:100-200,T:300-400,U:800-900
     :param comment: (Optional) Comment to add
     :return: uuid of the created list
     """
     try:
         with Client(self.h,
                     username=self.u,
                     password=self.device.get_encrypted_field('password'),
                     port=self.p) as cli:
             r = cli.create_port_list(name,
                                      port_range=port_range,
                                      comment=comment)
             return r.data['@id']
     except exceptions.HTTPError:
         return False, "BadPorts"
     except exceptions.ElementExists:
         return False, "AlreadyExists"
     except exceptions.AuthenticationError:
         return False, "AuthError"
     except IOError:
         return False, "ConnectError"
Beispiel #2
0
 def app_create_target(self, name, hosts, port_list=None, comment=None):
     """
     Creates a new target
     :param name: Name of the new target
     :param hosts: Comma separated list of hosts
     :param port_list: (Optional) uuid of the port list to use
     :param comment: (Optional) Comment to add
     :return: uuid of the created target
     """
     try:
         with Client(self.h,
                     username=self.u,
                     password=self.device.get_encrypted_field('password'),
                     port=self.p) as cli:
             r = cli.create_target(name,
                                   hosts,
                                   port_list=port_list,
                                   comment=comment)
             return r.data['@id']
     except exceptions.ElementNotFound:
         return False, "InvalidUUID"
     except exceptions.ElementExists:
         return False, "AlreadyExists"
     except exceptions.AuthenticationError:
         return False, "AuthError"
     except IOError:
         return False, "ConnectError"
Beispiel #3
0
 def app_create_task(self, name, target_uuid, config_uuid='daba56c8-73ec-11df-a475-002264764cea', scanner_uuid=None,
                     comment=None, schedule_uuid=None, alert_uuid=None):
     """
     Creates a new task
     :param name: Name of the task
     :param config_uuid: uuid of the config to use
     :param target_uuid: uuid of the target to scan
     :param scanner_uuid: (Optional) uuid of the scanner to use
     :param comment: (Optional) Comment to add
     :param schedule_uuid: (Optional) uuid of the schedule to use
     :return: uuid of the created task
     """
     try:
         with Client(self.h, username=self.u, password=self.device.get_encrypted_field('password'),
                     port=self.p) as cli:
             r = cli.create_task(name, config_uuid, target_uuid, scanner_uuid=scanner_uuid, comment=comment,
                                 schedule_uuid=schedule_uuid, alert_uuid=alert_uuid)
             return r.data['@id']
     except exceptions.ElementNotFound:
         return False, "InvalidUUID"
     except exceptions.ElementExists:
         return False, "AlreadyExists"
     except exceptions.AuthenticationError:
         return False, "AuthError"
     except IOError:
         return False, "ConnectError"
Beispiel #4
0
    def app_create_schedule(self, name, comment=None, first_time=None, local_server_time=False, duration=None,
                            duration_unit=None, period=None,
                            period_unit=None):
        """
        Creates a new schedule
        :param name: Name of the schedule
        :param comment: (Optional) Comment to add
        :param first_time: (Optional) First time to run the task, in the format "MM/DD/YYYY HH:MM _M" (12h)
        :param duration: (Optional) How long to run task before it is aborted
        :param duration_unit: (Optional) Units for duration
        :param period: (Optional) How often to run the task
        :param period_unit: (Optional) Units for period
        :param local_server_time: (Optional) UTC offset for local time in the format "+1:00" or "-1:00"
        :return: uuid of created schedule
        """

        time_json = None
        local_zone = None
        if first_time is not None:
            try:
                dt = datetime.strptime(first_time, '%m/%d/%Y %I:%M %p')

                if local_server_time is not False:
                    local_zone = get_localzone().zone

                time_json = {
                    "minute": dt.minute,
                    "hour": dt.hour,
                    "day_of_month": dt.day,
                    "month": dt.month,
                    "year": dt.year
                }
            except ValueError:
                return False, "BadTime"

        if ((duration is not None) ^ (duration_unit is not None)) or ((period is not None) ^ (period_unit is not None)):
            return False, "BadTime"

        if not self.valid_num(duration) or not self.valid_num(period):
            return False, "BadTime"

        try:
            with Client(self.h, username=self.u, password=self.device.get_encrypted_field('password'),
                        port=self.p) as cli:
                r = cli.create_schedule(name, comment=comment, first_time=time_json, duration=duration,
                                        duration_unit=duration_unit, period=period, period_unit=period_unit,
                                        timezone=local_zone)
                return r.data['@id']
        except exceptions.ElementExists:
            return False, "AlreadyExists"
        except exceptions.AuthenticationError:
            return False, "AuthError"
        except IOError:
            return False, "ConnectError"
Beispiel #5
0
 def app_start_task(self, uuid):
     """
     Starts the specified task
     :param uuid: uuid of task to execute
     :return: report uuid for this run
     """
     try:
         with Client(self.h, username=self.u, password=self.device.get_encrypted_field('password'),
                     port=self.p) as cli:
             r = cli.start_task(uuid)
             return r.data['report_id']
     except exceptions.ElementNotFound:
         return False, "InvalidUUID"
     except exceptions.AuthenticationError:
         return False, "AuthError"
     except IOError:
         return False, "ConnectError"
Beispiel #6
0
 def app_list_reports(self, name=None):
     """
     Lists all defined reports
     :param name: (Optional) Name to search for
     :return: List of matching reports
     """
     try:
         with Client(self.h, username=self.u, password=self.device.get_encrypted_field('password'),
                     port=self.p) as cli:
             if name is not None:
                 r = cli.list_reports(name=name)
             else:
                 r = cli.list_reports()
             return r.data
     except exceptions.AuthenticationError:
         return False, "AuthError"
     except IOError:
         return False, "ConnectError"
Beispiel #7
0
 def app_download_report_as_xml(self, uuid, filename):
     """
     Gets report by uuid, then writes to filename
     :param uuid: uuid of report to get
     :param filename: filename to write to
     :return:
     """
     try:
         with Client(self.h, username=self.u, password=self.device.get_encrypted_field('password'),
                     port=self.p) as cli:
             r = cli.download_report(uuid, as_element_tree=True)
             r.getroottree().write(filename)
             return True
     except exceptions.ElementNotFound:
         return False, "InvalidUUID"
     except exceptions.AuthenticationError:
         return False, "AuthError"
     except IOError:
         return False, "ConnectError"