前言
复现一个漏洞,需要 php5.4 的 lamp 环境,但网上没有找到很好的环境,所以自己搭建了一个。
编译安装PHP
前置环境配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| apt-get install docker.io
docker pull ubuntu:16.04
docker run -it -p8888:8888 ubuntu:16.04 /bin/bash
mv /etc/apt/sources.list /etc/apt/sources.list.bak echo "IyMgTm90ZSwgdGhpcyBmaWxlIGlzIHdyaXR0ZW4gYnkgY2xvdWQtaW5pdCBvbiBmaXJzdCBib290IG9mIGFuIGluc3RhbmNlDQojIyBtb2RpZmljYXRpb25zIG1hZGUgaGVyZSB3aWxsIG5vdCBzdXJ2aXZlIGEgcmUtYnVuZGxlLg0KIyMgaWYgeW91IHdpc2ggdG8gbWFrZSBjaGFuZ2VzIHlvdSBjYW46DQojIyBhLikgYWRkICdhcHRfcHJlc2VydmVfc291cmNlc19saXN0OiB0cnVlJyB0byAvZXRjL2Nsb3VkL2Nsb3VkLmNmZw0KIyMgICAgIG9yIGRvIHRoZSBzYW1lIGluIHVzZXItZGF0YQ0KIyMgYi4pIGFkZCBzb3VyY2VzIGluIC9ldGMvYXB0L3NvdXJjZXMubGlzdC5kDQojIyBjLikgbWFrZSBjaGFuZ2VzIHRvIHRlbXBsYXRlIGZpbGUgL2V0Yy9jbG91ZC90ZW1wbGF0ZXMvc291cmNlcy5saXN0LnRtcGwNCg0KIyBTZWUgaHR0cDovL2hlbHAudWJ1bnR1LmNvbS9jb21tdW5pdHkvVXBncmFkZU5vdGVzIGZvciBob3cgdG8gdXBncmFkZSB0bw0KIyBuZXdlciB2ZXJzaW9ucyBvZiB0aGUgZGlzdHJpYnV0aW9uLg0KZGViIGh0dHA6Ly9taXJyb3JzLmNsb3VkLmFsaXl1bmNzLmNvbS91YnVudHUvIHhlbmlhbCBtYWluDQpkZWItc3JjIGh0dHA6Ly9taXJyb3JzLmNsb3VkLmFsaXl1bmNzLmNvbS91YnVudHUvIHhlbmlhbCBtYWluDQoNCiMjIE1ham9yIGJ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pkZWItc3JjIGh0dHA6Ly9taXJyb3JzLmNsb3VkLmFsaXl1bmNzLmNvbS91YnVudHUvIHhlbmlhbC1zZWN1cml0eSBtYWluDQpkZWIgaHR0cDovL21pcnJvcnMuY2xvdWQuYWxpeXVuY3MuY29tL3VidW50dS8geGVuaWFsLXNlY3VyaXR5IHVuaXZlcnNlDQpkZWItc3JjIGh0dHA6Ly9taXJyb3JzLmNsb3VkLmFsaXl1bmNzLmNvbS91YnVudHUvIHhlbmlhbC1zZWN1cml0eSB1bml2ZXJzZQ0KIyBkZWIgaHR0cDovL21pcnJvcnMuY2xvdWQuYWxpeXVuY3MuY29tL3VidW50dS8geGVuaWFsLXNlY3VyaXR5IG11bHRpdmVyc2UNCiMgZGViLXNyYyBodHRwOi8vbWlycm9ycy5jbG91ZC5hbGl5dW5jcy5jb20vdWJ1bnR1LyB4ZW5pYWwtc2VjdXJpdHkgbXVsdGl2ZXJzZQ=="|base64 -d > /etc/apt/sources.list
apt-get install vim wget net-tools curl
apt install -y pkg-config build-essential autoconf bison re2c libxml2-dev libsqlite3-dev apache2 apache2-dev
|
下载并编译安装php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| cd /root wget https://www.php.net/distributions/php-5.4.45.tar.bz2 tar jxvf php-5.4.45.tar.bz2
cd php-5.4.45 ./buildconf --force ./configure --prefix=/home/php/php5445 --exec-prefix=/home/php/php5445/execdir --with-apxs2 make make install
/usr/share/apr-1.0/build/libtool --finish /root/php-5.4.45/libs
mv /root/php-5.4.45/php.ini-development /home/php/php5445/execdir/lib/php.ini
export PATH="/home/php/php5445/execdir/bin/:$PATH" >> ~/.bashrc
source ~/.bashrc
|
- 安装目录在
/home/php/php5445
- 执行文件目录在
/home/php/php5445/execdir/bin
由于我们并没有附带任何其他参数,执行的是 PHP 的默认安装,所以只安装了如下模块:
1
| Core ctype date dom ereg fileinfo filter hash iconv json libxml pcre PDO pdo_sqlite Phar posix Reflection session SimpleXML SPL sqlite3 standard tokenizer xml xmlreader xmlwriter
|
- 我们使用默认配置编译php,如果需要安装其他扩展,可能需要如下库。(下文我们会示例介绍如何编译安装mysql扩展)
如何编译其他模块
对于日常可能用的模块,我们需要先下载如下库
1
| apt-get install zlib1g-dev libbz2-dev libcurl4-openssl-dev openssl libssl-dev libpng-dev libjpeg-dev libmcrypt-dev libedit-dev libreadline-dev libmysqlclient-dev
|
libedit-dev libreadline-dev
是 php -a
也就是 php 交互模式需要的库 (readline 模块)
我会在下文示例编译安装 pdo_mysql 库,其他库的安装大同小异。
配合apache2
配置环境
1 2 3 4 5 6 7 8 9 10 11
| sed -i 's|Listen 80|Listen 8888|' /etc/apache2/ports.conf
echo 'U2VydmVyTmFtZSBsb2NhbGhvc3QKCjxGaWxlc01hdGNoIFwucGhwJD4KICAgIFNldEhhbmRsZXIgYXBwbGljYXRpb24veC1odHRwZC1waHAKPC9GaWxlc01hdGNoPg==' | base64 -d >> /etc/apache2/apache2.conf
a2dismod mpm_event a2enmod mpm_prefork
service apache2 restart
|
访问 http://ip:8888
成功运行
配合Mysql
安装 mysql5.7
1 2
| apt-get install mysql-client-5.7 mysql-server-5.7 -y
|
编译安装 mysqlnd
扩展(必须先安装它)
如下命令:
1 2 3 4 5 6 7
| cd /root/php-5.4.45/ext/mysqlnd mv config9.m4 config.m4 phpize ./configure --with-php-config=$(which php-config)
make
|
上述报错如下:
执行下列命令即可解决
1 2 3 4 5
| sed -i 's|ext/mysqlnd/php_mysqlnd_config.h|ext/mysqlnd/config.h|' /root/php-5.4.45/ext/mysqlnd/mysqlnd_portability.h
sed -i 's|ext/mysqlnd|/root/php-5.4.45/ext/mysqlnd|' /root/php-5.4.45/ext/mysqlnd/mysqlnd_portabilit.h
|
执行 php -a
可看到成功加载
编译安装 pdo_msyql
扩展
执行下列命令
查看php安装的模块 php -m
,成功安装 pdo_mysql 扩展
测试环境
- 启动环境
1
| # 启动 mysqlservice mysql restart# 启动 apacheservice apache2 restart
|
- 更改 index.php 代码
1
| echo "PD9waHANCnRyeSB7DQogICAgJGRiaCA9IG5ldyBQRE8oJ215c3FsOmhvc3Q9MTI3LjAuMC4xO2RibmFtZT1teXNxbCcsICdyb290JywgJ3Jvb3QnKTsNCiAgICBmb3JlYWNoKCRkYmgtPnF1ZXJ5KCdzaG93IGRhdGFiYXNlcycpIGFzICRyb3cpIHsNCiAgICAgICAgcHJpbnRfcigkcm93KTsNCiAgICB9DQogICAgJGRiaCA9IG51bGw7DQp9IGNhdGNoIChQRE9FeGNlcHRpb24gJGUpIHsNCiAgICBwcmludCAiRXJyb3IhOiAiIC4gJGUtPmdldE1lc3NhZ2UoKSAuICI8YnIvPiI7DQogICAgZGllKCk7DQp9DQo/Pg==" |base64 -d > /var/www/html/index.php
|
访问 curl localhost:8888
环境配置成功。
后记