サイバー脅威情報集約システム EXISTの構築

数週間前に、NICTER解析チームから、サイバー脅威情報を自動集約するツール「EXIST」が公開されました。
今回は、このEXISTをAWSサーバー(無料枠)を使って構築していきたいと思います。
https://blog.nicter.jp/2019/03/exist/
AWSでEC2インスタンスを立ち上げる
まず、AWSのアカウントを作成します。
AWSのServicesから「EC2」を選択します。
その後、真ん中にある青い色のボタン「Launch Instance」を選択します。

次に、左側にある「AWS Marketplace」を選択し、 「centos」と検索します。
EXISTでは、centOS7が想定されているので、centOS7を選択します。

すると、centOS7の料金表の詳細が表示されるので、軽く見て、「Continue」を選択します。
続いて、InstanceのTypeを選択する画面が出ますが、ここではデフォルトで選択されているものを使用します。(Free tier eligibleと書かれています。)
そして、青色のボタン「Review and Launch」を選択します。すると確認画面が現れるので、「Launch」を押すと、Keypairを作成する画面に移ります。
ここでは、鍵を新規作成してください。すると、(key_name).pemというものがダウンロードされるはずです。
しばらく待つと、設定が完了し、サーバーに接続できるようになります。
先ほどのDashboardから、左側のメニューからINSTANCES⇨Instancesを押すと、先ほど立ち上げたインスタンスがあるはずです。
右クリックをして、「Connect」を押すと接続方法が表示されます。ここの、4番に書かれているPublic DNSを後で使います。
ここからは、ローカルでの作業になります。今回は、ホームディレクトリにTestディレクトリを作って、その中で作業したいと思います。
mkdir ~/Test
cd ~/Test
ダウンロードしたpemファイルを、Testディレクトリの中に入れます。
mv ~/Downloads/(key_name).pem .
chmod 400 (key_name).pem
先ほど立てたインスタンスに、ssh接続をします。
ssh -i "(key_name).pem" centos@(Public DNS)
これで、AWS EC2のインスタンスの作成が終わりました!
EXISTの構築
gitのインストール
centOSにgitをインストールします。gitの公式ページに加えて、AWS上で動かすには足りないもの(gcc, wget, perl-ExtUtils-MakeMaker)をインストールします。
gitのバージョンは、最新のバージョンに合わせてください。
$ sudo -i
$ sudo yum -y remove git
$ yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker wget gcc
$ cd /usr/local/src/
$ wget https://www.kernel.org/pub/software/scm/git/git-2.21.0.tar.gz
$ tar zxvf git-2.21.0.tar.gz
$ cd git-2.21.0
$ make prefix=/usr/local all
$ make prefix=/usr/local install
$ exit
ユーザーがcentosの状態で、
$ git --version
git version 2.21.0
と出れば、gitの導入は完了です!
Python3の導入
ここでは、Python3を導入します。最新版のPython3.7ではpipでinstallする時にエラーが出るので、ここでは、3.6を使用しています。
$ sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
$ sudo yum install -y python36u python36u-devel python36u-libs
$ sudo ln -s /usr/bin/python3.6 /usr/bin/python3
$ sudo yum install -y python36u-pip
$ sudo ln -s /usr/bin/pip3.6 /usr/bin/pip3
$ python3 -V
Python 3.6.7
これでPython3の導入は完了です。
EXISTの導入
今回は、githubを用いていきたいと思います。
https://github.com/nict-csl/exist
こちらから、右上のForkというボタンを押します。すると、自分のレポジトリにForkされますので、そちらを使っていきたいと思います。
$ git clone https://github.com/(fork先のレポジトリ)
$ cd exist
$ python3.7 -m venv .
$ source bin/activate
$ pip install -r requirements.txt
pip installのコマンドの際、
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-o3_ky5zo/django-pure-pagination/setup.py", line 5, in <module>
README = readme.read()
File "/usr/lib64/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 672: ordinal not in range(128)
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-o3_ky5zo/django-pure-pagination/
以上のようなエラーが出た場合は、django-pure-paginationだけ、別でinstallをします。
pip install https://github.com/Burakhan/django-pure-pagination/archive/master.zip
そうすると、必要なものは全てInstallできるはずです。
MariaDB
基本的に、READMEに書かれている通りです。
$ curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
$ sudo yum install MariaDB-server MariaDB-client
ユーザーを新たに作ります。
CREATE USER exist IDENTIFIED BY 'exist2019';
CREATE DATABASE exist_db;
GRANT ALL PRIVILEGES ON `exist_db`.* TO 'exist'@'localhost' IDENTIFIED BY 'exist2019';
djangoの設定ファイルにDB情報を記載します。
$ cd intelligence/
$ cp settings.py.template settings.py
///// 変更点のみ /////
ALLOWED_HOSTS = [
# '192.168.56.101',
'ホスト名(~~~~.amazonaws.com)',
]
DATABASES = {
'default': {
'NAME': 'exist_db',
'USER': 'exist',
'PASSWORD': 'exist',
}
}
existのホームディレクトリまで戻って、
$ python manage.py makemigrations exploit reputation threat threat_hunter twitter twitter_hunter
$ python manage.py migrate
こうすると、必要なDBが作成されます。
Redis
$ sudo yum install redis
$ sudo systemctl start redis
$ sudo systemctl enable redis
Celery
これらも同様です。
$ which celery
/usr/bin/celery
$ sudo vi /etc/sysconfig/celery
# Name of nodes to start
# here we have a single node
CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
CELERY_BIN="~/exist/bin/celery"
# App instance to use
# comment out this line if you don't use an app
CELERY_APP="intelligence"
# or fully qualified:
#CELERY_APP="proj.tasks:app"
# How to call manage.py
CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
serviceでは注意する点があります。User・Group・WorkingDirectoryの値は、環境によって異なりますが、このサイトに従っていれば変更する必要はないかと思います。
$ sudo vi /etc/systemd/system/celery.service
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=centos
Group=centos
EnvironmentFile=/etc/sysconfig/celery
WorkingDirectory=/home/centos/exist
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
[Install]
WantedBy=multi-user.target
$ sudo mkdir /var/log/celery;
$ sudo chown centos:centos /var/log/celery
$ sudo mkdir /var/run/celery;
$ sudo chown centos:centos /var/run/celery
$ sudo systemctl start celery.service
$ sudo systemctl enable celery.service
Djangoの起動
AWSのDashboradの左のメニューから、SecurityGroupsを選択。該当するグループのInboundにおいて、「Cunstom TCP / 8080」を新たに加える。その後、ターミナル上で、
$ python manage.py runserver (ホスト名):8080
とすると、(ホスト名):8080でサイトを見ることができる。

以上で、EXISTの基本設定は終わりです。
各種設定
$ cd scripts/insert2db/conf/
$ cp insert2db.conf.template insert2db.conf
$ vi insert2db.conf
以下のスクリプトを実行することで、データを取得します。
$ python scripts/insert2db/reputation/insert2db.py
$ python scripts/insert2db/twitter/insert2db.py
$ python scripts/insert2db/exploit/insert2db.py
$ python scripts/insert2db/threat/insert2db.py
$ cd scripts/hunter/conf/
$ cp hunter.conf.template hunter.conf
$ vi hunter.conf
お疲れ様でした!!!!!
これで、EXISTの基本的な設定は終わりです!
あとは、各自で設定してもらえると、より使いやすくなると思います。
-
前の記事
LetsEncryptを使って、常時SSL化をする 2019.05.07
-
次の記事
記事がありません