iT邦幫忙

4

IT專題 – 安裝 pDNS (Passive DNS)

Joejo 2017-08-22 09:34:4018125 瀏覽

現場(環境):Ubuntu 14.04 、專案套件: bro、bro-pdns、mysql-server

原因:
當初要安裝這個的時候,主要是聽到中華電信再推資安文件內提到pDNS。
所以來了解一下這個東西到底是做什麼用的。之後發現好像蠻適合用在企業內部的。

當然如果運用於企業內的資料量完全無法與外部電信業者的資料量相比。
但pDNS最重要的東西就是資料。
這個資料用來做什麼? 上網查詢了一下文章,大約分為3個大項目。

(1) Domain Name曾經綁定再哪些IP上面
(2) 這個IP有沒有對應到多個網域名稱
(3) Domain Name 最早從什麼時候出現再網路上。

pDNS的資料可以做這個用途,再資料量變大的時候,可以判斷哪些可能是惡意網域名稱。

網路資料上,有說到有許多人允許我們連結他們建立的passive DNS,
這個相關的網站可能要再找找看。

但與其只使用他人提供的資料來判斷,不如自己再企業內建立一個一樣的pDNS,
透過企業內部的存取行為來作分析,
我們可以了解到到底企業內的同仁們到底曾經連線過哪些網域名稱。

運作初期,多數網域名稱都會是第一次偵測到並寫入資料庫當中。

但若運作了半年以上,突然蹦出一個網域名稱是第一次出現在pDNS內,
那這個網域名稱是否安全?當有人惡意的使用DNS DDoS來進行攻擊。

再我們的pDNS中,也可以初步搜尋到對方透過放大攻擊想反解的網域名稱是否存在。

這些都可以做些初步的資料判斷。


處理方式:

進行套件安裝 – bro (pDNS主程式)

#sudo apt-get update

安裝相關套件
#sudo apt-get install cmake make gcc g++ flex bison libpcap-dev libgeoip-dev libssl-dev python-dev zlib1g-dev libmagic-dev swig2.0

安裝bro主程式 (目前已經推到2.5版本)
但我比較習慣使用bro-2.4.1版本。
Bro 套件的網址 :https://www.bro.org/downloads/release/
#wget https://www.bro.org/downloads/release/bro-2.4.1.tar.gz
#tar -zxvf bro-2.4.1.tar.gz
#cd bro-2.4.1

路徑我們想放在nsm下面:
#mkdir /nsm
#./configure --prefix=/nsm/bro

====================| Bro Build Summary |=====================
Install prefix: /nsm/bro
Bro Script Path: /nsm/bro/share/bro
Debug mode: false
CC: /usr/bin/cc
CFLAGS: -Wall -Wno-unused -O2 -g -DNDEBUG
CXX: /usr/bin/c++
CXXFLAGS: -Wall -Wno-unused -O2 -g -DNDEBUG
CPP: /usr/bin/c++
Broker:
Broccoli: true
Broctl: true
Aux. Tools: true
GeoIP: true
gperftools found: false
tcmalloc: false
debugging: false
jemalloc: false

===============================================================
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ryu/bro-2.4.1/build

再來要安裝了,這邊會跑一小段時間(大概要10分鐘左右)。
#make && make install

設定環境變數名稱
export PATH=/nsm/bro/bin:$PATH

到這邊只是用bro 套件安裝好一個簡單的pDNS而已。
我們確認一下/nsm/bro/etc/node.cfg
這邊有說到他接收及回傳資料的網路接口是哪一個!!
下面的設定值內有說到,這邊是使用 [eth0]
#sudo vim /nsm/bro/etc/node.cfg

===============================================================
[bro]
type=standalone
host=localhost
interface=eth0

===============================================================

接著我們簡單的試著運作bro試試
測試運作:
#broctl

Hint: Run the broctl "deploy" command to get started.
Welcome to BroControl 1.4
Type "help" for help.
[BroControl] > status
Getting process status ...
Getting peer status ...
Name Type Host Status Pid Peers Started
bro standalone localhost stopped

[BroControl] > install

creating policy directories ...
installing site policies ...
generating standalone-layout.bro ...
generating local-networks.bro ...
generating broctl-config.bro ...
generating broctl-config.sh ...
updating nodes ...

[BroControl] > status

Getting process status ...
Getting peer status ...
Name Type Host Status Pid Peers Started
bro standalone localhost stopped

[BroControl] > start

starting bro ...

[BroControl] > status

Getting process status ...
Getting peer status ...
Name Type Host Status Pid Peers Started
bro standalone localhost running 15186 0 21 Aug 16:05:19

===============================================================

上面完成後,我們總是要設計一個儲存數據的地方 (mysql)。


來安裝mysql資料庫

#sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

因為是安裝資料庫,他跑到一半會要求你輸入登入mysql 用的 root密碼。

#sudo mysql_install_db

===============================================================

