跳到主內容

GeoLiteCity 找尋來源IP地理位置

因有時候想要了解都是那些國家來看服務那剛好 Graylog 剛好有提供這樣子的服務,可以讓您查看都是從那些國家來看服務的首先要先到 GeoLite2 提供免費的服務下載,提供有國家、城市及ASN,那我是下載城市這個檔案

109.01.10
在看功能是否有正常運行,發現到無法下載新版資訊,去官網查看,已經不在公開下載了要先註冊帳號後才能正常下載該資訊,所以要先註冊一個帳號才可以下載最新的資訊

設定流程


  • 先到官方註冊,免費註冊一個帳號,註冊完之後再登入之後,會看到可以手動下載地方。

geoip-01.png

  • 選擇要下載的檔案,我是選擇城市的檔案,下載他,並且使用 WinSCP 方法將檔案上傳到 /etc/graylog/server 該目錄下

geoip-02.png

  • 再進到 graylog 目錄下
cd /etc/graylog/server
  • 解壓剛剛上傳的檔案
解壓
tar -zxvf GeoLite2-City_20200107.tar.gz

進到剛剛解壓目錄下
cd GeoLite2-City_20200107

將 GeoLite2-City.mmdb 複製到 graylog 所指定目錄下
cp GeoLite2-City.mmdb /etc/graylog/server/
  • 到 Graylog 裡面設定啟用  system --> configurations

graylog-1.png

  • 選擇 Geo-Location Processor

graylog-2.png

  • 啟動服務,並且存檔

graylog-3.png

  • 選擇 Message Processors Configuration

graylog-4.png

  • 將 GeoIP Resolver 移動到最下面,並且存檔

graylog-5.png

如果是使用本網的這一篇文章 將 Nginx log 記錄傳到 Graylog 3 ,操作的話只要做到這裡就可以了,以下的操作請勿在操作了,會有重覆因文章裡的設定已經設定好了,只要啟動 GeoLiteCity 服務即可了,除非有使用其它的服務想要追查的話,再以操作創立一個新的設定。

  • 隨便到一個記錄上面有 IP 去下拉選擇 Grok pattern

graylog-6.png

  • 系統會帶到另一個頁面,並請填下以下的資訊,看您的 IP 那個名稱是什麼,如果是 remot_addr 寫的資訊是如下
    • Grok pattern 填入 remote_add=%{IP:remote_add}
    • condition 選擇 Only attempt extraction if field contalns string
    • Field contains string 填入remote_add
    • Extractor title 填入名稱或著取的資訊同名
    • 建立完之後重啟 Graylog 服務 systemctl restart graylog-server

graylog-7.png

  • 重啟完在看一下會出現如下的資訊

graylog-8.png

  • 可以從搜尋畫面左方選擇後面有 geolocation 按下 World Map

graylog-9.png

  • 畫面上面會出現地方會看到有一點紅紅那就是 IP 的來源所在位置

graylog-10.png

補充說明


自動更新 GeoIP 服務
因官方政策有修改不再公開提供下載,只能註冊後才可以下載並且正常運,官方公告,所以設定方式會有變化。

如果在安裝此套件之前先確認該系統是否已經有安裝 GeoIP 跟 geoipupdate 這兩個套件,否則在安裝的時候系統會顯示跟以上這兩個套件有衝突造成無法安裝,確認有安裝請事先將這兩個套件服務先移除後就可以正常安裝新版的套件服務

  • 錯誤訊息如下
# rpm -i geoipupdate_4.1.5_linux_amd64.rpm
        file /etc/GeoIP.conf from install of geoipupdate-4.1.5-1.x86_64 conflicts with file from package geoipupdate-2.5.0-1.el7.x86_64
        file /usr/bin/geoipupdate from install of geoipupdate-4.1.5-1.x86_64 conflicts with file from package geoipupdate-2.5.0-1.el7.x86_64

或者

# rpm -i geoipupdate_4.1.5_linux_amd64.rpm
        file /etc/GeoIP.conf from install of geoipupdate-4.1.5-1.x86_64 conflicts with file from package geoipupdate-2.5.0-1.el7.x86_64
        file /usr/bin/geoipupdate from install of geoipupdate-4.1.5-1.x86_64 conflicts with file from package geoipupdate-2.5.0-1.el7.x86_64
        file /usr/share/GeoIP from install of geoipupdate-4.1.5-1.x86_64 conflicts with file from package GeoIP-1.5.0-14.el7.x86_64
  • 下載 GeoIP Update 套件
#Centos
wget https://github.com/maxmind/geoipupdate/releases/download/v4.8.0/geoipupdate_4.8.0_linux_386.rpm

#Debian
wget https://github.com/maxmind/geoipupdate/releases/download/v4.8.0/geoipupdate_4.8.0_linux_386.deb
  • 安裝套件
#Centos
rpm -i geoipupdate_4.8.0_linux_386.rpm

#Debian
dpkg -i geoipupdate_4.8.0_linux_386.deb
  • 先登入進到後台,選擇 Services

geoip-03.png

  • 建立

geoip-04.png

  • 輸入 key ,因我是個人使用沒有銷售所以在該欄輸入 5 個零就可以了

geoip-05.png

  • 下載剛剛產生的設定檔

geoip-06.png

  • 設定 GeoIP
vim /etc/GeoIP.conf

設定要將檔案存放路徑

# Enter your account ID and license key below. These are available from
# https://www.maxmind.com/en/my_license_key. If you are only using free
# GeoLite databases, you may leave the 0 values.
AccountID 輸入剛剛產生出來的 ID
LicenseKey 輸入剛剛產生出來的 key

DatabaseDirectory /etc/graylog/server
  • 手動執行更新
/usr/bin/geoipupdate
  • 排程更新
crontab -e

30 7 * * 6 /usr/bin/geoipupdate

geoipupdate 套件更新


wget https://github.com/maxmind/geoipupdate/releases/download/v4.1.5/geoipupdate_4.1.5_linux_amd64.rpm
  • 更新套件
#CentOS
rpm -Uvh geoipupdate_4.1.5_linux_amd64.rpm

#Debian
dpkg -i geoipupdate_4.1.5_linux_amd64.deb

套件版本查詢


geoipupdate -V

補充說明


因系統環境已更為 Debian 來運行服務




參考相關網頁





補充參考