[Mikrotik][Routerboard] 2WAN+1LAN構成環境でのFirewall Rule例について。

mikrotik
ccr1016_fw.png

自宅環境では一番エッジ側にCloud Core Router(以下CCR)1016を配置していますが、2WAN+1LAN構成で使用しています。
参考になるか分かりませんが、これは導入しておけというFirewall Ruleについて記載しておこうと思います。

確認環境

なお検証環境は以下のとおりです。

[admin@MikroTik] /system resource> print
uptime: 2d2h32m40s
version: 6.8rc1
build-time: Dec/20/2013 13:41:42
free-memory: 7.5GiB
total-memory: 7.9GiB
cpu: tilegx
cpu-count: 16
cpu-frequency: 1000MHz
cpu-load: 0%
free-hdd-space: 435.6MiB
total-hdd-space: 512.0MiB
architecture-name: tile
board-name: CCR1016-12G
platform: MikroTik

ここではURLフィルタリングを指定しています。

/ip firewall layer7-protocol
add name=block_url regexp=nukes.directedat.asia.

ここでは、内側(LAN)について、アドレスリストという形で管理するように指定しています。ここで指定しておくことで、以下のFirewallルールでは、アドレス指定をしなくても設定したアドレスリストから指定することが出来ます。

/ip firewall address-list
add address=192.168.1.0/24 list=internal

ここから以下はFirewallルールの一例になります。他のRBシリーズの場合初期設定で簡素では有りますが、FirewallルールとNATルールが自動的に設定されますが、CCRでは一からの設定が必要となります。
外から悪意のあるアクセスを防ぐため、ひと通りのルールを追加してあります。わからないことがあれば、個別にご質問ください。

/ip firewall filter
add action=drop chain=input comment=Block_inbound_webUI dst-port=80 in-interface=all-ppp \
protocol=tcp
add action=add-src-to-address-list address-list=blocked-addr address-list-timeout=1w3d \
chain=input dst-port=80 in-interface=all-ppp protocol=tcp src-address-list=\
blocked-addr-stage1
add action=add-src-to-address-list address-list=blocked-addr-stage1 \
address-list-timeout=10m chain=input dst-port=80 in-interface=all-ppp protocol=tcp
add action=drop chain=input comment=Block_inbound_DNS dst-port=53 in-interface=all-ppp \
protocol=tcp src-address-list=blocked-addr
add action=add-src-to-address-list address-list=blocked-addr address-list-timeout=1w3d \
chain=input dst-port=53 in-interface=all-ppp protocol=tcp src-address-list=\
blocked-addr-stage1
add action=add-src-to-address-list address-list=blocked-addr-stage1 \
address-list-timeout=10m chain=input dst-port=53 in-interface=all-ppp protocol=tcp
add action=drop chain=input comment=Block_inbound_DNS dst-port=53 in-interface=all-ppp \
protocol=udp src-address-list=blocked-addr
add action=add-src-to-address-list address-list=blocked-addr address-list-timeout=1w3d \
chain=input dst-port=53 in-interface=all-ppp protocol=udp src-address-list=\
blocked-addr-stage1
add action=add-src-to-address-list address-list=blocked-addr-stage1 \
address-list-timeout=10m chain=input dst-port=53 in-interface=all-ppp protocol=udp
add action=tarpit chain=input connection-limit=3,32 protocol=tcp src-address-list=\
blocked-addr
add action=add-src-to-address-list address-list=blocked-addr address-list-timeout=1d \
chain=input connection-limit=100,32 protocol=tcp src-address-list=blocked-addr
add action=jump chain=forward comment="SYN Flood protect" connection-state=new disabled=\
yes jump-target=SYN-Protect protocol=tcp tcp-flags=syn
add chain=SYN-Protect connection-state=new limit=400,5 protocol=tcp tcp-flags=syn
add action=drop chain=SYN-Protect connection-state=new protocol=tcp tcp-flags=syn
add action=drop chain=known_viruses comment="windows - not EXACTLY a virus" dst-port=\
135-139 in-interface=all-ethernet protocol=tcp
add action=drop chain=known_viruses comment="windows - not EXACTLY a virus" dst-port=\
135-139 in-interface=all-ethernet protocol=udp
add action=drop chain=known_viruses comment="winXP netbios not EXACTLY a virus" \
dst-port=445 in-interface=all-ethernet protocol=udp
add action=drop chain=known_viruses comment="winXP netbios not EXACTLY a virus" \
dst-port=445 in-interface=all-ethernet protocol=tcp
add action=drop chain=known_viruses comment="msblast worm" dst-port=593 protocol=tcp
add action=drop chain=known_viruses comment="msblast worm" dst-port=4444 protocol=tcp
add action=drop chain=known_viruses comment="WITTY worm" dst-port=4000 protocol=tcp
add action=drop chain=known_viruses comment="SoBig.f worm" dst-port=995-999 protocol=tcp
add action=drop chain=known_viruses comment="SoBig.f worm" dst-port=8998 protocol=tcp
add action=drop chain=known_viruses comment="beagle worm" dst-port=2745 protocol=tcp
add action=drop chain=known_viruses comment="beagle worm" dst-port=4751 protocol=tcp
add action=drop chain=known_viruses comment="SQL Slammer" dst-port=1434 protocol=tcp
add action=drop chain=bad_people comment="Known Spammer" src-address=81.180.98.3
add action=drop chain=bad_people comment="Known Spammer" src-address=24.73.97.226
add action=drop chain=bad_people comment="Known Spammer" src-address=150.164.28.222
add action=drop chain=bad_people comment="Known Spammer" src-address=180.153.224.106
add action=drop chain=input comment="drop ssh brute forcers" dst-port=22 in-interface=\
all-ppp protocol=tcp src-address-list=ssh_blacklist
add action=add-src-to-address-list address-list=ssh_blacklist address-list-timeout=1w3d \
chain=input connection-state=new dst-port=22 in-interface=all-ppp protocol=tcp \
src-address-list=ssh_stage3
add action=add-src-to-address-list address-list=ssh_stage3 address-list-timeout=1m \
chain=input connection-state=new dst-port=22 in-interface=all-ppp protocol=tcp \
src-address-list=ssh_stage2
add action=add-src-to-address-list address-list=ssh_stage2 address-list-timeout=1m \
chain=input connection-state=new dst-port=22 in-interface=all-ppp protocol=tcp \
src-address-list=ssh_stage1
add action=add-src-to-address-list address-list=ssh_stage1 address-list-timeout=1m \
chain=input connection-state=new dst-port=22 in-interface=all-ppp protocol=tcp
add action=drop chain=input comment=\
"allows only 10 FTP login incorrect answers per minute" dst-port=21 in-interface=\
all-ppp protocol=tcp src-address-list=ftp_blacklist
add chain=output content="530 Login incorrect" dst-limit=1/1m,9,dst-address/1m protocol=\
tcp
add action=add-dst-to-address-list address-list=ftp_blacklist address-list-timeout=3h \
chain=output content="530 Login incorrect" protocol=tcp
add action=drop chain=forward comment="drop invalid connections DELETE" \
connection-state=invalid
add action=drop chain=input comment="Blocks SSH" dst-port=22 in-interface=all-ppp \
protocol=tcp
add action=jump chain=forward comment="Known virus ports DELETE" jump-target=\
known_viruses
add action=jump chain=forward comment="kill known bad source addresses DELETE" \
jump-target=bad_people
add action=jump chain=forward comment="Jump to Accepted List" jump-target=accept_list
add chain=forward comment="allow established connections DELETE" connection-state=\
established
add chain=forward comment="allow related connections DELETE" connection-state=related
add chain=forward comment="Allow All"

