我正在编写一些关于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" )
不起作用。
答案 0 :(得分:9)
嗯,你在module
中也有node
变量。因此,您可以要求vm
包并使用其require
方法运行代码:
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
和node
版本&gt; = 0.11.x中,您可以使用contextify
绑定:
var contextify = process.binding('contextify');
var script = new contextify.ContextifyScript(' console.log("im here, buddy") ');
script.runInThisContext();
node
版本&lt; 0.11.x您可以使用evals
绑定:
var evals = process.binding('evals');
var script = new evals.NodeScript(' console.log("here I am") ')
script.runInThisContext();
答案 1 :(得分:5)
module.require = undefined;
是不够的,因为require
继承自Module原型:
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');
答案 2 :(得分:0)
使用GeneratorFunction构造函数:
<i className="fas fa-user"></i>