Showing posts with label MikroTik. Show all posts
Showing posts with label MikroTik. Show all posts

Sunday, May 15, 2016

Home Lab - Cisco & MikroTik

I would like to share with you my experience of building a network home lab. If you think about to build your own network home lab some of this tips can be useful for you. At the beginning i want to discuss about why to do it and what is the goal of my lab. Even if you have other goal some basic concepts will stay the same.

The first assumption is to do it cheaply. If you live in country where GDP is not the biggest you want to build lab but don't spend all money for it ;)
The second assumption is to have switching, routing and WiFi in lab. Switching give you Cisco devices, routing you have in MikroTik (or Cisco) and WiFi in MikroTik. From my perspective it is the best combination and I will explain why.

When I had chosen Cisco switch I didn't want pass the exam certificate for CCNA or another. I'm just need a professional management switch. That is the reason why I buy End-of-life devices. My choice is Cisco Catalyst 2950 and Cisco Catalyst 3550. The first is switching only device (L2 only), the second switch is combination switching and routing (L2+L3).

Cisco 2950 is great switch, it have all what probably you will need. It have not large size like on switch, low power consumption and it is be quiet. And of course it's cheap today (2016). Another switch Cisco 3550 made a bad first impression for me, is big, noisy and it costs two times more than 2950. But, when I tested it I change the mind. Now it is my favorite switch, but in beginning I have big problem with it. When I enable routing it's working less then one minute and stop working. Upgrade system for highest available version resolved this problem.
So first things which you should do is upgrade all Cisco switch, but here is the next problem. Usually this switch from auctions is send without any cable. So you to have need own power cable and console cable. About console cable, it have two connector, RJ-45 to switch and DB9 to computer (serial port). If you don't have serial port in your computer you need also to have serial port to USB adapter. And finally you can use and upgrade your Cisco switch ;)

Another bad things about this switch is that it's very old and yet not support auto-negotiation (Auto-MDI/MDIX). The result is that, if you want connect two switch together via Ethernet cable you need crossover cable. Yes, and this moment I finish the bad news about Cisco switch 2950 and 3550, it's time for good news :)

I have three Cisco switch in my lab (two 3550 and one 2950), I think it's enough to test of switching (e.g. STP). Now I little describe the 3550 switch. As I had said earlier 3550 offers routing. 2950 can have only one IP address mainly to management, but 3550 can have many IP address. Each port can by used of two mode, routable (L3) and not routable (L2). But you can set L2 port with and without IP address, in result you get three combinations: port L3 (routing), L2+L3 (VLAN + IP) and L2 (VLAN without IP). If you use last combination you can simulate many switches in one 3550 switch. In fact one 3550 switch allows you to use one switch as one router and many switches. That its great.
And I will be forgot, 3550 is selling with two version SMI and EMI. The SMI version don't have all dynamic routing protocol, EMI version have got, but cost double than SMI version of devices. The next great good news is that in the SMI version you can upload EMI image without any technical problem.

Time to describe second type of device which is MikroTik. First of, why MikroTik? Two reasons. The first it's big configuring possibility and CLI interface. The second it's new model named MikroTik hAP lite. It is designed to home use and it's cost is low. In my country in 2016 year one new device of this model cost the same as one Cisco 2950 switch. MikroTik hAP lite has the same version operating system as MikroTik RouterBoard, it has only less powerful equipment (CPU, RAM, etc.). I've three this devices and I'm very happy with them. This MikroTik router with Cisco switch 3550 you can use at the same time as WAN router and as LAN AP, is sufficient that you divide ether-* ports in MikroTik. Generally I think Cisco switch and MikroTik router is a great combination.

The below presents schematic of network in my home lab. With this equipments and computers with Linux system I can simulate any type of Linux service in network (WAN, LAN, WLAN).


And how its looks.





Friday, April 15, 2016

MikroTik + remote syslog server

In this post I show how to configure Linux server syslog with MicroTik. The goal is register networks connectivity "like" the ISP must do. Why external syslog? Because it is the best way, first MikroTik don't has too much storage capacity, second external syslog service is the most flexible solution. Once configured logsys server can be used in future to gather logs from other devices e.g. switch.

So in our scenario we have two devices, MikroTik router and Linux server. First we configure the syslog service. I use Debian 8 (jessie) Linux distribution where default server logs is rsyslogd. In the config file /etc/rsyslog.conf we must uncomment the lines below.

# provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

Next we restart syslog service.

systemctl restart rsyslog.service

Of course you must check is your firewall not block traffic on the port UDP 514 in your server. If you use SELinux (like CentOS) you must also enable this port in SELinux.
This is all what you must do in your Linux server. Now we start configure our MikroTik router. First we set remote syslog server.

[admin@R2] /system logging action> print 
Flags: * - default 
 0 * name="memory" target=memory memory-lines=1000 memory-stop-on-full=no 

 1 * name="disk" target=disk disk-file-name="log" disk-lines-per-file=1000 disk-file-count=2 disk-stop-on-full=no 


 2 * name="echo" target=echo remember=yes 


 3 * name="remote" target=remote remote=0.0.0.0 remote-port=514 src-address=0.0.0.0 bsd-syslog=no syslog-time-format=bsd-syslog 

     syslog-facility=daemon syslog-severity=auto 
[admin@R2] /system logging action> set 3 remote=172.16.16.101

Next the logs marked as info (topics) redirects to remote target (action).

[admin@R2] > /system logging print 
Flags: X - disabled, I - invalid, * - default 
 #    TOPICS                                                       ACTION                                                      PREFIX    
 0  * info                                                         memory                                                                
 1  * error                                                        memory                                                                
 2  * warning                                                      memory                                                                
 3  * critical                                                     echo                        
[admin@R2] > /system logging set 0 action=remote 
[admin@R2] > /system logging print               
Flags: X - disabled, I - invalid, * - default 
 #    TOPICS                                                       ACTION                                                      PREFIX    
 0  * info                                                         remote                                                                
 1  * error                                                        memory                                                                
 2  * warning                                                      memory                                                                
 3  * critical                                                     echo                           

Now we do most important things. We create firewall rule in MikroTik from which it will depends range and intensity of data logs. In this example we will be log only TCP 80 and 443 traffic (http, https). To make this rule work we must put it top on the forward chain.

[admin@R2] /ip firewall filter> add chain=forward protocol=tcp dst-port=80,443 action=log disabled=no log-prefix=R2
[admin@R2] /ip firewall filter> print 
Flags: X - disabled, I - invalid, D - dynamic 
 0    chain=output action=accept log=no log-prefix="" 

 1    chain=input action=accept in-interface=ether1-gateway log=no log-prefix="" 

 2    chain=input action=accept log=no log-prefix="" 

 3    chain=forward action=accept log=no log-prefix="" 

 4    chain=input action=accept connection-state=new log=no log-prefix="" 

 5    ;;; default configuration
      chain=input action=accept protocol=icmp log=no log-prefix="" 

 6    ;;; default configuration
      chain=input action=accept connection-state=established,related log=no log-prefix="" 

 7 X  ;;; default configuration
      chain=input action=drop in-interface=ether1-gateway log=no log-prefix="" 

 8 X  ;;; default configuration
      chain=forward action=fasttrack-connection connection-state=established,related,new log=no log-prefix="" 

 9    ;;; default configuration
      chain=forward action=accept connection-state=established,related,new log=no log-prefix="" 

10    chain=forward action=accept in-interface=all-ethernet out-interface=all-ethernet log=no log-prefix="" 

11    chain=forward action=accept in-interface=ether1-gateway out-interface=ether2-master-local log=no log-prefix="" 

12    chain=forward action=accept in-interface=ether2-master-local out-interface=ether1-gateway log=no log-prefix="" 

13    ;;; default configuration
      chain=forward action=drop connection-state=invalid log=no log-prefix="" 

14 X  ;;; default configuration
      chain=forward action=drop connection-state=new connection-nat-state=!dstnat in-interface=ether1-gateway log=no log-prefix="" 

15    chain=forward action=log protocol=tcp dst-port=80,443 log=no log-prefix="R2" 
[admin@R2] /ip firewall filter> move 15 destination=2

But this rule has one defect, it logs everything, and we get a lot of data. So we must add special option which tells our rule to log only start new connection (state NEW).

[admin@R2] /ip firewall filter> set 2 connection-state=new       
[admin@R2] /ip firewall filter> print 
Flags: X - disabled, I - invalid, D - dynamic 
 0    chain=output action=accept log=no log-prefix="" 

 1    chain=input action=accept in-interface=ether1-gateway log=no log-prefix="" 

 2    chain=forward action=log connection-state=new protocol=tcp dst-port=80,443 log=no log-prefix="R2" 

