🚀 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}}
}