DependencyCheck+Jenkins扫描JAVA第三方依赖(CVE)漏洞_dependency-check

@XmlElement(name = “source”)
public String source;
@XmlElement(name = “severity”)
public String severity;
@XmlElement(name = “vuln-type”)
public String vulnType;
@XmlElement(name = “vuln-descript”)
public String description;
@XmlElement(name = “cve-id”)
public String cveId;
@XmlElement(name = “bugtraq-id”)
public String bugtraqId;
@XmlElement(name = “vuln-solution”)
public String solution;
}

三、使用DependencyCheck扫描

/root/dependency-check/bin/dependency-check.sh --project “test-服务端第三方依赖CVE漏洞扫描报告” --scan “**/*.jar” -n -f JSON -o “/var/www/html/test-服务端第三方依赖CVE漏洞扫描报告.json”

四、输出自定义扫描报告

1、使用pot-tl自定义word模板

2、解析json生成word报告

将上面扫描的“test-服务端第三方依赖CVE漏洞扫描报告.json”数据解析出来,填充到我们的自定义模板中,最终生成我们的word报告。

/**

  • 根据json报告解析漏洞信息并入库:方便后续直接构建word和excel
  • @param name 报告名称
  • @param sourePath json报告路径
  • @param reportPath word报告路径
  • @param gitHttpUrl git的http地址
  • @param gitBranch git分支
  • @return
  • @throws IOException
    */
    @GetMapping
    public String vulScanReport(String name, String sourePath,String reportPath,String gitHttpUrl,String gitBranch) throws IOException {
    //解析扫描的json报告,并将数据写入数据库
    int num = cnnvdService.analysisCveJsonv2(name,sourePath, gitHttpUrl, gitBranch);
    if(num>0){
    //读取扫描的漏洞数据,填充自定义模板,生成word报告
    String fileName = cnnvdService.createWord(name,reportPath,gitHttpUrl,gitBranch);
    return fileName;
    }else{
    return “”;
    }
    }

