2021-09-18 11:32:31 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import redis
|
2023-07-11 07:09:16 +00:00
|
|
|
from dynaconf import Dynaconf
|
2021-09-18 11:32:31 +00:00
|
|
|
|
2023-07-11 07:09:16 +00:00
|
|
|
# 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:
|
2021-09-18 11:32:31 +00:00
|
|
|
def __init__(self):
|
2024-04-05 14:03:23 +00:00
|
|
|
self.rdb = redis.Redis(
|
|
|
|
host=settings.redis.host,
|
|
|
|
port=settings.redis.port,
|
|
|
|
db=8,
|
|
|
|
decode_responses=True,
|
|
|
|
)
|
2021-09-18 11:32:31 +00:00
|
|
|
|
|
|
|
def guessCpe(self, words):
|
2021-10-19 16:30:29 +00:00
|
|
|
k = []
|
2021-09-18 11:32:31 +00:00
|
|
|
for keyword in words:
|
2021-09-22 05:37:42 +00:00
|
|
|
k.append(f"w:{keyword.lower()}")
|
2021-09-18 11:32:31 +00:00
|
|
|
|
|
|
|
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)
|
2021-09-18 11:32:31 +00:00
|
|
|
ranked.append((rank, cpe))
|
|
|
|
|
|
|
|
return sorted(ranked)
|