Beispiel #1
0
def _print_counts(dir):
    from pynasl.naslparse import naslparser
    
    logger.info("Counting started")
    
    files_with_cve = 0
    files_total = 0
    files_with_wrong_cve = 0
    for root,dirs,files in os.walk(dir):
        files_total += len(files)
        for name in files:
            if name.endswith('.nasl'):
                get_ref = GetCVERef()
                fullname = os.path.join(root, name)
                get_ref.visit(naslparser(fullname, True))
                # CAN - candidate
                if get_ref.cve_id and get_ref.cve_id.startswith(('"CVE', '"CAN')):
                    files_with_cve += 1
                elif get_ref.cve_id is not None:
                    logger.error("Strange CVE '%s' in file %s" % (get_ref.cve_id, name))
                    files_with_wrong_cve += 1
    
    logger.info("Counting ended")
    logger.info("Files with wrong CVE:%s" % files_with_wrong_cve)
    logger.info("Files with CVE:%s" % files_with_cve)
    logger.info("Total files:%s" % files_total)
Beispiel #2
0
def generate_graph(dir, script_name=None):
    
    from pynasl.naslparse import naslparser
    
    logger.info("Generating graph started")
    
    call_tree = CallGraph()
    total_files = 0
    for root,dirs,files in os.walk(dir):
        for name in files:
            if name.endswith('.inc') or (name.endswith('.nasl') and (script_name is None or script_name == name)):
                fullname = os.path.join(root, name)
                call_tree.set_caller_func(name)
                call_tree.set_file_name(name)
                
                call_tree.visit(naslparser(fullname, True))
                
                total_files += 1
                if total_files % 1000 == 0:
                    logger.info("Processed %s files" % total_files)
    
    logger.info("Generated graph with %s nodes and %s edges. Processed %s files" % 
                (call_tree.g.number_of_nodes(), call_tree.g.number_of_edges(), total_files))
    
    return call_tree.g
Beispiel #3
0
def _print_counts(dir):
    from pynasl.naslparse import naslparser

    logger.info("Counting started")

    files_with_cve = 0
    files_total = 0
    files_with_wrong_cve = 0
    for root, dirs, files in os.walk(dir):
        files_total += len(files)
        for name in files:
            if name.endswith('.nasl'):
                get_ref = GetCVERef()
                fullname = os.path.join(root, name)
                get_ref.visit(naslparser(fullname, True))
                # CAN - candidate
                if get_ref.cve_id and get_ref.cve_id.startswith(
                    ('"CVE', '"CAN')):
                    files_with_cve += 1
                elif get_ref.cve_id is not None:
                    logger.error("Strange CVE '%s' in file %s" %
                                 (get_ref.cve_id, name))
                    files_with_wrong_cve += 1

    logger.info("Counting ended")
    logger.info("Files with wrong CVE:%s" % files_with_wrong_cve)
    logger.info("Files with CVE:%s" % files_with_cve)
    logger.info("Total files:%s" % files_total)
Beispiel #4
0
def _log_family(plugins_dir, categorize_path=None):
    """logger script_family
    
    @param plugins_dir: string with path to directory with nasl scripts.
    @param categorize_path: string with path to directory
        to which nasl scripts will be categorized.
        Default value - None, that means not categorize nasl scripts.
    """
    from pynasl.naslparse import naslparser
    
    scripts_family = defaultdict(list)
    strange_family = []
    
    if categorize_path:
        if not os.path.exists(categorize_path):
            os.makedirs(categorize_path)
        else:
            shutil.rmtree(categorize_path)
        
    logger.info('Files processing started')
    total_files = 0
    for root, dirs, files in os.walk(plugins_dir):
        for name in files:
            if not name.endswith('.nasl'):
                continue
            
            family = FamilyGetter()
            full_path = os.path.join(root, name)
            family.visit(naslparser(full_path, True))
            
            if not family.family_name:
                strange_family.append(name)
            else:
                family_name = family.family_name[1:-1].replace(':','')
                scripts_family[family_name].append(name)
                
                if categorize_path:
                    dst = os.path.join(categorize_path, family_name)
                    try:
                        if not os.path.exists(dst):
                            os.makedirs(dst)
                        shutil.copy(full_path, dst)
                    except OSError, why:
                        logger.error(str(why))

            total_files += 1
            if total_files % 1000 == 0:
                logger.info("Processed %s files" % total_files)
