Halberd.ScanTask

Package Halberd :: Module ScanTask |
[hide private]
[frames] | no frames]

# Source Code for Module Halberd.ScanTask

      1  # -*- coding: iso-8859-1 -*-
      2
      3  """Scanning tasks.
      4
      5  @var default_scantime: Time to spend probing the target expressed in seconds.
      6  @type default_scantime: C{int}
      7
      8  @var default_parallelism: Number of parallel threads to launch for the scan.
      9  @type default_parallelism: C{int}
     10
     11  @var default_conf_dir: Path to the directory where the configuration file is
     12  located.
     13  @type default_conf_dir: C{str}
     14
     15  @var default_conf_file: Name of the default configuration file for halberd.
     16  @type default_conf_file: C{str}
     17
     18  @var default_ratio_threshold: Minimum clues-to-realservers ratio to trigger a
     19  clue reanalysis.
     20  @type default_ratio_threshold: C{float}
     21
     22  @var default_out: Default place where to write reports (None means stdout).
     23  @type default_out: C{str}
     24  """
     25
     26  # Copyright (C) 2004, 2005, 2006, 2010  Juan M. Bello Rivas <jmbr@superadditive.com>
     27  #
     28  # This program is free software; you can redistribute it and/or modify
     29  # it under the terms of the GNU General Public License as published by
     30  # the Free Software Foundation; either version 2 of the License, or
     31  # (at your option) any later version.
     32  #
     33  # This program is distributed in the hope that it will be useful,
     34  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     35  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     36  # GNU General Public License for more details.
     37  #
     38  # You should have received a copy of the GNU General Public License
     39  # along with this program; if not, write to the Free Software
     40  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     41
     42
     43  import os
     44
     45  import Halberd.conflib
     46
     47
     48  default_scantime = 15
     49
     50  default_parallelism = 4
     51
     52  default_conf_dir = os.path.join(os.path.expanduser('~'), '.halberd')
     53  default_conf_file = os.path.join(default_conf_dir,
     54                                   'halberd' + os.extsep + 'cfg')
     55
     56  default_ratio_threshold = 0.6
     57
     58  default_out = None
     59
     60
     61 -class ConfError(Exception):
     62      """Error with configuration file(s)
     63      """
     64 -    def __init__(self, msg):
     65          self.msg = msg
     66
     67 -    def __str__(self):
     68          return str(self.msg)
     69
     70
     71 -class ScanTask:
     72      """Describes the way a scan should be performed.
     73
     74      @ivar verbose: Display status information during the scan.
     75      @type verbose: C{bool}
     76
     77      @ivar debug: Display debug information.
     78      @type debug: C{bool}
     79
     80      @ivar urlfile: Root folder to use for storing results of MultiScans.
     81      @type urlfile: C{str}
     82
     83      @ivar url: URL to scan.
     84      @type url: C{str}
     85
     86      @ivar addr: Address of the target web server.
     87      @type addr: C{str}
     88
     89      @ivar proxy_serv_addr: Address + port where to listen when operating as a
     90      proxy.
     91      @type proxy_serv_addr: C{tuple}
     92
     93      @ivar out: File where to write reports. If it's not set, stdout will be
     94      used.
     95      @type out: C{str}
     96
     97      @ivar save: File or directory name where the results will be written.
     98      @type save: C{str}
     99
    100      @ivar keyfile: Key file for SSL connections.
    101      @type keyfile: C{str}
    102
    103      @ivar certfile: Certificate to be used for SSL connections.
    104      @type certfile: C{str}
    105
    106      @ivar clues: Sequence of clues obtained from the target.
    107      @type clues: C{list}
    108
    109      @ivar analyzed: Sequence of clues after the analysis phase.
    110      @type analyzed: C{list}
    111      """
    112 -    def __init__(self):
    113          self.scantime = default_scantime
    114          self.parallelism = default_parallelism
    115          self.conf_file = default_conf_file
    116          self.verbose = False
    117          self.debug = False
    118
    119          self.ratio_threshold = default_ratio_threshold
    120
    121          self.urlfile = ''
    122          self.url = ''
    123          self.addr = ''
    124
    125          self.proxy_serv_addr = ()
    126
    127          self.save = ''
    128
    129          self.out = default_out
    130
    131          self.keyfile = None
    132          self.certfile = None
    133
    134          self.clues = []
    135          self.analyzed = []
    136
    137
    138 -    def readConf(self):
    139          """Read configuration file.
    140
    141          This method tries to read the specified configuration file. If we try
    142          to read it at the default path and it's not there we create a
    143          bare-bones file and use that one.
    144
    145          @raise ConfError: If there's some problem creating or reading the
    146          configuration file.
    147          """
    148          # xxx - Move this into Halberd.conflib as a higher level function.
    149
    150          reader = Halberd.conflib.ConfReader()
    151
    152          try:
    153              reader.open(self.conf_file)
    154          except IOError:
    155              if self.conf_file == default_conf_file:
    156                  try:
    157                      os.mkdir(default_conf_dir)
    158                      reader.writeDefault(default_conf_file)
    159                      reader.open(default_conf_file)
    160                  except (OSError, IOError):
    161                      raise ConfError, 'unable to create a default conf. file'
    162              else:
    163                  raise ConfError, 'unable to open configuration file %s\n'
    164          except conflib.InvalidConfFile:
    165              raise ConfError, 'invalid configuration file %s\n' % self.conf_file
    166
    167          confvals = reader.parse()
    168          self.proxy_serv_addr = confvals[0]
    169          self.keyfile, self.certfile = confvals[1:]
    170
    171          reader.close()
    172
    173
    174  # vim: ts=4 sw=4 et
    175