Java 版本
获取源码
你可以使用 git 命令,也可以直接在 Github 下载 ZIP 包
git clone https://github.com/baidu/openrasp.git
准备环境
为了保证最大兼容性,我们建议使用 JDK 6 进行编译
为编译所依赖的JNI模块,需要安装 c++ 编译器和 cmake 生成器
以 CentOS 为例,使用如下命令,安装 g++ 5.3.1
yum install -y centos-release-scl
yum install -y devtoolset-4-gcc-c++
安装完成后,执行如下命令进入编译环境
scl enable devtoolset-4 bash
安装高版本 cmake
# 下载并解压到 /tmp,避免与已有 cmake 冲突
curl -L  https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-Linux-x86_64.tar.gz | tar zx -C /tmp
# 增加临时 PATH
export PATH=/tmp/cmake-3.15.3-Linux-x86_64/bin:$PATH
编译 openrasp-v8 基础库
以 64位 Linux 系统为例,在OpenRASP仓库根目录执行以下命令
# 更新 git submodule
git submodule update --init
# 编译 openrasp-v8
mkdir -p openrasp-v8/build64 && cd openrasp-v8/build64
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_LANGUAGES=java ..
make
# 复制动态链接库到 resources 目录
mkdir -p ../java/src/main/resources/natives/linux_64 && cp java/libopenrasp_v8_java.so $_
# 编译 v8-1.0-SNAPSHOT.jar,安装 v8-1.0-SNAPSHOT.jar 到 maven 本地仓库
cd ../java
mvn install
更多平台的编译细节请参考
- https://github.com/baidu-security/openrasp-v8/blob/master/.travis.yml
- https://github.com/baidu-security/openrasp-v8/tree/master/.travis
开始编译
- 方案1 - 使用 - IDEA Intellij进行操作- 导入 - maven 工程,目录选- agent/java,正常编译即可
- 方案2 - 使用 - mvn命令编译- 进入命令行, - cd <openrasp_path>/agent/java mvn versions:use-latest-releases -Dincludes=com.baidu.openrasp:sqlparser mvn clean package- 最终生成的 JAR 包在 - boot/target和- engine/target目录下,aka:- $openrasp_path/agent/java/boot/target/rasp.jar和- $openrasp_path/agent/java/engine/target/rasp.jar
- 方案3 - 使用 - build-java.sh生成安装包- 如果你使用 Linux 进行编译,可以执行源代码目录下面的 - build-java.sh进行编译和打包操作。这个脚本会在源代码根目录,生成- rasp-java.tar.gz和- rasp-java.zip两个文件
常见问题
1. maven 超时
如果总是出现超时错误,你可以考虑使用阿里云的镜像。
[ERROR] Plugin org.apache.maven.plugins:maven-resources-plugin:3.0.2 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:3.0.2: Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:3.0.2 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.196.215] failed: Operation timed out (Connection timed out) -> [Help 1]
   修改方法是编辑 ~/.m2/settings.xml,并填写如下内容
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors>
    <mirror>
     <id>aliyunmaven</id>
     <mirrorOf>central</mirrorOf>
     <name>阿里云公共仓库</name>
     <url>https://maven.aliyun.com/repository/central</url>
    </mirror>
    <mirror>
      <id>repo1</id>
      <mirrorOf>central</mirrorOf>
      <name>central repo</name>
      <url>http://repo1.maven.org/maven2/</url>
    </mirror>
    <mirror>
     <id>aliyunmaven</id>
     <mirrorOf>apache snapshots</mirrorOf>
     <name>阿里云阿帕奇仓库</name>
     <url>https://maven.aliyun.com/repository/apache-snapshots</url>
    </mirror>
  </mirrors>
  <proxies/>
  <activeProfiles/>
  <profiles>
    <profile>
        <repositories>
           <repository>
                <id>aliyunmaven</id>
                <name>aliyunmaven</name>
                <url>https://maven.aliyun.com/repository/public</url>
                <layout>default</layout>
                <releases>
                        <enabled>true</enabled>
                </releases>
                <snapshots>
                        <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>MavenCentral</id>
                <url>http://repo1.maven.org/maven2/</url>
            </repository>
            <repository>
                <id>aliyunmavenApache</id>
                <url>https://maven.aliyun.com/repository/apache-snapshots</url>
            </repository>
        </repositories>
     </profile>
  </profiles>
</settings>
2. git-commit-id-plugin 错误
如果你使用 JDK 1.6 编译,可能会遇到如下错误:
[ERROR] Plugin pl.project13.maven:git-commit-id-plugin:2.1.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for pl.project13.maven:git-commit-id-plugin:jar:2.1.5: Could not transfer artifact pl.project13.maven:git-commit-id-plugin:pom:2.1.5 from/to central (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_version -> [Help 1]
   这是一个 https 证书错误,解决方法是替换 maven 仓库地址为 http 版本。可以考虑修改 ~/.m2/settings.xml,并添加如下内容
<?xml version="1.0"?>
<settings>
  <mirrors>
    <mirror>
      <id>central-no-ssl</id>
      <name>Central without ssl</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
</settings>
3. 缺少 Premain-Class 属性
如果你使用IDE编译,最终可能出现这样的错误:
Failed to find Premain-Class manifest attribute in D:\apache-tomcat-7.0.82\rasp\rasp.jar
   这是因为IDE没有修改 META-INF 下面的文件。我们建议你使用 maven 编译,或者直接导入 maven 工程