[网络安全] DVWA之 Open HTTP Redirect 攻击姿势及解题详析合集

发布时间 2023-06-15 13:19:34作者: 秋说

Low level

主页面如下:

在这里插入图片描述
点击 Quote 1,发现url传递参数

在这里插入图片描述

源代码审计

源码如下:

<?php

if (array_key_exists ("redirect", $_GET) && $_GET['redirect'] != "") {
    header ("location: " . $_GET['redirect']);
    exit;
}

http_response_code (500);
?>
<p>Missing redirect target.</p>
<?php
exit;
?>

源码根据 $_GET 数组中的 redirect 键值,在 HTTP 响应头中设置 Location 字段,实现页面重定向的功能。具体实现步骤如下:

1.首先通过 array_key_exists 函数判断 $_GET 数组中是否存在 redirect 键值,如果存在且不为空,则调用 header 函数,否则返回 500 状态码。

2.调用 header 函数,将 Location 字段设置为 $_GET['redirect'] 的值,完成重定向操作。exit 函数用于终止脚本的执行,确保 header 函数的执行效果。

姿势

由于源码没有对重定向参数传递进行任何过滤

因此可构造Payload如下:
?redirect=跳转网页URL

示例如下:
?redirect=https://www.cnblogs.com/qiushuo/

在这里插入图片描述

Medium level

源代码审计

<?php

if (array_key_exists ("redirect", $_GET) && $_GET['redirect'] != "") {
    if (preg_match ("/http:\/\/|https:\/\//i", $_GET['redirect'])) {
        http_response_code (500);
        ?>
        <p>Absolute URLs not allowed.</p>
        <?php
        exit;
    } else {
        header ("location: " . $_GET['redirect']);
        exit;
    }
}

http_response_code (500);
?>
<p>Missing redirect target.</p>
<?php
exit;
?> 

该源码防止了绝对 URL 的重定向。 下面是代码的执行流程:

  1. 首先,代码检查是否存在 redirect 键在 $_GET 数组中,并且其值不为空。

  2. 如果满足条件,则进行进一步判断。

    • 使用正则表达式 preg_match 来检查 $_GET['redirect'] 的值是否以 "http://" 或 "https://" 开头(不区分大小写)。
    • 如果匹配到了这两个前缀,说明是一个绝对 URL,会返回 HTTP 响应码 500,并输出提示信息 " Absolute URLs not allowed ",然后程序终止执行。
    • 如果没有匹配到绝对 URL 的前缀,即以相对路径形式存在,将执行接下来的代码。
      • 使用 header 函数将浏览器重定向到 $_GET['redirect'] 指定的地址。
      • 执行 exit 终止后续代码的执行。
  3. 如果上述条件都不满足,即未提供 "redirect" 参数或其值为空,将返回 HTTP 响应码 500,并输出提示信息 "Missing redirect target.",然后程序终止执行。

姿势

  1. 使用其他方式实现重定向:尝试使用其他方法或工具来实现重定向效果,例如使用 JavaScript 进行重定向或者使用服务器端的重定向规则。

以下是一个示例 JavaScript 代码:

<script type="text/javascript">
    var redirectURL = "http://www.example.com"; // 设置要重定向的网页地址
    window.location.href = redirectURL; // 执行重定向
</script>

将上述代码插入到编辑器的代码中,加载该网页时,JavaScript 代码会将用户重定向到指定的网页。

  1. 添加重定向规则:如果具有服务器的访问权限,并且使用的是 Apache 或 Nginx 等服务器,可以通过修改服务器配置文件来添加重定向规则。例如,在 Apache 的 .htaccess 文件中添加以下规则实现重定向:

    RewriteEngine On
    RewriteRule ^myredirect$ http://www.baidu.com [L,R=301]
    

    在这个例子中,当用户访问 myredirect,服务器会将其重定向到百度的绝对 URL。

  2. 使用代理页面:创建一个中间代理页面,用户访问该页面时,该页面再将请求重定向到百度。

  • 创建一个名为 redirect.php 的 PHP 文件。
  • redirect.php 文件中添加以下代码:
<?php
header("Location: http://www.baidu.com");
exit;
?>
  • 上传 redirect.php 文件到服务器上,确保它可以被公开访问(例如放置在公共的网站根目录)。

  • 当用户访问 redirect.php 页面时,会被自动重定向到百度的绝对 URL。

High level

源代码审计

<?php

if (array_key_exists ("redirect", $_GET) && $_GET['redirect'] != "") {
    if (strpos($_GET['redirect'], "info.php") !== false) {
        header ("location: " . $_GET['redirect']);
        exit;
    } else {
        http_response_code (500);
        ?>
        <p>You can only redirect to the info page.</p>
        <?php
        exit;
    }
}

http_response_code (500);
?>
<p>Missing redirect target.</p>
<?php
exit;
?> 
  1. 首先,检查 $_GET 数组中是否存在名为 "redirect" 的参数,并且该参数的值不为空。

  2. 如果满足上述条件,进一步检查 $_GET['redirect'] 参数中是否包含字符串 "info.php"。这里使用了 strpos() 函数来检查字符串中是否包含指定的内容。

  3. 如果 $_GET['redirect'] 参数包含字符串 "info.php",则使用 header() 函数进行重定向,将用户重定向到 $_GET['redirect'] 中指定的页面。

  4. 如果 $_GET['redirect'] 参数不包含字符串 "info.php",则返回 HTTP 响应码 500(服务器内部错误)并输出一段提示信息:"You can only redirect to the info page."

  5. 如果没有传递 "redirect" 参数,或者其值为空,则同样返回 HTTP 响应码 500,并输出另一段提示信息:"Missing redirect target."

姿势

由代码审计得,在构造参数时是指包含字符串info.php即可
Payload:?redirect=https//www.baidu.com/?已定义参数=info.php

注意:需要参考网页支持的参数来传递info.php

例如 当该网页没有设定a参数的get传参时
GET ?redirect=https//www.baidu.com/?a=info.php 是无效的
因为字符串"info.php"并未传递给合法的参数

Impossible level

源代码审计

<?php

$target = "";

if (array_key_exists ("redirect", $_GET) && is_numeric($_GET['redirect'])) {
    switch (intval ($_GET['redirect'])) {
        case 1:
            $target = "info.php?id=1";
            break;
        case 2:
            $target = "info.php?id=2";
            break;
        case 99:
            $target = "https://digi.ninja";
            break;
    }
    if ($target != "") {
        header ("location: " . $target);
        exit;
    } else {
        ?>
        Unknown redirect target.
        <?php
        exit;
    }
}

?>
Missing redirect target. 

首先检查 $_GET 数组中是否存在名为 "redirect" 的参数,并且该参数的值是一个数字。如果满足这个条件,脚本会根据指定的值设置不同的重定向目标。

  • 如果 "redirect" 的值是 1,脚本将重定向到 info.php?id=1
  • 如果 "redirect" 的值是 2,脚本将重定向到 info.php?id=2
  • 如果 "redirect" 的值是 99,脚本将重定向到 https://digi.ninja

如果成功设置了目标 URL,脚本会使用 header() 函数进行重定向,并使用 exit 终止脚本的执行。

如果未能设置目标 URL,则显示 "Unknown redirect target." 的消息,并终止脚本的执行。

如果没有指定重定向目标,则显示 "Missing redirect target." 的消息,并终止脚本的执行。

总结

以上为[网络安全] DVWA之Open HTTP Redirect 攻击姿势及解题详析合集,考察php代码审计重定向相关知识。
我是秋说,我们下次见。