[vscweb-commit] SF.net SVN: vscweb: [453] branches/vsc-2.0/pyvscd/util
Brought to you by:
cirrusrex
|
From: <cir...@us...> - 2006-08-02 20:41:51
|
Revision: 453 Author: cirrusrex Date: 2006-08-02 13:41:45 -0700 (Wed, 02 Aug 2006) ViewCVS: http://svn.sourceforge.net/vscweb/?rev=453&view=rev Log Message: ----------- Added a python script to perform dns caching/updating for VSC - this replaces kluged perl daemon Modified Paths: -------------- branches/vsc-2.0/pyvscd/libvscmt/__init__.py branches/vsc-2.0/pyvscd/libvscmt/host.py Added Paths: ----------- branches/vsc-2.0/pyvscd/util/cachedns.py Modified: branches/vsc-2.0/pyvscd/libvscmt/__init__.py =================================================================== --- branches/vsc-2.0/pyvscd/libvscmt/__init__.py 2006-08-02 18:09:19 UTC (rev 452) +++ branches/vsc-2.0/pyvscd/libvscmt/__init__.py 2006-08-02 20:41:45 UTC (rev 453) @@ -2,4 +2,4 @@ Multi-thread aware libvsc for pyvscd """ -__all__ = ["plugins","scan", "nessusmt", "plugins"] +__all__ = ["plugins","scan", "nessusmt", "plugins", "host"] Modified: branches/vsc-2.0/pyvscd/libvscmt/host.py =================================================================== --- branches/vsc-2.0/pyvscd/libvscmt/host.py 2006-08-02 18:09:19 UTC (rev 452) +++ branches/vsc-2.0/pyvscd/libvscmt/host.py 2006-08-02 20:41:45 UTC (rev 453) @@ -27,17 +27,6 @@ c.close() return rdict -def updateNetBios(db, scan_id, host_addr_map): - """updates the netbios and mac address information of - the hosts in the host_addr_map (e.g.,returned from the above - function) - - Keyword Arguments: - db - MySQLdb.connections.Connection - scan_id - integer - host_addr_map - dict, of ip:hostid pairs - """ - pass def saveNessusResult(db,scan_id,host_id,result): """Saves a result from the Nessus server @@ -110,3 +99,97 @@ else: return False +def updateNetBios(db, scan_id, host_addr_map): + """updates the netbios and mac address information of + the hosts in the host_addr_map (e.g.,returned from the above + function) + + Keyword Arguments: + db - MySQLdb.connections.Connection + scan_id - integer + host_addr_map - dict, of ip:hostid pairs + """ + pass + +def getNewHosts(db, limit=None): + """retrieves all new hosts in the database, that is hosts who have been recently + added and do not have a dns address cached. + + Keyword Arguments: + db - MySQLdb.connections.Connection + """ + c = db.cursor() + __sql__ = "select hostid, address from Host where dns_name = address " + if limit is not None: + __sql__ += "LIMIT 0,%s" % (limit) + + print "Executing query: %s" % (__sql__) + c.execute(__sql__) + hosts = {} + row = c.fetchone() + while row is not None: + (hostid, address) = row + hosts[hostid] = address + row = c.fetchone() + + c.close() + return hosts + +def getRevisitableHosts(db, limit=None): + """retrieves all hosts in the database that did not have DNS records when initially dug + these hosts exist in the database with the dns_name "address*" where address is the host's + ip + + Keyword Arguments: + db - MySQLdb.connections.Connection + limit - integer (optional) + """ + c = db.cursor() + __sql__ = "select hostid, address from Host where dns_name regexp '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\\\\*' " + if limit is not None: + __sql__ += "LIMIT 0,%s" % (limit) + + print "Executing query: %s" % (__sql__) + c.execute(__sql__) + hosts = {} + row = c.fetchone() + while row is not None: + (hostid, address) = row + hosts[hostid] = address + row = c.fetchone() + c.close() + return hosts + +def getAllHosts(db): + """retrieves all hosts in the database + + Keyword Arguments: + db - MySQLdb.connections.Connection + """ + + c = db.cursor() + __sql__ = "select hostid, address, dns_name from Host" + c.execute(__sql__) + hosts = {} + row = c.fetchone() + while row is not None: + (hostid, address, dns_name) = row + hosts[hostid] = (address, dns_name) + row = c.fetchone() + c.close() + return hosts + +def setHostName(db, hostid, dns_name): + """sets the given host's dns_name + + Keyword Arguments: + db - MySQLdb.connections.Connection + host_id - integer + dns_name - string + """ + c = db.cursor() + __sql__ = "update Host set dns_name=%s where hostid=%s" + c.execute(__sql__, (dns_name, hostid)) + c.close() + db.commit() + Added: branches/vsc-2.0/pyvscd/util/cachedns.py =================================================================== --- branches/vsc-2.0/pyvscd/util/cachedns.py (rev 0) +++ branches/vsc-2.0/pyvscd/util/cachedns.py 2006-08-02 20:41:45 UTC (rev 453) @@ -0,0 +1,133 @@ +#!/usr/bin/env python2.4 + +""" +Python script for importing plugins from the Nessus server. +""" + +import sys +sys.path.append('../') +from ConfigParser import ConfigParser +from optparse import OptionParser +from socket import error as socket_error, gethostbyaddr +import socket +import time +import logging +import MySQLdb +from libvscmt import host + +def __main__(): + usage = "usage: %prog [options] [hosts]" + parser = OptionParser(usage=usage) + parser.add_option('-d', '--debug', action='store_true', dest='debug',help='enable debugging') + parser.add_option('-v', '--verbose', action='store_true', dest='verbose',help='enable debugging') + parser.add_option('-c', '--config', dest='conf', help='pyvscd config file') + parser.add_option('-n', '--nocommit', action='store_true', dest='nocommit', help='do not actually update plugin data in table') + parser.add_option('-m', '--mode', dest='mode', help='DNS Cache mode: n|new - update new hosts; r|revisit - attempt to reload dns information for all unknown hosts; a|all - reload all DNS information') + parser.add_option('-l', '--limit', dest='limit', help='Limit for records to check on new and revist operations') + + (options, args) = parser.parse_args() + if not options.conf and not options.mode: + parser.print_help() + sys.exit(-1) + + logging.basicConfig() + if options.verbose: + logging.getLogger('').setLevel(logging.INFO) + + if options.debug: + #set root logger to DEBUG + logging.getLogger('').setLevel(logging.DEBUG) + #our logger should inherit the root logging level + log = logging.getLogger('cachedns') + + if options.debug: + log.debug('Debugging enabled') + elif options.verbose: + log.debug('Verbose enabled') + limit = 255 #a class C subnet seems reasonable + if options.limit: + limit = options.limit + log.debug("Record limit set to %s" % (limit)) + else: + log.debug("Record limit set to default %s" % (limit)) + + #read our config file + config = ConfigParser() + config.read(options.conf) + if not config.has_section('mysql') or not config.has_section('general'): + log.error("The config file must contain a [mysql] and [general] section!") + sys.exit(-1) + + dbhost = config.get('mysql','host') + dbuser = config.get('mysql','username') + dbpasswd = config.get('mysql','password') + dbname = config.get('mysql','database') + root_domain = config.get('general', 'root_domain') + + #open MySQL Connection + db = MySQLdb.connect(host=dbhost, user=dbuser, passwd=dbpasswd, db=dbname) + + if options.mode == 'n' or options.mode == 'new': + log.debug("Updating DNS for new hosts...") + new_hosts = host.getNewHosts(db, limit=limit) + if len(new_hosts) == 0: + log.debug("No new hosts to cache...") + sys.exit(0) + + for (hostid, address) in new_hosts.iteritems(): + log.debug("Grabbing DNS for %s - %s" % (hostid, address)) + hostname = get_hostname(address) + if hostname is not None: + log.debug("Loading hostname %s for hostid %s" % (hostname, hostid)) + #code to do that + host.setHostName(db, hostid, hostname) + else: + log.debug("Marking host %s as nameless" % (hostid)) + #code to do that + host.setHostName(db, hostid, "%s*" % (address)) + elif options.mode == 'r' or options.mode == 'revisit': + log.debug("Updating DNS for revisited hosts") + rev_hosts = host.getRevisitableHosts(db, limit=limit) + if len(rev_hosts) == 0: + log.debug("No old hosts to revisit") + + for (hostid, address) in rev_hosts.iteritems(): + log.debug("Grabbing DNS for %s - %s" % (hostid, address)) + hostname = get_hostname(address) + if hostname is not None: + log.debug("Loading hostname %s for hostid %s" % (hostname, hostid)) + host.setHostName(db, hostid, hostname) + else: + log.debug("Host still has no name, passing...") + elif options.mode == 'a' or options.mode == 'all': + log.debug("Updating DNS for all hosts") + all_hosts = host.getAllHosts(db) + for (hostid, data) in all_hosts.iteritems(): + (address, dns_name) = data + log.debug("Grabbing DNS for %s - %s" % (hostid, address)) + hostname = get_hostname(address) + if hostname is not None and hostname != dns_name: + log.debug("Loading hostname %s for hostid %s" % (hostname, hostid)) + host.setHostName(db, hostid, hostname) + else: + log.debug("Host has no new name, passing...") + else: + log.error("No such option %s" % (options.mode)) + sys.exit(1) + +def get_hostname(address): + log = logging.getLogger('cachedns.get_hostname') + try: + (hostname, aliaslist, ipaddr_list) = gethostbyaddr(address) + log.debug("DNS name is %s" % (hostname)) + log.debug("Alias list is : " + ", ".join(aliaslist)) + return hostname + except socket.herror: + log.debug("No address found for host.") + return None + + + +if __name__ == '__main__': + __main__() + Property changes on: branches/vsc-2.0/pyvscd/util/cachedns.py ___________________________________________________________________ Name: svn:executable + * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |