Esempio n. 1
0
 def mapProcessToPortsInSpecialWay(self):
     invalidIpsMessage = ''
     errMsg = ''
     status = TCPDiscovery.OK
     try:
         socket_descriptors = get_socket_descriptors_by_pfiles(self.shUtils)
         for pid, socket_descriptor in socket_descriptors:
             local_ip, local_port, is_listen, protocol_type, _, _ = socket_descriptor
             local_ip = local_ip and sanitizeIps(local_ip)
             if local_ip and (not ip_addr.isValidIpAddress(local_ip)):
                 local_ip = netutils.resolveIP(self.shUtils, local_ip)
             if local_ip and (ip_addr.isValidIpAddress(local_ip)):
                 self._addTcpData(local_ip, local_port, pid, is_listen, protocol_type)
             else:
                 invalidIpsMessage = invalidIpsMessage + 'On parsing ports by pfiles invalid ip:<%s>\n' % local_ip
     except TimeoutException:
         errMsg = 'Failed to map processes to ports by pfiles - timeout, try to increase command timeout parameter'
         logger.debugException(errMsg)
         self.Framework.reportWarning(errMsg)
     except:
         errMsg = 'Failed to map processes to ports by pfiles:see communication log'
         logger.debugException(errMsg)
         self.Framework.reportError(errMsg)
         status = TCPDiscovery.ERROR
     if len(invalidIpsMessage) > 0:
         wrnMsg = 'There are invalid ips found while paring port by pfiles, check communication log'
         self.Framework.reportWarning(wrnMsg)
         logger.debug(wrnMsg + '\n' + invalidIpsMessage)
     return status
    def process(self, context):
        ConfigBasedPlugin.process(self, context)
        processFolder = self.getProcessFolder(context)
        discoveredVersion = ""
        try:
            discoveredVersion = context.application.getOsh().getAttributeValue("application_version_number")
        except:
            logger.debugException('')
        logger.info("Discovered version is: %s" % discoveredVersion)
        if discoveredVersion and not (discoveredVersion.startswith('9')
                                      or discoveredVersion.startswith('10')):
            raise applications.IgnoreApplicationException('UCMDB is not of a proper version')

        try:
            content = context.client.safecat(processFolder + '../../../conf/cmdb.conf')
        except:
            logger.reportWarning('Failed getting HP uCMDB configuration')
            return

        hostName = self.getPropertyByRegexp(r'dal\.datamodel\.host\.name=(.+)', content)
        dbType = self.getPropertyByRegexp(r'dal\.datamodel\.db\.type=(.+)', content)
        port = self.getPropertyByRegexp(r'dal\.datamodel\.port=(\d+)', content)
        sid = self.getPropertyByRegexp(r'dal\.datamodel\.sid=(\w+)', content)
        if ip_addr.isValidIpAddress(hostName):
            ipAddress = hostName
            ipAddress = ipAddress.encode('utf8').strip()
            hostName = netutils.getHostName(ipAddress)
        if (not sid) and hostName:
            sid = hostName.upper().split('.')[0]
        if hostName and dbType and port and sid:
            hostName = hostName.strip()
            resolver = netutils.IpResolver('', context.framework)
            dbHostIp = resolver.resolveHostIp(hostName)
            if dbHostIp:
                self.reportTopology(context, dbType, port, sid, dbHostIp)
            else:
                logger.warn('Failed resolving DB host "%s" ip address' % hostName)
        else:
            logger.warn('Failed parsing cmdb config file (datamodel part)')

        hostName = self.getPropertyByRegexp(r'dal\.history\.host\.name=(.+)', content)
        dbType = self.getPropertyByRegexp(r'dal\.history\.db\.type=(.+)', content)
        port = self.getPropertyByRegexp(r'dal\.history\.port=(\d+)', content)
        sid = self.getPropertyByRegexp(r'dal\.history\.sid=(\w+)', content)
        if ip_addr.isValidIpAddress(hostName):
            ipAddress = hostName
            ipAddress = ipAddress.encode('utf8').strip()
            hostName = netutils.getHostName(ipAddress)
        if (not sid) and hostName:
            sid = hostName.upper().split('.')[0]
        if hostName and dbType and port and sid:
            hostName = hostName.strip()
            resolver = netutils.IpResolver('', context.framework)
            dbHostIp = resolver.resolveHostIp(hostName)
            if dbHostIp:
                self.reportTopology(context, dbType, port, sid, dbHostIp)
            else:
                logger.warn('Failed resolving DB host "%s" ip address' % hostName)
        else:
            logger.warn('Failed parsing cmdb config file (history part)')
Esempio n. 3
0
 def mapProcessToPortsInSpecialWay(self):
     invalidIpsMessage = ''
     errMsg = ''
     status = TCPDiscovery.OK
     try:
         socket_descriptors = get_socket_descriptors_by_pfiles(self.shUtils)
         for pid, socket_descriptor in socket_descriptors:
             local_ip, local_port, is_listen, protocol_type, _, _ = socket_descriptor
             local_ip = local_ip and sanitizeIps(local_ip)
             if local_ip and (not ip_addr.isValidIpAddress(local_ip)):
                 local_ip = netutils.resolveIP(self.shUtils, local_ip)
             if local_ip and (ip_addr.isValidIpAddress(local_ip)):
                 self._addTcpData(local_ip, local_port, pid, is_listen,
                                  protocol_type)
             else:
                 invalidIpsMessage = invalidIpsMessage + 'On parsing ports by pfiles invalid ip:<%s>\n' % local_ip
     except TimeoutException:
         errMsg = 'Failed to map processes to ports by pfiles - timeout, try to increase command timeout parameter'
         logger.debugException(errMsg)
         self.Framework.reportWarning(errMsg)
     except:
         errMsg = 'Failed to map processes to ports by pfiles:see communication log'
         logger.debugException(errMsg)
         self.Framework.reportError(errMsg)
         status = TCPDiscovery.ERROR
     if len(invalidIpsMessage) > 0:
         wrnMsg = 'There are invalid ips found while paring port by pfiles, check communication log'
         self.Framework.reportWarning(wrnMsg)
         logger.debug(wrnMsg + '\n' + invalidIpsMessage)
     return status
Esempio n. 4
0
def getNatIPFromConfigurationFile():
    """
    Read IP or IP range from configuration file.
    @return: A list contains IPAddress objects and IPNetwork objects
    """
    NATIPConfigurationFileFolder = os.path.join(
        CollectorsParameters.BASE_PROBE_MGR_DIR,
        CollectorsParameters.getDiscoveryConfigFolder())
    NATIPConfigurationFile = os.path.join(NATIPConfigurationFileFolder,
                                          'NATIpAddress.xml')

    if not os.path.exists(NATIPConfigurationFile):
        logger.info("There is no NAT IP address defined.")
        return

    # Read tags from xml file
    builder = SAXBuilder()
    configDoc = builder.build(NATIPConfigurationFile)
    rootElement = configDoc.getRootElement()
    ipElements = rootElement.getChildren('Ip')
    ipRangeElements = rootElement.getChildren('IpRange')

    NAT_IPs = []

    # Read IPAddress, add valid one to NAT_IPs list
    if ipElements:
        for ipElement in ipElements:
            ip = ipElement.getText()
            if ip_addr.isValidIpAddress(ip):
                ipObj = ip_addr.IPAddress(ip)
                NAT_IPs.append(ipObj)

    # Read IP Ranges, create IPNetwork and add to NAT_IPs list
    if ipRangeElements:
        for ipRangeElement in ipRangeElements:
            ip_range_raw = ipRangeElement.getText()
            ips = ip_range_raw.split('-')
            ip_start = ips[0]
            ip_end = ips[1]

            if ip_addr.isValidIpAddress(ip_start) and ip_addr.isValidIpAddress(
                    ip_end):
                ip_start = ip_addr.IPAddress(ip_start)
                ip_end = ip_addr.IPAddress(ip_end)
                ips = ip_addr.summarize_address_range(ip_start, ip_end)
                logger.debug(ips)
                NAT_IPs.extend(ips)
            else:
                logger.warn(
                    "IP Range should start and end with valid IP address")

    return NAT_IPs
