|
| 1 | +# Build ModSecurity WASM Library |
| 2 | + |
| 3 | +This article will guide you to build your own ModSecurity WASM library using [Emscripten](https://emscripten.org/) toolchain. |
| 4 | + |
| 5 | +## Pre-requirements |
| 6 | + |
| 7 | +### Install Emscripten |
| 8 | + |
| 9 | +You can refer to the following steps to install the latest `Emscripten`. |
| 10 | + |
| 11 | +```shell |
| 12 | +# Get the emsdk repo |
| 13 | +git clone https://github.com/emscripten-core/emsdk.git |
| 14 | + |
| 15 | +# Enter that directory |
| 16 | +cd emsdk |
| 17 | + |
| 18 | +# Fetch the latest version of the emsdk (not needed the first time you clone) |
| 19 | +git pull |
| 20 | + |
| 21 | +# Download and install the latest SDK tools. |
| 22 | +./emsdk install latest |
| 23 | + |
| 24 | +# Make the "latest" SDK "active" for the current user. (writes .emscripten file) |
| 25 | +./emsdk activate latest |
| 26 | + |
| 27 | +# Activate PATH and other environment variables in the current terminal |
| 28 | +source ./emsdk_env.sh |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +### `wasi-sdk` setup |
| 34 | + |
| 35 | +- Download |
| 36 | + |
| 37 | + ```shell |
| 38 | + wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz |
| 39 | + ``` |
| 40 | + |
| 41 | +- Export it to `/opt/wasi-sdk` |
| 42 | + |
| 43 | +- Configure |
| 44 | + |
| 45 | + ```shell |
| 46 | + export WASI_SDK_PATH="/opt/wasi-sdk" |
| 47 | + ``` |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### Build PCRE WASM library |
| 52 | + |
| 53 | +```shell |
| 54 | +# Get the pcre library source code |
| 55 | +git clone https://github.com/maxfierke/libpcre.git -b mf-wasm32-wasi-cross-compile |
| 56 | + |
| 57 | +cd libpcre |
| 58 | +# This should compile successfully and place the compiled .a static library in targets/wasm32-wasi |
| 59 | +Run ./build_for_crystal.sh. |
| 60 | + |
| 61 | +# Copy the wams library to target directory |
| 62 | +cp targets/wasm32-wasi/*.a /usr/local/pcre |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +## Configure and build ModSecurity |
| 68 | + |
| 69 | +```shell |
| 70 | +# This is version for WASM ModSecurity |
| 71 | +git clone https://github.com/leyao-daily/ModSecurity.git |
| 72 | + |
| 73 | +cd ModSecurity |
| 74 | +# Build the configuration script |
| 75 | +./build.sh |
| 76 | + |
| 77 | +# Download the submodule |
| 78 | +git submodule init |
| 79 | +git submodule update |
| 80 | + |
| 81 | +# Configure ModSecurity with core functions |
| 82 | +emconfigure ./configure --without-yajl --without-geoip --without-libxml --without-curl --disable-shared --disable-examples --disable-libtool-lock --disable-debug-logs --with-pcre=./pcre-config |
| 83 | + |
| 84 | +# Build the library |
| 85 | +emmake make -j <num_cpus> |
| 86 | + |
| 87 | +# Install the library |
| 88 | +emmake make install |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +## Build your own wasm application |
| 95 | + |
| 96 | +```sehll |
| 97 | +emcc test.cc -L/usr/local/modsecurity/lib/ -lmodsecurity -L/usr/local/pcre/ -lpcre -o test.wasm -I/usr/local/modsecurity/include/ |
| 98 | +``` |
| 99 | + |
0 commit comments