#!/usr/bin/env python

import json
import os
import csv
import re

def get_all_datafile():
    json_list = []
    for dirname,folder,files in os.walk("."):
        for filename in files:
            if filename.endswith(".json"):
                json_list.append(filename)
    return json_list

def load(json_file):
    print(json_file)
    bug_list = []
    unnormal_list = []
    with open(json_file) as json_data:  
        data = json.load(json_data)
    #for key in data.keys():
    #    print(key)
    for items in data["CVE_Items"]:
        cve_info = items["cve"]
        #print(cve_info["CVE_data_meta"]["ID"])
        cve_id = cve_info["CVE_data_meta"]["ID"]

        kernel_flag = False
        # check whether it is Linux Kernel Vulnerability
        for vendor in cve_info["affects"]["vendor"]["vendor_data"]:
            if vendor["vendor_name"] != "linux":
                continue
            for product in vendor["product"]["product_data"]:
                #print(product["product_name"])
                if product["product_name"] == "linux_kernel":
                    kernel_flag = True
        if not kernel_flag:
            continue

        cwe_list = []
        description_list = []
        # check the CWE number
        overflow_flag = False
        pattern_cwe = ["CWE-121","CWE-122"]
        for cwe in cve_info["problemtype"]["problemtype_data"]:
            for dp in cwe["description"]:
                if dp["value"] != "NVD-CWE-Other" and dp["value"].find("CWE")!=-1 :
                    cwe_list.append(dp["value"])
                if dp["value"] in pattern_cwe:
                    overflow_flag = True
        # if not ,check the description
        pattern_description = ["uffer overflow"]
        if not overflow_flag:
            pattern = re.compile(r'(?:CWE-)\d+')
            for description_data in cve_info["description"]["description_data"]:
                des_CWEs = pattern.findall(description_data["value"])
                if des_CWEs:
                    for des_cwe in des_CWEs:
                        cwe_list.append(des_cwe)
                for target_description in pattern_description:
                    if description_data["value"].find(target_description) != -1:
                        # NOTE! just possible overflows
                        overflow_flag = True
                        description_list.append(description_data["value"])
                        break
        
        if not overflow_flag:
            continue
        
        # check if is possible CWE_id
        possible_pattern_cwe = ["CWE-787","CWE-788","CWE-119","CWE-118","CWE-20","CWE-19","CWE-18","CWE-699","CWE-693","CWE-1019","CWE-1000","CWE-1008","CWE-254","CWE-975"]
        possible_cwe_flag = False
        for cwe_id in cwe_list:
            if cwe_id in possible_pattern_cwe:
                possible_cwe_flag = True
                break
        if not possible_cwe_flag:
            unnormal_list.append([cve_id,cwe_list,description_list])
            print ("unnormal :",cve_id,cwe_list)

        bug_list.append([cve_id,cwe_list,description_list])

    print json_file[11:15],len(bug_list)
    return bug_list,unnormal_list

def write_bug(file_name,bug_list):
    for bug in bug_list:
        cve_id = bug[0]
        cve_row = []
        cve_row.append(cve_id)
        for cwe in bug[1]:
            cve_row.append(cwe)
        if bug[2]:
            for des in bug[2]:
                cve_row.append(des)
        csv.writer(file_name).writerow(cve_row)
    return 0

if __name__ == "__main__":
    json_files = get_all_datafile()
    
    # json_files = ["nvdcve-1.0-2018.json"]
    # print(json_files)
    print("==kernel overflow bugs==")
    res = open("kernel_overflow.csv","w")
    unnormal = open("unnormal_k_overflow.csv","w")
    for json_file in json_files:
        res_list,unnormal_list = load(json_file)
        # print res_list
        write_bug(res,res_list)
        write_bug(unnormal,unnormal_list)
    res.close()
    unnormal.close()

#!/usr/bin/env python

import json
import os
import csv
import re

def get_all_datafile():
    json_list = []
    for dirname,folder,files in os.walk("."):
        for filename in files:
            if filename.endswith(".json"):
                json_list.append(filename)
    return json_list