def getNatIPFromConfigurationFile():
    """
    Read IP or IP range from configuration file.
    @return: A list contains IPAddress objects and IPNetwork objects
    """
    NATIPConfigurationFileFolder = os.path.join(CollectorsParameters.BASE_PROBE_MGR_DIR,
                                         CollectorsParameters.getDiscoveryConfigFolder())
    NATIPConfigurationFile = os.path.join(NATIPConfigurationFileFolder, 'NATIpAddress.xml')

    if not os.path.exists(NATIPConfigurationFile):
        logger.info("There is no NAT IP address defined.")
        return

    # Read tags from xml file
    builder = SAXBuilder()
    configDoc = builder.build(NATIPConfigurationFile)
    rootElement = configDoc.getRootElement()
    ipElements = rootElement.getChildren('Ip')
    ipRangeElements = rootElement.getChildren('IpRange')

    NAT_IPs = []

    # Read IPAddress, add valid one to NAT_IPs list
    if ipElements:
        for ipElement in ipElements:
            ip = ipElement.getText()
            if ip_addr.isValidIpAddress(ip):
                ipObj = ip_addr.IPAddress(ip)
                NAT_IPs.append(ipObj)

    # Read IP Ranges, create IPNetwork and add to NAT_IPs list
    if ipRangeElements:
        for ipRangeElement in ipRangeElements:
            ip_range_raw = ipRangeElement.getText()
            ips = ip_range_raw.split('-')
            ip_start = ips[0]
            ip_end = ips[1]

            if ip_addr.isValidIpAddress(ip_start) and ip_addr.isValidIpAddress(ip_end):
                ip_start = ip_addr.IPAddress(ip_start)
                ip_end = ip_addr.IPAddress(ip_end)
                ips = ip_addr.summarize_address_range(ip_start, ip_end)
                logger.debug(ips)
                NAT_IPs.extend(ips)
            else:
                logger.warn("IP Range should start and end with valid IP address")

    return NAT_IPs
Esempio n. 6
0
def createIPEndpointOSHV(framework, ipAddress, portNum, portName, hostname = None, protocol = modeling.SERVICEADDRESS_TYPE_TCP):
    OSHVResult = ObjectStateHolderVector()
    if ip_addr.isValidIpAddress(hostname):
        hostname = None
    
    fqdn, aliasList = getHostNames(ipAddress, framework)
    
    hostOSH = modeling.createHostOSH(ipAddress, 'node', None, fqdn)
    ipOSH = modeling.createIpOSH(ipAddress, None, fqdn)
    link = modeling.createLinkOSH('containment', hostOSH, ipOSH)
    
    OSHVResult.add(hostOSH)
    OSHVResult.add(ipOSH)
    OSHVResult.add(link)
    
    ipPort = modeling.createServiceAddressOsh(hostOSH, ipAddress, portNum, protocol, portName)
    if fqdn:
        ipPort.setStringAttribute('ipserver_address', fqdn)
    
    if isValidFQDN(hostname):         
        ipPort.setStringAttribute('ipserver_address', hostname)
    
    #ipPort.addAttributeToList('itrc_alias', sv)
    #ipOSH.addAttributeToList('itrc_alias', sv)        
         
    #OSHVResult.add(modeling.createLinkOSH('usage', ipPort, ipOSH)) 
    OSHVResult.add(ipPort)
   
    return hostOSH, ipOSH, OSHVResult
Esempio n. 7
0
def createIPEndpointOSHV(framework,
                         ipAddress,
                         portNum,
                         portName,
                         hostname=None,
                         protocol=modeling.SERVICEADDRESS_TYPE_TCP):
    OSHVResult = ObjectStateHolderVector()
    if ip_addr.isValidIpAddress(hostname):
        hostname = None

    fqdn, aliasList = getHostNames(ipAddress, framework)

    hostOSH = modeling.createHostOSH(ipAddress, 'node', None, fqdn)
    ipOSH = modeling.createIpOSH(ipAddress, None, fqdn)
    link = modeling.createLinkOSH('containment', hostOSH, ipOSH)

    OSHVResult.add(hostOSH)
    OSHVResult.add(ipOSH)
    OSHVResult.add(link)

    ipPort = modeling.createServiceAddressOsh(hostOSH, ipAddress, portNum,
                                              protocol, portName)
    if fqdn:
        ipPort.setStringAttribute('ipserver_address', fqdn)

    if isValidFQDN(hostname):
        ipPort.setStringAttribute('ipserver_address', hostname)

    #ipPort.addAttributeToList('itrc_alias', sv)
    #ipOSH.addAttributeToList('itrc_alias', sv)

    #OSHVResult.add(modeling.createLinkOSH('usage', ipPort, ipOSH))
    OSHVResult.add(ipPort)

    return hostOSH, ipOSH, OSHVResult
