zl程序教程

您现在的位置是:首页 >  系统

当前栏目

centos7环境下ModSecurity-envoy编译和测试(一)-野路子技术宅

centos7技术测试 环境 编译 路子 Envoy
2023-06-13 09:16:40 时间

@@ ~~ ^ ^ &&

相信光,相信相信的力量.

技术是安身立命之本,实践出真知,熟能生巧,佐以业务能力,遇上风口之时,可逆天改命!

本文同步发布在作者的个人博客,欢迎转载。野路子技术宅博客

http://www.yousee.top

=====================================================================

这篇文章是最近2个月的工作成果,做个记录和备注。

最近一年都在参与某央企通信企业的安全产品研发工作,感谢合作方和大佬们给与学习机会,参与云原生、零信任技术和开源安全产品研究工作。

ModSecurity通过插件方式集成到envoy的资料比较少,只好通过各种手段推进工作任务,找同事交流请教、找资料学习和测试,终成本篇的手册。

先表个态度:感谢领导,感谢队友,感谢大佬指点和包容。

一、关于ModSecurity

modsec.png

ModSecurity是一个开源的、跨平台的Web应用防火墙(WAF),被称为WAF界的“瑞士军刀”。它可以通过检查Web服务接收到的数据,以及发送出去的数据来对网站进行安全防护。

1、站点链接

官方站点

https://github.com/SpiderLabs/ModSecurity

非官方中文站点

http://www.modsecurity.cn/

2、功能用途介绍

WAF主要用于网站的防护,拦截恶意攻击和请求,主要用途如下:

SQL Injection (SQLi):阻止SQL注入

Cross Site Scripting (XSS):阻止跨站脚本攻击

Local File Inclusion (LFI):阻止利用本地文件包含漏洞进行攻击

Remote File Inclusione(RFI):阻止利用远程文件包含漏洞进行攻击

Remote Code Execution (RCE):阻止利用远程命令执行漏洞进行攻击

PHP Code Injectiod:阻止PHP代码注入

HTTP Protocol Violations:阻止违反HTTP协议的恶意访问

HTTPoxy:阻止利用远程代理感染漏洞进行攻击

Sshllshock:阻止利用Shellshock漏洞进行攻击

Session Fixation:阻止利用Session会话ID不变的漏洞进行攻击

Scanner Detection:阻止黑客扫描网站

Metadata/Error Leakages:阻止源代码/错误信息泄露

Project Honey Pot Blacklist:蜜罐项目黑名单

GeoIP Country Blocking:根据判断IP地址归属地来进行IP阻断

二、编译环境

vmware安装centos虚拟机,构建代码编译环境。

centos7.9 minal

gcc 9.23.3

cmake 3.16

bazel 0.28.0

三、环境准备

1、vmware / visualbo 安装centos7

设置静态IP为192.168.43.224

安装详情略,有需要的筒子们请自行度娘或者狗狗。如有必要,私聊我哈。

2、准备上网工具

都已经21世纪了,不要浪费时间在无意义的网络上面,特别是IT人士,保持网络通畅的重要性不言而喻。

编译过程时间比较长,涉及到公共类库的下载、编译和校验,整过过程需要保持网络的通畅,需能访问github站点,个中的意味,自己体验哈。

2.1export配置网络代理

http代理端口为10811,socks5端口为10810,直接粘贴到centos终端命令行。


export http_proxy=http://192.168.43.224:10811

export https_proxy=https://192.168.43.224:10811

export ALL_PROXY=socks5://192.168.43.224:10810


2.2 设置git客户端代理

在代码更新过程中,git submodule会请求github拉取代码,网络不顺畅会导致异常,失败,这些都是大叔在实践中遇到的技术细节问题。

git config --global https.proxy http://192.168.43.224:10811

git config --global https.proxy https://192.168.43.224:10811

git config --global http.proxy socks5://192.168.43.224:10810

git config --global https.proxy socks5://192.168.43.224:10810

设置完毕2.1和2.2步骤,检查下是否成功。

curl www.狗狗.com

终端返回html标签记录,说明已经网络dialing设置成功。


四、代码下载

Modesecurity作为lib库,从apache插件发展成标准的开源waf类库,能灵活集成到nginx、apache、IIS以envoy。envoy作为云原生利器,用来做waf应用的资料比较少。

项目地址:

https://github.com/vmware-archive/ModSecurity-envoy

做好准备,下载代码。action ~ go!

1、建立工作目录

cd ~

mkdir ModeSecurity

