ntop ntopngユーザースクリプト ブラックリス国アラート

先日の記事「ntop ntopngユーザースクリプト アクティブ時間差分アラート」に続く第二弾です。

今回は通信しているIPアドレスが、ブラックリストに登録した国であった場合にアラートを発報するcountry_check.luaを検証したいと思います。

前回の記事で記載したユーザースクリプト開発に必要な知識は、本記事を読む前に一読しておいてください。

先ずはcountry_check.luaユーザースクリプトの設定を変更します。

前回と異なる点として、ホストアラートではなくフローアラートのユーザースクリプトを編集しているということです。

インターフェイスで検知したフローが、ブロックリスト登録した国の通信であった場合にアラートを発報します。

図1 ブランクリスト国設定

設定は、JP,US,UKといった2文字で設定します。

次に追記したソースコードを張っておきます。

--
-- (C) 2019-20 - ntop.org
--

local user_scripts = require("user_scripts")
local flow_consts = require("flow_consts")

local script = {
  -- Script category
  category = user_scripts.script_categories.security, 

  -- Priodity
  prio = 20, -- Higher priority (executed sooner) than default 0 priority

  -- This module is disabled by default
  default_enabled = false,

  -- The default configuration of this script
  default_value = {
    items = {},
  },

  -- See below
  hooks = {},

  -- Allow user script configuration from the GUI
  gui = {
    -- Localization strings, from the "locales" directory of the plugin
    i18n_title = "alerts_dashboard.blacklisted_country",
    i18n_description = "alerts_dashboard.blacklisted_country_descr",
    input_builder = "items_list",
    item_list_type = "country",
  }
}

-- #################################################################

-- A fast lookup table
local blacklisted_countries = nil

-- Defines an hook which is executed every time a procotol of a flow is detected
function script.hooks.protocolDetected(now, conf)
  if(blacklisted_countries == nil) then
    blacklisted_countries = {}

    for _, country in pairs(conf.items or {}) do
      blacklisted_countries[string.upper(country)] = true
    end
  end

  local cli_country = flow.getClientCountry()
  local srv_country = flow.getServerCountry()
  local is_blacklisted = false
  local flow_score = 60
  local cli_score, srv_score
  local info = {cli_blacklisted = false, srv_blacklisted = false}

  if(cli_country and blacklisted_countries[cli_country]) then
    io.write("cc:"..cli_country.."\n")
    info.cli_blacklisted = true
    is_blacklisted = true
    cli_score = 60
    srv_score = 10
  end

  if(srv_country and blacklisted_countries[srv_country]) then
    io.write("sc:"..srv_country.."\n")
    info.srv_blacklisted = true
    is_blacklisted = true
    cli_score = 10
    srv_score = 60
  end

  if(is_blacklisted) then
    -- Note: possibly nil
    info.cli_country = cli_country
    info.srv_country = srv_country

    flow.triggerStatus(
       flow_consts.status_types.status_blacklisted_country.create(
	  flow_consts.status_types.status_blacklisted_country.alert_severity,
	  cli_country,
	  srv_country,
	  info.cli_blacklisted,
	  info.srv_blacklisted
       ),
       flow_score,
       cli_score,
       srv_score
    )
  end
end

-- #################################################################

return script

追加したコードは、以下の2行だけです。

io.write("cc:"..cli_country.."\n")
.
snip
.
io.write("sc:"..srv_country.."\n")

ブラックリスト国であった場合、標準出力に国名を出力しています。

ntopngを手動で起動してください。

$sudo ntopng -i br0 -w 3000 --https-port 443 -m 192.168.1.0/24 --community

標準出力には、以下のように出力されるはずです。

sc:JP
sc:JP
cc:JP
sc:JP
sc:JP
sc:JP
sc:JP
sc:JP
sc:JP
sc:JP
sc:JP
sc:JP
図2 フローアラート確認

コードも短く非常にシンプルですね。

(Visited 155 times, 1 visits today)
The following two tabs change content below.
【好きなもの】 インフラ技術が好き。古いものが好き。 【生きてきたフィールド】 システム運用、ソフトウェア開発、ミドルウェア検証、OSSサポート、プリセールスエンジニア、プロジェクトマネジメント 【このサイトでの役割】 サイト管理者。