def DiscoveryMain(Framework):

    ip = Framework.getDestinationAttribute('ip')
    count = Framework.getDestinationAttribute('count') or 4

    if not ip_addr.isValidIpAddress(ip):
        logger.error('SECURITY ALERT - invalid ip address \'%s\' received, someone might be exploiting your system.' % ip)
        return build_OSHV_result('\'%s\' is not a valid ip address' % ip)

    os_name = get_os_name()
    count_option = '-n' if os_name.startswith('Windows') else '-c'

    logger.debug('going to ping %s %s times' % (ip, count))

    result = ''
    try:
        count = max(min(100, int(count)), 1) # 1 <= count <= 100
        p = subprocess.Popen(['ping', count_option, str(count), str(ip)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if p.returncode == 0 or p.returncode == 1:
            result = decode_result(stdout)
        else:
            result = decode_result(stderr)

    except Exception, e:
        result = str(e)
def resolveIpFromDns(Framework, ipOrDnsOrAlias, localShell, dnsServers = None):
    normalizedIp = str(ipOrDnsOrAlias).strip()

    if not normalizedIp or normalizedIp == "localhost" or (ip_addr.isValidIpAddress(normalizedIp) and (ip_addr.IPAddress(normalizedIp).is_loopback or ip_addr.IPAddress(normalizedIp).is_multicast)):
        logger.debug('Skipped ip [', normalizedIp, '] for next hop, because it is empty or loopback or not a valid ip address')
        return None

    if dnsServers is not None:
        logger.debug('Trying to resolve ip using provided dnsServers names')
        dnsResolver = netutils.DNSResolver(localShell)
        for dnsServer in dnsServers:
            logger.debug('Trying to resolve ip using DNS Server [', dnsServer, ']')
            try:
                resolvedIp = dnsResolver.resolveHostIp(normalizedIp, dnsServer)
                if resolvedIp is not None:
                    logger.debug('Resolved ip [', resolvedIp, '] from [', normalizedIp, '] using DNS Server [', dnsServer, ']')
                    return resolvedIp
            except:
                Framework.reportWarning(logger.prepareJythonStackTrace(''))
                logger.debug('Failed to resolve [', normalizedIp, ']')

    try:
        logger.debug('Trying to resolve ip using local DNS server')
        resolvedIp = netutils.resolveIP(localShell, normalizedIp)
        if resolvedIp is not None:
            logger.debug('Resolved ip [', resolvedIp, '] from [', normalizedIp, '] using configured local DNS Server or hosts file')
            return resolvedIp
        else:
            errorMessage = 'Failed to resolve ip from [' + normalizedIp + '] using configured local DNS Server or hosts file'
            Framework.reportWarning(errorMessage)
            logger.warn(errorMessage)
    except:
        Framework.reportWarning(logger.prepareJythonStackTrace(''))
        logger.warn(errorMessage)
    return resolvedIp
Esempio n. 10
0
def _build_fc_switch_osh(switch):
    '@types: Switch -> osh'
    ipAddress = switch.address
    if ipAddress and ip_addr.isValidIpAddress(ipAddress):
        fcSwitchOSH = modeling.createHostOSH(str(ipAddress), 'fcswitch')
    else:
        logger.debug('IP address not available for Switch <%s> with ID <%s>!! Creating Switch with ID as primary key...' % (switch.name, switch.id))
        hostKey = switch.id + ' (ECC ID)'
        fcSwitchOSH = modeling.createCompleteHostOSH('fcswitch', hostKey)
        fcSwitchOSH.setAttribute('data_note', 'IP address unavailable in ECC - Duplication of this CI is possible')

    used_ports = None
    if switch.portcount != None and switch.portcount_free != None:
        used_ports = switch.portcount - switch.portcount_free

    populateOSH(fcSwitchOSH, {
        'data_description': switch.sn,
        'fcswitch_wwn': switch.sn,
        'data_name': switch.name,
        'host_model': switch.host_model,
        'fcswitch_version': switch.version,
        'host_vendor': switch.host_vendor,
        'fcswitch_domainid': switch.domain_id,
        'fcswitch_availableports': switch.portcount,
        'fcswitch_freeports': switch.portcount_free,
        'fcswitch_connectedports': used_ports
    })
    fcSwitchOSH.setListAttribute('node_role', ['switch'])
    return fcSwitchOSH
def DiscoveryMain(Framework):

    ip = Framework.getDestinationAttribute('ip')
    count = Framework.getDestinationAttribute('count') or 4

    if not ip_addr.isValidIpAddress(ip):
        logger.error(
            'SECURITY ALERT - invalid ip address \'%s\' received, someone might be exploiting your system.'
            % ip)
        return build_OSHV_result('\'%s\' is not a valid ip address' % ip)

    os_name = get_os_name()
    count_option = '-n' if os_name.startswith('Windows') else '-c'

    logger.debug('going to ping %s %s times' % (ip, count))

    result = ''
    try:
        count = max(min(100, int(count)), 1)  # 1 <= count <= 100
        p = subprocess.Popen(
            ['ping', count_option, str(count),
             str(ip)],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if p.returncode == 0 or p.returncode == 1:
            result = decode_result(stdout)
        else:
            result = decode_result(stderr)

    except Exception, e:
        result = str(e)
Esempio n. 12
0
    def __parseInterfaces(self, results):
        'str -> List(NetworkInterface)'
        interfacesList = []
        if results:
            self.windowsDescriptionPattern = self.langBund.getString(
                'windows_ipconfig_str_description').strip()
            self.windowsPhysicalAddressPattern = self.langBund.getString(
                'windows_ipconfig_str_physical_address').strip()
            self.windowsIpAddressPattern = self.langBund.getString(
                'windows_ipconfig_str_ip_address').strip()
            self.windowsNetworkMaskPattern = self.langBund.getString(
                'windows_ipconfig_str_mask').strip()
            self.reqDhcpEnabledPattern = self.langBund.getString(
                'windows_ipconfig_req_dhcp_enabled').strip()
            self.dhcpEnabledPhrase = self.langBund.getString(
                'windows_ipconfig_dhcp_enabled_true').strip()
            interfaceDo = modeling.NetworkInterface('', '', [], [], None, 0)
            for line in results.split('\n'):
                if line.strip() == '':
                    if interfaceDo.macAddress and interfaceDo.description:
                        interfacesList.append(interfaceDo)
                        interfaceDo = modeling.NetworkInterface(
                            '', '', [], [], None, 0)
                matcher = re.match(self.windowsDescriptionPattern, line)
                if matcher:
                    interfaceDo.description = matcher.group(1).strip()
                    continue

                matcher = re.match(self.windowsPhysicalAddressPattern, line)
                if matcher:
                    interfaceDo.macAddress = matcher.group(1).strip()
                    continue

                matcher = re.match(self.windowsIpAddressPattern, line)
                if matcher:
                    ipAddr = matcher.group(1).strip()
                    if ip_addr.isValidIpAddress(matcher.group(1).strip()):
                        interfaceDo.ips.append(ip_addr.IPAddress(ipAddr))
                    if isinstance(ip_addr.IPAddress(ipAddr),
                                  (ip_addr.IPv6Address)):
                        interfaceDo.masks.append("")
                    continue

                matcher = re.match(self.windowsNetworkMaskPattern, line)
                if matcher:
                    interfaceDo.masks.append(matcher.group(1).strip())
                    continue

                matcher = re.match(self.reqDhcpEnabledPattern, line)
                if matcher:
                    resultStr = matcher.group(1).strip()
                    if resultStr and resultStr.lower(
                    ) == self.dhcpEnabledPhrase:
                        interfaceDo.dhcpEnabled = 1
            if interfaceDo.macAddress and interfaceDo.description:
                interfacesList.append(interfaceDo)

            return interfacesList
        raise ValueError("Failed getting interfaces")
Esempio n. 13
0
def createShellObj(shell,
                   client,
                   ip,
                   langBund,
                   language,
                   codePage,
                   arpMac=None,
                   connectedShellCredId=None):
    'Shell, str, langBundle, str, str -> osh'
    # make sure that 'ip' is an ip and not a dns name
    # the reason is to make application_ip attribute hold an ip and not a dns name,
    # hence, when the application will be a trigger it will find the probe
    clientType = shell.getClientType()
    if clientType == ClientsConsts.NTCMD_PROTOCOL_NAME:
        clientType = "ntcmd"
    logger.debug('creating object for obj_name=%s' % clientType)

    ipObj = ip
    if ip_addr.isValidIpAddress(ip):
        ipObj = ip_addr.IPAddress(ip)
    else:
        # maybe it's a hostname?
        hostname = ip
        try:
            ips = SocketDnsResolver().resolve_ips(hostname)
            ipObj = ips[0]
        except ResolveException:
            logger.reportWarning('Could not resolve hostname' + hostname)
            ipObj = ip

    shellOsh = ObjectStateHolder(clientType)

    shellOsh.setAttribute('application_ip', str(ipObj))
    shellOsh.setAttribute('data_name', clientType)

    if clientType != "ntcmd":
        shellOsh.setAttribute('application_port', shell.getPort())
        shellOsh.setContainer(modeling.createHostOSH(str(ipObj)))

    # UDA client has a property of version, it should be reported
    if clientType == ClientsConsts.DDM_AGENT_PROTOCOL_NAME:
        shellOsh.setAttribute('version', client.getVersion())

    if (language):
        shellOsh.setAttribute('language', language)
    if (codePage):
        shellOsh.setAttribute('codepage', codePage)

    shellOsh.setAttribute('credentials_id', shell.getCredentialId())

    if arpMac:
        shellOsh.setAttribute(TopologyConstants.ATTR_APPLICATION_ARP_MAC,
                              arpMac)

    if connectedShellCredId:
        shellOsh.setAttribute(TopologyConstants.ATTR_CONN_OS_CRED_ID,
                              connectedShellCredId)

    return shellOsh
Esempio n. 14
0
 def buildWinsServers(self):
     if self.winsServerIpList:
         for winsIpAddr in self.winsServerIpList:
             if ip_addr.isValidIpAddress(winsIpAddr, filter_client_ip=True):
                 winsHostOsh = modeling.createHostOSH(str(winsIpAddr))
                 winsAppOsh = modeling.createWinsOsh(str(winsIpAddr), winsHostOsh)
                 self.resultVector.add(winsHostOsh)
                 self.resultVector.add(winsAppOsh)
Esempio n. 15
0
def isValidFQDN(hostname):
    if not hostname:
        return False
    if ip_addr.isValidIpAddress(hostname):
        return False

    p = re.compile(ur'(?=^.{1,254}$)(^(?:(?!\d+\.|-)[a-zA-Z0-9_\-]{1,63}(?<!-)\.?)+(?:[a-zA-Z]{2,})$)', re.IGNORECASE | re.MULTILINE)
    return not p.match(hostname) is None
Esempio n. 16
0
 def buildDhcpServers(self):
     if self.dhcpServerIpList:
         for dhcpIpAddr in self.dhcpServerIpList:
             if ip_addr.isValidIpAddress(dhcpIpAddr, filter_client_ip=True):
                 dhcpHostOsh = modeling.createHostOSH(str(dhcpIpAddr))
                 dhcpAppOsh = modeling.createDhcpOsh(str(dhcpIpAddr), dhcpHostOsh)
                 self.resultVector.add(dhcpHostOsh)
                 self.resultVector.add(dhcpAppOsh)
Esempio n. 17
0
 def buildWinsServers(self):
     if self.winsServerIpList:
         for winsIpAddr in self.winsServerIpList:
             if ip_addr.isValidIpAddress(winsIpAddr, filter_client_ip=True):
                 winsHostOsh = modeling.createHostOSH(str(winsIpAddr))
                 winsAppOsh = modeling.createWinsOsh(
                     str(winsIpAddr), winsHostOsh)
                 self.resultVector.add(winsHostOsh)
                 self.resultVector.add(winsAppOsh)
Esempio n. 18
0
 def buildDhcpServers(self):
     if self.dhcpServerIpList:
         for dhcpIpAddr in self.dhcpServerIpList:
             if ip_addr.isValidIpAddress(dhcpIpAddr, filter_client_ip=True):
                 dhcpHostOsh = modeling.createHostOSH(str(dhcpIpAddr))
                 dhcpAppOsh = modeling.createDhcpOsh(
                     str(dhcpIpAddr), dhcpHostOsh)
                 self.resultVector.add(dhcpHostOsh)
                 self.resultVector.add(dhcpAppOsh)
Esempio n. 19
0
    def __buildServerApplicationOsh(self,
                                    citName,
                                    server,
                                    productName=None,
                                    dbType=None,
                                    platform=None):
        r'''@types: str, db.DatabaseServer, str, str, db_platform.Platform -> ObjectStateHolderVector
        @param citName: any inheritor of 'database' CIT
        '''

        osh = ObjectStateHolder(citName)

        if server.instance:
            osh.setAttribute('name', server.instance)
            osh.setAttribute('database_dbsid', server.instance)

        if not productName and platform:
            productName = platform.productName

        if not dbType and platform:
            dbType = platform.dbType

        vendor = platform and platform.vendor or server.vendor
        if vendor:
            osh.setStringAttribute('vendor', vendor)

        ip = server.address
        if ip and ip_addr.isValidIpAddress(ip):
            osh.setAttribute('application_ip', str(ip))
        if server.getPort():
            osh.setAttribute('application_port', server.getPort())

        if server.getDescription():
            osh.setAttribute('description', server.getDescription())

        if server.getVersion():
            osh.setAttribute('version', server.getVersion())

        if server.getVersionDescription():
            osh.setAttribute('application_version',
                             server.getVersionDescription())

        if server.startTime:
            osh.setAttribute('startup_time', server.startTime)

        if server.installationPath:
            osh.setAttribute('application_path', server.installationPath)

        osh.setAttribute('database_dbtype', dbType)

        osh.setAttribute('application_category', 'Database')
        if productName:
            modeling.setApplicationProductName(osh,
                                               applicationName=productName)
            osh.setAttribute('discovered_product_name', productName)
        return osh
Esempio n. 20
0
    def parseLSOFListen(self, line, listenIpPorts):
        try:
            IpPortIpPortStatusListenArray = None
            protocol = modeling.TCP_PROTOCOL
            protocolName = 'tcp'
            try:
                IpPortIpPortStatusListenArray = re.search(self.TCPRegExp, line)
                if IpPortIpPortStatusListenArray != None:
                    linkStatus = IpPortIpPortStatusListenArray.group(3)
                    if linkStatus.find(self.LISTEN_str) == -1:
                        return TCPDiscovery.OK
            except:
                return TCPDiscovery.OK

            if (IpPortIpPortStatusListenArray == None) and (self.UDPRegExp != None):
                try:
                    IpPortIpPortStatusListenArray = re.search(self.UDPRegExp, line)
                    protocol = modeling.UDP_PROTOCOL
                    protocolName = 'udp'
                except:
                    return TCPDiscovery.OK

            if IpPortIpPortStatusListenArray == None:
                return TCPDiscovery.OK

            pid = IpPortIpPortStatusListenArray.group(1)
            listenipPort = IpPortIpPortStatusListenArray.group(2)
            ip = listenipPort.split(':')[0]

            if not self.isIpv4(ip):
                logger.debug ('Skipping not valid IPv4 address %s' % ip)
                return TCPDiscovery.OK

            if not ip_addr.isValidIpAddress(ip) and not str(ip).startswith('0.'):
                errorMessage = 'On parsing listen ports by lsof for protocol ' + protocolName + ' extracted invalid ip:<' + ip + '>'
                logger.warn(errorMessage)
                errobj = errorobject.createError(errorcodes.IP_PARSING_ERROR, [protocolName, ip, 'On parsing listen ports by lsof'], errorMessage)
                logger.reportWarningObject(errobj)
                return TCPDiscovery.OK

            #if ip_addr.IPAddress(ip).get_is_loopback():
            #    return TCPDiscovery.OK

            port = listenipPort.split(':')[1]

            #sometimes we get on UDP something like this:
            #postmaste 2412  sfmdb    7u  IPv4 0xe00000015f8ed100        0t0  UDP 127.0.0.1:49176->127.0.0.1:49176
            #in this case split by ':' brings us at port 49176->127.0.0.1
            port = re.search('\d+',port).group(0)

            #this is TCP port and we add it to port_process table for future connections discovery
            self._addTcpData(ip, port, pid, 1, protocol, listenIpPorts = listenIpPorts)
            return TCPDiscovery.OK
        except:
            logger.errorException('parseLSOFListen:failed to process TCP entry: ', line)
            return TCPDiscovery.ERROR
Esempio n. 21
0
def isValidFQDN(hostname):
    if not hostname:
        return False
    if ip_addr.isValidIpAddress(hostname):
        return False

    p = re.compile(
        ur'(?=^.{1,254}$)(^(?:(?!\d+\.|-)[a-zA-Z0-9_\-]{1,63}(?<!-)\.?)+(?:[a-zA-Z]{2,})$)',
        re.IGNORECASE | re.MULTILINE)
    return not p.match(hostname) is None
Esempio n. 22
0
 def __resolve_hostnames_by_local_dns(self, ip_string):
     if not ip_addr.isValidIpAddress(ip_string):
         raise ValueError(ip_string + ' is not a valid address')
     try:
         hostname, aliaslist, _ = socket.gethostbyaddr(ip_string)
         if not hostname or hostname == ip_string:
             raise _HOSTNAME_RESOLVE_EXCEPTION
         return [hostname] + aliaslist
     except socket.herror:
         raise _HOSTNAME_RESOLVE_EXCEPTION
Esempio n. 23
0
def _resolve_host_ips(host, hostName, hostDnsName):
    ''' Resolve address to IPs
    @types: str, str, str -> list[IPAddress]'''
    if ip_addr.isValidIpAddress(host.ip):
        return (host.ip,)
    try:
        address = hostDnsName or hostName
        return dns_resolver.SocketDnsResolver().resolve_ips(address)
    except dns_resolver.ResolveException:
        return []
Esempio n. 24
0
def _discover_host_ips(host, hostName, hostDnsName, ips_set, allowDnsLookup):
    '@types: Host, str, str, set[IPAddress], bool -> list[ip_addr.IPAddress]'
    ips = []
    if ip_addr.isValidIpAddress(host.ip):
        ips = [ip_addr.IPAddress(host.ip)]
    elif allowDnsLookup:
        ips = _resolve_host_ips(host, hostName, hostDnsName)

    if ips and ips_set.issuperset(set(ips)):
        ips = []
    return ips
Esempio n. 25
0
def _discover_switch_ips(switch, ips_set, allowDnsLookup):
    '@types: Switch, set[IPAddress], bool -> list[IPAddress]'
    ips = []
    if ip_addr.isValidIpAddress(switch.address):
        ips = [ip_addr.IPAddress(switch.address)]
    elif allowDnsLookup:
        ips = _resolve_switch_ips(switch)

    if ips and ips_set.issuperset(set(ips)):
        ips = []
    return ips
Esempio n. 26
0
def _resolve_switch_ips(switch):
    ''' Resolve address to IPs

    @types: str -> list[IPAddress]'''
    address = switch.address
    if ip_addr.isValidIpAddress(address):
        return (ip_addr.IPAddress(address),)
    try:
        hostname = _get_name_only(switch.name)
        return dns_resolver.SocketDnsResolver().resolve_ips(hostname)
    except dns_resolver.ResolveException:
        return []
Esempio n. 27
0
    def __parseInterfaces(self, results):
        "str -> List(NetworkInterface)"
        interfacesList = []
        if results:
            self.windowsDescriptionPattern = self.langBund.getString("windows_ipconfig_str_description").strip()
            self.windowsPhysicalAddressPattern = self.langBund.getString(
                "windows_ipconfig_str_physical_address"
            ).strip()
            self.windowsIpAddressPattern = self.langBund.getString("windows_ipconfig_str_ip_address").strip()
            self.windowsNetworkMaskPattern = self.langBund.getString("windows_ipconfig_str_mask").strip()
            self.reqDhcpEnabledPattern = self.langBund.getString("windows_ipconfig_req_dhcp_enabled").strip()
            self.dhcpEnabledPhrase = self.langBund.getString("windows_ipconfig_dhcp_enabled_true").strip()
            interfaceDo = modeling.NetworkInterface("", "", [], [], None, 0)
            for line in results.split("\n"):
                if line.strip() == "":
                    if interfaceDo.macAddress and interfaceDo.description:
                        interfacesList.append(interfaceDo)
                        interfaceDo = modeling.NetworkInterface("", "", [], [], None, 0)
                matcher = re.match(self.windowsDescriptionPattern, line)
                if matcher:
                    interfaceDo.description = matcher.group(1).strip()
                    continue

                matcher = re.match(self.windowsPhysicalAddressPattern, line)
                if matcher:
                    interfaceDo.macAddress = matcher.group(1).strip()
                    continue

                matcher = re.match(self.windowsIpAddressPattern, line)
                if matcher:
                    ipAddr = matcher.group(1).strip()
                    if ip_addr.isValidIpAddress(matcher.group(1).strip()):
                        interfaceDo.ips.append(ip_addr.IPAddress(ipAddr))
                    if isinstance(ip_addr.IPAddress(ipAddr), (ip_addr.IPv6Address)):
                        interfaceDo.masks.append("")
                    continue

                matcher = re.match(self.windowsNetworkMaskPattern, line)
                if matcher:
                    interfaceDo.masks.append(matcher.group(1).strip())
                    continue

                matcher = re.match(self.reqDhcpEnabledPattern, line)
                if matcher:
                    resultStr = matcher.group(1).strip()
                    if resultStr and resultStr.lower() == self.dhcpEnabledPhrase:
                        interfaceDo.dhcpEnabled = 1
            if interfaceDo.macAddress and interfaceDo.description:
                interfacesList.append(interfaceDo)

            return interfacesList
        raise ValueError("Failed getting interfaces")
Esempio n. 28
0
 def buildDnsServers(self):
     if self.dnsServerIpList:
         # accept IPv4 addresses only
         # currently IPv6 addresses can not be assigned to any probe
         # and are not a part of reconciliation, so node CI can not be
         # created based on instance of IPv6 address
         isIPv4 = lambda ip: ip_addr.IPAddress(ip).get_version() == 4
         for dnsIpAddr in filter(isIPv4, self.dnsServerIpList):
             if ip_addr.isValidIpAddress(dnsIpAddr, filter_client_ip=True):
                 dnsHostOsh = modeling.createHostOSH(str(dnsIpAddr))
                 dnsAppOsh = modeling.createDnsOsh(str(dnsIpAddr), dnsHostOsh)
                 self.resultVector.add(dnsHostOsh)
                 self.resultVector.add(dnsAppOsh)
Esempio n. 29
0
def parse_from_address(address, resolve_ips_fn):
    r'@types: str, (str->[ipaddr.IPAddress]?) -> hana_host.Host'
    host = None
    if ip_addr.isValidIpAddress(address):
        ips = [address]
        host = Host(ips=ips)
    else:
        ips = resolve_ips_fn(address)
        if is_fqdn(address):
            host = parse_from_fqdn(address, ips)
        else:
            host = Host(ips=ips, name=address)
    return host
Esempio n. 30
0
    def __buildServerApplicationOsh(self, citName, server,
                          productName=None, dbType=None,
                          platform=None):
        r'''@types: str, db.DatabaseServer, str, str, db_platform.Platform -> ObjectStateHolderVector
        @param citName: any inheritor of 'database' CIT
        '''

        osh = ObjectStateHolder(citName)

        if server.instance:
            osh.setAttribute('name', server.instance)
            osh.setAttribute('database_dbsid', server.instance)

        if not productName and platform:
            productName = platform.productName

        if not dbType and platform:
            dbType = platform.dbType

        vendor = platform and platform.vendor or server.vendor
        if vendor:
            osh.setStringAttribute('vendor', vendor)

        ip = server.address
        if ip and ip_addr.isValidIpAddress(ip):
            osh.setAttribute('application_ip', str(ip))
        if server.getPort():
            osh.setAttribute('application_port', server.getPort())

        if server.getDescription():
            osh.setAttribute('description', server.getDescription())

        if server.getVersion():
            osh.setAttribute('version', server.getVersion())

        if server.getVersionDescription():
            osh.setAttribute('application_version', server.getVersionDescription())

        if server.startTime:
            osh.setAttribute('startup_time', server.startTime)

        if server.installationPath:
            osh.setAttribute('application_path', server.installationPath)

        osh.setAttribute('database_dbtype', dbType)

        osh.setAttribute('application_category', 'Database')
        if productName:
            modeling.setApplicationProductName(osh, applicationName=productName)
            osh.setAttribute('discovered_product_name', productName)
        return osh
 def getHostAddressesbyNetinterfaceByPowerShell(self, nodeName):
     ip = None
     getHostAddressCmd = "Get-ClusterNetworkInterface | Where-Object {$_.Node -match '%s'} | Where-Object {$_.Name -match '%s - Public'} | fl Address" % (nodeName, nodeName)
     output = self.executeCmdByPowerShell(getHostAddressCmd)
     for line in output.strip().splitlines():
         if (line.find(':') != -1):
             ip = line.split(':')[1].strip()
         if ip_addr.isValidIpAddress(ip):
             ip = ip_addr.IPAddress(ip)
             break
         ip = None
     return (ip
             and (ip,)
             or ())
Esempio n. 32
0
 def buildDnsServers(self):
     if self.dnsServerIpList:
         # accept IPv4 addresses only
         # currently IPv6 addresses can not be assigned to any probe
         # and are not a part of reconciliation, so node CI can not be
         # created based on instance of IPv6 address
         isIPv4 = lambda ip: ip_addr.IPAddress(ip).get_version() == 4
         for dnsIpAddr in filter(isIPv4, self.dnsServerIpList):
             if ip_addr.isValidIpAddress(dnsIpAddr, filter_client_ip=True):
                 dnsHostOsh = modeling.createHostOSH(str(dnsIpAddr))
                 dnsAppOsh = modeling.createDnsOsh(str(dnsIpAddr),
                                                   dnsHostOsh)
                 self.resultVector.add(dnsHostOsh)
                 self.resultVector.add(dnsAppOsh)
Esempio n. 33
0
def createShellObj(shell, client, ip, langBund, language, codePage, arpMac = None, connectedShellCredId = None):
    'Shell, str, langBundle, str, str -> osh'
    # make sure that 'ip' is an ip and not a dns name
    # the reason is to make application_ip attribute hold an ip and not a dns name,
    # hence, when the application will be a trigger it will find the probe
    clientType = shell.getClientType()
    if clientType == ClientsConsts.NTCMD_PROTOCOL_NAME:
        clientType = "ntcmd"
    logger.debug('creating object for obj_name=%s' % clientType)

    ipObj = ip
    if ip_addr.isValidIpAddress(ip):
        ipObj = ip_addr.IPAddress(ip)
    else:
        # maybe it's a hostname?
        hostname = ip
        try:
            ips = SocketDnsResolver().resolve_ips(hostname)
            ipObj = ips[0]
        except ResolveException:
            logger.reportWarning('Could not resolve hostname' + hostname)
            ipObj = ip

    shellOsh = ObjectStateHolder(clientType)

    shellOsh.setAttribute('application_ip', str(ipObj))
    shellOsh.setAttribute('data_name', clientType)

    if clientType != "ntcmd":
        shellOsh.setAttribute('application_port', shell.getPort())
        shellOsh.setContainer(modeling.createHostOSH(str(ipObj)))

    # UDA client has a property of version, it should be reported
    if clientType == ClientsConsts.DDM_AGENT_PROTOCOL_NAME:
        shellOsh.setAttribute('version', client.getVersion())

    if(language):
        shellOsh.setAttribute('language', language)
    if(codePage):
        shellOsh.setAttribute('codepage', codePage)

    shellOsh.setAttribute('credentials_id', shell.getCredentialId())

    if arpMac:
        shellOsh.setAttribute(TopologyConstants.ATTR_APPLICATION_ARP_MAC, arpMac)

    if connectedShellCredId:
        shellOsh.setAttribute(TopologyConstants.ATTR_CONN_OS_CRED_ID, connectedShellCredId)

    return shellOsh
Esempio n. 34
0
    def doPortScan(self, ip, tcpPorts, udpPorts):

        if not ip_addr.isValidIpAddress(ip):
            raise ValueError("IP address is not valid")

        tcpRanges = self.splitPortsByRanges(tcpPorts)
        udpRanges = self.splitPortsByRanges(udpPorts)

        options = dict.fromkeys(['-v', '-sS', '-n', '-oX', '-p', '"%s"' % str(ip)])
        options['-oX'] = '-'

        if udpRanges:
            options['-sU'] = None

        ip_obj = ip_addr.IPAddress(str(ip))
        if ip_obj.get_version() == 6:
            logger.debug("Given ip address have IPv6 format. Using nmap with IPv6 support...")
            options['-6'] = None

        options['-p'] = self.genNmapRange(tcpRanges, udpRanges)

        if options['-p']:
            cmdLine = self.buildCommandLine(options)
        else:
            raise ValueError('Nothing to scan')

        result = []
        if len(cmdLine) > Nmap.LIMIT_CMDLINE:
            logger.debug("Command line is to big. Splitting...")
            portsChunk = self.genNmapRangeWithLimit(tcpRanges, udpRanges, Nmap.PORTRANGE_LIMIT)
            logger.debug("Nmap will be executed %s times" % len(portsChunk))
            for portRange in portsChunk:
                options['-p'] = portRange
                isTCP = portRange.find('T:') != -1
                isUDP = portRange.find('U:') != -1
                if isTCP:
                    options['-sS'] = None
                elif options.has_key('-sS'):
                    del options['-sS']
                if isUDP:
                    options['-sU'] = None
                elif options.has_key('-sU'):
                    del options['-sU']
                cmdLine = self.buildCommandLine(options)
                result.extend(self._execute(cmdLine, self._parseOpenPortsFromXml))
        else:
            result.extend(self._execute(cmdLine, self._parseOpenPortsFromXml))

        return result
Esempio n. 35
0
    def parseTcpListenPorts(self, line, listenIpPorts):
        linkStatus = ''
        try:
            IpPortIpPortStatusListenArray = None
            protocol = modeling.TCP_PROTOCOL
            protocolName = 'tcp'
            try:
                IpPortIpPortStatusListenArray = re.compile(self.TCPRegExp).search(line)
                if IpPortIpPortStatusListenArray != None:
                    linkStatus = IpPortIpPortStatusListenArray.group(5).upper()
                    if (linkStatus.find(self.LISTEN_str) == -1) and (linkStatus.find("LISTEN") == -1):
                        return TCPDiscovery.OK
            except:
                return TCPDiscovery.OK

            if (IpPortIpPortStatusListenArray == None) and (self.UDPRegExp != None) and (len(self.UDPRegExp) > 0):
                try:
                    IpPortIpPortStatusListenArray = re.search(self.UDPRegExp, line)

                    if (IpPortIpPortStatusListenArray != None) and len(IpPortIpPortStatusListenArray.groups()) == 3:
                        linkStatus = IpPortIpPortStatusListenArray.group(3)
                    protocol = modeling.UDP_PROTOCOL
                    protocolName = 'udp'
                except:
                    return TCPDiscovery.OK

            if IpPortIpPortStatusListenArray == None:
                return TCPDiscovery.OK

            ip = self.__parseIpv6(IpPortIpPortStatusListenArray.group(1))
            port = IpPortIpPortStatusListenArray.group(2)

            if not ip_addr.isValidIpAddress(ip):
                errorMessage = 'On parsing listen ports by netstat for protocol ' + protocolName + ' extracted invalid ip:<' + ip + '>'
                logger.warn(errorMessage)
                errobj = errorobject.createError(errorcodes.IP_PARSING_ERROR, [protocolName, ip, 'On parsing listen ports by netstat'], errorMessage)
                logger.reportWarningObject(errobj)
                return TCPDiscovery.OK

            #if ip_addr.IPAddress(ip).get_is_loopback():
            #    return TCPDiscovery.OK

            pid = self.getPid(linkStatus)
            self._addTcpData(ip, port, pid, 1, protocol, listenIpPorts = listenIpPorts)

            return TCPDiscovery.OK
        except:
            logger.errorException('parseTcpListenPorts:failed to process TCP entry: ', line)
            return TCPDiscovery.ERROR
Esempio n. 36
0
def normalize(activeIps):
    ''' process the ip result from the list
    @types: list(activeIps) -> list(resultList)'''
    resultList = []
    for ip in activeIps:
        if (ip not in resultList):
            # Break the curr result by ':' <Reply-IP>:<Pinged-IP>
            # In case where we ping a virtual ip we get the reply from the real ip
            token = ip.split(':')
            if (len(token) == 2):
                ip = token[1]
            if (not ip_addr_util.isValidIpAddress(ip)):#compatible to ipv6
                continue
            resultList.append(ip)
    return resultList
Esempio n. 37
0
def get_host(address, resolve_ips_fn):
    r'''
    @types: str, (str->[ipaddr.IPAddress]?) -> db2_host.Host
    @deprecated: use host_base_parser.parse_from_address instead
    '''
    host = None
    if ip_addr.isValidIpAddress(address):
        ips = [address]
        host = db2_host.Host(ips)
    else:
        ips = resolve_ips_fn(address)
        if db2_host.is_fqdn(address):
            host = db2_host.parse_host_from_fqdn(address, ips)
        else:
            host = db2_host.Host(ips, name=address)
    return host
Esempio n. 38
0
def get_host(address, resolve_ips_fn):
    r'''
    @types: str, (str->[ipaddr.IPAddress]?) -> db2_host.Host
    @deprecated: use host_base_parser.parse_from_address instead
    '''
    host = None
    if ip_addr.isValidIpAddress(address):
        ips = [address]
        host = db2_host.Host(ips)
    else:
        ips = resolve_ips_fn(address)
        if db2_host.is_fqdn(address):
            host = db2_host.parse_host_from_fqdn(address, ips)
        else:
            host = db2_host.Host(ips, name=address)
    return host
Esempio n. 39
0
    def buildDatabaseServerPdo(self, dsnInfo):
        """

        :param dsnInfo: DSN Data object
        :type dsnInfo: odbc.DSNInfo
        :return: Database Server Data object
        :rtype: db.DatabaseServer
        """
        address = dsnInfo.address
        if self.__dnsResolver and not ip_addr.isValidIpAddress(address):
            address = self.__dnsResolver.resolve_ips(address)
            if address:
                address = address[0]
        return db_builder.buildDatabaseServerPdo(dsnInfo.driver,
                                                 dsnInfo.database, address,
                                                 dsnInfo.port)
Esempio n. 40
0
    def buildDatabaseServerPdo(self, dsnInfo):
        """

        :param dsnInfo: DSN Data object
        :type dsnInfo: odbc.DSNInfo
        :return: Database Server Data object
        :rtype: db.DatabaseServer
        """
        address = dsnInfo.address
        if self.__dnsResolver and not ip_addr.isValidIpAddress(address):
            address = self.__dnsResolver.resolve_ips(address)
            if address:
                address = address[0]
        return db_builder.buildDatabaseServerPdo(dsnInfo.driver,
                                                 dsnInfo.database,
                                                 address,
                                                 dsnInfo.port)
Esempio n. 41
0
def parse_from_address(address, resolve_ips_fn):
    r'@types: str, (str->[ipaddr.IPAddress]?) -> host_parser.HostDescriptor'
    if not address:
        raise ValueError('Invalid address')
    if not resolve_ips_fn:
        raise ValueError('Invalid resolve_ips_fn')
    host = None
    if ip_addr.isValidIpAddress(address):
        ips = [ip_addr.IPAddress(address)]
        host = HostDescriptor(ips=ips)
    else:
        ips = resolve_ips_fn(address)
        if is_fqdn(address):
            host = parse_from_fqdn(address, ips)
        else:
            host = HostDescriptor(ips=ips, name=address)
    return host
Esempio n. 42
0
def parse_from_address(address, resolve_ips_fn):
    r'@types: str, (str->[ipaddr.IPAddress]?) -> host_parser.HostDescriptor'
    if not address:
        raise ValueError('Invalid address')
    if not resolve_ips_fn:
        raise ValueError('Invalid resolve_ips_fn')
    host = None
    if ip_addr.isValidIpAddress(address):
        ips = [ip_addr.IPAddress(address)]
        host = HostDescriptor(ips=ips)
    else:
        ips = resolve_ips_fn(address)
        if is_fqdn(address):
            host = parse_from_fqdn(address, ips)
        else:
            host = HostDescriptor(ips=ips, name=address)
    return host
Esempio n. 43
0
 def getHostAddressesbyNetinterface(self, nodeName):
     ip = None
     # This command will fail if Network name is not "Public"
     buff = self.__exec('netint /node:%s /net:Public /prop:Address' %
                        nodeName)
     endOfHeader = 0
     for line in buff.strip().splitlines():
         if (line.find('-----') != -1) and (endOfHeader == 0):
             endOfHeader = 1
             continue
         if endOfHeader == 1:
             attrs = line.split()
             if (len(attrs) < 4):
                 continue
             ip = attrs[4].strip()
             if ip_addr.isValidIpAddress(ip):
                 ip = ip_addr.IPAddress(ip)
                 break
             ip = None
     return (ip and (ip, ) or ())
Esempio n. 44
0
 def getHostAddressesbyNetinterface(self, nodeName):
     ip = None
     # This command will fail if Network name is not "Public"
     buff = self.__exec('netint /node:%s /net:Public /prop:Address' % nodeName)
     endOfHeader = 0
     for line in buff.strip().splitlines():
         if (line.find('-----') != -1) and (endOfHeader == 0):
             endOfHeader = 1
             continue
         if endOfHeader == 1:
             attrs = line.split()
             if (len(attrs) < 4):
                 continue
             ip = attrs[4].strip()
             if ip_addr.isValidIpAddress(ip):
                 ip = ip_addr.IPAddress(ip)
                 break
             ip = None
     return (ip
             and (ip,)
             or ())
Esempio n. 45
0
    def build(self, device):
        if device is None:
            raise ValueError("device is None")

        deviceClass = self.getDeviceClass(device)
        deviceOsh = ObjectStateHolder(deviceClass)
        deviceOsh.setBoolAttribute('host_iscomplete', True)

        deviceOsh = self.setDeviceRoles(device, deviceOsh)

        hostName = device.hostName
        domainName = None

        if hostName and not ip_addr.isValidIpAddress(hostName):
            hostName, domainName = self.getHostNameAndDomain(hostName)

        if hostName:
            deviceOsh.setStringAttribute('name', hostName)
        if domainName:
            deviceOsh.setStringAttribute('host_osdomain', domainName)

        if device.serialNumber:
            modeling.setHostSerialNumberAttribute(deviceOsh,
                                                  device.serialNumber)

        if device.model:
            modeling.setHostModelAttribute(deviceOsh, device.model)

        if device.vendor:
            deviceOsh.setStringAttribute('discovered_vendor', device.vendor)

        if device.softwareVersion:
            deviceOsh.setStringAttribute('discovered_os_version',
                                         device.softwareVersion)

        if device.memory:
            memoryMb = int(device.memory / (1024 * 1024))
            modeling.setHostMemorySizeAttribute(deviceOsh, memoryMb)

        return deviceOsh
Esempio n. 46
0
    def build(self, device):
        if device is None:
            raise ValueError("device is None")

        deviceClass = self.getDeviceClass(device)
        deviceOsh = ObjectStateHolder(deviceClass)
        deviceOsh.setBoolAttribute("host_iscomplete", True)

        deviceOsh = self.setDeviceRoles(device, deviceOsh)

        hostName = device.hostName
        domainName = None

        if hostName and not ip_addr.isValidIpAddress(hostName):
            hostName, domainName = self.getHostNameAndDomain(hostName)

        if hostName:
            deviceOsh.setStringAttribute("name", hostName)
        if domainName:
            deviceOsh.setStringAttribute("host_osdomain", domainName)

        if device.serialNumber:
            modeling.setHostSerialNumberAttribute(deviceOsh, device.serialNumber)

        if device.model:
            modeling.setHostModelAttribute(deviceOsh, device.model)

        if device.vendor:
            deviceOsh.setStringAttribute("discovered_vendor", device.vendor)

        if device.softwareVersion:
            deviceOsh.setStringAttribute("discovered_os_version", device.softwareVersion)

        if device.memory:
            memoryMb = int(device.memory / (1024 * 1024))
            modeling.setHostMemorySizeAttribute(deviceOsh, memoryMb)

        return deviceOsh
Esempio n. 47
0
def resolveIpAddress(url):
    ips = []
    try:
        match = re.findall("(https?)://([\w.]+):(\d+)/", url)
        for parameters in match:
            if len(parameters) >= 2:
                protocol = parameters[0]
                name = parameters[1]
                logger.debug("resolveIpAddress-hostname:", name)
                if not ip_addr.isValidIpAddress(name):
                    resolver = dns_resolver.SocketDnsResolver()
                    ips = resolver.resolve_ips(name)
                else:
                    ips.append(IPAddress(name))
            else:
                msg = "wrong format of url:" + str(url)
                logger.reportWarning(msg)
                logger.error("wrong format of url:", url)
    except:
        msg = "Failed to resolve host name: " + str(url)
        logger.reportWarning(msg)
        logger.error(msg, str(sys.exc_info()[1]))
    return ips
Esempio n. 48
0
def is_fqdn(address):
    if not address or ip_addr.isValidIpAddress(address):
        raise ValueError('Invalid address')
    return address.count('.') > 0
Esempio n. 49
0
    def doDiscover(self, hypervisor_osh):
        logger.info('Begin discovering...')

        vector = ObjectStateHolderVector()
        vector.add(hypervisor_osh)
        logger.info('Discover current host...')
        current_host = self.getHost()
        xen_server_osh = self.builder.build_xen_server(current_host)
        vector.add(xen_server_osh)

        hosts = self.getHosts()
        pool = self.getPool()
        pool_osh = None
        if pool:
            pool_osh = self.builder.build_pool(pool)
            master = pool.getMaster()
            vector.add(pool_osh)
            if current_host == master:
                master_osh = xen_server_osh
            else:
                master_osh = self.builder.build_xen_server(master)
            pool_osh.setContainer(master_osh)

        for host in hosts:
            host_osh = self.builder.build_xen_server(host)
            if pool_osh:
                vector.add(
                    modeling.createLinkOSH('containment', pool_osh, host_osh))
            cpus = host.getHostCPUs()
            for cpu in cpus:
                cpu_osh = self.builder.build_cpu(cpu, host_osh)
                vector.add(cpu_osh)

        logger.info('Discover vm appliance...')
        vm_apps = VMAppliance.getAll(self.session)
        vm_apps_map = {}
        for vm_app in vm_apps:
            vm_app_osh = self.builder.build_vm_app(vm_app)
            vm_app_osh.setContainer(xen_server_osh)
            vector.add(vm_app_osh)
            vm_apps_map[vm_app.uuid] = vm_app_osh

        logger.info('Discover SR...')
        srs = self.getSRs()
        all_vdi = []
        for sr in srs:
            logger.debug('sr:', sr)
            sr_osh = self.builder.build_sr(sr)
            sr_osh.setContainer(xen_server_osh)
            sr.osh = sr_osh
            vector.add(sr_osh)

            vdis = sr.getVDIs()
            for vdi in vdis:
                logger.debug('vdi:', vdi)
                vdi_osh = self.builder.build_vdi(vdi)
                vdi_osh.setContainer(sr_osh)
                vector.add(vdi_osh)
                vdi.osh = vdi_osh
                all_vdi.append(vdi)

        logger.info("Discover network...")
        networks = self.getNetworks()
        for network in networks:
            network_osh = self.builder.build_network(network)
            network_osh.setContainer(xen_server_osh)
            network.osh = network_osh
            vector.add(network_osh)

        logger.info('Discover pbd...')
        pbds = current_host.getPBDs()
        for pbd in pbds:
            pbd_osh = self.builder.build_pbd(pbd)
            pbd_osh.setContainer(xen_server_osh)
            vector.add(pbd_osh)
            sr = pbd.getSR()
            sr_ = [x.osh for x in srs if x == sr]
            if sr_:
                sr_osh = sr_[0]
                vector.add(
                    modeling.createLinkOSH('dependency', sr_osh, pbd_osh))

        logger.info('Discover pif...')
        pifs = current_host.getPIFs()
        for pif in pifs:
            pif_osh = self.builder.build_pif(pif)
            if pif_osh:
                vector.add(pif_osh)
                pif_osh.setContainer(xen_server_osh)
                network = pif.getNetwork()
                network_ = [x.osh for x in networks if x == network]
                if network_:
                    network_osh = network_[0]
                    vector.add(
                        modeling.createLinkOSH('usage', network_osh, pif_osh))

        logger.info('Discover vm...')
        vms = current_host.getVMs()
        for vm in vms:
            if vm.isControlDomain():
                continue
            vm_osh = self.builder.build_vm(vm)
            vector.add(vm_osh)
            vector.add(
                modeling.createLinkOSH('execution_environment', hypervisor_osh,
                                       vm_osh))

            vm_app = vm.getAppliance()
            if vm_app and vm_app.uuid in vm_apps_map:
                vector.add(
                    modeling.createLinkOSH('membership',
                                           vm_apps_map[vm_app.uuid], vm_osh))

            xem_domain_osh = self.builder.build_xen_domain_config(
                vm, vm.getVMMetrics())
            xem_domain_osh.setContainer(vm_osh)
            vector.add(xem_domain_osh)

            vbds = vm.getVBDs()
            logger.debug('Discover vbd on vm:', vm.uuid)
            for vbd in vbds:
                vbd_osh = self.builder.build_vbd(vbd)
                vbd_osh.setContainer(vm_osh)
                vector.add(vbd_osh)
                vdi = vbd.getVDI()
                if vdi:
                    vdi_ = [x.osh for x in all_vdi if x == vdi]
                    if vdi_:
                        vdi_osh = vdi_[0]
                        vector.add(
                            modeling.createLinkOSH('dependency', vbd_osh,
                                                   vdi_osh))

            logger.debug('Discover vif on vm:', vm.uuid)
            vifs = vm.getVIFs()
            if_index_map = {}
            for vif in vifs:
                vif_osh = self.builder.build_vif(vif)
                if vif_osh:
                    vif_osh.setContainer(vm_osh)
                    vector.add(vif_osh)

                    network = vif.getNetwork()
                    network_ = [x.osh for x in networks if x == network]
                    if network_:
                        network_osh = network_[0]
                        vector.add(
                            modeling.createLinkOSH('usage', vif_osh,
                                                   network_osh))

                    device = vif.getDevice()
                    if device is not None:
                        if_index_map[device] = vif_osh

            logger.info('Discover ips of vm:', vm.uuid)
            vm_guest_metrics = vm.getVMGuestMetrics()
            if vm_guest_metrics:
                if_to_ip_map = vm_guest_metrics.getIPMap()
                for (if_index, ips) in if_to_ip_map.iteritems():
                    for ip in ips:
                        if ip_addr.isValidIpAddress(ip):
                            ip = ip_addr.IPAddress(ip)
                            ip_osh = modeling.createIpOSH(ip)
                            vector.add(ip_osh)
                            vector.add(
                                modeling.createLinkOSH('containment', vm_osh,
                                                       ip_osh))
                            if if_index in if_index_map:
                                if_osh = if_index_map[if_index]
                                vector.add(
                                    modeling.createLinkOSH(
                                        'containment', if_osh, ip_osh))

        return vector
Esempio n. 50
0
    def _parseCdpLayer2Output(self, output):
        results = {}
        if output:
            blocks = re.split('----------------------------------------',
                              output)
            for block in blocks:
                if not re.search('Device ID', block):
                    continue

                m = re.search('System Name:\s+([\w\-\.\/]+)', block)
                system_name = m and m.group(1)
                if not system_name:
                    continue

                m = re.search('Interface:\s+([\w\-\.\/]+),', block)
                local_interface_name = m and m.group(1)
                if not local_interface_name:
                    continue

                remote_ips = []
                elems = re.findall('IPv[46] Address:(.+)\r?\n?Platform', block,
                                   re.DOTALL)
                if m:
                    for elem in elems:
                        remote_ips = [
                            ip_addr.IPAddress(raw_ip.strip())
                            for raw_ip in elem.split(',')
                            if ip_addr.isValidIpAddress(raw_ip.strip())
                        ]
                m = re.search('Platform:\s+([\w\-\.]+),', block)
                platform = m and m.group(1)

                #                '''
                #                Capability Codes: R - Router, T - Trans-Bridge, B - Source-Route-Bridge
                #                          S - Switch, H - Host, I - IGMP, r - Repeater,
                #                          V - VoIP-Phone, D - Remotely-Managed-Device,
                #                          s - Supports-STP-Dispute
                #                '''
                m = re.search('Capabilities:\s+([\w\-\.]+)', block)
                type = m and m.group(1)
                mac = None
                iface_name = None
                m = re.search(
                    'Port\s+ID\s+\(outgoing\s+port\):\s+([\w\-\.\:\/]+)',
                    block)  #can be interface name, interface mac.
                if not m:
                    continue

                if netutils.isValidMac(m.group(1)):
                    mac = netutils.parseMac(m.group(1))
                else:
                    iface_name = m.group(1)
                m = re.search('Version:(.+)Advertisement', block, re.DOTALL)
                description = m and m.group(1).strip()
                try:
                    remote_list = results.get(local_interface_name, [])
                    remote_list.append(
                        layer2.RemotePeer(system_name, iface_name, mac,
                                          remote_ips, platform, type,
                                          description))
                    results[local_interface_name] = remote_list
                except:
                    logger.debugException('')

        return results
Esempio n. 51
0
def DiscoveryMain(Framework):
    """
    Retrieving a list of LDAP ports we strive to connect to domain controller in
    member role first. So we have to choose the lowest port number in the list.
    """
    protocolName = "LDAP"

    OSHVResult = ObjectStateHolderVector()

    baseDn = Framework.getParameter('baseDN') or None
    if baseDn == 'NA':
        baseDn = None
    ipAddress = Framework.getDestinationAttribute('ip_address')
    credentialsId = Framework.getDestinationAttribute('credentials_id')
    applicationPort = Framework.getDestinationAttribute("application_port")
    serviceAddressPort = Framework.getDestinationAttribute('port')

    if not applicationPort or applicationPort == 'NA':
        applicationPort = serviceAddressPort

    # build environment and connect
    envBuilder = active_directory_utils.LdapEnvironmentBuilder(applicationPort)
    client = Framework.createClient(credentialsId, envBuilder.build())

    discoverer = oracle_ldap_discoverer.OracleLdapDiscoverer(client, baseDn)

    try:
        probe_client  = Framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME)
        probe_shell = shellutils.ShellFactory().createShell(probe_client)

        resolver = netutils.FallbackResolver([netutils.JavaDnsResolver(), netutils.DnsResolverByShell(probe_shell)])

        # discover
        servers = discoverer.discover()

        # report
        endpoint_builder = netutils.ServiceEndpointBuilder()
        endpoint_reporter = netutils.EndpointReporter(endpoint_builder)

        oracle_builder = db_builder.Oracle()
        reporter = db.OracleTopologyReporter(oracle_builder, endpoint_reporter)

        for dbServer in servers:
            if dbServer:
                try:
                    address = dbServer.address
                    if not ip_addr.isValidIpAddress(address):
                        ips = resolver.resolveIpsByHostname(address)
                        if ips and len(ips) > 0:
                            dbServer.address = str(ip_addr.IPAddress(ips[0]))
                    # get Host OSH
                    if not (dbServer.address
                            and netutils.isValidIp(address)
                            and not netutils.isLocalIp(address)):
                        raise ValueError("Address for the specified server is not valid or is local")
                    hostOsh = modeling.createHostOSH(dbServer.address)

                    # report database
                    OSHVResult.addAll(reporter.reportServerAndDatabases(dbServer, hostOsh))

                    # report TNS Listener
                    listener = db.OracleListener(dbServer.address, dbServer.getPort())
                    OSHVResult.addAll(reporter.reportTnsListener(listener, hostOsh))

                    # report Oracle Service Names
                    if dbServer.getServiceNames():
                        OSHVResult.addAll(reporter.reportServiceNameTopology(dbServer.getServiceNames(), listener.getOsh(), dbServer.getOsh()))

                except netutils.ResolveException:
                    logger.error("Cannot resolve " + dbServer.address + ". Skip")
    except oracle_ldap_discoverer.OracleContextDiscoveryException, ex:
        msg = str(ex)
        logger.debugException(msg)
        logger.error(msg)