{"id":1342,"date":"2023-05-30T16:31:05","date_gmt":"2023-05-30T15:31:05","guid":{"rendered":"https:\/\/www.floyd.ch\/?p=1342"},"modified":"2023-05-31T07:48:47","modified_gmt":"2023-05-31T06:48:47","slug":"macos-built-in-vpn-ikev2-force-remove-vpn-dns-resolver","status":"publish","type":"post","link":"https:\/\/www.floyd.ch\/?p=1342","title":{"rendered":"MacOS built-in VPN IKEv2 force remove VPN DNS resolver"},"content":{"rendered":"<p>This is for once not a security related post, but as I couldn&#8217;t find one important detail on the Internet, I thought I&#8217;ll share my story.<\/p>\n<p>When connecting with the MacOS built-in VPN service to a server, MacOS (12, Monterey) will happily accept all the parameters coming from the VPN server: Force their DNS setting, force their IP address routes (in this case route all traffic through VPN), etc. and there is no GUI to change it.<\/p>\n<p>As I didn&#8217;t want to route all traffic through the VPN and as I didn&#8217;t want DNS resolutions to go through the VPN (except for certain domains), I wanted to reconfigure MacOS to do as I like. The routing issue was straight forward by adding a couple of specific routes (10.0.0.0\/8) and then telling MacOS to use my usual default gateway in my network (192.168.1.1):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/sbin\/route -nv add -net 10.0.0.0\/8 -interface ipsec0\r\n\/sbin\/route change default 192.168.1.1\r\n<\/pre>\n<p>However, when it came to changing the DNS settings, there are many <a href=\"https:\/\/superuser.com\/questions\/86184\/change-dns-server-from-terminal-or-script-on-mac-os-x\" rel=\"noopener\" target=\"_blank\">not very helpful<\/a> links that suggest that you change the &#8220;Service Order&#8221; (you can&#8217;t, IKEv2 VPNs do not get a Service entry) via GUI (network settings) or command line (<code>networksetup<\/code>). So that was not an option.<\/p>\n<p>What sounded absolutely plausible is to change the SearchOrder (sometimes shown as just &#8220;order&#8221; in MacOS tools) of the VPN-DNS server to a higher value. This was attempted by <a href=\"https:\/\/rakhesh.com\/powershell\/vpn-client-over-riding-dns-on-macos\/\" rel=\"noopener\" target=\"_blank\">Rakhesh<\/a> but also didn&#8217;t work as he explains. He then explains that he overwrites the VPN&#8217;s DNS IP address with the one we want (<code>d.add ServerAddresses * 192.168.1.1<\/code>), but that didn&#8217;t work for me either and that just lead to not being able to do DNS resolving at all (my guess would be MacOS then tries to reach 192.168.1.1 via the VPN interface, which doesn&#8217;t work). So for me all available approaches didn&#8217;t work.<\/p>\n<p>However, I found out that I can change something called &#8220;PrimaryRank&#8221; from &#8220;first&#8221; to &#8220;second&#8221;, which then made the DNS server of the VPN disappear as a resolver in the &#8220;DNS configuration&#8221; section of <code>scutil --dns<\/code> and everything worked as expected:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ sudo scutil\r\n&gt; get State:\/Network\/Service\/E6&#x5B;REDACTED]57C\r\n&gt; d.show\r\n&lt;dictionary&gt; {\r\n  PrimaryRank : First\r\n}\r\n&gt; d.add PrimaryRank Second\r\n&gt; set State:\/Network\/Service\/E6&#x5B;REDACTED]57C\r\n&gt; exit\r\n<\/pre>\n<p>The only problem is that I need to change that value back to &#8220;first&#8221; before I connect again. So the entire script I run and then prompts me to connect the VPN:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\n# OPTIONS: default gateway and DNS server to use for normal Internet connection\r\nGW_TO_USE=&quot;192.168.1.1&quot;\r\nDNS_TO_USE=&quot;$GW_TO_USE&quot;\r\n\r\n# Run this script after connection in the Network settings of MacOS to the VPN\r\n\r\n# Check if running as root\r\nif &#x5B; $EUID -ne 0 ]; then\r\n    echo &quot;This script should be run as root.&quot; &gt; \/dev\/stderr\r\n    exit 1\r\nfi\r\n\r\necho &quot;+ Fixing PrimaryRank to the original value&quot;\r\nscutil &lt;&lt; EOF\r\nget State:\/Network\/Service\/E6&#x5B;REDACTED]57C\r\nd.add PrimaryRank First\r\nset State:\/Network\/Service\/E6&#x5B;REDACTED]57C\r\nexit\r\nEOF\r\n\r\nread -p &quot;Connect VPN now, then press Enter to continue&quot; &lt;\/dev\/tty\r\n\r\necho &quot;+ Sleeping 3 seconds to make sure VPN is correctly connected...&quot;\r\nsleep 3\r\n\r\n##\r\n# Routing part\r\n##\r\n\r\necho &quot;+ Adding a manual route for VPN IP address range&quot;\r\n\/sbin\/route -nv add -net 10.0.0.0\/8 -interface ipsec0\r\n\r\necho &quot;+ Removing VPN as the default gateway&quot;\r\n\/sbin\/route change default &quot;$GW_TO_USE&quot;\r\n\r\n##\r\n# DNS part\r\n##\r\n\r\necho &quot;+ Last line of \/etc\/resolv.conf:&quot;\r\ntail -1 \/etc\/resolv.conf\r\n\r\necho &quot;+ add DNS for *.example.org in \/etc\/resolver\/example.org, it will be the last line from \/etc\/resolv.conf!&quot;\r\ntail -1 \/etc\/resolv.conf &gt; \/etc\/resolver\/example.org\r\necho &quot;+ Last time we checked this was:&quot;\r\necho 'nameserver 10.15.7.8'\r\n\r\necho &quot;+ Fixing VPN DNS always being used&quot;\r\nscutil &lt;&lt; EOF\r\nget State:\/Network\/Service\/E6&#x5B;REDACTED]57C\r\nd.add PrimaryRank Second\r\nset State:\/Network\/Service\/E6&#x5B;REDACTED]57C\r\nexit\r\nEOF\r\n\r\necho &quot;+ sleeping for 2 seconds&quot;\r\nsleep 2\r\n\r\necho &quot;+ Your new \/etc\/resolv.conf:&quot;\r\ntail -1 \/etc\/resolv.conf\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is for once not a security related post, but as I couldn&#8217;t find one important detail on the Internet, I thought I&#8217;ll share my story. When connecting with the MacOS built-in VPN service to a server, MacOS (12, Monterey) &hellip; <a href=\"https:\/\/www.floyd.ch\/?p=1342\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[42,219,222,221,223,220,218],"class_list":["post-1342","post","type-post","status-publish","format-standard","hentry","category-useful-scripts","tag-dns","tag-ikev2","tag-primaryrank","tag-scutil","tag-searchorder","tag-serveraddresses","tag-vpn"],"_links":{"self":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts\/1342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1342"}],"version-history":[{"count":12,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts\/1342\/revisions"}],"predecessor-version":[{"id":1354,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts\/1342\/revisions\/1354"}],"wp:attachment":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}