...

We still can improve our rule using options limit, which work like in Linux iptables. The point is, when we have 100 connections from the same host per minute we can limit this data to e.g. only first 5 of them.
Ok, time to test our solution. Now I connect to the Internet from my tablet through MikroTik and I get this log:

root@probook:~# tail /var/log/user.log 
Apr  2 19:37:45 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:33976->216.58.209.46:443, len 60
Apr  2 19:37:46 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:37422->216.58.209.42:443, len 60
Apr  2 19:37:54 probook gnome-session[1456]: (gnome-settings-daemon:1519): GLib-CRITICAL **: Source ID 2018 was not found when attempting to remove it
Apr  2 19:46:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43287->212.77.100.114:443, len 60
Apr  2 19:49:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43288->212.77.100.114:443, len 60
Apr  2 19:52:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43289->212.77.100.114:443, len 60
Apr  2 19:56:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43290->212.77.100.114:443, len 60
Apr  2 19:59:29 probook google-chrome.desktop[2048]: [2048:2079:0402/195929:ERROR:socket_stream.cc(210)] Closing stream with result -7
Apr  2 20:00:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43291->212.77.100.114:443, len 60
Apr  2 20:04:25 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43292->212.77.100.114:443, len 60

The logs we can identified by prefix R2, which I add to the firewall rule or by source IP address. You can change destination where is the logs saved [1]. The R2 is the name of my MikroTik in my home lab, and I had connected via VLAN20, what you can see in logs.

[1] http://www.rsyslog.com/storing-messages-from-a-remote-system-into-a-specific-file/

Saturday, April 2, 2016

Mikrotik + zewnętrzny serwer logów syslog

W tym wpisie pokażę prosty przepis jak skonfigurować serwer logów z MikroTik'iem. Z założenia chodziło o to, aby zapisywać logi ruchu sieciowego, tak jak to niby ISP ma obowiązek ;). Dlaczego na serwerze zewnętrznym? Bo to najlepszy sposób, po pierwsze MikroTik nie ma zbyt zasobnej pamięci, a po drugie to najbardziej elastyczna metoda. Raz skonfigurowany serwer logów może być później wykorzystany wielokrotnie, np. do zbierania logów również ze switcha.

Więc mamy dwa urządzenia, router MikroTik (MT) i serwer Linux. Najpierw na serwerze ustawiamy demona logów. Jak to robię na Debianie 8 (jessie) gdzie jest rsyslog. W pliku /etc/rsyslog.conf usuwamy komentarze z poniższych linii:

# provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

Następnie resetujemy usługę:

systemctl restart rsyslog.service

Oczywiście trzeba się upewnić, czy port UDP 514 na naszym serwerze jest otwarty w zaporze, w przypadku systemu CentOS trzeba jeszcze włączyć ten port w SELinux.
I to by było wszystko co musimy właściwie zrobić na Linuksie, teraz przechodzimy do MT i ustawiamy zdalny serwer logów:

[admin@R2] /system logging action> print 
Flags: * - default 
 0 * name="memory" target=memory memory-lines=1000 memory-stop-on-full=no 

 1 * name="disk" target=disk disk-file-name="log" disk-lines-per-file=1000 disk-file-count=2 disk-stop-on-full=no 


 2 * name="echo" target=echo remember=yes 


 3 * name="remote" target=remote remote=0.0.0.0 remote-port=514 src-address=0.0.0.0 bsd-syslog=no syslog-time-format=bsd-syslog 

     syslog-facility=daemon syslog-severity=auto 
[admin@R2] /system logging action> set 3 remote=172.16.16.101

Następnie logi typu info przekierujemy do celu remote:

[admin@R2] > /system logging print 
Flags: X - disabled, I - invalid, * - default 
 #    TOPICS                                                       ACTION                                                      PREFIX    
 0  * info                                                         memory                                                                
 1  * error                                                        memory                                                                
 2  * warning                                                      memory                                                                
 3  * critical                                                     echo                        
[admin@R2] > /system logging set 0 action=remote 
[admin@R2] > /system logging print               
Flags: X - disabled, I - invalid, * - default 
 #    TOPICS                                                       ACTION                                                      PREFIX    
 0  * info                                                         remote                                                                
 1  * error                                                        memory                                                                
 2  * warning                                                      memory                                                                
 3  * critical                                                     echo                           

