카테고리 없음

PostgreSQL Security

남용2 2019. 11. 22. 15:37

보안에 있어서 Client Authentication과 Database Role, Data 암호화는 중요한 역할을 한다.

1. Client Authentication

참고 : https://www.postgresql.org/docs/10/auth-methods.html

2. Role 부여 및 Public에 대한 Role 회수

 

3. Data 암호화

암호화 관련 함수를 사용하기 위해 pgcrypto 암호화 모듈을 설치한다. 

CREATE EXTENSION pgcrypto;

pgcrypto 모듈의 function 중 Password Hashing FunctionsRaw Encryption Functions 에 대한 설명이다.

1) Password Hashing Functions

crypt() 및 gen_salt() 함수는 Password Hashing을 위해 특별히 설계되었으며, 아래와 같이 사용할 수 있다.

SELECT CRYPT('암호화할 문자열', gen_salt('암호화방식'));

 

pgcrypto를 이용하여 MD5 방식으로 암호화 및 확인하는 방법이다.

INSERT INTO t_crypt (user_id, user_pw) VALUES (1, CRYPT('password01', gen_salt('md5'))), (2, CRYPT('password02', gen_salt('md5')));

 

SELECT * FROM t_crypt WHERE user_pw = CRYPT('password01', user_pw);

 

* Supported Algorithms for crypt()

Algorithm Max Password Length Adaptive Salt Bits Output Length Description
bf  72 yes 128 60 Blowfish-based, variant 2a
md5 unlimited no 48 24 MD5-based crypt
xdes  8 yes 24 20 Extended DES
des  8 no 12 13 Original UNIX crypt 

 

[참고]

# \df crypt
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-------+------------------+---------------------+--------
public | crypt | text | text, text | normal

 

# \df gen_salt
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+----------+------------------+---------------------+--------
public | gen_salt | text | text | normal
public | gen_salt | text | text, integer | normal

2) Raw Encryption Functions

* encrypt/decrypt : 암호화/복호화

* conver_to/conver_from : 문자열 변환/복원

SELECT encrypt ('Hello World', 'my key', 'aes');

SELECT decrypt ('\xe3d6d1ddea318dbf88e34421fd095727', 'my key' , 'aes');

-- 문자열 변환

SELECT convert_from('\x48656c6c6f20576f726c64', 'utf-8');

 

[참고]

# \df encrypt
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+---------+------------------+---------------------+--------
public | encrypt | bytea | bytea, bytea, text | normal

 

# \df decrypt
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+---------+------------------+---------------------+--------
public | decrypt | bytea | bytea, bytea, text | normal