Showing posts with label VPN. Show all posts
Showing posts with label VPN. Show all posts

Thursday, November 15, 2018

Security: Site to Site IPsec VPN

In this post we configure a basic site to site IPsec VPN. I have configured three cisco routers R1, R2 and R3. R1 and R3 are connected through R2. They are routed using static route configuration.




Checking the interface of R1.

 R1(config)#do show ip interface brief  
 Interface       IP-Address   OK? Method Status        Protocol   
 FastEthernet0/0    10.10.10.10   YES manual up          up   
 FastEthernet0/1    192.168.10.1  YES manual up          down  

Checking the interface of R3

 R3(config)#do show ip interface brief  
 Interface       IP-Address   OK? Method Status        Protocol   
 FastEthernet0/0    11.11.11.11   YES manual up          up   
 FastEthernet0/1    192.168.20.1  YES manual up          down   

Configure access-list to permit the interesting traffic from R1 to R3.

 R1(config)#do show access-list  
 R1(config)#access-list 110 permit ip 192.168.10.0 0.0.0.255 192.168.30.0 0.0.0.255  
 R1(config)#do show access-list  
 Extended IP access list 110  
   10 permit ip 192.168.10.0 0.0.0.255 192.168.30.0 0.0.0.255  

Now, we configure ISAKMP - Security Association.

 R1(config)#crypto isakmp policy 10  
 R1(config-isakmp)#encryption aes 256  
 R1(config-isakmp)#authentication pre-share  
 R1(config-isakmp)#group 14  
 R1(config-isakmp)#exit  

 R1(config)#crypto isakmp key vpnp@!! address 11.11.11.1  
 R1(config)#crypto ipsec transform-set VPN-SET esp-aes esp-sha-hmac  
 R1(config)#crypto map VPN-MAP 10 ipsec-isakmp  
 % NOTE: This new crypto map will remain disabled until a peer  
     and a valid access list have been configured.  
 R1(config-crypto-map)#description VPN connection to Router3  
 R1(config-crypto-map)#set peer 11.11.11.1  
 R1(config-crypto-map)#set transform-set VPN-SET  
 R1(config-crypto-map)#match address 110  
 R1(config-crypto-map)#exit  

We set the crypto map to the interface s0/1/0 on R1.

 R1(config)#int s0/1/0  
 R1(config-if)#crypto map VPN-MAP  
 *Jan 3 07:16:26.785: %CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is ON  
 R1(config-if)#exit  

Then we check the IPSEC Security Association.

 R1(config)#do show crypto ipsec sa  
 interface: Serial0/1/0  
   Crypto map tag: VPN-MAP, local addr 10.10.10.1  
   protected vrf: (none)  
   local ident (addr/mask/prot/port): (192.168.10.0/255.255.255.0/0/0)  
   remote ident (addr/mask/prot/port): (192.168.30.0/255.255.255.0/0/0)  
   current_peer 11.11.11.1 port 500  
   PERMIT, flags={origin_is_acl,}  
   #pkts encaps: 0, #pkts encrypt: 0, #pkts digest: 0  
   #pkts decaps: 0, #pkts decrypt: 0, #pkts verify: 0  
   #pkts compressed: 0, #pkts decompressed: 0  
   #pkts not compressed: 0, #pkts compr. failed: 0  
   #pkts not decompressed: 0, #pkts decompress failed: 0  
   #send errors 0, #recv errors 0  
    local crypto endpt.: 10.10.10.1, remote crypto endpt.:11.11.11.1  
    path mtu 1500, ip mtu 1500, ip mtu idb Serial0/1/0  
    current outbound spi: 0x0(0)  
    inbound esp sas:  
    inbound ah sas:  
    inbound pcp sas:  
    outbound esp sas:  
    outbound ah sas:  
    outbound pcp sas:  

Configure access-list to allow interesting traffic from R3 to R1.

 R3(config)#access-list 110 permit ip 192.168.30.0 0.0.0.255 192.168.10.0 0.0.0.255   

