#!/usr/bin/python3

import pymongo
import types
import json
import sys


PORT=27017
client = pymongo.MongoClient("localhost", PORT)
db = client.cuckoo_db
collection = db.malware_results


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("give a name of signature to get description and severity, if '-' it gives all signatures")
        exit(0)

    pattern = sys.argv[1]
    if pattern == "-":
        pattern = ""
    found_sigs = {}
    for js in collection.find():
        for sig in js.get("signatures", []):
            name = sig.get("name", "")

            if pattern in name and name not in found_sigs.keys():
                found_sigs[name] = {"description": sig.get("description", ""),
                                    "severity": sig.get("severity", ""),
                                    "occurrences": 1}
            elif pattern in name:
                found_sigs[name]["occurrences"] = found_sigs[name]["occurrences"] + 1

            
            
    for name in found_sigs.keys():
        print(f'{name}:\t{found_sigs[name].get("description", "")}\nseverity = {found_sigs[name].get("severity", "")}\noccurrences = {found_sigs[name].get("occurrences", "")}\n')

