第26天:安全开发-PHP应用&模版引用&Smarty渲染&MVC模型&数据联动&RCE安全

发布于:2024-04-24 ⋅ 阅读:(27) ⋅ 点赞:(0)

第二十六天

Untitled

一、PHP新闻显示-数据库操作读取显示

1.新闻列表

  1. 数据库创建新闻存储
  2. 代码连接数据库读取
  3. 页面进行自定义显示

二、PHP模版引用-自写模版&Smarty渲染

1.自写模版引用

  1. 页面显示样式编排
  2. 显示数据插入页面
  3. 引用模版调用触发

2.Smarty模版引用

1.下载:https://github.com/smarty-php/smarty/releases
2.使用:
  1. 创建一个文件夹,命名为smarty-demo。
  2. 下载Smarty对应版本并解压缩到该文件夹中。
  3. 创建一个PHP文件,命名为index.php,并在文件中添加以下代码:
3…源码写法:
<?php
// 引入 Smarty 类文件
require('smarty-demo/libs/Smarty.class.php');
// 创建 Smarty 实例
$smarty = new Smarty;
// 设置 Smarty 相关属性
$smarty->template_dir = 'smarty-demo/templates/';
$smarty->compile_dir = 'smarty-demo/templates_c/';
$smarty->cache_dir = 'smarty-demo/cache/';
$smarty->config_dir = 'smarty-demo/configs/';
// 赋值变量到模板中
$smarty->assign('title', '欢迎使用 Smarty');
// 显示模板
$smarty->display('index.tpl');
?>
4、创建一个名为index.tpl的模板文件,并将以下代码复制到上述点定义文件夹中
<!DOCTYPE html>
<html>
<head>
<title>{$title}</title>
</head>
<body>
<h1>{$title}</h1>
<p>这是一个使用 Smarty 的例子。</p>
</body>
</html>

三、PHP模版安全-RCE代码执行&三方漏洞

1.代码RCE安全测试

1.自写模版的安全隐患
  1. 如果在数据库中任何地方添加<?php phpinfo();?> ,在调用数据库内容的时候会自动显示
  2. 如果在html模板源码中加入<?php phpinfo();?> ,在执行HTML并不会显示,但通过php解析调用,则依然会展示有关内容
2.第三方Smarty的安全隐患
  1. 安全风险: 显示详细的 PHP 信息可能透露有关服务器配置的敏感信息,包括 PHP 版本、扩展和路径。攻击者可以利用这些信息来识别潜在的漏洞
  2. 信息泄露: 在生产环境中,显示详细的 PHP 信息是不推荐的,因为存在信息泄露的风险。攻击者可能利用这些信息更好地了解服务器的设置并识别潜在的攻击点
  3. 服务器加固: 安全最佳实践涉及将服务器信息的暴露最小化,以减少攻击面。应该限制不必要的服务器环境信息,以降低攻击表面

四、环境复现

1.新闻列表

<?php
// 包含数据库配置文件
include 'config.php';

// 从GET请求中获取id参数,如果不存在则默认为1
$id = $_GET['id'] ?? '1';

// 构建SQL查询语句
$sql = "select * from new where id=$id";

// 执行查询并获取结果集
$data = mysqli_query($con, $sql);

// 使用mysqli_fetch_row遍历结果集的每一行
while ($row = mysqli_fetch_row($data)) {
    // 输出标题,注意:mysqli_fetch_row返回的是枚举数组,索引从0开始
    echo "标题: <title>" . $row[1] . "</title><br>";

    // 输出第二列数据
    echo $row[2] . "<br>";

    // 输出第三列数据
    echo $row[3] . "<br>";

    // 输出图片,注意:在HTML中使用$row[4]作为图片路径
    echo "<img src='$row[4]' width='300' height='300'></img><br>";
}

// 关闭数据库连接
mysqli_close($con);
?>

2.自编程模板引用

// 从文件中读取HTML模板内容
$template = file_get_contents('new.html');

