#!/usr/bin/env python

import os
import sys
from os.path import abspath, basename, dirname, exists, join
from glob import glob
from subprocess import call

# path
OPT_WKS                 = abspath(dirname(__file__))

OPT_DIR_VTDOC           = join(OPT_WKS, "vtdoc", "analyze")

# enums
OPT_ENUM_PLAT           = ["mac", "win"]
OPT_ENUN_TAGS           = ["10", "11", "15", "dc"]

# commands
OPT_CMD_BOX             = join(OPT_WKS, "box.py")

# consts
OPT_CVE_LIMIT           = 10

# main
if __name__ == "__main__":
    lst = glob(join(OPT_DIR_VTDOC, "exploit", "cve-2016-*"))
    lst = sorted(lst, reverse = True)

    cves = dict()
    for fn in lst:
        c = basename(fn)

        with open(fn, "r") as f:
            content = f.readlines()

        dist = dict()
        for l in content:
            toks = l.strip().split(",")

            if toks[1] not in dist:
                dist[toks[1]] = []

            if len(dist[toks[1]]) >= OPT_CVE_LIMIT:
                continue

            dist[toks[1]].append(toks[0])

        cves[c] = []
        for k in dist:
            cves[c].extend(dist[k])

    for c in cves:
        samples = cves[c]
        for s in samples:
            for t in OPT_ENUN_TAGS:
                for p in OPT_ENUM_PLAT:
                    cmd = ["python", OPT_CMD_BOX, p, t, s]
                    call(cmd)