def load(json_file):
    print(json_file)
    bug_list = []
    unnormal_list = []
    with open(json_file) as json_data:  
        data = json.load(json_data)
    #for key in data.keys():
    #    print(key)
    for items in data["CVE_Items"]:
        cve_info = items["cve"]
        #print(cve_info["CVE_data_meta"]["ID"])
        cve_id = cve_info["CVE_data_meta"]["ID"]

        kernel_flag = False
        # check whether it is Linux Kernel Vulnerability
        for vendor in cve_info["affects"]["vendor"]["vendor_data"]:
            if vendor["vendor_name"] != "linux":
                continue
            for product in vendor["product"]["product_data"]:
                #print(product["product_name"])
                if product["product_name"] == "linux_kernel":
                    kernel_flag = True
        if not kernel_flag:
            continue

        cwe_list = []
        description_list = []
        # check the CWE number
        overflow_flag = False
        pattern_cwe = ["CWE-121","CWE-122"]
        for cwe in cve_info["problemtype"]["problemtype_data"]:
            for dp in cwe["description"]:
                if dp["value"] != "NVD-CWE-Other" and dp["value"].find("CWE")!=-1 :
                    cwe_list.append(dp["value"])
                if dp["value"] in pattern_cwe:
                    overflow_flag = True
        # if not ,check the description
        pattern_description = ["uffer overflow"]
        if not overflow_flag:
            pattern = re.compile(r'(?:CWE-)\d+')
            for description_data in cve_info["description"]["description_data"]:
                des_CWEs = pattern.findall(description_data["value"])
                if des_CWEs:
                    for des_cwe in des_CWEs:
                        cwe_list.append(des_cwe)
                for target_description in pattern_description:
                    if description_data["value"].find(target_description) != -1:
                        # NOTE! just possible overflows
                        overflow_flag = True
                        description_list.append(description_data["value"])
                        break
        
        if not overflow_flag:
            continue
        
        # check if is possible CWE_id
        possible_pattern_cwe = ["CWE-787","CWE-788","CWE-119","CWE-118","CWE-20","CWE-19","CWE-18","CWE-699","CWE-693","CWE-1019","CWE-1000","CWE-1008","CWE-254","CWE-975"]
        possible_cwe_flag = False
        for cwe_id in cwe_list:
            if cwe_id in possible_pattern_cwe:
                possible_cwe_flag = True
                break
        if not possible_cwe_flag:
            unnormal_list.append([cve_id,cwe_list,description_list])
            print ("unnormal :",cve_id,cwe_list)

        bug_list.append([cve_id,cwe_list,description_list])

    print json_file[11:15],len(bug_list)
    return bug_list,unnormal_list

def write_bug(file_name,bug_list):
    for bug in bug_list:
        cve_id = bug[0]
        cve_row = []
        cve_row.append(cve_id)
        for cwe in bug[1]:
            cve_row.append(cwe)
        if bug[2]:
            for des in bug[2]:
                cve_row.append(des)
        csv.writer(file_name).writerow(cve_row)
    return 0

if __name__ == "__main__":
    json_files = get_all_datafile()
    
    # json_files = ["nvdcve-1.0-2018.json"]
    # print(json_files)
    print("==kernel overflow bugs==")
    res = open("kernel_overflow.csv","w")
    unnormal = open("unnormal_k_overflow.csv","w")
    for json_file in json_files:
        res_list,unnormal_list = load(json_file)
        # print res_list
        write_bug(res,res_list)
        write_bug(unnormal,unnormal_list)
    res.close()
    unnormal.close()

#!/usr/bin/env python #for key in data.keys(): #    print(key) #print(cve_info["CVE_data_meta"]["ID"]) # check whether it is Linux Kernel Vulnerability #print(product["product_name"]) # check the CWE number # if not ,check the description # NOTE! just possible overflows # check if is possible CWE_id # json_files = ["nvdcve-1.0-2018.json"] # print(json_files) # print res_list