package com.yunhuang.autosafe.common.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.util.PoitlIOUtils;
import com.yunhuang.autosafe.common.entity.;
import com.yunhuang.autosafe.common.mapper.CnnvdMapper;
import com.yunhuang.autosafe.common.service.
;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.;
import java.util.
;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CnnvdServiceImpl extends ServiceImpl<CnnvdMapper, Cnnvd> implements CnnvdService {

@Autowired
private NvdMyService nvdMyService;
@Autowired
private CveService cveService;
@Autowired
private ScanDependencieService dependencieService;
@Autowired
private ScanVulnerabilitieService vulnerabilitieService;
@Resource
private RedisTemplate redisTemplate;

private final static String REDIS_NVD_KEY = “cve_data”;

/**

  • 根据json报告解析漏洞信息并入库:方便后续直接构建word和excel
  • @param name 报告名称
  • @param sourePath json报告路径
  • @param gitHttpUrl git的http地址
  • @param gitBranch git分支
  • @return
  • @throws IOException
    */
    @Override
    public Integer analysisCveJsonv2(String name, String sourePath, String gitHttpUrl, String gitBranch) throws IOException {
    //数据库统一记录时间
    Date createTime = new Date();
    String jsonStr = readFileContent(sourePath);
    JSONObject jsonObject = JSONUtil.parseObj(jsonStr);

//依赖
String dependencies = jsonObject.getStr(“dependencies”);
JSONArray array = JSONUtil.parseArray(dependencies);

//依赖下的漏洞
List dependencieList = JSONUtil.toList(array, ScanDependencie.class);
System.out.println(“--------------->依赖项总数:”+dependencieList.size());

//漏洞详情列表
List dependencieVulList = new ArrayList<>();
//漏洞清单汇总
List vulnerabilitiesAll = new ArrayList<>();
//扫描到的漏洞清单
List cveScanList = new ArrayList<>();

int i = 1;

//遍历依赖:构建导出数据
for(ScanDependencie d :dependencieList){
if(d.getVulnerabilities()==null || d.getVulnerabilities().size()==0) continue;

int pindex = i++;
String depId = UUID.randomUUID().toString().replaceAll(“-”,“”);
d.setId(depId);
d.setName(name);
d.setSortIndex(pindex);
d.setFileName(d.getFileName().replaceAll(" “,”"));
d.setGitHttpUrl(gitHttpUrl);
d.setGitBranch(gitBranch);
d.setScanDate(createTime);
if(StringUtils.isNotBlank(d.getDescription())) d.setDescription(d.getDescription().trim());

int m = 1;
//遍历依赖下的漏洞
List vulnerabilities = d.getVulnerabilities();
for(ScanVulnerabilitie vul : vulnerabilities){
if(vul.getName().indexOf(“CVE”)==-1) continue;

//添加到扫描漏洞清单
cveScanList.add(vul.getName());

int index = m++;
vul.setDependencieId(depId);
vul.setSortIndex(index);
vul.setSortIndexStr(pindex+“.”+index);
vul.setScanDate(createTime);

vulnerabilitiesAll.add(vul);
}

dependencieVulList.add(d);
}

//清除之前的数据
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq(“git_http_url”,gitHttpUrl);
List oldList = dependencieService.list(wrapper);
List depIds = oldList.stream().map(ScanDependencie::getId).collect(Collectors.toList());

if(depIds.size()>0){
QueryWrapper wrapper2 = new QueryWrapper<>();
wrapper2.in(“dependencie_id”,depIds);
vulnerabilitieService.remove(wrapper2);
}
dependencieService.remove(wrapper);

//保存扫描到的依赖数据:dependencieVulList
dependencieService.saveBatch(dependencieVulList);
//保存扫描到的依赖数据:vulnerabilitiesAll
vulnerabilitieService.saveBatch(vulnerabilitiesAll);
System.out.println(“--------------->有漏洞的依赖项:”+dependencieVulList.size());
System.out.println(“--------------->漏洞总数:”+vulnerabilitiesAll.size());
System.out.println(“”);
return vulnerabilitiesAll.size();
}

@Override
public String createWord(String name, String reportPath, String gitHttpUrl, String gitBranch) throws IOException {

//所有的有漏洞的依赖
List dependencieList = new ArrayList<>();
//所有的漏洞清单
List vulList = new ArrayList<>();
//漏洞清单分组
Map<String, List> listMap = new HashMap<>();
//漏洞编码
List cveIds = new ArrayList<>();
//对应的cnnvd信息(中文解释)
Map<String, Cnnvd> cnnvdMap = new HashMap<>();
//nvd漏洞原始信息(英文内容)
Map<String, Cve> cveMap = new HashMap<>();

QueryWrapper wrapper1 = new QueryWrapper();
wrapper1.eq(“name”,name).eq(“git_http_url”,gitHttpUrl).orderByAsc(“sort_index”);
dependencieList = dependencieService.list(wrapper1);

List depIds = dependencieList.stream().map(ScanDependencie::getId).collect(Collectors.toList());
if(depIds==null || depIds.size()==0) return null;

QueryWrapper wrapper2 = new QueryWrapper<>();
wrapper2.in(“dependencie_id”,depIds).orderByAsc(“id”);
vulList = vulnerabilitieService.list(wrapper2);
//漏洞根据依赖id分组
listMap = vulList.stream().collect(Collectors.groupingBy(ScanVulnerabilitie::getDependencieId));

cveIds = vulList.stream().map(ScanVulnerabilitie::getName).distinct().collect(Collectors.toList());
//根据漏洞编码查询cnnvd漏洞库
QueryWrapper wrapper3 = new QueryWrapper<>();
wrapper3.in(“cve_id”,cveIds);
List nvdList = this.list(wrapper3);
cnnvdMap = nvdList.stream().collect(Collectors.toMap(Cnnvd::getCveId, Function.identity()));

//根据漏洞编码查询nvd漏洞库
QueryWrapper wrapper4 = new QueryWrapper<>();
wrapper4.in(“id”,cveIds);
List cveList = cveService.list(wrapper4);
cveMap = cveList.stream().collect(Collectors.toMap(Cve::getId, Function.identity()));

//--------------------------------开始准备word数据----------------------------------//
for(ScanDependencie d : dependencieList){

if(!listMap.containsKey(d.getId())) continue;
List vuls = listMap.get(d.getId());
for(ScanVulnerabilitie vul : vuls){
if(cnnvdMap.containsKey(vul.getName())){
Cnnvd cnnvd = cnnvdMap.get(vul.getName());
if(cnnvd==null) continue;

vul.setSeverity(cnnvd.getSeverity());
vul.setVulnType(cnnvd.getVulnType());
vul.setCnnvd(cnnvd.getId());
vul.setTitle(cnnvd.getName());
vul.setDescription(cnnvd.getDescription());
}else{
Cve cve = cveMap.get(vul.getName());
if(cve==null) continue;

vul.setSeverity(cve.getSeverity());
vul.setDescription(cve.getDescription());
}
}

d.setVulnerabilities(vuls);
}

Map<String,Object> map = new HashMap<>();
map.put(“gitHttpUrl”, gitHttpUrl);
map.put(“gitBranch”, gitBranch);
map.put(“reportDate”, DateUtil.today());
map.put(“projectName”,name);
map.put(“vulnerabilitieDependencie”,dependencieList.size());
map.put(“vulnerabilitieNum”,vulList.size());
map.put(“dependencieList”,dependencieList);
map.put(“depList”,dependencieList);
map.put(“vulList”,vulList);

LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder().bind(“depList”, policy).bind(“vulList”, policy).build();

String fileName = name+“-第三方依赖CVE漏洞扫描报告.docx”;
String outputFile = reportPath+fileName;
XWPFTemplate template = XWPFTemplate.compile(this.getClass().getClassLoader().getResourceAsStream(“templates/第三方依赖CVE漏洞扫描报告模板.docx”),config).render(map);
template.write(new FileOutputStream(outputFile));
PoitlIOUtils.closeQuietlyMulti(template);
return fileName;
}

public String readFileContent(String filePath) throws IOException {

// 指定文件路径
File file = new File(filePath);

// 创建 BufferedReader 和 InputStreamReader 实例以读取文件
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, “UTF-8”);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

// 创建 StringBuilder 实例存储文件内容
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
sb.append(“\n”); // 每行后添加换行符
}

