🚀 CodeAstra-7b:开源的先进漏洞检测模型
CodeAstra-7b 是一款专门针对多种编程语言进行漏洞检测的微调语言模型。它基于强大的 Mistral-7B-Instruct-v0.2 模型,经过专门训练,能够在广泛的流行编程语言中识别潜在的安全漏洞,为开发者、安全研究人员和代码审计人员提供了强大的代码安全检测工具。
🚀 快速开始
环境准备
你可以使用以下 Python 代码来加载 CodeAstra-7b 模型并进行代码分析:
import torch
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
peft_model_id = "rootxhacker/CodeAstra-7B"
config = PeftConfig.from_pretrained(peft_model_id)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_4bit=True, device_map='auto')
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
model = PeftModel.from_pretrained(model, peft_model_id)
def get_completion(query, model, tokenizer):
inputs = tokenizer(query, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
code_to_analyze = """
def user_input():
name = input("Enter your name: ")
print("Hello, " + name + "!")
user_input()
"""
query = f"Analyze this code for vulnerabilities and quality issues:\n{code_to_analyze}"
result = get_completion(query, model, tokenizer)
print(result)
这段脚本会加载 CodeAstra-7b 模型和分词器,并提供一个生成完成结果的函数。你可以使用这个设置来分析代码中的漏洞和质量问题。
✨ 主要特性
- 多语言支持:能够检测 Go、Python、C、C++、Fortran、Ruby、Java、Kotlin、C#、PHP、Swift、JavaScript 和 TypeScript 等多种语言的漏洞。
- 先进性能:在漏洞检测任务中取得了前沿的成果。
- 自定义数据集:基于专门为全面漏洞检测而精心策划的专有数据集进行训练。
- 大规模训练:利用 A100 GPU 进行高效且强大的训练。
📊 性能对比
CodeAstra-7b 在漏洞检测准确率方面显著优于现有模型。以下是对比表格:
模型 |
准确率 (%) |
gpt4o |
88.78 |
CodeAstra-7b |
83.00 |
codebert-base-finetuned-detect-insecure-code |
65.30 |
CodeBERT |
62.08 |
RoBERTa |
61.05 |
TextCNN |
60.69 |
BiLSTM |
59.37 |
从表格中可以看出,CodeAstra-7b 达到了令人印象深刻的 83% 的准确率,在漏洞检测领域大幅超越了其他先进模型。
📖 详细文档
预期用途
CodeAstra-7b 旨在协助开发者、安全研究人员和代码审计人员识别源代码中潜在的安全漏洞。它可以集成到开发工作流程、代码审查过程中,也可以作为独立的代码分析工具使用。
多种漏洞场景
需要注意的是,虽然 CodeAstra-7b 在大多数情况下能够出色地发现安全问题,但当同一代码片段中存在两到三个漏洞时,其性能可能会有所不同,模型可能无法始终正确识别所有漏洞。用户应意识到这一局限性,并将该模型作为更广泛、多方面安全审查过程的一部分。
训练过程
CodeAstra-7b 是从 Mistral-7B-Instruct-v0.2 基础模型进行微调而来,使用了专门为多种编程语言的漏洞检测而编译的自定义数据集。训练过程利用了 A100 GPU 以确保最佳性能和效率。
🔧 技术细节
测试设备
作者使用了如 Cvefix、YesWeHack 漏洞代码库、使用大语言模型合成生成的代码以及 OWASP Juice Shop 源代码等数据集中的代码片段对 CodeAstra-7b 进行了测试,并将这些易受攻击的脚本与 GPT4、GPT4o 等大语言模型进行了评估对比。
📄 许可证
CodeAstra-7b 采用 Apache 许可证 2.0 版发布。
Copyright 2024 [Harish Santhanalakshmi Ganesan]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
🙏 致谢
我们要感谢 Mistral AI 团队提供了优秀的基础模型,它是 CodeAstra-7b 的基石。
⚠️ 重要提示
- 该模型可能无法捕获所有漏洞或代码质量问题,应作为全面安全和代码审查策略的一部分使用。
- 当同一代码片段中存在两到三个漏洞时,模型可能无法正确识别所有漏洞。
- 可能会出现误报情况,结果应由人类专家进行验证。
- 模型的性能可能会因被分析代码的复杂性和上下文而有所不同。
- CodeAstra 的性能取决于输入代码片段的长度。
📖 引用
如果你在研究或项目中使用了 CodeAstra-7b,请按以下方式引用:
@software{CodeAstra-7b,
author = {Harish Santhanalakshmi Ganesan},
title = {CodeAstra-7b: State-of-the-Art Vulnerability Detection Model},
year = {2024},
howpublished = {\url{https://huggingface.co/rootxhacker/CodeAstra-7b}}
}