块评估&&新function
我只是在编写codewars.com上的一些随机拼图,并且很好奇,如果任何人都可以想到一个方法来评估代码后,下面的代码已经运行:
eval = function(){}; delete Function.prototype.constructor; Function = undefined; // the following are to block require('vm') -- if anyone wants to run this // in production it may be better to block that one module (others?) require = undefined; module.__proto__.require = undefined; // added this due to alexpod's answer, modified due to Fabrício Matté's :) module.constructor = undefined; // added this due to alexpod's answer
这是在node.js,所以setTimeout( "string" )
不起作用。
那么,你也有node
module
variables。 所以你可以使用require
方法来要求vm
包和运行代码:
var vm = module.require('vm'); vm.runInThisContext(' console.log("hello") ');
UPD好了,你更新了这个问题,但我们可以再次入侵:
var vm = module.constructor.prototype.require('vm'); vm.runInThisContext(' console.log("hello") ');
UPD2另一种变体:
var vm = module.constructor._load('vm'); vm.runInThisContext(' console.log("hello") ');
UPD3条件改变了,所以下一个变种:
module.constructor.prototype._compile(' console.log("again hacked") '); // or module.__proto__._compile(' console.log("again hacked") '); // or Object.getPrototypeOf(module)._compile(' console.log("again hacked") ');
我认为更好地设置module = undefined
使问题更复杂:)
UPD4还有另一个变种没有module
🙂
process.stdin.push(' console.log("here we are") \n ');
但它只适用于CLI(“repl”)
UPD5同样在iojs
和版本> = 0.11.x的node
,您可以使用contextify
绑定:
var contextify = process.binding('contextify'); var script = new contextify.ContextifyScript(' console.log("im here, buddy") '); script.runInThisContext();
在版本<0.11.x的node
,您可以使用evals
绑定:
var evals = process.binding('evals'); var script = new evals.NodeScript(' console.log("here I am") ') script.runInThisContext();
module.require = undefined;
是不够的,因为require
是从模块原型inheritance的:
module.require = undefined; var vm = module.__proto__.require('vm'); vm.runInThisContext('console.log(1)');
相反,你应该:
module.__proto__.require = undefined; // now this fails and you can't use the __proto__ trick: var vm = module.require('vm');