// 使用mysqli_fetch_row遍历结果集的每一行
while ($row = mysqli_fetch_row($data)) {
    // 从结果集中获取每一列的值,并存储到相应的变量中
    $page_title = $row[1];
    $heading = $row[2];
    $subheading = $row[3];
    $content = $row[4];
    $item = $row[5];
}

// 替换HTML模板中的占位符
$template = str_replace('{page_title}', $page_title, $template);
$template = str_replace('{heading}', $subheading, $template);
$template = str_replace('{subheading}', $subheading, $template);
$template = str_replace('{content}', $content, $template);
$template = str_replace('{$item}', $item, $template);

// 将PHP代码嵌入HTML模板中并执行
eval('?>' . $template);

// 关闭数据库连接
mysqli_close($con);

3.Smarty模版引用

<?php
// 引入 Smarty 类文件
require('smarty/libs/Smarty.class.php');

// 创建 Smarty 实例
$smarty = new Smarty;

// 设置 Smarty 相关属性
$smarty->template_dir = 'smarty/templates/';  // 设置模板文件的目录
// 指定 Smarty 模板文件所在的目录,这里使用了相对路径,可以根据实际情况修改
$smarty->compile_dir = 'smarty/templates_c/';  // 设置编译文件的目录
// 指定 Smarty 编译生成的模板文件所存放的目录,用于存放 Smarty 编译生成的 PHP 文件
$smarty->cache_dir = 'smarty/cache/';  // 设置缓存文件的目录
// 指定 Smarty 缓存文件的存放目录,用于存放 Smarty 缓存文件
$smarty->config_dir = 'smarty/configs/';  // 设置配置文件的目录
// 指定 Smarty 配置文件的存放目录,用于存放 Smarty 配置文件

// 赋值变量到模板中
$smarty->assign('title', '欢迎使用 Smarty');  // 将变量 'title' 赋值为 '欢迎使用 Smarty'

// 显示模板
$smarty->display('index.tpl');  // 使用 'index.tpl' 模板文件进行显示
?>

4.Smarty3模版引用——有漏洞

<?php

// 定义 Smarty 根目录路径常量
define('SMARTY_ROOT_DIR', str_replace('\\', '/', __DIR__));

// 定义 Smarty 编译文件目录路径常量
define('SMARTY_COMPILE_DIR', SMARTY_ROOT_DIR . '/smarty3/tmp/templates_c');

// 定义 Smarty 缓存文件目录路径常量
define('SMARTY_CACHE_DIR', SMARTY_ROOT_DIR . '/smarty3/tmp/cache');

// 包含 Smarty 类文件
include_once(SMARTY_ROOT_DIR . '/smarty3/libs/Smarty.class.php');

// 自定义 Smarty 资源类,继承自 Smarty_Resource_Custom
class testSmarty extends Smarty_Resource_Custom
{
    // 重写 fetch 方法
    protected function fetch($name, &$source, &$mtime)
    {
        // 自定义模板内容,这里只是示例,实际情况应该从数据库或其他来源获取模板内容
        $template = "CVE-2017-1000480 smarty PHP code injection";
        // 将模板内容赋值给 $source 参数
        $source = $template;
        // 设置模板修改时间为当前时间
        $mtime = time();
    }
}

// 创建 Smarty 实例
$smarty = new Smarty();

// 设置 Smarty 缓存文件目录
$smarty->setCacheDir(SMARTY_CACHE_DIR);

// 设置 Smarty 编译文件目录
$smarty->setCompileDir(SMARTY_COMPILE_DIR);

// 注册自定义资源类型 'test',并指定使用 testSmarty 类处理该资源类型
$smarty->registerResource('test', new testSmarty);

// 获取要显示的模板名称,并通过 $_GET['eval'] 获取,存在安全风险,应谨慎使用
$templateName = 'test:' . $_GET['eval'];

// 显示模板
$smarty->display($templateName);

?>