ここではMangleというものを設定します。簡単に言うとパケットにマーキングし、NATなどに対して、個別のルーティング設定などを行うことです。

Mangle is a kind of ‘marker’ that marks packets for future processing with special marks. Many other facilities in RouterOS make use of these marks, e.g. queue trees, NAT, routing. They identify a packet based on its mark and process it accordingly. The mangle marks exist only within the router, they are not transmitted across the network.

http://wiki.mikrotik.com/wiki/Manual:IP/Firewall/Mangle

ここでは主に2ISPを設定することからLoad Balancingを行うような設定になっています。

/ip firewall mangle
add action=mark-connection chain=prerouting connection-state=new dst-address-list=\
!internal dst-address-type=!local in-interface=internal new-connection-mark=ISP1 \
per-connection-classifier=both-addresses-and-ports:2/0 src-address-list=internal
add action=mark-connection chain=prerouting connection-state=new dst-address-list=\
!internal dst-address-type=!local in-interface=internal new-connection-mark=ISP2 \
per-connection-classifier=both-addresses-and-ports:2/1 src-address-list=internal
add action=mark-routing chain=prerouting connection-mark=ISP1 in-interface=internal \
new-routing-mark=ISP1 passthrough=no
add action=mark-routing chain=prerouting connection-mark=ISP2 connection-state=new \
in-interface=internal new-routing-mark=ISP2 passthrough=no
add chain=prerouting dst-address=0.0.0.0/0 in-interface=internal
add action=mark-connection chain=input in-interface=ISP1 new-connection-mark=WAN1_mark
add action=mark-connection chain=input in-interface=ISP2 new-connection-mark=WAN2_mark
add action=mark-routing chain=output connection-mark=WAN1_mark new-routing-mark=to_ISP1
add action=mark-routing chain=output connection-mark=WAN2_mark new-routing-mark=to_ISP2

ここはNATルールの設定になります。特に外側からの接続を想定していないので、内側からのmasqueradeしかせってしていません。

/ip firewall nat
add action=masquerade chain=srcnat out-interface=ISP1
add action=masquerade chain=srcnat out-interface=ISP2

ここではルーティングの設定を行うわけですが、Load Balancingの関係上、アドミニストレーティブディスタンスをずらして設定してあります。
詳細については以下を参照してください。

Manual:IP/Route - MikroTik Wiki
/ip route
add check-gateway=ping distance=20 gateway=ISP1 routing-mark=ISP1
add check-gateway=ping distance=10 gateway=ISP2 routing-mark=ISP2

ここでは、Mangleで設定したルーティングマークをどのルーティングテーブルで捌くか、という設定を行うことが出来ます。一括でmainテーブルで処理することも可能ですが、上記でパケットマーキングをしているのでマーキング毎にテーブルを分割してあります。

/ip route rule
add dst-address=192.168.1.0/24 table=main
add interface=ISP1 table=main
add interface=ISP1 table=ISP1
add interface=ISP2 table=main
add interface=ISP2 table=ISP2
add routing-mark=ISP1 table=ISP1
add routing-mark=ISP2 table=ISP2

もしかしたら抜けているルールなどあるかもしれませんが、ひと通りの部分で網羅していると思います。
問題ある部分がありましたらご指摘いただければ幸いです。

それでは楽しいRouterboardライフをお送りください。

この記事を書いた人

kometchtech

うつ病を患いながら、IT業界の末席にいるおっさんエンジニア。科学計算をしたことがないのに、HPC分野にお邪魔している。興味のある分野で学習したことをblogにまとめつつ、うつ病の経過症状のメモも置いておく日々。じつはRouterboard User Group JPの中の人でもある。 Amazon欲しいものリスト / Arm板を恵んでくれる人募集中

kometchtechをフォローする
タイトルとURLをコピーしました