Metasploit使ってみた

2020-02-14 2020-02-15

RubyのキラーアプリはRailsじゃなくてMetasploitだと思います。

病的なインストールガイド

PostgreSQL

btrfsならinstall -d -o 71 -g 71 /var/lib/postgresql; chattr +C /var/lib/postgresqlでCoWを無効にしておく。 71postgresのuid/gid。NixOSはuid/gidの対応を ids.nix で与えている。

services.postgresql = {
  enable = true;
  package = pkgs.postgresql_11;
  authentication = lib.mkForce ''
    hostssl all all 127.0.0.1/32 cert clientcert=1
    hostssl all all ::1/128 cert clientcert=1
    local all postgres peer
  '';
  extraConfig = ''
    ssl = on
    ssl_ca_file = '/var/lib/postgresql/ca.crt'
    ssl_cert_file = '/var/lib/postgresql/localhost.crt'
    ssl_key_file = '/var/lib/postgresql/localhost.key'
  '';
};

SSL on PostgreSQL

パスワードの設定が嫌なので、SSL接続を設定する。 前節のservices.postgresql.authentication(pg_hba.conf)はほぼSSL接続しか受け入れない。

自己署名CAと証明書を用意する。 秘密鍵に対して署名要求(csr)を発行して署名の繰り返し。

openssl ecparam -name secp521r1 -genkey -out ca.key
openssl req -new -key ca.key -out ca.csr
openssl x509 -req -days 7300 -signkey ca.key -in ca.csr -out ca.crt

openssl ecparam -name secp521r1 -genkey -out localhost.key
openssl req -new -key localhost.key -out localhost.csr
openssl x509 -req -days 7300 -CA ca.crt -CAkey ca.key -CAcreateserial -in localhost.csr -out localhost.crt

openssl ecparam -name secp521r1 -genkey -out user.key
openssl req -new -key user.key -out user.csr
openssl x509 -req -days 7300 -CA ca.crt -CAkey ca.key -CAcreateserial -in user.csr -out user.crt

user.crtのCN(Common Name)はPostgreSQLに接続するユーザ名と一致させておく。 localhost.crtのCNをlocalhostにしておくとverify-fullすら可能。

cf. https://ozuma.hatenablog.jp/entry/20130511/1368284304

cf. https://joelonsql.com/2013/04/27/securing-postgresql-using-hostssl-cert-clientcert1/

検算用コマンド。csrcrtfileコマンドでも確認できる。

openssl ec -text -noout -in *.key
openssl req -text -noout -in *.csr
openssl x509 -text -noout -in *.crt
openssl verify -CAfile ca.crt localhost.crt
openssl verify -CAfile ca.crt user.crt

ca.{crt,key}localhost.{crt,key}/var/lib/postgresql直下に、 user.{crt,key}~/.postgresql/user.{crt,key}にでも配置する。

先んじてmsfユーザとmsfデータベースを作成しておく。 services.postgresql.ensure{Databases,Users}でも多分できる。

$ sudo -u postgres -i
postgres $ createuser --interactive
Enter name of role to add: msf
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
postgres $ createdb msf
postgres $ exit

postgresというシステムユーザに1回でもログインしたくないなら、 CNをpostgresにしたpostgres.crtを用意するとpsqlから設定できる。

$ export PGSSLMODE=verify-full
$ export PGSSLCERT=~/.postgresql/postgres.crt
$ export PGSSLKEY=~/.postgresql/postgres.key
$ export PGSSLROOTCERT=/var/lib/postgresql/ca.crt
$ psql -h localhost -p 5432 -U postgres -d postgres
psql (11.6)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=> CREATE ROLE <CN of user.crt> INHERIT LOGIN;
CREATE ROLE
postgres=> CREATE DATABASE msf;
CREATE DATABASE
postgres=> \q
$ export PGSSLCERT=~/.postgresql/user.crt
$ export PGSSLKEY=~/.postgresql/user.key
$ psql -h localhost -p 5432 -U <CN of user.crt> -d msf
psql (11.6)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

msf=> \q

ローカルにmsfユーザを作らず、パスワードも必要ないログインが設定できた。

Metasploit

MetasploitはRailsのActiveRecordでDBに接続する。 ActiveRecordのPostgreSQL adapterはruby-pgを使っているので、libpqのオプションが使える。

https://www.postgresql.org/docs/current/libpq-connect.html

$ cat ~/.msf4/database.yml
production:
  adapter: postgresql
  database: msf
  username: <CN of user.crt>
  host: localhost
  port: 5432
  pool: 5
  timeout: 5
  sslmode: verify-full
  sslrootcert: /var/lib/postgresql/ca.crt
  sslcert: <location of user.crt>
  sslkey: <location of user.key>

NixOSを使っているので、インストール自体は一発で完了。

$ nix-shell -p metasploit
$ msfconsole
...
       =[ metasploit v5.0.45-dev                          ]
+ -- --=[ 1918 exploits - 1074 auxiliary - 330 post       ]
+ -- --=[ 556 payloads - 45 encoders - 10 nops            ]
+ -- --=[ 4 evasion                                       ]

msf5 >

これだけ頑張っても検索がチョット早くなるだけらしいですが。