Luv{Flag}
article thumbnail
Published 2023. 7. 17. 11:37
[NoSQL] Redis DB -1 Web/SQL, NoSQL
반응형

 

Redis 에서 발생 할 수 있는 취약점을 알아보기 전에, 먼저 redis 에 대해 알아보자

 

 

 

Redis

레디스는 In-Memory 데이터베이스

모든 데이터를 메모리에 저장하고 조회

 

디스크 기반 저장소의 메모리는 매우 작아서, disk 기반

메모리 기반 저장소(redis)

메모리에 상주하고 있기 때문에 자동적으로 휘발, disk는 옵셔널함

  • 레디스는 캐쉬서버인가? ⇒ 캐시서버로 사용가능하지만 저장소임
  • 디스크보다 매우 빠르다
  • data expire 가능(지정된 시간 이후에 만료가능)
  • data 타입 ⇒ key-value
  • 다양한 자료구조 지원
  • “/etc/redis/redis.conf” 레디스 설정 파일, requirepass는 패스워드를 지정

 

$sudo apt install redis-tools

$sudo apt install redis-server

$redis-cli 실행

 

 


 

 

Redis Common Keys

  • Redis 의 키는 문자열이다.
  • SORT는 입력된 키에 해당하는 아이템을 정렬하여 보여준다.
  • 기존에 정렬되지 않은 상태로 저장된 set같은 경우, 커맨드를 이용한 정렬이 가능하므로 유용하게 사용될 수 있다.
  • EXISTS 커맨드는 해당 키가 레디스에 있는지 확인하고, DEL 커맨드는 값에 관계없이 키를 삭제한다.
  • TYPE 커맨드는 해당 키에 연결된 자료구조가 어떤 형태인지 반환한다.

 

 


 

 

Redis Data structure

[string]

→ 모든 종류의 문자열을 저장할 수있다.

→ JPEG 이미지저장. HTML fragment 캐시하는 용도로 자주 사용한다.

→ 문자열을 다른 문자열에 매핑

 

=> 100초의 기간을 가진 test 키를 생성해 보겠다.

127.0.0.1:6379> set test "test1" ex 100
OK

127.0.0.1:6379> ttl test
(integer) 95

127.0.0.1:6379> get test
"test1"

 

set test “test_to_test” ex 100 < 100초의 기간을 가진 test 키 생성

ttl test < test 키의 남은 시간 확인

get test < test 키 value 가져오기

 

 

 

[Hash] - hset

 

→ 키 - 필드

→ key에 대한 filed의 갯수에는 제한이 없다

→ field와 value로 구성된다는 면에서 hash는 RDB의 table과 비슷하다

→ hash key는 table의 PK, field는 column, value는 value로 볼 수 있다. key가 PK와 같은 역할을 하기 때문에 key 하나는 table의 row와 같다

 

 

127.0.0.1:6379> hset hashtest field1 "hihello"  # hashtest 에 field1값은 "hihello"
(integer) 1

127.0.0.1:6379> hgetall hashtest      #field1 hash 삭제
1) "field1"
2) "hihello"

127.0.0.1:6379> hget hashtest field1  #field1 hash 가져오기
"hello"

127.0.0.1:6379> hdel hashtest field1  #field1 hash 삭제
(integer) 1

127.0.0.1:6379> hget hashtest field1
(nil)

 

 

 

 

[List] - lset

 

→ linked list 형태로 연결

→ 특정 값이나 인덱스로 데이터를 찾거나 삭제할 수 있음

 

 

 


 

 

[Reids] - Redis Persistence

 

Redis가 디스크에 데이터를 쓰는 방법 - data 영속성을 위해 사용하는 두 방식 AOF,  RDB

 

 

 

RDB (Redis DataBase)

 지정된 간격으로 데이터 세트의 특정 시점 스냅샷을 수행한다.
리부팅 시에 스냅 파일을 읽어 데이터를 복구시킨다.

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behavior will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

 

redis.conf - redis default 설정 파일 (RDB)

  • 900초 동안 1번 이상의 key 변경이 발생하면 저장
  • 300초 동안 10번 이상의 key 변경이 발생하면 저장
  • 60초 동안 10000번 이상의 key 변경이 발생하면 저장
  • save x초 동안 y 번 이상의 key 변경이 발생하면 저장한다.
  • 조건 여러개의 중복 적용이 가능하다.

 

dump.rdb

  •  스냅샷 파일의 이름은 "dump.rdb", 바이너리 파일이다.
  • redis 에 import 또는 load 하여 사용한다.

 

자세한 파일 구조는 아래 링크에서 확인 할 수 있다.

https://github.com/sripathikrishnan/redis-rdb-tools/wiki/Redis-RDB-Dump-File-Format

 

Redis RDB Dump File Format

Parse Redis dump.rdb files, Analyze Memory, and Export Data to JSON - sripathikrishnan/redis-rdb-tools

github.com

 

 

 

AOF (Append Only File)

조회를 제외한 서버에서 수신한 모든 쓰기 작업을 기록한다. 즉, 명령이 실행될 때마다 해당 명령이 파일에 기록다.
역시 AOF 파일로 저장된다.

 

 

 

appendonly no

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

 

redis.conf - redis default 설정 파일 (AOF)

  • appendonly [ yes | no ] - AOF 사용 여부 
  • appendfilename [ filename ] - AOF 파일 명 지정
  • appendfsync [ always | everysec | no ] -  AOF 파일에 기록하는 시간 지정
  • auto-aof-rewrite-percentage [ 0-100 ] - rewrite 하는 최소 사이즈 지정 (%)
  • auto-aof-rewrite-min-size [ size ] - rewrite 하는 최소 사이즈 지정

 

appendonly.aof 

  • AOF 파일은 text 파일이므로 edit 가능하다 → 명령어 수정/삭제 후 서버 재부팅시 적용가능
  • AOF 파일은 특정 조건에서 rewrite 한다.
  • rewrite 시에 이전 기록은 모두 삭제되며 최종 메모리안 값이 저장된다.

 

 

+ AOF와 RDB를 같이 사용할 수 있다.

 

 


 

[Reids] - Redis vulnerabilities

  • array type
  • SSRF
  • Serialize / Deserialize
  • SAVE - WEBSHELL

 

다음글에서는, Redis에서 일어날 수 있는 취약점에 대해 알아보고 실습 해보도록 하겠다.

 

 

 

 

Reference

 

http://redisgate.kr/redis/configuration/persistence.php

https://redis.io/docs/data-types/

https://redis.io/docs/management/persistence/

 

 

 

반응형

검색 태그

loading