diff --git a/php/.gitignore b/php/.gitignore new file mode 100644 index 0000000..bd1fbd6 --- /dev/null +++ b/php/.gitignore @@ -0,0 +1,2 @@ +/php-* + diff --git a/php/README.md b/php/README.md new file mode 100644 index 0000000..391e21a --- /dev/null +++ b/php/README.md @@ -0,0 +1,34 @@ +# Build PHP as static PIE + +Build the [PHP](https://www.php.net/) interpreter as static PIE ELF running on Linux + +## Requirements + +Make sure the following packages are installed: + +* gcc >= 8 +* GNU Make +* autoconf +* bison +* re2c +* libxml2 development files +* libsqlite3 development files + +On Ubuntu, you can install these using: + +``` +sudo apt install -y pkg-config build-essential autoconf bison re2c \ + libxml2-dev libsqlite3-dev +``` + +## Build + +The `php` static PIE ELF file is located in the current directory. +In order to rebuild it you have to run the `build.sh` bash script + +The scripts downloads, unpack, patches and build the `php` static PIE ELF file. + +## Run + +In order to run a script using the static PIE php you can pass it as an argument to the ELF file. + diff --git a/php/build.sh b/php/build.sh new file mode 100755 index 0000000..3d09234 --- /dev/null +++ b/php/build.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +PHP_VERSION=8.1.11 +PHP_URL="https://www.php.net/distributions/php-$PHP_VERSION.tar.gz" + +#clean up +rm -rf php-* + +echo -n "Downloading PHP $PHP_VERSION ... " +wget -q "$PHP_URL" +echo "" + +echo -n "Unpacking PHP $PHP_VERSION ... " +tar xzf php-$PHP_VERSION.tar.gz +echo "" + +pushd php-$PHP_VERSION > /dev/null 2>&1 || exit 1 + +echo "Configuring PHP $PHP_VERSION ... " +#iconv and opcache seem to have problems with being in a static build +CFLAGS=-fPIC ./configure +#patch the Makefile +sed -e '/^BUILD_C/s/ -export-dynamic//g' -e 's/^EXTRA_LIBS = .*/EXTRA_LIBS = -lrt -lxml2 -lsqlite3 -lz -llzma -licuuc -licudata -lstdc++ -lm/g' -e '/^EXTRA_LDFLAGS_PROGRAM/s/$/ -static-pie/g' -i Makefile +echo "" + +echo "Building PHP $PHP_VERSION ... " +make -j "$(nproc)" +popd > /dev/null 2>&1 || exit 1 + +ln -fn php-$PHP_VERSION/sapi/cli/php . + +rm -rf php-* diff --git a/php/helloworld.php b/php/helloworld.php new file mode 100644 index 0000000..bf7366c --- /dev/null +++ b/php/helloworld.php @@ -0,0 +1,2 @@ + + diff --git a/php/php b/php/php new file mode 100755 index 0000000..030e664 Binary files /dev/null and b/php/php differ