← 返回列表

使用codeshell进行代码安全评估

单站 代码审计SDL 发布 Tue, 07 Nov 2023 18:42:00 +0800 入库 2026-11-18T05:30:03

sanduo 2023-11-07 18:42 北京

使用codeshell进行代码安全评估

北京大学软件工程国家工程研究中心知识计算实验室联合四川天府银行AI实验室,正式开源70亿参数的代码大模型CodeShell,成为同等规模最强代码基座。与此同时,团队将软件开发代码助手的完整解决方案全部开源,人手一个本地化轻量化的智能代码助手的时代已经来临!

CodeShell代码:https://github.com/WisdomShell/codeshell

CodeShell基座模型:https://huggingface.co/WisdomShell/CodeShell-7B

代码助手VSCode插件:https://github.com/WisdomShell/codeshell-vscode

目前拥有以下优势:

安全狗,重点关注安全检测功能。codeshell号称可以检测代码中的潜在安全风险,如可能出现的SQL注入、跨站脚本攻击等,帮助排查安全性风险。对代码进行深入分析,检测潜在的错误、冗余代码和性能瓶颈,并为开发者提供相应的修复建议;基于代码逻辑,自动创建测试用例,以辅助进行代码测试和验证,确保代码的正确性和稳定性。

获取模型 我这里选取的是CodeShell-7B-Chat,如果资源不够,可以采用量化模型。

git clone https://huggingface.co/WisdomShell/CodeShell-7B-Chat
首先克隆项目

git clone https://github.com/WisdomShell/codeshell
cd codeshell
conda create -n codeshell python=3.10
conda activate codeshell
pip install requirements.txt

昨天作者遗漏requirements.txt,作者响应很及时,很快就补上了,如果需要评估web demo,还需要另外安装依赖

pip install demos/requirements_web_demo.txt
个人环境还缺乏部分依赖,手动安装就行,灵活处理。

pip install bitsandbytes``python web_demo.py --server-port=80 --server-name=0.0.0.0 -c /data/models/CodeShell-7B-Chat 
正常加载demo

注意:我这里使用显卡为3090ti,显存为24G,运行全量模型占用显存大约16G,仅供参考。

low_cpu_mem_usage=True or a device_map requires Accelerate: pip install accelerate
手动安装

换模型或者换显卡资源

prompt参考:

检查以下java代码,是否存在安全性问题,请给出优化建议:
评估代码片段为:

package com.suyu.secexample.rce.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@Controller
public class rcecontroller {
    @RequestMapping("/rce")
    public String input(){
        return "rce/rce";
    }
    @PostMapping("/rceoutput")
    public String index(@RequestParam("command") String command, Model model){
        if(command=="" | command==null){
            command= "whoami";
        }
        Process p = null;
        String result = null;
        try {
            p = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String s = null;
        while (true) {
            try {
                if (!((s = reader.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            result = s;
        }
        model.addAttribute("result",result);
        return "rce/rceoutput";
    }
}

结果如下:

占用资源:

遇到不准确及幻觉

vscode应用市场检索:CodeShell VSCode,直接安装即可,也可以自行编译安装。

使用vscode组件,有两种模式,一种使用CPU,采用llama_cpp量化的形式加载,速度稍微缓慢一些,另外一种是使用text-generation-inference(TGI)加载模型。详情详解项目README。插件配置如下:

注意:

1.正常配置地址和端口

2.这里只支持llama.cpp和tgi两种连接方式,目前(2023-11-07)不支持openai格式,需要手动修改插件

3.tokens根据实际情况修改

这里尝试编译llama_cpp_for_codeshell

git clone https://github.com/WisdomShell/llama_cpp_for_codeshell.git
cd llama_cpp_for_codeshell
make

运行:

./server -m ./models/codeshell-chat-q4_0.gguf --host 127.0.0.1 --port 8080
注意,这里使用的是量化模型,加载模型要注意。结果没什么影响,只是速度有些缓慢。

docker run --gpus 'all' --shm-size 1g -p 9090:80 -v /data/models:/data \
        --env LOG_LEVEL="info,text_generation_router=debug" \
        ghcr.nju.edu.cn/huggingface/text-generation-inference:1.0.3 \
        --model-id /data/CodeShell-7B-Chat --num-shard 1 \
        --max-total-tokens 5000 --max-input-length 4096 \
        --max-stop-sequences 12 --trust-remote-code

这里使用南大的ghcr镜像源,速度不错,推荐给大家。个人设备是24G显存,使用TGI加载模型会爆显存。

我这里由于操作系统的语言是英文,导致返回的结果为英文,codeshell是支持中文的。对于代码自动补全我就不演示了,各位自行摸索。

1、可以辅助代码审计人员分析代码,只能辅助,没当作SAST来使用,再说SAST也有大量误报,都需要人工来复核。

2、由于codeshell 上下文token限制,没法去分析业务系统,代码释义可能不准确,这些在使用的过程中需要注意。

3、codeshell是首个支持代码安全分析的大模型,相当不容易。

原文链接:https://mp.weixin.qq.com/s?__biz=MzI2NTExNzcxNQ==&mid=2247484221&idx=1&sn=879fc4c065637b1af2d9c46a81cc8f13