Configure IPsec on R3.

 R3(config)#crypto isakmp policy 10  
 R3(config-isakmp)#encryption aes 256  
 R3(config-isakmp)#authentication pre-share  
 R3(config-isakmp)#group 14  
 R3(config-isakmp)#exit  

 R3(config)#crypto isakmp key vpnp@!! address 10.10.10.1  
 R3(config)#crypto ipsec transform-set VPN-SET esp-aes esp-sha-hmac  
 R3(config)#crypto map VPN-MAP 10 ipsec-isakmp  
 % NOTE: This new crypto map will remain disabled until a peer  
     and a valid access list have been configured.  
 R3(config-crypto-map)#description VPN connection to Router1  
 R3(config-crypto-map)#set peer 10.10.10.1  
 R3(config-crypto-map)#set transform-set VPN-SET  
 R3(config-crypto-map)#match address 110  
 R3(config-crypto-map)#exit  

Apply the crypto map to the interface s0/0/0.

 R3(config)#int s0/0/0  
 R3(config-if)#crypto map VPN-MAP  
 *Jan 3 07:16:26.785: %CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is ON  

Then we can check the IPsec Security Association in R1 after sending some ping requests from PC3 to PC1. We can see the number of encrypted and decrypted packets.

 R1(config)#do show crypto ipsec sa  
 interface: Serial0/1/0  
   Crypto map tag: VPN-MAP, local addr 10.10.10.1  
   protected vrf: (none)  
   local ident (addr/mask/prot/port): (192.168.10.0/255.255.255.0/0/0)  
   remote ident (addr/mask/prot/port): (192.168.30.0/255.255.255.0/0/0)  
   current_peer 11.11.11.1 port 500  
   PERMIT, flags={origin_is_acl,}  
   #pkts encaps: 6, #pkts encrypt: 6, #pkts digest: 0  
   #pkts decaps: 7, #pkts decrypt: 7, #pkts verify: 0  
   #pkts compressed: 0, #pkts decompressed: 0  
   #pkts not compressed: 0, #pkts compr. failed: 0  
   #pkts not decompressed: 0, #pkts decompress failed: 0  
   #send errors 0, #recv errors 0  
    local crypto endpt.: 10.10.10.1, remote crypto endpt.:11.11.11.1  
    path mtu 1500, ip mtu 1500, ip mtu idb Serial0/1/0  
    current outbound spi: 0x451F673A(1159685946)  
    inbound esp sas:  
    spi: 0x080D393D(135084349)  
  --More--   

Thus, we have successfully setup a site-to-site IPsec VPN.

Saturday, March 10, 2018

Linux: Configure VPN using NMCLI

Recently I was trying to connect to a PPTP VPN from my home workstation, which has fedora 30 installed and I wasn't able to. So I researched online about it and figured how to create a VPN connection using 'Network Manager' tool nmcli.

Below I have demonstrated the method of creating the connection.

Create Connection

 [spameggs@foobar ~]$ sudo nmcli connection add connection.id VPN_2 connection.type vpn connection.interface-name wlp2s0 connection.permissions spameggs vpn.service-type pptp vpn.data gateway=199.202.117.191   
 [spameggs@foobar ~]$ sudo nmcli connection modify VPN_2 vpn.user-name numb vpn.secrets password=damisk76  

We can check the connection details from the related file VPN_2.nmconnection created in the below location.

 [spameggs@foobar system-connections]$ pwd  
 /etc/NetworkManager/system-connections  

 [spameggs@foobar system-connections]$ sudo cat VPN_2.nmconnection   
 [sudo] password for spameggs:   
 [connection]  
 id=VPN_2  
 uuid=1342c044-108a-447c-8712-de988346fccb  
 type=vpn  
 interface-name=wlp2s0  
 permissions=user:spameggs:;  
   
 [vpn]  
 gateway=199.202.117.191  
 service-type=org.freedesktop.NetworkManager.pptp  
 user-name=numb  
 [vpn-secrets]  
 password=damisk76  
 [ipv4]  
 dns-search=  
 method=auto  
 [ipv6]  
 addr-gen-mode=stable-privacy  
 dns-search=  
 method=auto  

Test Connection


