1/30/2020

When frequently db connection is lost in Metasploit, How to solved

Sometimes DB Connection suddenly disappears when you are using Metasploit.
간혹 Metasploit을 사용하고 있으면 갑자기 DB Connection이 사라지곤합니다.
Database not connected

it's easy to solve with simple tricks.
제 환경에서도 특정 PC에서 유독 잘 끊어지는데요, 간단한 트릭으로 쉽게 해결이 가능합니다.

Change timeout in config

By default, Metasploit's DB setting has a timeout of 5 sec. It's usually a timeout that won't be a problem at all, but there are times when traffic is backed up (when there's a lot of scanning or other work) and you get more than 5 seconds.
기본적으로 Metasploit의 DB 설정에는 timetout이 5로 잡혀있습니다. 보통은 전혀 지장없을 timeout이지만, 전체적으로 트래픽이 밀릴 때 (스캔이나 각종 작업이 많이 돌아갈 때) 5초를 넘기는 경우가 발생합니다.

In case of this, it's much better to increase the timeout figure to some extent to reduce the hassle of using it.
이런 경우를 대비해서 timeout 수치를 어느정도 늘려줘서 사용하는게 번거로운 작업을 줄일 수 있어서 훨씬 좋습니다.

# before
timeout: 5

# after
timeout: 10

If used with a git clone, it is specified in the directory config/database.yml, and is located in the path below if installed with a MacOS pkg.
직접 clone 해서 사용하는 경우 해당 디렉토리 내 config/database.yml에 명시되어 있으며 MacOS pkg로 설치한 경우 아래 경로에 위치합니다. (아마 대부분 다 비슷합니다.)

/opt/metasploit-framework/embedded/framework/config/database.yml
# To set up a metasploit database, follow the directions hosted at:
# http://r-7.co/MSF-DEV#set-up-postgresql
#
# Kali Linux and the Omnibus installers both include an easy wrapper script for
# managing your database, which may be more convenient than rolling your own.development: &pgsql
  adapter: postgresql
  database: *****
  username: *****
  password: *****
  host: localhost
  port: 5432
  pool: 200
  timeout: 10  # (5=>10 등 높게 설정, Increase the timeout.)

alias to re-connection commands

If you have to set the timeout short or if it keeps disconnecting even if you stretch it long, you have to reconnect the db, which is a very troublesome process. If defined as yml, -y option is available, but if used individually, db address, user, passwd, etc. must be entered in the command line, so it is a bit troublesome. So if you use yml, you can easy re-connect database it with alias.
어쩔수 없이 timeout을 짧게 잡아야하거나, 길게 늘여줘도 자꾸 끊긴다면 db를 재접속 해야하는데요, 이 과정이 굉장히 귀찮기 나름입니다. yml로 정의 한 경우 -y 옵션으로 가능하지만, 개별적으로 사용하신다면 db 주소, user , passwd 등의 정보를 command line에서 모두 입력해야하기 때문에 좀 귀찮습니다. 그래서 yml을 쓴다면 그대로, 없다면 새로 하나 만든 후 alias로 re-connect 하는 명령을 만들어주면 편리합니다.

우선 yml 파일을 이용한 db연결은 아래와 같습니다.
db_connect -y /opt/metasploit-framework/embedded/framework/config/database.yml

Create a command using the alias command inside the metasploit console. my case is dbdb.
metasploit 콘솔 내부에서 alias 명령을 이용해 명령어를 만들어둡시다. 저는 dbdb라고 했어요.

alias dbdb "db_connect -y /opt/metasploit-framework/embedded/framework/config/database.yml"

The command line allows you to reconnect immediately.
dbdb라고 이름을 지정했고, 명령행에서 사용하면 바로 재 연결이 가능합니다.



dbdb
[*] Connected to remote_data_service: (https://localhost:5432). Connection type: http. Connection name: local-https-data-service.

don't want to add to alias every time, so please refer to the article below to make sure that it is registered automatically during init metasploit.
매번 alias에 추가하는것도 귀찮으니 아래 글 참고해서 startup 시 자동 등록되도록 하면 깔끔합니다.

https://www.hahwul.com/2017/07/metasploit-make-custom-msfconsole-with.html


HAHWUL

Security engineer, Gopher and H4cker!

Share: | Coffee Me: