# netflow-hex — исполнитель правил управления на стороне RouterOS. # # Ставится install.rsc как /system/script netflow-hex-ctl и запускается # планировщиком раз в 10 с (и при загрузке). Контейнер НЕ имеет прав записи # в роутер: он только публикует желаемое состояние, а этот скрипт сам # забирает его, проверяет по белому списку и применяет. # # Что скрипт трогает — и только это: # /ip(v6)/firewall/address-list списки nfhex-r-s|d, nfhex-protect (comment netflow-hex:p) # /ip(v6)/firewall/filter|mangle правила chain=nfhex с comment netflow-hex:r:… # /queue/tree nfhex-r-dn|up # /ip/firewall/connection удаление живых соединений цели (FastTrack) # В цепочку nfhex переходит forward (правило-якорь из install.rsc); первыми в # ней стоят return для nfhex-protect — защищённые адреса не блокируются # никогда, что бы ни пришло от контейнера. input/output не трогаются вовсе: # управление роутером, его DNS и DHCP отрезать нельзя. # # Входные строки не исполняются (:parse/:execute не используются) — значения # проверяются и подставляются типизированными аргументами команд. # # Формат (text/plain, строки через \n): # nfhex 1 # p защитить адрес (устройство администратора) # r <-|порты> # a <адрес[/префикс]> адрес в список правила # end :local url "http://172.17.0.2:8080/api/control/desired" :local maxRules 32 :local maxEntries 2000 :global nfhexSerial :global nfhexRuns :if ([:typeof $nfhexRuns] != "num") do={ :set nfhexRuns 0 } :set nfhexRuns ($nfhexRuns + 1) # ---------- проверки (функции видят только свои аргументы) ---------- :local isNum do={ :return ([:len $1] > 0 && [:len $1] <= $2 && ($1 ~ "^[0-9]+\$")) } :local isPorts do={ :return ($1 = "-" || ([:len $1] <= 64 && ($1 ~ "^[0-9]{1,5}(-[0-9]{1,5})?(,[0-9]{1,5}(-[0-9]{1,5})?)*\$"))) } # адрес → "" если плохой, иначе нормализованная строка (v4 без /32, v6 с префиксом) :local normAddr do={ :local a $1 :local h $a :local p "" :local sl [:find $a "/"] :if ([:typeof $sl] = "num") do={ :set h [:pick $a 0 $sl]; :set p [:pick $a ($sl + 1) [:len $a]] } :if ([:typeof [:find $h ":"]] = "num") do={ :if ([:typeof [:toip6 $h]] != "ip6") do={ :return "" } :if ($p = "") do={ :set p "128" } :if (!($p ~ "^(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-8])\$")) do={ :return "" } :local lh [:tostr $h] :if ($lh ~ "^(ff|FF|fe[89abAB]|FE[89AB])") do={ :return "" } :return ($h . "/" . $p) } :if ([:typeof [:toip $h]] != "ip") do={ :return "" } :local ip [:toip $h] :local o1 [:tonum [:pick $h 0 [:find $h "."]]] :if ($o1 = 0 || $o1 = 127 || $o1 >= 224) do={ :return "" } :if ($p = "" || $p = "32") do={ :return $h } :if (!($p ~ "^(2[4-9]|3[01])\$")) do={ :return "" } :local m (255.255.255.255 << (32 - [:tonum $p])) :return ([:tostr ($ip & $m)] . "/" . $p) } # ---------- забрать желаемое ---------- :local data "" :do { :local r [/tool/fetch url=$url output=user as-value] :if (($r->"status") = "finished") do={ :set data ($r->"data") } } on-error={ :set data "" } :local lines [:toarray ""] :local pos 0 :local dl [:len $data] :while ($pos < $dl) do={ :local e [:find $data "\n" $pos] :if ([:typeof $e] != "num") do={ :set e $dl } :set lines ($lines, [:pick $data $pos $e]) :set pos ($e + 1) } :local ok false :local serial "" :if ([:len $lines] >= 2) do={ :local h ($lines->0) :if ([:pick $h 0 8] = "nfhex 1 ") do={ :set serial [:pick $h 8 [:len $h]] } :if ($serial ~ "^[0-9a-f]{1,32}\$" && ($lines->([:len $lines] - 1)) = "end") do={ :set ok true } } # нет связи или ответ битый — ничего не трогаем: записи истекут сами по timeout :if (!$ok) do={ :set data "" } # неизменное состояние сверяем полностью раз в 5 минут (и после загрузки) :local full ($ok && ($serial != $nfhexSerial || ($nfhexRuns % 30) = 1)) :if ($full) do={ # ---------- защищённые адреса роутера ---------- :local prot [:toarray ""] :foreach i in=[/ip/address/find] do={ :local a [/ip/address/get $i address] :set prot ($prot, [:toip [:pick $a 0 [:find $a "/"]]]) } :foreach i in=[/ip/route/find where dst-address=0.0.0.0/0] do={ :local g [:tostr [/ip/route/get $i gateway]] :if ([:typeof [:toip $g]] = "ip") do={ :set prot ($prot, [:toip $g]) } } :foreach s in=([/ip/dns/get servers], [/ip/dns/get dynamic-servers]) do={ :if ([:typeof [:toip $s]] = "ip") do={ :set prot ($prot, [:toip $s]) } } :local prot6 [:toarray ""] :foreach i in=[/ipv6/address/find] do={ :local a [/ipv6/address/get $i address] :set prot6 ($prot6, [:pick $a 0 [:find $a "/"]]) } # ---------- разбор ---------- :local rules [:toarray ""] :local want [:toarray ""] :local nrules 0 :local nent 0 :local bad 0 :foreach ln in=$lines do={ :local f [:toarray ""] :local p 0 :while ($p <= [:len $ln]) do={ :local e [:find $ln " " $p] :if ([:typeof $e] != "num") do={ :set e [:len $ln] } :set f ($f, [:pick $ln $p $e]) :set p ($e + 1) } :local t ($f->0) :if ($t = "r" && [:len $f] = 9) do={ :local id ($f->1) :local act ($f->2) :if ([$isNum ($id) 6] && ($act = "block" || $act = "limit") && (($f->3) = "0" || ($f->3) = "1") && (($f->4) = "0" || ($f->4) = "1") && (($f->3) . ($f->4)) != "00" && (($f->5) = "any" || ($f->5) = "tcp" || ($f->5) = "udp") && [$isPorts ($f->6)] && [$isNum ($f->7) 8] && [$isNum ($f->8) 8] && $nrules < $maxRules) do={ :if (($f->6) != "-" && ($f->5) = "any") do={ :set bad ($bad + 1) } else={ :if ($act = "limit" && [:tonum ($f->7)] < 64 && [:tonum ($f->8)] < 64) do={ :set bad ($bad + 1) } else={ :set ($rules->("r" . $id)) ($act . "|" . ($f->3) . "|" . ($f->4) . "|" . ($f->5) . "|" . ($f->6) . "|" . ($f->7) . "|" . ($f->8)) :set nrules ($nrules + 1) } } } else={ :set bad ($bad + 1) } } :if (($t = "a" && [:len $f] = 5) || ($t = "p" && [:len $f] = 3)) do={ :local id "" :local side "p" :local raw ($f->1) :local ttl ($f->2) :if ($t = "a") do={ :set id ($f->1); :set side ($f->2); :set raw ($f->3); :set ttl ($f->4) } :local a [$normAddr $raw] :local good ($a != "" && [$isNum $ttl 7] && [:tonum $ttl] >= 30 && [:tonum $ttl] <= 2678400 && $nent < $maxEntries) :if ($good && $t = "a") do={ :set good ([$isNum $id 6] && ($side = "s" || $side = "d") && [:typeof ($rules->("r" . $id))] = "str") } # блокируемый адрес не должен накрывать адрес роутера, шлюз или DNS :if ($good && $t = "a") do={ :if ([:typeof [:find $a ":"]] = "num") do={ :foreach x in=$prot6 do={ :if ([:pick $a 0 [:find $a "/"]] = $x) do={ :set good false } } } else={ :local h $a :local plen 32 :if ([:typeof [:find $a "/"]] = "num") do={ :set h [:pick $a 0 [:find $a "/"]]; :set plen [:tonum [:pick $a ([:find $a "/"] + 1) [:len $a]]] } :local m (255.255.255.255 << (32 - $plen)) :foreach x in=$prot do={ :if (([:toip $h] & $m) = ($x & $m)) do={ :set good false } } :if (([:toip $h] & 255.255.255.0) = 172.17.0.0) do={ :set good false } } :if (!$good) do={ :log warning ("netflow-hex: address " . $a . " is protected, not blocking (rule " . $id . ")") } } :if ($good) do={ :local l "nfhex-protect" :if ($t = "a") do={ :set l ("nfhex-r" . $id . "-" . $side) } :set ($want->($l . "|" . $a)) [:tonum $ttl] :set nent ($nent + 1) } else={ :set bad ($bad + 1) } } } :local addR 0 :local delR 0 :local addA 0 :local delA 0 # ---------- лишние объекты: правило исчезло или поменялось ---------- :foreach fam in={"ip";"ipv6"} do={ :foreach menu in={"filter";"mangle"} do={ :local ids :if ($fam = "ip" && $menu = "filter") do={ :set ids [/ip/firewall/filter/find where comment~"^netflow-hex:r[0-9]+:"] } :if ($fam = "ip" && $menu = "mangle") do={ :set ids [/ip/firewall/mangle/find where comment~"^netflow-hex:r[0-9]+:"] } :if ($fam = "ipv6" && $menu = "filter") do={ :set ids [/ipv6/firewall/filter/find where comment~"^netflow-hex:r[0-9]+:"] } :if ($fam = "ipv6" && $menu = "mangle") do={ :set ids [/ipv6/firewall/mangle/find where comment~"^netflow-hex:r[0-9]+:"] } :foreach i in=$ids do={ :local c :if ($fam = "ip" && $menu = "filter") do={ :set c [/ip/firewall/filter/get $i comment] } :if ($fam = "ip" && $menu = "mangle") do={ :set c [/ip/firewall/mangle/get $i comment] } :if ($fam = "ipv6" && $menu = "filter") do={ :set c [/ipv6/firewall/filter/get $i comment] } :if ($fam = "ipv6" && $menu = "mangle") do={ :set c [/ipv6/firewall/mangle/get $i comment] } # comment: netflow-hex:r:<роль>: :local rest [:pick $c 13 [:len $c]] :local id [:pick $rest 0 [:find $rest ":"]] :local spec [:pick $rest ([:find $rest ":" ([:find $rest ":"] + 1)] + 1) [:len $rest]] :if (($rules->("r" . $id)) != $spec) do={ :if ($fam = "ip" && $menu = "filter") do={ /ip/firewall/filter/remove $i } :if ($fam = "ip" && $menu = "mangle") do={ /ip/firewall/mangle/remove $i } :if ($fam = "ipv6" && $menu = "filter") do={ /ipv6/firewall/filter/remove $i } :if ($fam = "ipv6" && $menu = "mangle") do={ /ipv6/firewall/mangle/remove $i } :set delR ($delR + 1) } } } } :foreach i in=[/queue/tree/find where comment~"^netflow-hex:r[0-9]+:"] do={ :local c [/queue/tree/get $i comment] :local rest [:pick $c 13 [:len $c]] :local id [:pick $rest 0 [:find $rest ":"]] :local spec [:pick $rest ([:find $rest ":" ([:find $rest ":"] + 1)] + 1) [:len $rest]] :if (($rules->("r" . $id)) != $spec) do={ /queue/tree/remove $i; :set delR ($delR + 1) } } # ---------- лишние адреса ---------- :foreach i in=[/ip/firewall/address-list/find where (list~"^nfhex-r[0-9]+-[sd]\$" || (list="nfhex-protect" && comment="netflow-hex:p"))] do={ :local k ([/ip/firewall/address-list/get $i list] . "|" . [/ip/firewall/address-list/get $i address]) :if ([:typeof ($want->$k)] != "num") do={ /ip/firewall/address-list/remove $i; :set delA ($delA + 1) } else={ /ip/firewall/address-list/set $i timeout=($want->$k) :set ($want->$k) -1 } } :foreach i in=[/ipv6/firewall/address-list/find where (list~"^nfhex-r[0-9]+-[sd]\$" || (list="nfhex-protect" && comment="netflow-hex:p"))] do={ :local k ([/ipv6/firewall/address-list/get $i list] . "|" . [/ipv6/firewall/address-list/get $i address]) :if ([:typeof ($want->$k)] != "num") do={ /ipv6/firewall/address-list/remove $i; :set delA ($delA + 1) } else={ /ipv6/firewall/address-list/set $i timeout=($want->$k) :set ($want->$k) -1 } } # ---------- недостающие адреса ---------- :foreach k,ttl in=$want do={ :if ($ttl > 0) do={ :local l [:pick $k 0 [:find $k "|"]] :local a [:pick $k ([:find $k "|"] + 1) [:len $k]] :local cm "netflow-hex" :if ($l = "nfhex-protect") do={ :set cm "netflow-hex:p" } :do { :if ([:typeof [:find $a ":"]] = "num") do={ /ipv6/firewall/address-list/add list=$l address=$a timeout=$ttl comment=$cm } else={ /ip/firewall/address-list/add list=$l address=$a timeout=$ttl comment=$cm # новое устройство под правилом: его живые (в т.ч. fasttrack) соединения рвём, # иначе правило подействует только на новые :if ($l ~ "-s\$" && [:typeof [:find $a "/"]] != "num") do={ :local rx ("^" . $a . ":") /ip/firewall/connection/remove [find where src-address~$rx || dst-address~$rx] } } :set addA ($addA + 1) } on-error={ :log warning ("netflow-hex: failed to add address " . $a . " to " . $l) } } } # ---------- недостающие правила ---------- :foreach rk,spec in=$rules do={ :local id [:pick $rk 1 [:len $rk]] :local cm ("netflow-hex:r" . $id . ":") :if ([:len [/ip/firewall/filter/find where comment~("^" . $cm)]] = 0 && [:len [/ip/firewall/mangle/find where comment~("^" . $cm)]] = 0) do={ :local v [:toarray ""] :local p 0 :while ($p <= [:len $spec]) do={ :local e [:find $spec "|" $p] :if ([:typeof $e] != "num") do={ :set e [:len $spec] } :set v ($v, [:pick $spec $p $e]) :set p ($e + 1) } :local act ($v->0) :local hasS (($v->1) = "1") :local hasD (($v->2) = "1") :local proto ($v->3) :local ports ($v->4) :local sl ("nfhex-r" . $id . "-s") :local dl ("nfhex-r" . $id . "-d") :local mk ("nfhex-r" . $id) :foreach fam in={"ip";"ipv6"} do={ # прямое (клиент → сервер) и обратное направление; каждое правило создаётся # выключенным и включается только после того, как заданы все условия — # иначе на миг оно совпало бы со всем трафиком :foreach dir in={"f";"b"} do={ :local n :local role ($dir . "1") :if ($act = "block") do={ :if ($fam = "ip") do={ :set n [/ip/firewall/filter/add chain=nfhex action=drop disabled=yes comment=($cm . $role . ":" . $spec)] } else={ :set n [/ipv6/firewall/filter/add chain=nfhex action=drop disabled=yes comment=($cm . $role . ":" . $spec)] } } else={ :if ($fam = "ip") do={ :set n [/ip/firewall/mangle/add chain=nfhex action=mark-connection new-connection-mark=$mk passthrough=yes connection-mark=no-mark disabled=yes comment=($cm . $role . ":" . $spec)] } else={ :set n [/ipv6/firewall/mangle/add chain=nfhex action=mark-connection new-connection-mark=$mk passthrough=yes connection-mark=no-mark disabled=yes comment=($cm . $role . ":" . $spec)] } } :local srcL $sl :local dstL $dl :if ($dir = "b") do={ :set srcL $dl; :set dstL $sl } :local useSrc (($dir = "f" && $hasS) || ($dir = "b" && $hasD)) :local useDst (($dir = "f" && $hasD) || ($dir = "b" && $hasS)) :if ($fam = "ip" && $act = "block") do={ :if ($useSrc) do={ /ip/firewall/filter/set $n src-address-list=$srcL } :if ($useDst) do={ /ip/firewall/filter/set $n dst-address-list=$dstL } :if ($proto != "any") do={ /ip/firewall/filter/set $n protocol=$proto } :if ($ports != "-" && $dir = "f") do={ /ip/firewall/filter/set $n dst-port=$ports } :if ($ports != "-" && $dir = "b") do={ /ip/firewall/filter/set $n src-port=$ports } /ip/firewall/filter/enable $n } :if ($fam = "ipv6" && $act = "block") do={ :if ($useSrc) do={ /ipv6/firewall/filter/set $n src-address-list=$srcL } :if ($useDst) do={ /ipv6/firewall/filter/set $n dst-address-list=$dstL } :if ($proto != "any") do={ /ipv6/firewall/filter/set $n protocol=$proto } :if ($ports != "-" && $dir = "f") do={ /ipv6/firewall/filter/set $n dst-port=$ports } :if ($ports != "-" && $dir = "b") do={ /ipv6/firewall/filter/set $n src-port=$ports } /ipv6/firewall/filter/enable $n } :if ($fam = "ip" && $act = "limit") do={ :if ($useSrc) do={ /ip/firewall/mangle/set $n src-address-list=$srcL } :if ($useDst) do={ /ip/firewall/mangle/set $n dst-address-list=$dstL } :if ($proto != "any") do={ /ip/firewall/mangle/set $n protocol=$proto } :if ($ports != "-" && $dir = "f") do={ /ip/firewall/mangle/set $n dst-port=$ports } :if ($ports != "-" && $dir = "b") do={ /ip/firewall/mangle/set $n src-port=$ports } /ip/firewall/mangle/enable $n } :if ($fam = "ipv6" && $act = "limit") do={ :if ($useSrc) do={ /ipv6/firewall/mangle/set $n src-address-list=$srcL } :if ($useDst) do={ /ipv6/firewall/mangle/set $n dst-address-list=$dstL } :if ($proto != "any") do={ /ipv6/firewall/mangle/set $n protocol=$proto } :if ($ports != "-" && $dir = "f") do={ /ipv6/firewall/mangle/set $n dst-port=$ports } :if ($ports != "-" && $dir = "b") do={ /ipv6/firewall/mangle/set $n src-port=$ports } /ipv6/firewall/mangle/enable $n } :set addR ($addR + 1) } # ограничение: пакеты размеченного соединения — в «вниз» (к клиенту) и «вверх» :if ($act = "limit") do={ :foreach w in={"dn";"up"} do={ :local n :if ($fam = "ip") do={ :set n [/ip/firewall/mangle/add chain=nfhex action=mark-packet connection-mark=$mk new-packet-mark=($mk . "-" . $w) passthrough=no disabled=yes comment=($cm . "m" . $w . ":" . $spec)] } else={ :set n [/ipv6/firewall/mangle/add chain=nfhex action=mark-packet connection-mark=$mk new-packet-mark=($mk . "-" . $w) passthrough=no disabled=yes comment=($cm . "m" . $w . ":" . $spec)] } # «вниз» — к устройству (dst в списке s) или от адресов цели (src в списке d) :local L $sl :local toS ($w = "dn") :if (!$hasS) do={ :set L $dl; :set toS ($w = "up") } :if ($fam = "ip") do={ :if ($toS) do={ /ip/firewall/mangle/set $n dst-address-list=$L } else={ /ip/firewall/mangle/set $n src-address-list=$L } /ip/firewall/mangle/enable $n } else={ :if ($toS) do={ /ipv6/firewall/mangle/set $n dst-address-list=$L } else={ /ipv6/firewall/mangle/set $n src-address-list=$L } /ipv6/firewall/mangle/enable $n } } } } :if ($act = "limit") do={ :if ([:tonum ($v->5)] > 0) do={ /queue/tree/add name=($mk . "-dn") parent=global packet-mark=($mk . "-dn") max-limit=([:tonum ($v->5)] * 1000) comment=($cm . "qdn:" . $spec) } :if ([:tonum ($v->6)] > 0) do={ /queue/tree/add name=($mk . "-up") parent=global packet-mark=($mk . "-up") max-limit=([:tonum ($v->6)] * 1000) comment=($cm . "qup:" . $spec) } # уже размеченных и fasttrack-соединений устройства касается только после разрыва :foreach i in=[/ip/firewall/address-list/find where list=$sl] do={ :local a [/ip/firewall/address-list/get $i address] :if ([:typeof [:find $a "/"]] != "num") do={ :local rx ("^" . $a . ":") /ip/firewall/connection/remove [find where src-address~$rx || dst-address~$rx] } } } :log info ("netflow-hex: rule " . $id . " " . $act . " applied") } } :set nfhexSerial $serial :if ($addR + $delR + $addA + $delA + $bad > 0) do={ :log info ("netflow-hex: rules +" . $addR . " -" . $delR . ", addresses +" . $addA . " -" . $delA . ", rejected lines " . $bad) } }