Teraz najważniejsze, ustawiamy regułę w zaporze MT, to od niej zależy stopień i intensywność logowania. Ja założyłem, iż przechwytujemy tylko ruch po protokole TCP i portach 80 i 443 (czyli http i https). Musimy również umieścić naszą regułę na początku łańcucha forward:


[admin@R2] /ip firewall filter> add chain=forward protocol=tcp dst-port=80,443 action=log disabled=no log-prefix=R2
[admin@R2] /ip firewall filter> print 
Flags: X - disabled, I - invalid, D - dynamic 
 0    chain=output action=accept log=no log-prefix="" 

 1    chain=input action=accept in-interface=ether1-gateway log=no log-prefix="" 

 2    chain=input action=accept log=no log-prefix="" 

 3    chain=forward action=accept log=no log-prefix="" 

 4    chain=input action=accept connection-state=new log=no log-prefix="" 

 5    ;;; default configuration
      chain=input action=accept protocol=icmp log=no log-prefix="" 

 6    ;;; default configuration
      chain=input action=accept connection-state=established,related log=no log-prefix="" 

 7 X  ;;; default configuration
      chain=input action=drop in-interface=ether1-gateway log=no log-prefix="" 

 8 X  ;;; default configuration
      chain=forward action=fasttrack-connection connection-state=established,related,new log=no log-prefix="" 

 9    ;;; default configuration
      chain=forward action=accept connection-state=established,related,new log=no log-prefix="" 

10    chain=forward action=accept in-interface=all-ethernet out-interface=all-ethernet log=no log-prefix="" 

11    chain=forward action=accept in-interface=ether1-gateway out-interface=ether2-master-local log=no log-prefix="" 

12    chain=forward action=accept in-interface=ether2-master-local out-interface=ether1-gateway log=no log-prefix="" 

13    ;;; default configuration
      chain=forward action=drop connection-state=invalid log=no log-prefix="" 

14 X  ;;; default configuration
      chain=forward action=drop connection-state=new connection-nat-state=!dstnat in-interface=ether1-gateway log=no log-prefix="" 

15    chain=forward action=log protocol=tcp dst-port=80,443 log=no log-prefix="R2" 
[admin@R2] /ip firewall filter> move 15 destination=2

Tak wygląda reguła w najprostszej postaci, ale ma ona pewien defekt, loguje wszystko! Więc dodajemy odpowiednią opcję aby logował tylko początek połączenia (stan NEW):

[admin@R2] /ip firewall filter> set 2 connection-state=new       
[admin@R2] /ip firewall filter> print 
Flags: X - disabled, I - invalid, D - dynamic 
 0    chain=output action=accept log=no log-prefix="" 

 1    chain=input action=accept in-interface=ether1-gateway log=no log-prefix="" 

 2    chain=forward action=log connection-state=new protocol=tcp dst-port=80,443 log=no log-prefix="R2" 

...

Można jeszcze ją ulepszyć poprzez dodanie opcji limit, działa ona tak jak w iptables. Chodzi o to, że gdy mamy np. 100 połączeń na minutę do tego samego hosta to wystarczy nam tylko np. 5 z nich w logach.
Ok, czas na test, łączę się z tabletu z internetem przez MT i w systemie mam logi:

root@probook:~# tail /var/log/user.log 
Apr  2 19:37:45 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:33976->216.58.209.46:443, len 60
Apr  2 19:37:46 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:37422->216.58.209.42:443, len 60
Apr  2 19:37:54 probook gnome-session[1456]: (gnome-settings-daemon:1519): GLib-CRITICAL **: Source ID 2018 was not found when attempting to remove it
Apr  2 19:46:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43287->212.77.100.114:443, len 60
Apr  2 19:49:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43288->212.77.100.114:443, len 60
Apr  2 19:52:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43289->212.77.100.114:443, len 60
Apr  2 19:56:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43290->212.77.100.114:443, len 60
Apr  2 19:59:29 probook google-chrome.desktop[2048]: [2048:2079:0402/195929:ERROR:socket_stream.cc(210)] Closing stream with result -7
Apr  2 20:00:07 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43291->212.77.100.114:443, len 60
Apr  2 20:04:25 172.16.16.5 firewall,info R2 forward: in:bridge_vlan20 out:ether1-gateway, src-mac 98:52:b1:79:31:94, proto TCP (SYN), 172.31.20.200:43292->212.77.100.114:443, len 60