Installing MySQL system tables...
........so you should just press enter here.
Enter current password for root (enter for none):
(詢問你剛剛安裝時輸入的root密碼)

OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
(是否更改root的密碼)
... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
(是否要移除匿名的帳號)
.., Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
(是否拒絕root遠端登入)
... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
(移除測試用的DB)

  • Dropping test database...

ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...

  • Removing privileges on test database...
    ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
(重新載入privilege權限的資料表)
... Success!

Cleaning up...

installation should now be secure.
Thanks for using MySQL!

===============================================================

接下來是要創建mysql 內的 pDNS Database
(1)如果是第一次建立,就創見這個表。
#mysql -u root -p
Enter password:
mysql> create database pdns;

(2)如果之前就已經有資料了。 就用sqldump 把資料塞回來就好。
因為之前偷懶的使用將全部的Database都匯出來,所以這邊只要全塞進去就好
之前我匯出的sql 檔名稱:all_pDNS0808.sql
#sudo mysqldump --user=root -p --all-databases < all_pDNS0808.sql

完成後再次登入mysql看一下是否database都在。
#mysql -u root -p
Enter password:
mysql> show databases;

+-------------------------+

| Database |

+-------------------------+

| information_schema |

| mysql |

| pdns | ← 有看到pdns匯進來了

| performance_schema|

+-------------------------+

4 rows in set (0.00 sec)

(附註說明) 你要進行mysqldump匯入的sql檔,你要有執行權限
#sudo chmod 755 all_pDNS0808.sql
因為很多時候你傳輸資料到其他主機上時,其實只有644權限。
這邊要注意一下。


安裝bro-pdns

在那之前接著我們要使用pip安裝一些套件。
沒有安裝pip的,再去搜尋一下一些方法
#wget https://bootstrap.pypa.io/get-pip.py
#python get-pip.py

透過pip下載 bottle
#sudo pip install bottle
安裝python-sqlalchemy
#sudo apt-get install python-sqlalchemy

再來是安裝最重要的bro-pdns套件,這是由其他高手寫好放在git上的。
#sudo git clone https://github.com/JustinAzoff/bro-pdns.git

把整包抓下來後,
將bro-pdns整包資料移到我們的路徑下(/nsm/bro/share/bro/site)
#sudo mv bro-pdns /nsm/bro/share/bro/site/.

修改設定檔
#sudo vim /nsm/bro/share/bro/site/local.bro

添加資料庫兩行連線資訊
(1) 第一行是指令我們剛剛移進來的bro-pdns路徑。
@load /nsm/bro/share/bro/site/bro-pdns

(2)第2行是指資料庫的連線資訊
redef PDNS::uri = "mysql://root:密碼@localhost/pdns";
設定完後載入這個系統 (經過測試後,這邊載不載入都沒差,他在運作時都會自動載入)
#broctl deploy

啟動服務
#BRO_PDNS_DB=mysql://root:密碼@localhost/pdns /nsm/share/bro/site/bro-pdns/bro_pdns.py serve
接著我們就可以試著用API方式來調出我們的網域資訊

運作狀況測試:
localhost的部份可以修改成你的IP位址 或者 DomainName。
服務運作是走TCP-8081 port。
#BRO_PDNS_DB=mysql://root:密碼@localhost/pdns /nsm/bro/share/bro/site/bro-pdns/bro_pdns.py serve
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8081/
Hit Ctrl-C to quit.

你的測試方法也會是像
http://[pDNS的IP位址]:8081/dns/[想查詢的網域]
EX:http://10.10.10.1:8081/dns/www.google.com

有資料就會像下面:
{"records": [{"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.152", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.153", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.157", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.158", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.162", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.163", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.167", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.168", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.172", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}, {"count": 1, "last": "2017-04-20 08:53:42", "ttl": 236, "answer": "139.175.107.173", "query": "www.google.com", "type": "A", "first": "2017-04-20 08:53:42"}

沒資料則是
{"records": []}

這樣子,我認為蒐集一段時間後,可以了解到企業內部的人通常會查詢什麼樣的網站內容。
如果pDNS查詢突然出現了,不常出現的網站內容,或者突然煎有大量的查詢狀況就得要注意。

如果連線出現錯誤
ImportError: No module named MySQLdb
解決問題方法: (更新並安裝python-mysqldb)
#sudo apt-get install python-mysqldb

參考網址內有教學,如何將這個API調用出來的資料變漂亮。
但我的主要用途用不到這個。
如果有興趣,大家可以參考他的網站。有比較詳細的說明

(本篇完)


參考網址:

http://www.freebuf.com/articles/network/103815.html

https://nullsecure.org/building-your-own-passivedns-feed/

引用自我的BLOG: http://it001.pixnet.net/blog/post/325951393-it%e5%b0%88%e9%a1%8c-%e2%80%93-%e5%ae%89%e8%a3%9d-pdns-%28passive-dns%29%28%e8%b3%87%e5%ae%89%29


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言