cd ModSecurity

2、下载代码

git clone git@github.com:octarinesec/ModSecurity-envoy.git

git clone git@github.com:SpiderLabs/ModSecurity.git

#如果下载代码有问题,需要设置git 的ssh权限,有问题的可以留言!

git submodule update --init

bazel build //:envoy

3、安装依赖包和工具

安装unzip

yum install -y unzip

centos7安装bazel0.28.0

wget https://github.com/bazelbuild/bazel/releases/download/0.28.0/bazel-0.28.0-installer-linux-x86_64.sh

chmod +x ./bazel-0.28.0-installer-linux-x86_64.sh

./bazel-0.28.0-installer-linux-x86_64.sh --user

bazel version

安装ninja

yum install -y ninja-build

安装失败,提示没有yum源,狗狗搜索ninja,找到歪果仁大神的资料。

ninja参考资料

步骤如下:

cd /etc/yum.repos.d/

wget https://download.opensuse.org/repositories/home:danci1973/CentOS_7/home:danci1973.repo --no-check-certificate

yum install ninja

安装pcre包

yum install -y pcre

yum install -y pcre-devel

通过centos-release-scl工具包安装gcc9

老狗大叔通过gcc源码编译安装,耗费4个多小时才完成make && make install,一路走来,苦不堪言的。这是个笨到家的方法!

通过狗狗搜索引擎,找到scl神奇的工具包,事半功倍,效率高到飞起!

参照 https://blog.csdn.net/qq_42819333/article/details/127442286

安装步骤:

yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils #安装最新版本的GCC

scl enable devtoolset-9 bash

echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile #修改环境变量

gcc -v

升级CMAKE

centos系统自带的cmake版本过低,引起编译错误,提示cmake2.8版本异常,需升级cmake3.16版本。

升级cmake

https://linuxfere.com/how-to-install-latest-cmake-on-centos

curl -LO https://github.com/Kitware/CMake/releases/download/v3.22.2/cmake-3.22.2-linux-x86_64.tar.gz

tar -xvf cmake-3.22.2-linux-x86_64.tar.gz

mv cmake-3.22.2-linux-x86_64 /usr/local/cmake

echo 'export PATH="/usr/local/cmake/bin:$PATH"' >> ~/.bashrc

source ~/.bashrc

cmake --version

安装patch

yum -y install patch

其他依赖包

yum install -y libtool realpath clang-format-5.0 automake

yum install -y g++ flex bison curl doxygen libyajl-dev libgeoip-dev libtool dh-autoreconf libcurl4-gnutls-dev libxml2 libpcre++-dev libxml2-dev

如果出现下列异常提示,是对应的包已经废弃或者yum源找不到对应的包,需到pkgs.org查找对应的安装包。

异常提示:

没有可用软件包 libyajl-dev

没有可用软件包 libgeoip-dev

没有可用软件包 dh-autoreconf

没有可用软件包 libcurl4-gnutls-dev

没有可用软件包 libpcre++-dev

没有可用软件包 libxml2-dev

不用着急,我们通过关键包名,找到对应的软件包。

参照资料

https://centos.pkgs.org/7/centos-x86_64/yajl-2.0.4-4.el7.x86_64.rpm.html

yum install -y yajl

yum install -y libxml2-devel

GeoIP包参照

https://centos.pkgs.org/7/centos-x86_64/GeoIP-1.5.0-14.el7.x86_64.rpm.html

https://centos.pkgs.org/7/centos-x86_64/GeoIP-devel-1.5.0-14.el7.i686.rpm.html

#安装GeoIP数据库

yum install GeoIP-data

#开发工具包

yum install GeoIP-devel

#lcurl工具包

ecntos自带的culr是旧版本,无法满足编译要求,需更新libcurl。

参照文章

https://centos.pkgs.org/7/centos-x86_64/libcurl-7.29.0-59.el7.x86_64.rpm.html

yum install libcurl

五、代码编译

1、切换编译环境到gcc 9

scl enable devtoolset-9 bash

gcc -v

查看gcc版本9.23.3

##加载envoy代码分支

cd ModSecurity-envoy

git submodule update --init

rm -rf envoy

切换envoy1.11.0版本

git clone -b v1.11.0 https://github.com/envoyproxy/envoy.git

通过bazel命令构建工程代码

bazel build //:envoy

编译错误修正记录

因本人能力和水平有限,仅设定本次工作目标是修正代码错误,保证代码能正常通过和运行,是不严谨和取巧的方式。也恳请C++的大神朋友请不吝指教和指点。

