zoukankan      html  css  js  c++  java
  • Natas11 Writeup(常见编码、异或逆推、修改cookie)

    Natas11:

    页面提示cookie被异或加密保护,查看源码,发现了一个预定义参数和三个函数。

    //预定义参数,猜测将showpassword设置为yes即可得到密码。
    $defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff"); 
    
    //异或加密函数
    function xor_encrypt($in) {
        $key = '<censored>';	//预定参数key
        $text = $in;			//输入参数
        $outText = '';			//输出参数
    
        // Iterate through each character
        for($i=0;$i<strlen($text);$i++) {					//for循环,遍历输入参数
        $outText .= $text[$i] ^ $key[$i % strlen($key)];	//将输入参数对应位和key对应位异或,key位数不够则从头循环,结果存到输出参数
        }
    
        return $outText;		//返回加密结果
    }
    
    //加载函数:将$_COOKIE["data"]解密还原,存为 $mydata 数组,返回$mydata。
    function loadData($def) {
        global $_COOKIE;
        $mydata = $def;
        if(array_key_exists("data", $_COOKIE)) {
        $tempdata = json_decode(xor_encrypt(base64_decode($_COOKIE["data"])), true);
        if(is_array($tempdata) && array_key_exists("showpassword", $tempdata) && array_key_exists("bgcolor", $tempdata)) {
            if (preg_match('/^#(?:[a-fd]{6})$/i', $tempdata['bgcolor'])) {
            $mydata['showpassword'] = $tempdata['showpassword'];
            $mydata['bgcolor'] = $tempdata['bgcolor'];
            }
        }
        }
        return $mydata;
    }
    
    //保存函数:将传入的参数,经过编码处理,存入$_COOKIE["data"]中。
    function saveData($d) {
        setcookie("data", base64_encode(xor_encrypt(json_encode($d))));
    }
    
    $data = loadData($defaultdata);
    
    if(array_key_exists("bgcolor",$_REQUEST)) {
        if (preg_match('/^#(?:[a-fd]{6})$/i', $_REQUEST['bgcolor'])) {
            $data['bgcolor'] = $_REQUEST['bgcolor'];
        }
    }
    
    saveData($data);
    
    //将showpassword设置为yes即可得到密码
    if($data["showpassword"] == "yes") {
        print "The password for natas12 is <censored><br>";
    }
    

    主要思路就是构造新的输入参数,使得"showpassword"=>"yes",编码后得到新的data。这就要求要知道key的值,而已有一个默认值,由此逆推得到key。

    通过burp抓包,可以发现$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff")对应的data值为“ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw=”。

    我们知道,异或的逆操作还是异或。由此我们可以逆推得到key。$key = ’qw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jq’

    <?php
    $defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");
    $data= 'ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw=';
    function xor_encrypt($in,$out) {
        $key ='' ;
        $text = $in;
        for($i=0;$i<strlen($text);$i++) {
        $key .= $text[$i] ^ $out[$i];
        }
        return $key;
    }
     echo xor_encrypt(json_encode($defaultdata),base64_decode($data)); 
    ?>
    

    再利用key,构造新data。新data=ClVLIh4ASCsCBE8lAxMacFMOXTlTWxooFhRXJh4FGnBTVF4sFxFeLFMK

    <?php
    $defaultdata = array( "showpassword"=>"yes", "bgcolor"=>"#ffffff");
    function xor_encrypt($in) {
        $key = 'qw8J';
        $text = $in;
        $outText = '';
     
        // Iterate through each character
        for($i=0;$i<strlen($text);$i++) {
        $outText .= $text[$i] ^ $key[$i % strlen($key)];
        }
        return $outText;
    }
    echo base64_encode(xor_encrypt(json_encode($defaultdata)));
    ?>
    

    将新的data替换cookie中的data,点击Go,得到flag。

    flag:EDXp0pS26wLKHZy1rDBPUZk0RKfLGIR3

     
    参考:https://www.cnblogs.com/ichunqiu/p/9554885.html
  • 相关阅读:
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale
    CodeForces 785B Anton and Classes
    CodeForces 785A Anton and Polyhedrons
    爱奇艺全国高校算法大赛初赛C
    爱奇艺全国高校算法大赛初赛B
    爱奇艺全国高校算法大赛初赛A
    EOJ 3265 七巧板
    EOJ 3256 拼音魔法
    EOJ 3262 黑心啤酒厂
  • 原文地址:https://www.cnblogs.com/zhengna/p/12382831.html
Copyright © 2011-2022 走看看