Logi możemy identyfikować poprzez prefix R2 jaki dodałem w regule zapory lub przez adres IP źródła. Jeżeli chcesz aby logi zapisywały są we wskazanej lokalizacji, to oczywiście możesz to zmienić[1]. R2 to oznaczenie mojego MT w moim labie a ja łączyłem się przez VLAN20, co jest zresztą w logach.

Wednesday, January 6, 2016

Rozbudowa sieciowego lab-u

Ostatnimi czasy postanowiłem rozbudować mój domowy lab, który opisywałem już wcześniej w poście Domowy lab Cisco + MikroTik. W tamtym poście skupiłem się głównie na sprzęcie Cisco, ponieważ wtedy to właśnie z niego składał się mój domowy lab, ale wspominałem również o nowym tanim modelu MikroTik-a hAP lite. Tym razem poszedłem w stronę zakupu routerów MikrotTik, dokupiłem ich parę. Niestety (tak mi się wydawało na początku) teraz nie można już kupić hAP lite w leżącej obudowie, cóż zmienili design ale ta stojąca jest dobra i wcale się nie przewraca jak powpinamy wszystkie kable - pięknie stoi i prezentuje się. Warto byłoby napisać o tym jak wygląda taki MikroTik w środku, co ciekawe w leżącej obudowie jest inny chip niż w stojące, ale to różnica głównie producencka, bo architektura jest ta sama. Jako ciekawostkę mogę podać, iż w środku znajduje się tylko jeden chip, który robi za WiFi, Ethernet, Switching, USB, te ostatnie obsługiwane jest tylko jako zasilanie. Jednym słowem jest to bardzo lekka konstrukcja, dlatego jest zasilana przez port USB 5V, a wszystkie razem w moim labie pobierają 7.5W. Oczywiście ich procesory mają odpowiednio mniejszą moc, ale coś za coś. Odpaliłem je nawet z power banku.
Połączenie routerów MikroTik i switch-ów Cisco to piękne rozwiązanie. MikroTik daje Ci routing i wireless, zresztą wystarczy spojrzeć na stronę certyfikacyjną. Tak tak, MikroTik od jakiegoś czasu uruchomił fajny program certyfikacyjny z mapką gdzie najbliżej znajdziesz specjalistę do urządzeń MikroTik.
Więc teraz w swoim labie mam piękny routing, również z Cisco (switch 3550), wireless (czyli AP-ki MikroTik, które mają full opcji dla WiFi) oraz switching na Cisco. A teraz pochwale się jak pięknie wygląda mój lab.




A teraz przedstawię schemat sieci jaki zbudowałem.


Tu należy się trochę wyjaśnień, zresztą i tak chciałem poruszyć temat tego jak fajnie można wykorzystać przemądre switche Cisco Catalist 3550. Zauważ, iż wewnątrz siatki routerów (przeważnie kable czerwone) użyłem prawdziwych publicznych adresów IP, co wyraźnie odróżnia je jako routing WAN. Oczywiście na tej samej linii może być i WAN i LAN. Ale skupmy się na niebieskich przerywanych liniach, to LAN, sieci brzegowe. Tam użyłem adresacji prywatnej, ale połączone jest to z WiFi za pomocą VLAN-ów. Otóż od routera R1, R2 i R3 idzie kabel do jednego 3550, ale jest to tak skonfigurowane, jakby były to fizycznie inne switche w innej lokalizacji. W 3550 możemy robić porty tylko w L3 (routing), L2+L3 (VLAN + IP), oraz tylko L2 (VLAN bez IP) i tą ostatnią opcje zastosowałem. Dzięki temu iż te VLAN-y nie mają IP to nie mogą komunikować się z całym routingiem, który jednocześnie jest uruchomiony wewnątrz 3550. Gdybym ustalił adres IP dla tych VLAN-ów i włączył routing, to okazałoby się iż np. sieć 10.4.24.0/24 ma bezpośrednie połączenie z 22.22.22.254, a tak to wszystkie połączone urządzenia do tej sieci, czy to przez kabel za pomocą portów w 3550, czy przez WiFi do sieci 22.22.22.254 muszą przebyć drogę przez routing WAN. Podsumowując, tam gdzie są niebieskie przerywane linie, można na tym schemacie wstawić oddzielne switch za routerem i efekt będzie taki sam.