728x90
반응형
SMALL
반응형
SMALL

 

Spring Boot + Spring Data JPA + Hibernate + PostgreSQL + Docker를 이용해서 프로젝트를 진행하고 있는데, 지속적으로 내 데이터베이스가 사라지는 현상이 나타났다.

 

처음에는 원인을 DB 과부하라고 생각했어서 쿼리 튜닝부터 커넥션 수 변경, 커넥션 지속 시간 변경, 커넥션 타임아웃 시간 변경, 간헐적으로 시간이 오래 걸릴 가능성이 있는 서비스 로직과 트랜잭션 분리 등 정말 갖가지 방법을 동원해서 유지보수를 진행했는데도 터진다.. 

 

데이터베이스가 그렇다고 무거운것도 아니다.. 기껏해봐야 데이터베이스 덤프 사이즈는255.4K..

아무래도 이건 다른 원인이 있을까 싶어 구글링부터 ChatGPT, Postgresql 매뉴얼까지 검토를 했는데 한가지 의심가는 문구를 봤다.

Postgres database getting hacked multi times.
How To Secure PostgreSQL Against Automated Attacks
 

 

설마라고 생각을 하면서도 당연하게 그럴 수 있다는 사실을 간과하고 있었던거 같은데 우선은 postgresql 로그를 찾아봤다.

그런데 이게 뭐지? 알수도 없는 이름의 유저로 접근하려는 로그가 계속 찍히고 있었다.

2023-12-04 13:17:37.490 KST [73] DETAIL:  Role "q" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:21:27.365 KST [22] LOG:  could not parse file name "pg_logical/snapshots/cpu_hu"
2023-12-04 13:22:22.404 KST [112] FATAL:  password authentication failed for user "mc"
2023-12-04 13:22:22.404 KST [112] DETAIL:  Role "mc" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:26:27.672 KST [22] LOG:  could not parse file name "pg_logical/snapshots/cpu_hu"
2023-12-04 13:27:02.437 KST [138] FATAL:  password authentication failed for user "e"
2023-12-04 13:27:02.437 KST [138] DETAIL:  Role "e" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:28:27.259 KST [147] FATAL:  pg_hba.conf rejects connection for host "78.153.140.30", user "postgres", database "postgres", SSL off
2023-12-04 13:31:44.040 KST [165] FATAL:  password authentication failed for user "tecnico"
2023-12-04 13:31:44.040 KST [165] DETAIL:  Role "tecnico" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:36:28.199 KST [190] FATAL:  password authentication failed for user "eshop"
2023-12-04 13:36:28.199 KST [190] DETAIL:  Role "eshop" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:41:08.902 KST [215] FATAL:  password authentication failed for user "ricardo"
2023-12-04 13:41:08.902 KST [215] DETAIL:  Role "ricardo" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:45:50.217 KST [240] FATAL:  password authentication failed for user "marc"
2023-12-04 13:45:50.217 KST [240] DETAIL:  Role "marc" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:50:33.655 KST [294] FATAL:  password authentication failed for user "admin01"
2023-12-04 13:50:33.655 KST [294] DETAIL:  Role "admin01" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:55:17.143 KST [319] FATAL:  password authentication failed for user "project"
2023-12-04 13:55:17.143 KST [319] DETAIL:  Role "project" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 13:59:57.796 KST [343] FATAL:  password authentication failed for user "biblioteca"
2023-12-04 13:59:57.796 KST [343] DETAIL:  Role "biblioteca" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 14:04:38.642 KST [368] FATAL:  password authentication failed for user "anonymous"
2023-12-04 14:04:38.642 KST [368] DETAIL:  Role "anonymous" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 14:09:22.720 KST [394] FATAL:  password authentication failed for user "auditor"
2023-12-04 14:09:22.720 KST [394] DETAIL:  Role "auditor" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 14:14:07.191 KST [420] FATAL:  password authentication failed for user "bill"
2023-12-04 14:14:07.191 KST [420] DETAIL:  Role "bill" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 14:18:49.173 KST [473] FATAL:  password authentication failed for user "devops"
2023-12-04 14:18:49.173 KST [473] DETAIL:  Role "devops" does not exist.
        Connection matched pg_hba.conf line 136: "host all all all md5"
2023-12-04 14:21:28.269 KST [22] LOG:  could not parse file name "pg_logical/snapshots/cpu_hu"
2023-12-04 14:21:33.406 KST [495] FATAL:  password authentication failed for user "postgres"
2023-12-04 14:21:33.406 KST [495] DETAIL:  Password does not match for user "postgres".
        Connection matched pg_hba.conf line 136: "host all all all md5"

....

 

내가 근데 외부 접근 가능 Address를 all로 하고 있었나? 싶어서 pg_hba.conf 파일을 열어보았다.

그런데 실제로 host all all all md5로 설정이 되어있었다. 아마 내가 dev 환경에서 컨테이너를 띄워 작업하는 중에 이렇게 설정을 해두고 바꾸지 않았나보다..

 

 

아차 싶었다. 우선 가장 빠르게 한 작업은 Docker Compose로 애플리케이션 서버, 디비 서버, 디비 백업 서버를 띄울 때 네트워크를 붙였다. 그리고 딱 세개의 네트워크만 접근 허용을 가능하도록 pg_hba.conf 파일을 변경했다.

 

이제 다른 Address에서 접속하더라도 Reject된다. 이 나쁜 놈들.

 

그리고 한가지 더 변경해줄 사항이 있다. postgresql.conf 파일에 'CONNECTIONS AND AUTHENTICATION' 섹션이 있다.

그 부분에서 listen_addresses = "애플리케이션 컨테이너 ip, 디비 컨테이너 ip, 디비 백업 컨테이너 ip" 로 설정해줘야 한다.

 

 

이제 내가 열어둔 주소가 아닌 주소는 모든지 다 들어올 수 없다. 이렇게 하고 나서 데이터베이스가 아직까지 터지지 않고 있다.. 진짜 해킹인가..? 

 

2023-12-11
이 글을 작성하고 현재 2023-12-11 여전히 데이터베이스는 멀쩡히 살아있다.. 진짜 해킹이었고 어마무시하게 자동화된 해킹 공격을 하더라..

 

2023-12-14
이 글을 작성하고 현재 2023-12-14 여전히 멀쩡히 살아있다. 확실하다.
728x90
반응형
LIST

+ Recent posts