前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一道js审计题

一道js审计题

原创
作者头像
c2k2o6
发布2022-02-15 14:22:50
2500
发布2022-02-15 14:22:50
举报
文章被收录于专栏:c2k2o6

https://blog.csdn.net/qq_32445689/article/details/50889249

https://mp.weixin.qq.com/s/FPbOoOnn-Wa0ArELLRraIA

代码语言:javascript
复制
function test(arr) {
    var random_value = "ac1a39300ce7ee8b6cff8021fd7b0b5caf5bc1c316697bd8f22e00f9fab710d6b8dba23ca80f6d80ca697e7aa26fd5f6";
    var check = "20150303";

    if((arr === null || arr === undefined)) {
        console.log("arr is null or undefined.");
        return;
    }

    if(!arr.hasOwnProperty('length')) {
        console.log("length property is null or undefined.");
        return;
    }

    if(arr.length >= 0) {
        console.log("i think you're not geek. From now on, a GEEK Only!");
        return;
    }

    if(Object.getPrototypeOf(arr) !== Array.prototype) {
        console.log("Oh.... can you give me an array?");
        return;
    }

    var length = check.length;
    for(var i=0;i<length;i++) {
        arr[i] = random_value[Math.floor(Math.random() * random_value.length)];
    }

    for(i=0;i<length;i++) {
        if(arr[i] !== check[i]) {
            console.log("Umm... i think 2015/03/03 is so special day.\nso you must set random value to 20150303 :)");
            return;
        }
    }
    console.log("Yay!!");
}

要求输出 最后的 Yay!!

看样子我们需要输入一个数组

代码语言:javascript
复制

var arr=[];
test(arr);

会输出
i think you're not geek. From now on, a GEEK Only!

数组的长度不能是 >= 0

如果我直接 给数组的长度赋值

arr.length = -1

会报错

图片
图片

定义一个对象 其中有 length属性

代码语言:javascript
复制
var arr={length:-1};
test(arr);
输出
Oh.... can you give me an array?

if(Object.getPrototypeOf(arr) !== Array.prototype) {
这里要求必须是数组
代码语言:javascript
复制

Object.getPrototypeOf()
The Object.getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.
返回prototype属性

修改arr的prototype属性 指向 Array.prototype

代码语言:javascript
复制

var arr={length:-1};
arr.prototype=Array.prototype;
test(arr);

还是不行
Oh.... can you give me an array?
代码语言:javascript
复制

Array.prototype
[constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]
代码语言:javascript
复制

Object.getPrototypeOf(arr)
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
还是object对象类型 而不是数组类型

采用另外一种对象生成的方式

代码语言:javascript
复制
var MyArr = function(){
    this.length=-1;
}
MyArr.prototype = Array.prototype;
var arr=new MyArr();
test(arr);

输出
Umm... i think 2015/03/03 is so special day.
so you must set random value to 20150303 :)

过了 进入下一关
代码语言:javascript
复制

下一关
出题人给arr赋值 而且是随机数 然后还需要和 check相等
有没有方法让 对象的值 无法更改 从外部赋值完成 再传入函数中
Object.freeze() 方法
代码语言:javascript
复制
Object.freeze() 方法可以冻结一个对象。冻结对象是指那些不能添加新的属性,不能修改已有属性的值,不能删除已有属性,以及不能修改已有属性的可枚举性、可配置性、可写性的对象。也就是说,这个对象永远是不可变的。该方法返回被冻结的对象。
语法EDIT Object.freeze(obj)
代码语言:javascript
复制
var MyArr=function(){
    this.length=-1;
}
MyArr.prototype=Array.prototype
var arr=new MyArr()
arr[0]='2';
arr[1]='0';
arr[2]='1';
arr[3]='5';
arr[4]='0';
arr[5]='3';
arr[6]='0';
arr[7]='3';
Object.freeze(arr);
test(arr);
输出
Yay!! 
成功
图片
图片

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档