Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
failed = set()
req = (select([self.tables.scan.id,
self.tables.script.name,
self.tables.script.port,
self.tables.script.output,
self.tables.script.data])
.select_from(join(join(self.tables.scan, self.tables.port),
self.tables.script))
.where(and_(self.tables.scan.schema_version == 14,
self.tables.script.name == "http-git")))
for rec in self.db.execute(req):
if rec.name in rec.data:
try:
data = xmlnmap.change_http_git(rec.data[rec.name])
except Exception:
utils.LOGGER.warning("Cannot migrate host %r", rec.id,
exc_info=True)
failed.add(rec.id)
else:
if data:
self.db.execute(
update(self.tables.script)
.where(and_(self.tables.script.port == rec.port,
self.tables.script.name == rec.name))
.values(data={rec.name: data})
)
self.db.execute(
update(self.tables.scan)
.where(and_(self.tables.scan.schema_version == 14,
self.tables.scan.id.notin_(failed)))
def _migrate_schema_13_14(self):
"""Converts a record from version 13 to version 14. Version 14 changes
the structured output for ssh-hostkey and ls scripts to prevent a same
field from having different data types.
"""
failed = set()
scripts = [
script_name
for script_name, alias in viewitems(xmlnmap.ALIASES_TABLE_ELEMS)
if alias == 'ls'
]
scripts.append('ssh-hostkey')
req = (select([self.tables.scan.id,
self.tables.script.name,
self.tables.script.port,
self.tables.script.output,
self.tables.script.data])
.select_from(join(join(self.tables.scan, self.tables.port),
self.tables.script))
.where(and_(self.tables.scan.schema_version == 13,
self.tables.script.name.in_(scripts))))
for rec in self.db.execute(req):
if rec.name in rec.data:
migr_func = (
xmlnmap.change_ssh_hostkey
"""
failed = set()
req = (select([self.tables.scan.id,
self.tables.script.name,
self.tables.script.port,
self.tables.script.output,
self.tables.script.data])
.select_from(join(join(self.tables.scan, self.tables.port),
self.tables.script))
.where(and_(self.tables.scan.schema_version == 11,
self.tables.script.name.in_(["fcrdns",
"rpcinfo"]))))
for rec in self.db.execute(req):
if rec.name in rec.data:
migr_func = {
'fcrdns': xmlnmap.change_fcrdns_migrate,
'rpcinfo': xmlnmap.change_rpcinfo,
}[rec.name]
try:
data = migr_func(rec.data[rec.name])
except Exception:
utils.LOGGER.warning("Cannot migrate host %r", rec.id,
exc_info=True)
failed.add(rec.id)
else:
if data:
self.db.execute(
update(self.tables.script)
.where(and_(self.tables.script.port == rec.port,
self.tables.script.name == rec.name))
.values(data={rec.name: data})
)
def __init__(self, host, dbname,
colname_scans="scans", colname_hosts="hosts",
colname_oldscans="archivesscans",
colname_oldhosts="archiveshosts",
**kargs):
MongoDB.__init__(self, host, dbname, **kargs)
DBNmap.__init__(self)
self.content_handler = xmlnmap.Nmap2Mongo
self.output_function = None
self.colname_scans = colname_scans
self.colname_hosts = colname_hosts
self.colname_oldscans = colname_oldscans
self.colname_oldhosts = colname_oldhosts
self.indexes = {
self.colname_hosts: [
([('scanid', pymongo.ASCENDING)], {}),
([('schema_version', pymongo.ASCENDING)], {}),
([('addr', pymongo.ASCENDING)], {}),
([('starttime', pymongo.ASCENDING)], {}),
([('endtime', pymongo.ASCENDING)], {}),
([('source', pymongo.ASCENDING)], {}),
([('categories', pymongo.ASCENDING)], {}),
([
('labels.group', pymongo.ASCENDING),
"""
failed = set()
req = (select([self.tables.scan.id,
self.tables.script.name,
self.tables.script.port,
self.tables.script.output,
self.tables.script.data])
.select_from(join(join(self.tables.scan, self.tables.port),
self.tables.script))
.where(and_(self.tables.scan.schema_version == 12,
self.tables.script.name.in_(["ms-sql-info",
"smb-enum-shares"]))))
for rec in self.db.execute(req):
if rec.name in rec.data:
migr_func = {
'ms-sql-info': xmlnmap.change_ms_sql_info,
'smb-enum-shares': xmlnmap.change_smb_enum_shares,
}[rec.name]
try:
data = migr_func(rec.data[rec.name])
except Exception:
utils.LOGGER.warning("Cannot migrate host %r", rec.id,
exc_info=True)
failed.add(rec.id)
else:
if data:
self.db.execute(
update(self.tables.script)
.where(and_(self.tables.script.port == rec.port,
self.tables.script.name == rec.name))
.values(data={rec.name: data})
)
rtt = Column(Float)
host = Column(String(255), index=True)
domains = Column(SQLARRAY(String(255)), index=True)
class _Scan(object):
id = Column(Integer, primary_key=True)
addr = Column(SQLINET, nullable=False)
# source = Column()
info = Column(SQLJSONB)
time_start = Column(DateTime)
time_stop = Column(DateTime)
state = Column(String(32))
state_reason = Column(String(32))
state_reason_ttl = Column(Integer)
schema_version = Column(Integer, default=xmlnmap.SCHEMA_VERSION)
# Nmap
class N_Association_Scan_ScanFile(Base, _Association_Scan_ScanFile):
__tablename__ = 'n_association_scan_scanfile'
__table_args__ = (
ForeignKeyConstraint(['scan'], ['n_scan.id'], ondelete='CASCADE'),
ForeignKeyConstraint(['scan_file'], ['n_scan_file.sha256'],
ondelete='CASCADE')
)
class N_ScanFile(Base, _ScanFile):
__tablename__ = "n_scan_file"
def searchscript(name=None, output=None, values=None):
"""Search a particular content in the scripts results.
"""
req = {}
if name is not None:
req['id'] = name
if output is not None:
req['output'] = output
if values is not None:
if name is None:
raise TypeError(".searchscript() needs a `name` arg "
"when using a `values` arg")
for field, value in values.iteritems():
req["%s.%s" % (xmlnmap.ALIASES_TABLE_ELEMS.get(name, name),
field)] = value
if not req:
return {"ports.scripts": {"$exists": True}}
if len(req) == 1:
field, value = req.items()[0]
return {"ports.scripts.%s" % field: value}
return {"ports.scripts": {"$elemMatch": req}}
structured output for http-headers script.
"""
failed = []
req = (select([self.tables.scan.id,
self.tables.script.port,
self.tables.script.output,
self.tables.script.data])
.select_from(join(join(self.tables.scan, self.tables.port),
self.tables.script))
.where(and_(self.tables.scan.schema_version == 8,
self.tables.script.name == "http-headers")))
for rec in self.db.execute(req):
if 'http-headers' not in rec.data:
try:
data = xmlnmap.add_http_headers_data({
'id': "http-headers",
'output': rec.output
})
except Exception:
utils.LOGGER.warning("Cannot migrate host %r", rec.id,
exc_info=True)
failed.append(rec.id)
else:
if data:
self.db.execute(
update(self.tables.script)
.where(and_(
self.tables.script.port == rec.port,
self.tables.script.name == "http-headers"
))
.values(data={"http-headers": data})
failed = set()
req = (select([self.tables.scan.id,
self.tables.script.name,
self.tables.script.port,
self.tables.script.output,
self.tables.script.data])
.select_from(join(join(self.tables.scan, self.tables.port),
self.tables.script))
.where(and_(self.tables.scan.schema_version == 12,
self.tables.script.name.in_(["ms-sql-info",
"smb-enum-shares"]))))
for rec in self.db.execute(req):
if rec.name in rec.data:
migr_func = {
'ms-sql-info': xmlnmap.change_ms_sql_info,
'smb-enum-shares': xmlnmap.change_smb_enum_shares,
}[rec.name]
try:
data = migr_func(rec.data[rec.name])
except Exception:
utils.LOGGER.warning("Cannot migrate host %r", rec.id,
exc_info=True)
failed.add(rec.id)
else:
if data:
self.db.execute(
update(self.tables.script)
.where(and_(self.tables.script.port == rec.port,
self.tables.script.name == rec.name))
.values(data={rec.name: data})
)
self.db.execute(
assert doc["schema_version"] == 2
update = {"$set": {"schema_version": 3}}
updated_ports = False
updated_scripts = False
migrate_scripts = set([
"afp-ls", "nfs-ls", "smb-ls", "ftp-anon", "http-ls"
])
for port in doc.get('ports', []):
for script in port.get('scripts', []):
if script['id'] in migrate_scripts:
if script['id'] in script:
script["ls"] = xmlnmap.change_ls(
script.pop(script['id']))
updated_ports = True
elif "ls" not in script:
data = xmlnmap.add_ls_data(script)
if data is not None:
script['ls'] = data
updated_ports = True
for script in doc.get('scripts', []):
if script['id'] in migrate_scripts:
data = xmlnmap.add_ls_data(script)
if data is not None:
script['ls'] = data
updated_scripts = True
if updated_ports:
update["$set"]["ports"] = doc['ports']
if updated_scripts:
update["$set"]["scripts"] = doc['scripts']
return update