Beispiel #5
0
def _log_family(plugins_dir, categorize_path=None):
    """logger script_family
    
    @param plugins_dir: string with path to directory with nasl scripts.
    @param categorize_path: string with path to directory
        to which nasl scripts will be categorized.
        Default value - None, that means not categorize nasl scripts.
    """
    from pynasl.naslparse import naslparser

    scripts_family = defaultdict(list)
    strange_family = []

    if categorize_path:
        if not os.path.exists(categorize_path):
            os.makedirs(categorize_path)
        else:
            shutil.rmtree(categorize_path)

    logger.info('Files processing started')
    total_files = 0
    for root, dirs, files in os.walk(plugins_dir):
        for name in files:
            if not name.endswith('.nasl'):
                continue

            family = FamilyGetter()
            full_path = os.path.join(root, name)
            family.visit(naslparser(full_path, True))

            if not family.family_name:
                strange_family.append(name)
            else:
                family_name = family.family_name[1:-1].replace(':', '')
                scripts_family[family_name].append(name)

                if categorize_path:
                    dst = os.path.join(categorize_path, family_name)
                    try:
                        if not os.path.exists(dst):
                            os.makedirs(dst)
                        shutil.copy(full_path, dst)
                    except OSError, why:
                        logger.error(str(why))

            total_files += 1
            if total_files % 1000 == 0:
                logger.info("Processed %s files" % total_files)
Beispiel #6
0
def create_statistic(plugins_dir):
    from pynasl.naslparse import naslparser
    
    stat = NaslStatistic()
    
    logger.info('Files processing started')
    total_files = 0
    for root,dirs,files in os.walk(plugins_dir):
        for name in files:
            if name.endswith(('.nasl', '.inc')):
                stat.preprocess_file(name)
                fullname = os.path.join(root, name)
                stat.visit(naslparser(fullname, True))

            total_files += 1
            if total_files % 1000 == 0:
                logger.info("Processed %s files" % total_files)
    logger.info('Files processing finished')
    
    stat.finalize_calculations()

    _write_detailed_statistic(stat)
    
    _write_main_statistic(stat, 'statistic.txt')
Beispiel #7
0
def create_statistic(plugins_dir):
    from pynasl.naslparse import naslparser

    stat = NaslStatistic()

    logger.info('Files processing started')
    total_files = 0
    for root, dirs, files in os.walk(plugins_dir):
        for name in files:
            if name.endswith(('.nasl', '.inc')):
                stat.preprocess_file(name)
                fullname = os.path.join(root, name)
                stat.visit(naslparser(fullname, True))

            total_files += 1
            if total_files % 1000 == 0:
                logger.info("Processed %s files" % total_files)
    logger.info('Files processing finished')

    stat.finalize_calculations()

    _write_detailed_statistic(stat)

    _write_main_statistic(stat, 'statistic.txt')
Beispiel #8
0
def ast2py_str(path):
    from pynasl.naslparse import naslparser

    ast_string = Translator()
    return ast_string.visit(naslparser(path, True))
Beispiel #9
0
def _print_AST(file_name):
    from pynasl.naslparse import naslparser
    
    ast_string = AST2String()
    print(ast_string.visit(naslparser(file_name, True)))
Beispiel #10
0
def ast2py_str(path):
    from pynasl.naslparse import naslparser
    
    ast_string = Translator()
    return ast_string.visit(naslparser(path, True))