// 关闭 BufferedReader 和 InputStreamReader
bufferedReader.close();
inputStreamReader.close();
fileInputStream.close();

// 返回文件内容的字符串形式
return sb.toString();
}

}

五、使用jenkins实现自动化

我这里是使用jenkins的流水线SCM

def nameStr = ‘xxx’
def gitHttpUrl = ‘https://git.xxx.com/aaaa/xxx.git’
def gitsshUrl = ‘git@git.xxx.com:aaaa/xxx.git’
def gitBranch = ‘master’

pipeline {
agent any
stages {
stage(‘拉取代码’) {
steps {
script{
echo ‘----拉取代码----’
git branch: gitBranch, credentialsId: ‘xxxxxxxx’, url: gitHttpUrl

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注网络安全获取)
img

本人从事网路安全工作12年,曾在2个大厂工作过,安全服务、售后服务、售前、攻防比赛、安全讲师、销售经理等职位都做过,对这个行业了解比较全面。

最近遍览了各种网络安全类的文章,内容参差不齐,其中不伐有大佬倾力教学,也有各种不良机构浑水摸鱼,在收到几条私信,发现大家对一套完整的系统的网络安全从学习路线到学习资料,甚至是工具有着不小的需求。

最后,我将这部分内容融会贯通成了一套282G的网络安全资料包,所有类目条理清晰,知识点层层递进,需要的小伙伴可以点击下方小卡片领取哦!下面就开始进入正题,如何从一个萌新一步一步进入网络安全行业。

学习路线图

其中最为瞩目也是最为基础的就是网络安全学习路线图,这里我给大家分享一份打磨了3个月,已经更新到4.0版本的网络安全学习路线图。

相比起繁琐的文字,还是生动的视频教程更加适合零基础的同学们学习,这里也是整理了一份与上述学习路线一一对应的网络安全视频教程。

网络安全工具箱

当然,当你入门之后,仅仅是视频教程已经不能满足你的需求了,你肯定需要学习各种工具的使用以及大量的实战项目,这里也分享一份我自己整理的网络安全入门工具以及使用教程和实战。

项目实战

最后就是项目实战,这里带来的是SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~

面试题

归根结底,我们的最终目的都是为了就业,所以这份结合了多位朋友的亲身经验打磨的面试题合集你绝对不能错过!

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

g.csdnimg.cn/img_convert/bcd1787ce996787388468bb227d8f959.jpeg)

项目实战

最后就是项目实战,这里带来的是SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~

面试题

归根结底,我们的最终目的都是为了就业,所以这份结合了多位朋友的亲身经验打磨的面试题合集你绝对不能错过!

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-tiu5fd6K-1712653457924)]

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值