Commit 1cf745a9 authored by Pieter-Jan Moreels's avatar Pieter-Jan Moreels
Browse files

update black-/whitelist import & export

parent 0915384d
Loading
Loading
Loading
Loading
+23 −24
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import sys
runPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(runPath, ".."))

import json
import re

from lib.Toolkit import toStringFormattedCPE
@@ -109,39 +110,37 @@ class CPEList:
        count = 0
        # read each line from the import file and regex them to a cpe format
        try:
            for line in importFile:
            for line in json.load(importFile):
                try:
                    cpe, cpeType=line.strip().split("|")
                    if self.insert(cpe, cpeType):
                    t = line['type']
                    if t not in ['cpe', 'targetsoftware', 'targethardware']:
                      continue
                    cpe = line['id']
                    if 'comments' in line:
                        cpe += "#" + "#".join(line['comments'])
                    if self.insert(cpe, t):
                        count += 1
                except:
                    continue
            if self.args.v:
                print("%s products added to the list"%(count))
        except IOError:
            print('Could not open the file')
            print('The list is corrupted!')
            sys.exit()

    # export a file that represents the cpe list
    def exportList(self, exportFile):
        count = 0
        # check if file exists already
        if not os.path.exists(exportFile) or self.args.f:
    def exportList(self, exportFile=None):
        listed = getattr(db, "get"+self.collection)()
        output = json.dumps(listed, sort_keys=True, indent=2)
        if exportFile == None:
            return output
        else:
            if not os.path.exists(exportFile) or self.args.f:
                export = open(exportFile, 'w')
            for listedID in listed:
                count += 1
                commentString = ""
                # check if there are comments
                if 'comments' in listedID:
                    comments = listedID['comments']
                    # separate the comments
                    for comment in comments:
                        commentString = commentString + '#' + comment
                export.write(listedID['id'] + commentString + "|" + listedID["type"] + '\n')
                export.write(output)
                export.close()
                if self.args.v:
                print("%s listed items exported"%(count))
                    print("%s listed items exported"%(len(listed)))
            else:
                print("file already exists")

+3 −3
Original line number Diff line number Diff line
@@ -39,12 +39,12 @@ collection = "blacklist"
# Functions
def importBlacklist(importFile):
    oList = CPEList(collection, args)
    oList.importList(importFile)
    return oList.importList(importFile)


def exportBlacklist(exportFile):
def exportBlacklist(exportFile=None):
    oList = CPEList(collection, args)
    oList.exportList(exportFile)
    return oList.exportList(exportFile)


def dropBlacklist():
+3 −3
Original line number Diff line number Diff line
@@ -38,12 +38,12 @@ collection = "whitelist"

def importWhitelist(importFile):
    oList = CPEList(collection, args)
    oList.importList(importFile)
    return oList.importList(importFile)


def exportWhitelist(exportFile):
def exportWhitelist(exportFile=None):
    oList = CPEList(collection, args)
    oList.exportList(exportFile)
    return oList.exportList(exportFile)


def dropWhitelist():