在编译过程重,会出现4次代码提示错误,下面一一记录。

第一次错误

ERROR: An error occurred during the fetch of repository 'com_github_eile_tclap':

java.io.IOException: Error downloading https://github.com/eile/tclap/archive/tclap-1-2-1-release-final.tar.gz to /root/.cache/bazel/_bazel_root/0d8d6857ae4850c8e935cdce2c56c02a/external/com_github_eile_tclap/tclap-1-2-1-release-final.tar.gz: GET returned 404 Not Foun

https://github.com/eile/tclap/archive/tclap-1-2-1-release-final.tar.gz

通过错误包关键字查找文件

cd /root/.cache/bazel

find . | xargs grep -ril "eile/tclap/archive"

find /root/.cache/bazel/ | xargs grep -ril "eile/tclap/archive"

找到文件repository_locations.bzl,修正地址路径

vi /root/.cache/bazel/_bazel_root/0d8d6857ae4850c8e935cdce2c56c02a/external/envoy/bazel/repository_locations.bzl

修改为

https://github.com/mirror/tclap/archive/tclap-1-2-1-release-final.tar.gz

保存,继续编译

bazel build //:envoy

第二次错误,三方类库代码异常

/root/.cache/bazel/_bazel_root/0d8d6857ae4850c8e935cdce2c56c02a/external/com_github_datadog_dd_opentracing_cpp/BUILD.bazel

external/com_github_datadog_dd_opentracing_cpp/src/tracer.cpp

101 | return std::move(span);

修正方式

vi /root/.cache/bazel/_bazel_root/0d8d6857ae4850c8e935cdce2c56c02a/external/com_github_datadog_dd_opentracing_cpp/src/tracer.cpp

注释

//return std::move(span);

return nullptr;

保存,继续编译

bazel build //:envoy

第三次错误

n file included from external/envoy/source/exe/main_common.cc:7:

bazel-out/k8-fastbuild/bin/external/envoy/source/common/common/_virtual_includes/compiler_requirements_lib/common/common/compiler_requirements.h:14:2: error: #error "Your toolchain has set _GLIBCXX_USE_CXX11_ABI to a value that uses a std::string " "implementation that is not thread-safe. This may cause rare and difficult-to-debug errors " "if std::string is passed between threads in any way. If you accept this risk, you may define " "ENVOY_IGNORE_GLIBCXX_USE_CXX11_ABI_ERROR=1 in your build."

14 | #error "Your toolchain has set _GLIBCXX_USE_CXX11_ABI to a value that uses a std::string " \

  |  ^~~~~

Target //:envoy-static failed to build

Use --verbose_failures to see the command lines of failed build steps.

注释调宏定义

#error

root@localhost ModSecurity-envoy# ll bazel-out/k8-fastbuild/bin/external/envoy/source/common/common/_virtual_includes/compiler_requirements_lib/common/common/compiler_requirements.h

vi /root/.cache/bazel/_bazel_root/0d8d6857ae4850c8e935cdce2c56c02a/external/envoy/source/common/common/compiler_requirements.h

注释#error宏定义

保存,继续编译

bazel build //:envoy

无法找到lib包异常

编译test阶段,提示无法找到包的异常,提示:

/usr/bin/ld: cannot find -lxxx

参照文章:

https://www.cnblogs.com/zhming26/p/6164131.html

https://blog.csdn.net/AXW2013/article/details/51207498

https://www.jianshu.com/p/ccaf688f54c0

前面准备阶段已经完成工具包安装,在系统路径下建立连接即可。

建立软连接到加载目录:

cd /opt/rh/devtoolset-9/root/usr/lib/gcc/x86_64-redhat-linux/9

ln -sfT /usr/lib64/libyajl.so.2 ./libyajl.so

ln -sfT /usr/bin/curl ./curl

ln -sfT /usr/bin/curl ./libcurl.so

cd /opt/rh/devtoolset-9/root/usr/lib/gcc/x86_64-redhat-linux/9

ln -sfT /usr/lib64/libcurl.so.4 ./libcurl.so

继续编译

bazel build //:envoy

后续出现异常,根据提示解决问题,完成最终编译。

时间已经到 3:06 2023/1/10,明天还要上班,今天先到这里。

欢迎转载,请保留出处。

野路子技术宅

http://www.yousee.top/articles/57

关于waf的测试和问题,留到下一篇博文做介绍和总结。

感谢耐心看完我的分享,也希望对您有帮助!