root/trunk/umit/core/UmitConf.py @ 5535

Revision 5535, 20.3 kB (checked in by nopper, 7 months ago)

Fixes for #374

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2005-2006 Insecure.Com LLC.
5# Copyright (C) 2007-2008 Adriano Monteiro Marques
6#
7# Author: Adriano Monteiro Marques <adriano@umitproject.org>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23import re
24import os
25
26from types import StringTypes
27from ConfigParser import NoSectionError, NoOptionError, DuplicateSectionError
28
29from umit.core.Paths import Path
30from umit.core.ScanProfileConf import scan_profile_file
31from umit.core.UmitLogging import log
32from umit.core.UmitConfigParser import UmitConfigParser
33from umit.core.I18N import _
34
35class UmitConf(object):
36    def __init__(self):
37        self.parser = Path.config_parser
38
39    def save_changes(self):
40        self.parser.save_changes()
41
42    def get_colored_diff(self):
43        try:
44            cd = self.parser.get('diff', 'colored_diff')
45            if cd == "False" or \
46                cd == "false" or \
47                cd == "0" or \
48                cd == "" or \
49                cd == False:
50                return False
51            return True
52        except:
53            return True
54
55    def set_colored_diff(self, enable):
56        if not self.parser.has_section('diff'):
57            self.parser.add_section('diff')
58
59        self.parser.set('diff', 'colored_diff', str(enable))
60
61    def get_diff_mode(self):
62        try: return self.parser.get('diff', 'diff_mode')
63        except: return "compare"
64
65    def set_diff_mode(self, diff_mode):
66        if not self.parser.has_section('diff'):
67            self.parser.add_section('diff')
68       
69        self.parser.set('diff', 'diff_mode', diff_mode)
70
71    colored_diff = property(get_colored_diff, set_colored_diff)
72    diff_mode = property(get_diff_mode, set_diff_mode)
73
74
75class SearchConfig(UmitConfigParser, object):
76    def __init__(self):
77        self.parser = Path.config_parser
78
79        self.section_name = "search"
80        if not self.parser.has_section(self.section_name):
81            self.create_section()
82
83    def save_changes(self):
84        self.parser.save_changes()
85
86    def create_section(self):
87        self.parser.add_section(self.section_name)
88        self.directory = ""
89        self.file_extension = "usr"
90        self.save_time = "60;Days"
91        self.store_results = True
92        self.search_db = True
93
94    def _get_it(self, p_name, default):
95        return self.parser.get(self.section_name, p_name, default)
96
97    def _set_it(self, p_name, value):
98        self.parser.set(self.section_name, p_name, value)
99       
100    def boolean_sanity(self, attr):
101        if attr == True or \
102           attr == "True" or \
103           attr == "true" or \
104           attr == "1":
105
106            return 1
107
108        return 0
109
110    def get_directory(self):
111        return self._get_it("directory", "")
112
113    def set_directory(self, directory):
114        self._set_it("directory", directory)
115
116    def get_file_extension(self):
117        return self._get_it("file_extension", "usr").split(";")
118
119    def set_file_extension(self, file_extension):
120        if type(file_extension) == type([]):
121            self._set_it("file_extension", ";".join(file_extension))
122        elif type(file_extension) in StringTypes:
123            self._set_it("file_extension", file_extension)
124
125    def get_save_time(self):
126        return self._get_it("save_time", "60;Days").split(";")
127
128    def set_save_time(self, save_time):
129        if type(save_time) == type([]):
130            self._set_it("save_time", ";".join(save_time))
131        elif type(save_time) in StringTypes:
132            self._set_it("save_time", save_time)
133
134    def get_store_results(self):
135        return self.boolean_sanity(self._get_it("store_results", True))
136
137    def set_store_results(self, store_results):
138        self._set_it("store_results", self.boolean_sanity(store_results))
139
140    def get_search_db(self):
141        return self.boolean_sanity(self._get_it("search_db", True))
142
143    def set_search_db(self, search_db):
144        self._set_it("search_db", self.boolean_sanity(search_db))
145
146    def get_converted_save_time(self):
147        try:
148            return int(self.save_time[0]) * self.time_list[self.save_time[1]]
149        except:
150            # If something goes wrong, return a save time of 60 days
151            return 60 * 60 * 24 * 60
152
153    def get_time_list(self):
154        # Time as key, seconds a value
155        return {_("Hours"): 60 * 60,
156                _("Days"): 60 * 60 * 24,
157                _("Weeks"): 60 * 60 * 24 * 7,
158                _("Months"): 60 * 60 * 24 * 7 * 30,
159                _("Years"): 60 * 60 * 24 * 7 * 30 * 12,
160                _("Minutes"): 60,
161                _("Seconds"): 1}
162   
163    directory = property(get_directory, set_directory)
164    file_extension = property(get_file_extension, set_file_extension)
165    save_time = property(get_save_time, set_save_time)
166    store_results = property(get_store_results, set_store_results)
167    search_db = property(get_search_db, set_search_db)
168    converted_save_time = property(get_converted_save_time)
169    time_list = property(get_time_list)
170
171
172class Profile(UmitConfigParser, object):
173    def __init__(self, user_profile=None, *args):
174        UmitConfigParser.__init__(self, *args)
175
176        if not user_profile:
177            user_profile = scan_profile_file
178
179        fconf = open(user_profile, 'r')
180        self.readfp(fconf, user_profile)
181
182        fconf.close()
183        del(fconf)
184
185        self.attributes = {}
186
187    def _get_it(self, profile, attribute):
188        if self._verify_profile(profile):
189            return self.get(profile, attribute)
190        return ""
191
192    def _set_it(self, profile, attribute, value=''):
193        if self._verify_profile(profile):
194            return self.set(profile, attribute, value)
195
196    def add_profile(self, profile_name, **attributes):
197        log.debug(">>> Add Profile '%s': %s" % (profile_name, attributes))
198        try:
199            self.add_section(profile_name)
200        except DuplicateSectionError:
201            return None
202        except ValueError:
203            return None
204
205        for attr in attributes:
206            if attr != "options":
207                self._set_it(profile_name, attr, attributes[attr])
208
209        options = attributes["options"]
210        if isinstance(options, basestring):
211            self._set_it(profile_name, "options", options)
212            # Assuming there are no values for these options
213            options = {}
214        elif isinstance(options, dict):
215            self._set_it(profile_name, "options", ",".join(options))
216
217        for opt in options:
218            if options[opt]:
219                self._set_it(profile_name, opt, options[opt])
220        self.save_changes()
221
222    def remove_profile(self, profile_name):
223        try: self.remove_section(profile_name)
224        except: pass
225        self.save_changes()
226
227    def _verify_profile(self, profile_name):
228        if profile_name not in self.sections():
229            return False
230        return True
231
232class CommandProfile (Profile, object):
233    def __init__(self, user_profile=''):
234        if not user_profile:
235            user_profile = scan_profile_file
236       
237        Profile.__init__(self, user_profile)
238       
239    def get_command(self, profile):
240        return self._get_it(profile, 'command')
241
242    def get_hint(self, profile):
243        return self._get_it(profile, 'hint')
244
245    def get_description(self, profile):
246        return self._get_it(profile, 'description')
247   
248    def get_annotation(self, profile):
249        return self._get_it(profile, 'annotation')
250
251    def get_options(self, profile):
252        dic = {}
253        options_result = self._get_it(profile, 'options')
254        if options_result.strip()=='':
255            return dic
256       
257        for opt in options_result.split(','):
258            try:
259                dic[unicode(opt.strip())] = self._get_it(profile, opt)
260            except NoOptionError:
261                dic[unicode(opt.strip())] = None
262        return dic
263
264    def set_command(self, profile, command=''):
265        self._set_it(profile, 'command', command)
266
267    def set_hint(self, profile, hint=''):
268        self._set_it(profile, 'hint', hint)
269   
270    def set_description(self, profile, description=''):
271        self._set_it(profile, 'description', description)
272   
273    def set_annotation (self, profile, annotation=''):
274        self._set_it(profile, 'annotation', annotation)
275   
276    def set_options(self, profile, options={}):
277        for opt in options:
278            if options[opt]:
279                self._set_it(profile, opt, options[opt])
280        self._set_it(profile, 'options', ",".join(options))
281
282    def get_profile(self, profile_name):
283        return {'profile':profile_name, \
284                'command':self.get_command(profile_name), \
285                'hint':self.get_hint(profile_name), \
286                'description':self.get_description(profile_name), \
287                'annotation':self.get_annotation(profile_name),\
288                'options':self.get_options(profile_name)}
289
290
291class NmapOutputHighlight(object):
292    setts = ["bold", "italic", "underline", "text", "highlight", "regex"]
293   
294    def __init__(self):
295        self.parser = Path.config_parser
296
297    def save_changes(self):
298        self.parser.save_changes()
299
300    def __get_it(self, p_name):
301        property_name = "%s_highlight" % p_name
302
303        try:
304            return self.sanity_settings([self.parser.get(property_name,
305                                                         prop,
306                                                         True) \
307                                         for prop in self.setts])
308        except:
309            settings = []
310            prop_settings = self.default_highlights[p_name]
311            settings.append(prop_settings["bold"])
312            settings.append(prop_settings["italic"])
313            settings.append(prop_settings["underline"])
314            settings.append(prop_settings["text"])
315            settings.append(prop_settings["highlight"])
316            settings.append(prop_settings["regex"])
317
318            self.__set_it(p_name, settings)
319
320            return self.sanity_settings(settings)
321
322    def __set_it(self, property_name, settings):
323        property_name = "%s_highlight" % property_name
324        settings = self.sanity_settings(list(settings))
325
326        for pos in xrange(len(settings)):
327            self.parser.set(property_name, self.setts[pos], settings[pos])
328
329    def sanity_settings(self, settings):
330        """This method tries to convert insane settings to sanity ones ;-)
331        If user send a True, "True" or "true" value, for example, it tries to
332        convert then to the integer 1.
333        Same to False, "False", etc.
334
335        Sequence: [bold, italic, underline, text, highlight, regex]
336        """
337        #log.debug(">>> Sanitize %s" % str(settings))
338       
339        settings[0] = self.boolean_sanity(settings[0])
340        settings[1] = self.boolean_sanity(settings[1])
341        settings[2] = self.boolean_sanity(settings[2])
342
343        tuple_regex = "[\(\[]\s?(\d+)\s?,\s?(\d+)\s?,\s?(\d+)\s?[\)\]]"
344        if type(settings[3]) == type(""):
345            settings[3] = [int(t) \
346                           for t in re.findall(tuple_regex, settings[3])[0]]
347
348        if type(settings[4]) == type(""):
349            settings[4]= [int(h) \
350                          for h in re.findall(tuple_regex, settings[4])[0]]
351
352        return settings
353
354    def boolean_sanity(self, attr):
355        if attr == True or attr == "True" or attr == "true" or attr == "1":
356            return 1
357        return 0
358
359    def get_date(self):
360        return self.__get_it("date")
361
362    def set_date(self, settings):
363        self.__set_it("date", settings)
364
365    def get_hostname(self):
366        return self.__get_it("hostname")
367
368    def set_hostname(self, settings):
369        self.__set_it("hostname", settings)
370
371    def get_ip(self):
372        return self.__get_it("ip")
373
374    def set_ip(self, settings):
375        self.__set_it("ip", settings)
376
377    def get_port_list(self):
378        return self.__get_it("port_list")
379
380    def set_port_list(self, settings):
381        self.__set_it("port_list", settings)
382
383    def get_open_port(self):
384        return self.__get_it("open_port")
385
386    def set_open_port(self, settings):
387        self.__set_it("open_port", settings)
388
389    def get_closed_port(self):
390        return self.__get_it("closed_port")
391
392    def set_closed_port(self, settings):
393        self.__set_it("closed_port", settings)
394
395    def get_filtered_port(self):
396        return self.__get_it("filtered_port")
397
398    def set_filtered_port(self, settings):
399        self.__set_it("filtered_port", settings)
400
401    def get_details(self):
402        return self.__get_it("details")
403
404    def set_details(self, settings):
405        self.__set_it("details", settings)
406       
407    def get_enable(self):
408        enable = True
409        try:
410            enable = self.parser.get("output_highlight", "enable_highlight")
411        except NoSectionError:
412            self.parser.set("output_highlight", "enable_highlight", str(True))
413       
414        if enable == "False" or enable == "0" or enable == "":
415            return False
416        return True
417
418    def set_enable(self, enable):
419        if enable == False or enable == "0" or enable is None or enable == "":
420            self.parser.set("output_highlight", "enable_highlight", str(False))
421        else:
422            self.parser.set("output_highlight", "enable_highlight", str(True))
423
424    date = property(get_date, set_date)
425    hostname = property(get_hostname, set_hostname)
426    ip = property(get_ip, set_ip)
427    port_list = property(get_port_list, set_port_list)
428    open_port = property(get_open_port, set_open_port)
429    closed_port = property(get_closed_port, set_closed_port)
430    filtered_port = property(get_filtered_port, set_filtered_port)
431    details = property(get_details, set_details)
432    enable = property(get_enable, set_enable)
433
434    # These settings are made when there is nothing set yet. They set
435    # the "factory" default to highlight colors
436    default_highlights = {
437            "date": {
438                "bold": str(True),
439                "italic": str(False),
440                "underline": str(False),
441                "text": [0, 0, 0],
442                "highlight": [65535, 65535, 65535],
443                "regex": "\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}\s.{1,4}"
444                },
445            "hostname": {
446                "bold": str(True),
447                "italic": str(True),
448                "underline": str(True),
449                "text": [0, 111, 65535],
450                "highlight": [65535, 65535, 65535],
451                "regex":"(\w{2,}://)?(([a-zA-Z0-9]|-)+\.)+([a-zA-Z]{2,}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)([\w\d#/])*"
452                },
453            "ip": {
454                "bold": str(True),
455                "italic": str(False),
456                "underline": str(False),
457                "text": [0, 0, 0],
458                "highlight": [65535, 65535, 65535],
459                "regex": "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
460                },
461            "port_list": {
462                "bold": str(True),
463                "italic": str(False),
464                "underline": str(False),
465                "text": [0, 1272, 28362],
466                "highlight": [65535, 65535, 65535],
467                "regex": "PORT\s+STATE\s+SERVICE(\s+VERSION)?[^\n]*"
468                },
469            "open_port": {
470                "bold": str(True),
471                "italic": str(False),
472                "underline": str(False),
473                "text": [0, 41036, 2396],
474                "highlight": [65535, 65535, 65535],
475                "regex": "\d{1,5}/.{1,5}\s+open\s+.*"
476                },
477            "closed_port": {
478                "bold": str(False),
479                "italic": str(False),
480                "underline": str(False),
481                "text": [65535, 0, 0],
482                "highlight": [65535, 65535, 65535],
483                "regex":"\d{1,5}/.{1,5}\s+closed\s+.*"
484                },
485            "filtered_port": {
486                "bold": str(False),
487                "italic": str(False),
488                "underline": str(False),
489                "text": [38502, 39119, 0],
490                "highlight": [65535, 65535, 65535],
491                "regex": "\d{1,5}/.{1,5}\s+filtered\s+.*"
492                },
493            "details": {
494                    "bold": str(True),
495                    "italic": str(False),
496                    "underline": str(True),
497                    "text": [0, 0, 0],
498                    "highlight": [65535, 65535, 65535],
499                    "regex": "^(\w{2,}[\s]{,3}){,4}:"
500                    }
501            }
502
503class DiffColors(object):
504    def __init__(self):
505        self.parser = Path.config_parser
506        self.section_name = "diff_colors"
507
508    def save_changes(self):
509        self.parser.save_changes()
510
511    def __get_it(self, p_name):
512        return self.sanity_settings(self.parser.get(self.section_name, p_name))
513
514    def __set_it(self, property_name, settings):
515        settings = self.sanity_settings(settings)
516        self.parser.set(self.section_name, property_name, settings)
517
518    def sanity_settings(self, settings):
519        log.debug(">>> Sanitize %s" % str(settings))
520       
521        tuple_regex = "[\(\[]\s?(\d+)\s?,\s?(\d+)\s?,\s?(\d+)\s?[\)\]]"
522        if type(settings) == type(""):
523            settings = [int(t) for t in re.findall(tuple_regex, settings)[0]]
524
525        return settings
526
527    def get_unchanged(self):
528        return self.__get_it("unchanged")
529
530    def set_unchanged(self, settings):
531        self.__set_it("unchanged", settings)
532
533    def get_added(self):
534        return self.__get_it("added")
535
536    def set_added(self, settings):
537        self.__set_it("added", settings)
538
539    def get_modified(self):
540        return self.__get_it("modified")
541
542    def set_modified(self, settings):
543        self.__set_it("modified", settings)
544
545    def get_not_present(self):
546        return self.__get_it("not_present")
547
548    def set_not_present(self, settings):
549        self.__set_it("not_present", settings)
550
551    unchanged = property(get_unchanged, set_unchanged)
552    added = property(get_added, set_added)
553    modified = property(get_modified, set_modified)
554    not_present = property(get_not_present, set_not_present)
555
556class Plugins(object):
557    def __init__(self):
558        self.parser = Path.config_parser
559        self.section_name = "plugins"
560        self.separator = os.pathsep
561
562        if not self.parser.has_section(self.section_name):
563            self.create_section()
564
565    def save_changes(self):
566        self.parser.save_changes()
567
568    def create_section(self):
569        from os.path import join
570        self.paths = [join(Path.config_dir, "plugins")]
571        self.plugins = ""
572
573    def __get_it(self, p_name):
574        value = None
575
576        try:
577            try:
578                value = self.parser.get(self.section_name, p_name)
579            except:
580                pass
581        finally:
582            return self.sanity_settings(value)
583
584    def __set_it(self, property_name, settings):
585        settings = self.sanity_settings(settings)
586        self.parser.set(self.section_name, property_name, settings)
587
588    def sanity_settings(self, settings):
589        # FIXME: more sensed :D
590        if not settings:
591            return ""
592        return settings
593
594    def get_paths(self):
595        return filter(None, self.__get_it("paths").split(self.separator))
596
597    def set_paths(self, settings):
598        self.__set_it("paths", self.separator.join(settings))
599
600    def get_plugins(self):
601        return filter(None, self.__get_it("plugins").split(self.separator))
602
603    def set_plugins(self, settings):
604        self.__set_it("plugins", self.separator.join(settings))
605
606    paths = property(get_paths, set_paths)
607    plugins = property(get_plugins, set_plugins)
608
609# Exceptions
610class ProfileNotFound:
611    def __init__ (self, profile):
612        self.profile = profile
613    def __str__ (self):
614        return "No profile named '"+self.profile+"' found!"
615
616class ProfileCouldNotBeSaved:
617    def __init__ (self, profile):
618        self.profile = profile
619    def __str__ (self):
620        return "Profile named '"+self.profile+"' could not be saved!"
621
622
623if __name__ == "__main__":
624    pass
Note: See TracBrowser for help on using the browser.