Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
["--no-countports", "10", "100"], "-countports:10-100",
)
self.check_nmap_count_value(
"nmap_extended_eu_count",
ivre.db.db.nmap.searchcountry(['EU', 'CH', 'NO']),
["--country=EU,CH,NO"], "country:EU,CH,NO"
)
# Filters
addr = next(ivre.db.db.nmap.get(
ivre.db.db.nmap.flt_empty, fields=["addr"]
))['addr']
self.check_nmap_count_value(1, ivre.db.db.nmap.searchhost(addr),
['--host', ivre.utils.force_int2ip(addr)],
ivre.utils.force_int2ip(addr))
result = next(ivre.db.db.nmap.get(
ivre.db.db.nmap.searchhost(addr)
))
self.assertEqual(result['addr'], addr)
self.check_count_value_api(1, ivre.db.db.nmap.flt_and(
ivre.db.db.nmap.searchhost(addr),
ivre.db.db.nmap.searchhost(addr),
), database=ivre.db.db.nmap)
recid = ivre.db.db.nmap.getid(
next(ivre.db.db.nmap.get(ivre.db.db.nmap.flt_empty))
)
self.check_count_value_api(1, ivre.db.db.nmap.searchid(recid),
database=ivre.db.db.nmap)
self.assertIsNotNone(
ivre.db.db.nmap.getscan(
ivre.db.db.nmap.getscanids(
ivre.db.db.nmap.searchcountopenports(10, 100, neg=True),
["--no-countports", "10", "100"], "-countports:10-100",
)
self.check_nmap_count_value(
"nmap_extended_eu_count",
ivre.db.db.nmap.searchcountry(['EU', 'CH', 'NO']),
["--country=EU,CH,NO"], "country:EU,CH,NO"
)
# Filters
addr = next(ivre.db.db.nmap.get(
ivre.db.db.nmap.flt_empty, fields=["addr"]
))['addr']
self.check_nmap_count_value(1, ivre.db.db.nmap.searchhost(addr),
['--host', ivre.utils.force_int2ip(addr)],
ivre.utils.force_int2ip(addr))
result = next(ivre.db.db.nmap.get(
ivre.db.db.nmap.searchhost(addr)
))
self.assertEqual(result['addr'], addr)
self.check_count_value_api(1, ivre.db.db.nmap.flt_and(
ivre.db.db.nmap.searchhost(addr),
ivre.db.db.nmap.searchhost(addr),
), database=ivre.db.db.nmap)
recid = ivre.db.db.nmap.getid(
next(ivre.db.db.nmap.get(ivre.db.db.nmap.flt_empty))
)
self.check_count_value_api(1, ivre.db.db.nmap.searchid(recid),
database=ivre.db.db.nmap)
self.assertIsNotNone(
ivre.db.db.nmap.getscan(
firstseen = r['firstseen']
lastseen = r['lastseen']
if 'addr' in r and r['addr']:
if r['source'].startswith('PTR-'):
print('%s PTR %s (%s, %s time%s, %s - %s)' % (
utils.force_int2ip(r['addr']),
r['value'], r['source'][4:], r['count'],
r['count'] > 1 and 's' or '',
firstseen,
lastseen,
))
elif r['source'].startswith('A-') or r['source'].startswith('AAAA-'):
print('%s %s %s (%s, %s time%s, %s - %s)' % (
r['value'],
r['source'].split('-', 1)[0],
utils.force_int2ip(r['addr']),
':'.join(r['source'].split('-')[1:]),
r['count'],
r['count'] > 1 and 's' or '',
firstseen,
lastseen,
))
else:
utils.LOGGER.warning("Cannot display record %r", r)
else:
if r['source'].split('-')[0] in ['CNAME', 'NS', 'MX']:
print('%s %s %s (%s, %s time%s, %s - %s)' % (
r['value'],
r['source'].split('-', 1)[0],
r['targetval'],
':'.join(r['source'].split('-')[1:]),
r['count'],
def _insert_or_update(self, timestamp, values, lastseen=None):
stmt = insert(self.tables.passive)\
.values(dict(values, addr=utils.force_int2ip(values['addr'])))
try:
self.db.execute(stmt)
except IntegrityError:
whereclause = and_(
self.tables.passive.addr == values['addr'],
self.tables.passive.sensor == values['sensor'],
self.tables.passive.recontype == values['recontype'],
self.tables.passive.source == values['source'],
self.tables.passive.value == values['value'],
self.tables.passive.targetval == values['targetval'],
self.tables.passive.info == values['info'],
self.tables.passive.port == values['port']
)
upsert = {
'firstseen': func.least(
self.tables.passive.firstseen,
def _disp_recs_tail(flt, field, nbr):
recs = list(db.passive.get(
flt, sort=[(field, -1)], limit=nbr))
recs.reverse()
for r in recs:
if 'addr' in r:
print(utils.force_int2ip(r['addr']), end=' ')
else:
print(r['targetval'], end=' ')
disp_rec(r)
def _disp_recs_tailf(flt, field):
# 1. init
firstrecs = list(db.passive.get(
flt, sort=[(field, -1)], limit=10))
firstrecs.reverse()
# in case we don't have (yet) records matching our criteria
r = {'firstseen': 0, 'lastseen': 0}
for r in firstrecs:
if 'addr' in r:
print(utils.force_int2ip(r['addr']), end=' ')
else:
print(r['targetval'], end=' ')
disp_rec(r)
sys.stdout.flush()
# 2. loop
try:
while True:
prevtime = r[field]
time.sleep(1)
for r in db.passive.get(
db.passive.flt_and(
baseflt,
db.passive.searchnewer(prevtime,
new=field == 'firstseen'),
),
sort=[(field, 1)]):
rec[field] = int(utils.datetime2timestamp(rec[field]))
for port in rec.get('ports', []):
if 'screendata' in port:
port['screendata'] = utils.encode_b64(port['screendata'])
for script in port.get('scripts', []):
if "masscan" in script:
try:
del script['masscan']['raw']
except KeyError:
pass
if not flt_params.ipsasnumbers:
if 'traces' in rec:
for trace in rec['traces']:
trace['hops'].sort(key=lambda x: x['ttl'])
for hop in trace['hops']:
hop['ipaddr'] = utils.force_int2ip(hop['ipaddr'])
addresses = rec.get('addresses', {}).get('mac')
if addresses:
newaddresses = []
for addr in addresses:
manuf = utils.mac2manuf(addr)
if manuf and manuf[0]:
newaddresses.append({'addr': addr, 'manuf': manuf[0]})
else:
newaddresses.append({'addr': addr})
rec['addresses']['mac'] = newaddresses
yield "%s\t%s" % ('' if i == 0 else ',\n',
json.dumps(rec, default=utils.serialize))
check = subdb.cmp_schema_version_host(rec)
if check:
version_mismatch[check] = version_mismatch.get(check, 0) + 1
# XXX-WORKAROUND-PGSQL
def disp_recs_std(flt, sort, limit, skip):
old_addr = None
sort = sort or [('addr', 1), ('port', 1), ('recontype', 1), ('source', 1)]
for rec in db.passive.get(flt, sort=sort, limit=limit, skip=skip):
if 'addr' not in rec or not rec['addr']:
continue
if old_addr != rec['addr']:
if old_addr is not None:
print()
old_addr = rec['addr']
print(utils.force_int2ip(old_addr))
ipinfo = db.data.infos_byip(old_addr)
if ipinfo:
if 'address_type' in ipinfo:
print('\t', end=' ')
print(ipinfo['address_type'], end=' ')
print()
if 'country_code' in ipinfo:
print('\t', end=' ')
print(ipinfo['country_code'], end=' ')
if 'country_name' in ipinfo:
cname = ipinfo['country_name']
else:
try:
cname = db.data.country_name_by_code(
ipinfo['country_code']
)
def ip2internal(addr):
return utils.force_int2ip(addr)
if manuf and manuf[0]:
out.write(' (%s)' % manuf[0])
out.write('\n')
if showtraceroute and record.get('traces'):
for trace in record['traces']:
proto = trace['protocol']
if proto in ['tcp', 'udp']:
proto += '/%d' % trace['port']
out.write('\tTraceroute (using %s)\n' % proto)
hops = trace['hops']
hops.sort(key=lambda hop: hop['ttl'])
for hop in hops:
out.write(
'\t\t%3s %15s %7s\n' % (
hop['ttl'],
utils.force_int2ip(hop['ipaddr']),
hop['rtt'],
)
)
if showos and record.get('os', {}).get('osclass'):
osclasses = record['os']['osclass']
maxacc = str(max(int(x['accuracy']) for x in osclasses))
osclasses = [osclass for osclass in osclasses
if osclass['accuracy'] == maxacc]
out.write('\tOS fingerprint\n')
for osclass in osclasses:
out.write(
'\t\t%(osfamily)s / %(type)s / %(vendor)s / '
'accuracy = %(accuracy)s\n' % osclass)