Files
mosec-maven-plugin/README.md

201 lines
4.6 KiB
Markdown
Raw Normal View History

2020-07-28 15:38:11 +08:00
# MOSEC-MAVEN-PLUGIN
用于检测maven项目的第三方依赖组件是否存在安全漏洞。
该项目是基于 [snyk-maven-plugin](https://github.com/snyk/snyk-maven-plugin.git) 的二次开发。
2020-08-11 13:41:25 +08:00
2020-07-29 16:29:08 +08:00
## 关于我们
Websitehttps://security.immomo.com
WeChat:
2020-07-29 16:34:52 +08:00
<img src="https://momo-mmsrc.oss-cn-hangzhou.aliyuncs.com/img-1c96a083-7392-3b72-8aec-bad201a6abab.jpeg" width="200" hegiht="200" align="center" /><br>
2020-07-29 16:29:08 +08:00
2020-08-11 13:41:25 +08:00
2020-07-28 15:38:11 +08:00
## 版本要求
Maven >= 3.1
2020-08-11 13:41:25 +08:00
2020-07-28 15:38:11 +08:00
## 安装
2020-08-11 13:18:40 +08:00
#### 向pom.xml中添加plugin仓库 (项目级安装)
2020-07-28 15:38:11 +08:00
2020-07-29 16:06:25 +08:00
```xml
<!-- pom.xml -->
2020-08-11 13:18:40 +08:00
2020-07-29 16:06:25 +08:00
<pluginRepositories>
<pluginRepository>
<id>gh</id>
<url>https://raw.githubusercontent.com/momosecurity/mosec-maven-plugin/master/mvn-repo/</url>
2020-07-29 16:06:25 +08:00
</pluginRepository>
</pluginRepositories>
2020-07-28 15:38:11 +08:00
```
2020-08-11 13:18:40 +08:00
#### 向maven配置中添加plugin仓库 (全局安装)
```xml
<!-- settings.xml -->
<!-- 添加pluginGroup可简化命令行参数 -->
<pluginGroups>
<pluginGroup>com.immomo.momosec</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>momo-plugin</id>
<pluginRepositories>
<pluginRepository>
<id>gh</id>
<url>https://raw.githubusercontent.com/momosecurity/mosec-maven-plugin/master/mvn-repo/</url>
2020-08-11 13:18:40 +08:00
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>momo-plugin</activeProfile>
</activeProfiles>
```
2020-08-11 13:41:25 +08:00
2020-07-28 15:38:11 +08:00
## 使用
首先运行 [MOSEC-X-PLUGIN Backend](https://github.com/momosecurity/mosec-x-plugin-backend.git)
#### 命令行使用
```
> cd your_maven_project_dir/
2020-08-11 13:18:40 +08:00
2020-07-28 15:38:11 +08:00
> MOSEC_ENDPOINT=http://127.0.0.1:9000/api/plugin \
mvn com.immomo.momosec:mosec-maven-plugin:1.0.7:test \
2022-04-11 14:15:39 +08:00
-DonlyProvenance=true \
-Dseverity=High
2020-07-28 15:38:11 +08:00
2020-08-11 13:18:40 +08:00
// 或简化方式
2020-07-28 15:38:11 +08:00
> MOSEC_ENDPOINT=http://127.0.0.1:9000/api/plugin \
2022-04-11 14:15:39 +08:00
mvn mosec:test -DonlyProvenance=true -Dseverity=High
2020-07-28 15:38:11 +08:00
```
#### 项目中使用
```xml
<!-- pom.xml -->
<plugins>
<plugin>
<groupId>com.immomo.momosec</groupId>
<artifactId>mosec-maven-plugin</artifactId>
2021-06-21 11:25:09 +08:00
<version>1.0.8</version>
2020-07-28 15:38:11 +08:00
<executions>
<execution>
<id>test</id>
<goals>
2020-08-11 13:18:40 +08:00
<goal>test</goal> <!-- test过程执行 -->
2020-07-28 15:38:11 +08:00
</goals>
</execution>
</executions>
<configuration>
<endpoint>http://127.0.0.1:9000/api/plugin</endpoint>
2022-04-11 14:15:39 +08:00
<severityLevel>High</severityLevel>
2020-07-28 15:38:11 +08:00
<onlyProvenance>true</onlyProvenance>
<failOnVuln>true</failOnVuln>
</configuration>
</plugin>
</plugins>
```
2020-08-11 13:41:25 +08:00
2020-08-11 13:18:40 +08:00
## 帮助
```shell script
> mvn mosec:help -Ddetail=true
mosec:test
Available parameters:
endpoint
上报API
User property: endpoint
failOnVuln (Default: true)
发现漏洞即编译失败
User property: failOnVuln
includeProvidedDependency (Default: false)
是否包含Provided Scope依赖
User property: includeProvidedDependency
onlyAnalyze (Default: false)
仅分析依赖,不上报
User property: onlyAnalyze
2020-08-11 13:18:40 +08:00
onlyProvenance (Default: false)
仅检查直接依赖
User property: onlyProvenance
outputDepToFile (Default: )
2022-04-12 10:35:03 +08:00
输出依赖树到文件。设置-DonlyAnalyze=true仅输出依赖树否则输出依赖树及漏洞检查结果
User property: outputDepToFile
2020-08-11 13:18:40 +08:00
severityLevel (Default: High)
威胁等级 [High|Medium|Low]
User property: severity
```
2020-08-11 13:41:25 +08:00
2020-08-11 13:18:40 +08:00
## 使用效果
以 src/test/resources/projects/vuln-project 项目为例。
[WARNING] 部分给出漏洞警告Path: 为漏洞依赖链Fix version 为组件安全版本。
程序返回值为1表示发现漏洞。返回值为0即为未发现问题。
2020-08-11 14:03:43 +08:00
![usage](./static/usage.jpg)
2020-08-11 13:18:40 +08:00
2020-08-11 13:41:25 +08:00
2020-08-11 13:18:40 +08:00
## 检测原理
MOSEC-MAVEN-PLUGIN使用`org.apache.maven:maven-core`组件中提供的`aether-api`提取依赖并构建依赖树。
该方法可以准确提取maven项目所使用的依赖以及确定的依赖版本。
最终依赖树会交由 [MOSEC-X-PLUGIN-BACKEND](https://github.com/momosecurity/mosec-x-plugin-backend.git) 检测服务进行检测,并返回结果。
相关数据结构请参考 MOSEC-X-PLUGIN-BACKEND [README.md](https://github.com/momosecurity/mosec-x-plugin-backend/blob/master/README.md).
2020-08-11 13:41:25 +08:00
2020-07-28 15:38:11 +08:00
## 开发
#### Intellij 远程调试 Maven 插件
2020-07-29 17:39:45 +08:00
1.将mosec-maven-plugin拉取至本地仓库
2020-07-28 15:38:11 +08:00
2020-07-29 17:39:45 +08:00
2.git clone mosec-maven-plugin 源码
2020-07-28 15:38:11 +08:00
2020-07-29 17:39:45 +08:00
3.Intellij 打开mosec-maven-plugin项目新建 Remote Configuration 并填入如下信息
2020-07-28 15:38:11 +08:00
2020-08-11 14:03:43 +08:00
![remote-configuration](./static/remote-configuration.jpg)
2020-07-28 15:38:11 +08:00
4.在另一个maven工程中执行如下命令
```shell script
2021-06-21 11:25:09 +08:00
> mvnDebug com.immomo.momosec:mosec-maven-plugin:1.0.8:test
2020-07-28 15:38:11 +08:00
```
2021-06-21 11:25:09 +08:00
5.回到Intellij中下断点开始Debug