cpe-guesser/lib/cpeguesser.py

39 lines
912 B
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import valkey
from dynaconf import Dynaconf
# Configuration
2024-04-05 14:03:23 +00:00
settings = Dynaconf(settings_files=["../config/settings.yaml"])
2021-10-19 16:30:29 +00:00
class CPEGuesser:
def __init__(self):
self.rdb = valkey.Valkey(
host=settings.valkey.host,
port=settings.valkey.port,
2024-04-05 14:03:23 +00:00
db=8,
decode_responses=True,
)
def guessCpe(self, words):
2021-10-19 16:30:29 +00:00
k = []
for keyword in words:
k.append(f"w:{keyword.lower()}")
maxinter = len(k)
cpes = []
for x in reversed(range(maxinter)):
ret = self.rdb.sinter(k[x])
cpes.append(list(ret))
result = set(cpes[0]).intersection(*cpes)
ranked = []
for cpe in result:
2024-04-05 14:03:23 +00:00
rank = self.rdb.zrank("rank:cpe", cpe)
ranked.append((rank, cpe))
return sorted(ranked, reverse=True)