| 1 | # Copyright (C) 2005 Insecure.Com LLC. |
|---|
| 2 | # |
|---|
| 3 | # Author: Adriano Monteiro Marques <py.adriano@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | |
|---|
| 19 | from types import StringTypes |
|---|
| 20 | from xml.dom import minidom |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class NmapOptions: |
|---|
| 24 | '''Manipulate an xml file with the nmap options, retrieving it's informa- |
|---|
| 25 | tions. This Class doesn't write information inside the xml file yet! |
|---|
| 26 | ''' |
|---|
| 27 | def __init__ (self, profile): |
|---|
| 28 | '''__init__ (profile) |
|---|
| 29 | |
|---|
| 30 | Constructor receives xml options file path as argument, and start |
|---|
| 31 | it's parsing. |
|---|
| 32 | ''' |
|---|
| 33 | # Starting file's parsing |
|---|
| 34 | self.option_xml = minidom.parse (open (profile)) |
|---|
| 35 | |
|---|
| 36 | # Root tag of the xml file. |
|---|
| 37 | self.root_tag = 'nmap_options' |
|---|
| 38 | |
|---|
| 39 | # Here we take every 'groups' and 'options' elements insite root tag |
|---|
| 40 | self.nmap_options = self.__get_nmap_options () |
|---|
| 41 | |
|---|
| 42 | self.options = self.__turn_into_dict(self.nmap_options) |
|---|
| 43 | |
|---|
| 44 | def get_command_option (self, option, args=[]): |
|---|
| 45 | '''get_command_option (option, args=[]) |
|---|
| 46 | |
|---|
| 47 | Get the command option for the given registered 'option'. |
|---|
| 48 | Additional argument 'args' will be merged with the command option |
|---|
| 49 | if it is needed. 'args' can be a list of argument (if command option |
|---|
| 50 | require more than one argument) or a single argument within a string. |
|---|
| 51 | |
|---|
| 52 | Important: Only give an argument if the command option will use it, |
|---|
| 53 | otherwise you'll get an error, trying to merge 'args' with the command |
|---|
| 54 | option |
|---|
| 55 | ''' |
|---|
| 56 | if type (args) in StringTypes and args: |
|---|
| 57 | args = [args] |
|---|
| 58 | |
|---|
| 59 | if args: |
|---|
| 60 | args = tuple (args) |
|---|
| 61 | return self.__ga_opt (option, 'option') % args |
|---|
| 62 | else: |
|---|
| 63 | return self.__ga_opt (option, 'option') |
|---|
| 64 | |
|---|
| 65 | def get_hint (self, option): |
|---|
| 66 | return self.__ga_opt (option, 'hint') |
|---|
| 67 | |
|---|
| 68 | def get_arguments (self, option): |
|---|
| 69 | return self.__get_cutted_list \ |
|---|
| 70 | (self.__ga_opt (option, 'arguments')) |
|---|
| 71 | |
|---|
| 72 | def get_need_root (self, option): |
|---|
| 73 | need = self.__ga_opt (option, 'need_root') |
|---|
| 74 | |
|---|
| 75 | if need == "0" or need == "False" or need == "false": |
|---|
| 76 | return False |
|---|
| 77 | else: |
|---|
| 78 | return True |
|---|
| 79 | |
|---|
| 80 | def get_option (self, option): |
|---|
| 81 | return {'name':option, |
|---|
| 82 | 'option':self.get_command_option(option), |
|---|
| 83 | 'hint':self.get_hint(option), |
|---|
| 84 | 'arguments':self.get_arguments(option), |
|---|
| 85 | 'need_root':self.get_need_root(option)} |
|---|
| 86 | |
|---|
| 87 | def get_options_list (self): |
|---|
| 88 | return self.__get_list (self.options) |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | # Private Methods |
|---|
| 92 | def __ga_opt (self, element_name, attribute_name): |
|---|
| 93 | '''Get option elements attributes''' |
|---|
| 94 | try: |
|---|
| 95 | opt = self.options \ |
|---|
| 96 | [element_name.encode('utf8')].getAttribute(attribute_name) |
|---|
| 97 | return opt |
|---|
| 98 | except KeyError: |
|---|
| 99 | raise OptionNotFound (element_name) |
|---|
| 100 | |
|---|
| 101 | def __get_cutted_list (self, list): |
|---|
| 102 | return [i.strip() for i in list.split(';') if i != ''] |
|---|
| 103 | |
|---|
| 104 | def __get_list (self, dict): |
|---|
| 105 | return [element for element in dict.keys()] |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | def __get_nmap_options (self): |
|---|
| 110 | elements = self.option_xml.getElementsByTagName\ |
|---|
| 111 | (self.root_tag)[0].childNodes |
|---|
| 112 | elements_list = [] |
|---|
| 113 | for element in elements: |
|---|
| 114 | try: |
|---|
| 115 | if element.tagName == 'option': |
|---|
| 116 | elements_list.append(element) |
|---|
| 117 | except: pass |
|---|
| 118 | |
|---|
| 119 | return elements_list |
|---|
| 120 | |
|---|
| 121 | def __turn_into_dict (self, list_of_elements): |
|---|
| 122 | elements_dict = {} |
|---|
| 123 | for element in list_of_elements: |
|---|
| 124 | elements_dict [element.getAttribute ('name')] = element |
|---|
| 125 | |
|---|
| 126 | return elements_dict |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | # Exceptions |
|---|
| 131 | class OptionNotFound (Exception): |
|---|
| 132 | def __init__ (self, option): |
|---|
| 133 | self.option = option |
|---|
| 134 | def __str__ (self): |
|---|
| 135 | return "No option named '"+self.option+"' found!" |
|---|
| 136 | |
|---|
| 137 | if __name__ == '__main__': |
|---|
| 138 | from pprint import pprint |
|---|
| 139 | |
|---|
| 140 | teste = NmapOptions ('options.xml') |
|---|
| 141 | print teste.get_command_option('Min parallel hosts', '5') |
|---|
| 142 | print teste.get_options_list() |
|---|