I thought I’d share a straight-forward configuration script that allows Windows 10 to connect via IKEv2 VPN to a MikroTik. I’ve tested this on Windows 10 version 2004 and RouterOS 6.45.9 (longterm) + 6.47.3 (stable). I’ve tested this on the following Mikrotik hardware CRS125, CCR1009, HexS, RB750, RB951, RB2011
With Covid-19 I’ve had a lot of small businesses that suddenly wanted to remote in from home. I have been setting them up using an IKEv2 VPN between Windows 10 and the MikroTik router. I’ve been using it for a number of months. I’ve assembled this from various instructions on the internet and added the proper powershell commands to make it work.
I thought there would be value in putting the whole configuration together (both client and server) in one post that has been tested with the latest versions .
Set up MikroTik
Create Certificates on MikroTik
/certificate add name=template-CA common-name=ca.domain.com subject-alt-name=DNS:ca.domain.com days-valid=3650 key-size=2048 organization=domain.com key-usage=crl-sign,key-cert-sign
/certificate add name=template-Router common-name=remote.domain.com subject-alt-name=DNS:1234567890ab.sn.mynetname.net days-valid=1095 key-size=2048 organization=domain.com key-usage=tls-server
/certificate add name=template-User common-name=Template subject-alt-name=email:[email protected] days-valid=396 key-size=2048 organization=domain.com key-usage=tls-client
/certificate add name=CA copy-from=template-CA
/certificate add name=Router copy-from=template-Router
/certificate add [email protected] copy-from=template-User
/certificate sign CA
/certificate set trusted=yes CA
/certificate sign Router ca=CA
/certificate set trusted=yes Router
/certificate sign [email protected] ca=CA
/certificate set trusted=yes [email protected]
Export the Certificates to the MikroTik’s file system
/certificate export-certificate CA type=pem
/certificate export-certificate Router type=pem
/certificate export-certificate [email protected] type=pkcs12 export-passphrase=certKeyPass
Set up IP Pool for VPN users
/ip pool add name=pool-vpn ranges=10.10.10.10-10.10.10.50
Configure IPSec on MikroTik
# Replace 192.168.0.0/24 with the remote network
/ip ipsec mode-config add name="modeconf-vpn" address-pool=pool-vpn address-prefix-length=32 split-include=192.168.0.0/24 system-dns=no
/ip ipsec proposal add name="proposal-vpn" lifetime=8h pfs-group=none auth-algorithms=sha512,sha256,sha1 enc-algorithms=aes-256-cbc,aes-256-ctr,aes-256-gcm,aes-192-ctr,aes-192-gcm,aes-128-cbc,aes-128-ctr,aes-128-gcm
/ip ipsec profile add name="profile-vpn" nat-traversal=yes proposal-check=obey dh-group=modp2048,modp1536,modp1024 enc-algorithm=aes-256,aes-192,aes-128 hash-algorithm=sha256
/ip ipsec policy group add name=”group-vpn”
/ip ipsec policy add dst-address=10.10.10.0/24 group="group-vpn" proposal="proposal-vpn" src-address=0.0.0.0/0 template=yes ipsec-protocols=esp level=require protocol=all action=encrypt
# replace x.x.x.x with the public IP address
/ip ipsec peer add name=”peer-WAN” exchange-mode=ike2 address=0.0.0.0/0 local-address=x.x.x.x passive=yes send-initial-contact=yes profile="profile-vpn"
Add the IPSec Identities
/ip ipsec identity add auth-method=digital-signature certificate=Router generate-policy=port-strict match-by=certificate mode-config="modeconf-vpn" peer="peer-WAN" policy-template-group="group-vpn" remote-id=user-fqdn:[email protected] [email protected]
Add the Firewall rules
Place them higher up in the filter rules - and as u/Kaldek pointed out in the comments, they need to be before the FastTrack rules.
/ip firewall filter add chain=input action=accept protocol=udp dst-port=500,4500 comment="IPSec Policies"
/ip firewall filter add chain=input action=accept protocol=ipsec-esp
/ip firewall filter add chain=forward action=accept ipsec-policy=in,ipsec
/ip firewall filter add chain=forward action=accept ipsec-policy=out,ipsec
Download Certficates
Typically in Winbox you just go into files and right-click the certificates you exported and select “Download”
Install Certificates on Windows 10 Client (PowerShell)
# Replace CertPass with the certificate password you put when exporting
$certpass = $(ConvertTo-SecureString -String "CertPass" -AsPlainText -Force)
Push-Location "C:\Users\JohnDoe\Desktop\CertsFromMikrotik"
Import-Certificate -FilePath 'cert_export_CA.crt' -CertStoreLocation Cert:\LocalMachine\root
Import-Certificate -FilePath 'cert_export_Router.crt' -CertStoreLocation Cert:\LocalMachine\CA
Import-PfxCertificate -FilePath '[email protected]' -CertStoreLocation Cert:\LocalMachine\My -Password $certpass
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($(Join-Path -Path $(Get-Location) -ChildPath cert_export_CA.crt))
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($(Join-Path -Path $(Get-Location) -ChildPath cert_export_CA.crt))
Create the VPN Connection on Windows 10 Client (PowerShell)
# Replace 1234567890ab.sn.mynetname.net with your router's dynamic DNS name
Add-VpnConnection -Name "VPN Test" -ServerAddress "1234567890ab.sn.mynetname.net" -TunnelType Ikev2 -SplitTunneling -EncryptionLevel Required -AuthenticationMethod MachineCertificate -MachineCertificateIssuerFilter $cert
All done - the user should be able to just click on the network icon in the system tray, and click “Connect” on the VPN …
Note:
If you get IKE authentication credentials are unacceptable on Windows 10, and you’ve used the above instructions … then most of the time it is caused because the Router certificate does not match the hostname you are trying to connect to. The subject-alt-name should be the same hostname that you are trying to connect to from the Windows VPN client.
EDIT:
Solving the 8-Minute disconnect issue with Mac OS X Catalina (10.15.x)
Catalina will do a rekey at the 8 minute mark, and as a result it will cause the client to disconnect. According to this post (https://forum.netgate.com/topic/105807/macos-10-12-ikev2-disconnects-after-8-minutes/7) these are the encryption / hash / dhgroup values that will not cause the 8min disconnect:
Enc: AES256 / Hash: SHA256 / DH group: 14 (2048 bit)
Enc: AES256 / Hash: SHA256 / DH group: 5 (1536 bit)
Enc: AES256 / Hash: SHA256 / DH group: 19 (NIST ECP 256)
To get MacOS 10.15x to stay connected I reconfigured the Mikrotik IPSec proposal and profile to use AES256 / SHA256 / DH Group 14:
/ip ipsec proposal add name="proposal-vpn" lifetime=8h pfs-group=none auth-algorithms=sha256 enc-algorithms=aes-256-cbc
/ip ipsec profile add name="profile-vpn" nat-traversal=yes proposal-check=obey dh-group=modp2048 enc-algorithm=aes-256 hash-algorithm=sha256
It was now able to connect and stay connected. But Windows 10 failed to connect - throwing a “Policy Error” when trying to connect. To get around this, I went into Powershell and manually set the IPSec configuration.
Set-VpnConnectionIpsecConfiguration -ConnectionName "<VPN Name>" -AuthenticationTransformConstants SHA256128 -CipherTransformConstants AES256 -DHGroup Group14 -EncryptionMethod AES256 -PfsGroup None -IntegrityCheckMethod SHA256 -Force
Now I could connect …