
该工具是一个静态代码安全扫描工具,主要用于检测PHP代码中的安全漏洞。它通过分析代码中的特定模式(如硬编码凭证、高熵字符串、远程代码执行漏洞等)来识别潜在的安全问题,并格式化输出检测结果。
argparse库解析用户输入的参数。漏洞检测模块:
regex_indicators = '\\((.*?)(\\$_GET\\[.*?\\]|\\$_FILES\\[.*?\\]|\\$_POST\\[.*?\\]|\\$_REQUEST\\[.*?\\]|\\$_COOKIES\\[.*?\\]|\\$_SESSION\\[.*?\\]|\\$(?!this|e-)[a-zA-Z0-9_]*)(.*?)\\)' payloads = [ ["eval", "Remote Command Execution", ["escapeshellarg", "escapeshellcmd"]], ["include", "File Inclusion", []], ["mysql_query", "SQL Injection", ["mysql_real_escape_string"]], ["echo", "Cross Site Scripting", ["htmlentities", "htmlspecialchars"]], ] 通过正则表达式和预定义的漏洞规则,检测PHP代码中的安全漏洞。 结果展示模块:
def display(path, payload, vulnerability, line, declaration_text, declaration_line, colored, occurrence, plain): header = "{}Potential vulnerability found : {}{}{}".format('' if plain else '\033[1m', '' if plain else '\033[92m', payload[1], '' if plain else '\033[0m') line = "n°{}{}{} in {}".format('' if plain else '\033[92m', line, '' if plain else '\033[0m', path) vuln = nth_replace("".join(vulnerability), colored, "{}".format('' if plain else '\033[92m') + colored + "{}".format('' if plain else '\033[0m'), occurrence) vuln = "{}({})".format(payload[0], vuln) print("-" * (int(columns) - 1)) print("Name \t{}".format(header)) print("-" * (int(columns) - 1)) print("{}Line {} {}".format('' if plain else '\033[1m', '' if plain else '\033[0m', line)) print("{}Code {} {}".format('' if plain else '\033[1m', '' if plain else '\033[0m', vuln)) 格式化输出漏洞信息,包括漏洞类型、代码行号、代码片段等。 命令行接口模块:
parser = argparse.ArgumentParser() parser.add_argument('--dir', action='store', dest='dir', help="Directory to analyse") parser.add_argument('--plain', action='store_true', dest='plain', help="No color in output") results = parser.parse_args() if results.dir is not None: sys.setrecursionlimit(1000000) if os.path.isfile(results.dir): analysis(results.dir, results.plain) else: recursive(results.dir, 0, results.plain) scanresults() else: parser.print_help() 解析用户输入的参数,启动工具并输出结果。 辅助功能模块:
def nth_replace(string, old, new, n): if string.count(old) >= n: left_join = old right_join = old groups = string.split(old) nth_split = [left_join.join(groups[:n]), right_join.join(groups[n:])] return new.join(nth_split) return string.replace(old, new) 提供字符串替换、漏洞行号查找、代码清理等功能。 该工具通过正则表达式匹配、Shannon熵计算、递归文件扫描等技术,实现了对PHP代码中多种安全漏洞的检测。其核心在于漏洞规则定义和结果展示,适用于代码审计和安全测试场景。 github链接地址:https://github.com/swisskyrepo/Vulny-Code-Static-Analysis.git