I could not connect the VPN and I see the following logs from journalctl.

 Started the VPN service, PID 6152  
 Saw the service appear; activating connection  
 VPN connection: (ConnectInteractive) reply received  
 VPN plugin: state changed: starting (3)  
 VPN plugin: failed: connect-failed (1)  
 VPN plugin: state changed: stopping (5)  
 VPN service disappeared  

Load Modules


I did further research and found that we need to load some modules. There are two methods for this process. For temporary use we can run the following command from a privileged user.

 [root@drone ~]# for module in nf_nat_pptp nf_conntrack_pptp
 > do  
 > modprobe $module;  
 > done  

Or we can load these modules on boot permanently by adding the above module names in the file /etc/modules-load.d/netfilter.conf as follows.

 [spameggs@foobar ~]$ sudo cat /etc/modules-load.d/netfilter.conf  
 nf_nat_pptp  
 nf_conntrack_pptp  

We need to reboot for the changes to occur.

Configure Firewall Rules


There is no need to make any changes in Selinux. However, we have to add few rules in the firewall to allow PPTP connections.

 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --add-port=1723/tcp   
 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT  
 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --direct --add-rule ipv6 filter INPUT 0 -p gre -j ACCEPT  
 [spameggs@foobar ~]$ sudo firewall-cmd --permanent --add-masquerade  
 [spameggs@foobar ~]$ sudo firewall-cmd --reload  

Reconnect Successfully


We can see below that the connection is finally established successfully.

 [spameggs@foobar ~]$ nmcli connection up VPN_2   
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)  

Verify Connection


We can verify by checking the output of ifconfig. A new ppp0 interface has been automatically created with the IP address 192.168.101.149.

 ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1400  
     inet 192.168.101.149 netmask 255.255.255.255 destination 192.168.101.150  
     ppp txqueuelen 3 (Point-to-Point Protocol)  
     RX packets 1271 bytes 1398779 (1.3 MiB)  
     RX errors 0 dropped 0 overruns 0 frame 0  
     TX packets 1213 bytes 127555 (124.5 KiB)  
     TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  

The command ip addr also shows the ppp0 interface.

 [foobar@drone system-connections]$ ip addr  
 12: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1400 qdisc fq_codel state UNKNOWN group default qlen 3  
   link/ppp   
   inet 192.168.101.149 peer 192.168.101.151/32 scope global ppp0  
     valid_lft forever preferred_lft forever  

We can check the routes for the VPN traffic by running ip route and route commands.

 [spameggs@foobar ~]$ ip route  
 default dev ppp0 proto static scope link metric 50   
 default via 192.168.100.1 dev wlp2s0 proto dhcp metric 600   
 192.168.100.0/24 dev wlp2s0 proto kernel scope link src 192.168.100.29 metric 600   
 192.168.100.1 dev wlp2s0 proto static scope link metric 600   
 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown   
 192.168.101.149 dev ppp0 proto kernel scope link src 192.168.101.148 metric 50   
 199.202.117.191 via 192.168.100.1 dev wlp2s0 src 192.168.100.29   
 199.202.117.191 via 192.168.100.1 dev wlp2s0 proto static metric 600   

 [spameggs@foobar ~]$ route  
 Kernel IP routing table  
 Destination   Gateway     Genmask     Flags Metric Ref  Use Iface  
 default     0.0.0.0     0.0.0.0     U   50   0    0 ppp0  
 default     _gateway    0.0.0.0     UG  600  0    0 wlp2s0  
 192.168.100.0   0.0.0.0     255.255.255.0  U   600  0    0 wlp2s0  
 _gateway    0.0.0.0     255.255.255.255 UH  600  0    0 wlp2s0  
 192.168.122.0  0.0.0.0     255.255.255.0  U   0   0    0 virbr0  
 192.168.101.149 0.0.0.0     255.255.255.255 UH  50   0    0 ppp0  
 199.202.117.191 _gateway    255.255.255.255 UGH  0   0    0 wlp2s0  
 199.202.117.191 _gateway    255.255.255.255 UGH  600  0    0 wlp2s0  

Disconnect VPN


Now we disconnect the VPN connection by issuing the below command.

 [spameggs@foobar ~]$ nmcli connection down VPN_2   
 Connection 'VPN_2' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)