/usr/lib/python2.7/dist-packages/Halberd/shell.py is in python-halberd 0.2.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | # -*- coding: iso-8859-1 -*-
"""Provides scanning patterns to be used as building blocks for more complex
scans.
Strategies are different ways in which target scans may be done. We provide
basic functionality so more complex stuff can be built upon this.
"""
# Copyright (C) 2004, 2005, 2006, 2010 Juan M. Bello Rivas <jmbr@superadditive.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import Halberd.crew
import Halberd.logger
import Halberd.reportlib
import Halberd.clues.file
import Halberd.clues.analysis as analysis
class ScanError(Exception):
"""Generic error during scanning.
"""
def __init__(self, msg):
self.msg = msg
def __str__(self):
return str(self.msg)
class BaseStrategy:
"""Defines the strategy used to scan.
A strategy is a certain way to use the program. Theses can be layered to
build a bigger strategy doing more complex things, etc.
"""
def __init__(self, scantask):
self.task = scantask
self.logger = Halberd.logger.getLogger()
def execute(self):
"""Executes the strategy.
"""
pass
# ---------------------------
# Higher-level helper methods
# ---------------------------
def _scan(self):
"""Allocates a work crew of scanners and launches them on the target.
"""
assert self.task.url and self.task.addr
self.task.clues = []
self.task.analyzed = []
crew = Halberd.crew.WorkCrew(self.task)
self.task.clues = crew.scan()
def _analyze(self):
"""Performs clue analysis.
"""
if len(self.task.clues) == 0:
return
self.task.analyzed = analysis.analyze(self.task.clues)
self.task.analyzed = analysis.reanalyze(self.task.clues,
self.task.analyzed, self.task.ratio_threshold)
class UniScanStrategy(BaseStrategy):
"""Scan a single URL.
"""
def __init__(self, scantask):
BaseStrategy.__init__(self, scantask)
if not self.task.url:
raise ScanError, 'Didn\'t provide an URL to scan'
if self.task.addr:
# The user passed a specific address as a parameter.
self.addrs = [self.task.addr]
else:
host = Halberd.util.hostname(self.task.url)
self.logger.info('looking up host %s... ', host)
try:
self.addrs = Halberd.util.addresses(host)
except KeyboardInterrupt:
raise ScanError, 'interrupted by the user'
if not self.addrs:
raise ScanError, 'unable to resolve %s' % host
self.addrs.sort()
self.logger.info('host lookup done.')
if len(self.addrs) > 1:
for addr in self.addrs:
#self.logger.debug('%s resolves to %s', host, addr)
self.logger.info('%s resolves to %s', host, addr)
def execute(self):
"""Scans, analyzes and presents results coming a single target.
"""
if self.task.save:
cluedir = Halberd.clues.file.ClueDir(self.task.save)
for self.task.addr in self.addrs:
self._scan()
self._analyze()
Halberd.reportlib.report(self.task)
if self.task.save:
cluedir.save(self.task.url,
self.task.addr,
self.task.clues)
class MultiScanStrategy(BaseStrategy):
"""Scan multiple URLs.
"""
def __init__(self, scantask):
BaseStrategy.__init__(self, scantask)
if not self.task.urlfile:
raise ScanError, 'An urlfile parameter must be provided'
self.urlfp = open(self.task.urlfile, 'r')
def _targets(self, urlfp):
"""Obtain target addresses from URLs.
@param urlfp: File where the list of URLs is stored.
@type urlfp: C{file}
@return: Generator providing the desired addresses.
"""
for url in urlfp:
if url == '\n':
continue
# Strip end of line character and whitespaces.
url = url[:-1].strip()
host = Halberd.util.hostname(url)
if not host:
self.logger.warn('unable to extract hostname from %s', host)
continue
self.logger.info('looking up host %s... ', host)
try:
addrs = Halberd.util.addresses(host)
except KeyboardInterrupt:
raise ScanError, 'interrupted by the user'
self.logger.info('host lookup done.')
for addr in addrs:
yield (url, addr)
def execute(self):
"""Launch a multiple URL scan.
"""
cluedir = Halberd.clues.file.ClueDir(self.task.save)
for url, addr in self._targets(self.urlfp):
self.task.url = url
self.task.addr = addr
self.logger.info('scanning %s (%s)', url, addr)
self._scan()
cluedir.save(url, addr, self.task.clues)
self._analyze()
Halberd.reportlib.report(self.task)
class ClueReaderStrategy(BaseStrategy):
"""Clue reader strategy.
Works by reading and analyzing files of previously stored clues.
"""
def __init__(self, scantask):
BaseStrategy.__init__(self, scantask)
def execute(self):
"""Reads and interprets clues.
"""
self.task.clues = Halberd.clues.file.load(self.task.cluefile)
self._analyze()
self.task.url = self.task.cluefile
Halberd.reportlib.report(self.task)
# vim: ts=4 sw=4 et
|