Showing posts with label vpn. Show all posts
Showing posts with label vpn. Show all posts

Thursday, October 4, 2018

HttpClient.GetAsync times out when connected to VPN

Leave a Comment

C# 4.5.2 framework HttpClient.GetAsync() method works fine on Windows 10 when system is not using VPN.

When VPN is connected HttpClient.GetAsync() call to the same address just blocks until it times out. Both Edge and Chrome have no issues accessing that same address.

Is there a way to see what is happening? What is HttpClient doing differently?

Update: Got some interesting clues by calling Dns.GetHostEntry(). Without VPN this call returned only IPv4 addresses that all could be connected to. With VPN client connected Dns.GetHostEntry() returned additional IPv6 addresses at the top of the list. Connection to all IPv6 addresses timed out but all IPv4 ones still worked OK. Now is there a way to figure out without trying to connect which addresses work and which ones do not?

2 Answers

Answers 1

In my experience, this sounds like a VPN / firewall issue to me. One quick thing to toggle in windows is under you VPN adapter properties, try unchecking "Use default gateway on remote network" - I know it sounds like a long shot but have had this problem in the past... enter image description here

Answers 2

Have to answer this myself as this problem has a simple cause but very confusing symptoms.

The root cause:

DNS reports only IPv4 addresses for the host when system is not connected to VPN. All IPv4 addresses are usable.

When VPN connection is active DNS returns IPv6 addresses in addition to IPv4. IPv4 addresses are still accessible but IPv6 are not.

The cause of such invalid network configuration is still a mystery that deserves its own separate post.

Confusing part:

Some apps work no matter what VPN connection status is.

"But web browser can connect to the same host with or without VPN." True. Browsers may use Happy eyeballs approach attempting to connect using both IPv4 and IPv6 at the same time.

"But my old app has not problems connecting." Also true. Some older and not so old apps use IPv4 protocol by default. Support for IPv6 or IPv4+IPv6 has to be explicitly implemented.

"But it works sometimes". This happens when VPN connections are not reliable. It leads to all sorts of solutions that are mere coincidences.

What exactly is happening:

HttpClient.GetAsync() uses default DNS resolution and can connect using both IPv4 and IPv6 addresses. It does not discriminate and there is no direct way to influence protocol selection. If DNS returns inaccessible address then HttpClient may use that invalid address to connect resulting in timeout.

Possible workarounds:

The best: ask IT to fix IPv6 DNS issues. DNS should not report inaccessible addresses.

Good: implement Happy eyeballs approach. Connect to both IPv6 and IPv4 host addresses using numeric IP instead of automatic resolution using host name.

OK: Always connect to IPv4 using numeric IP.

Here is the piece of code that shows how to connect to a specific IP address:

// Get DNS entries for the host. var hostEntry = Dns.GetHostEntry(uri.Host);   // Get IPv4 address var ip4 = hostEntry.AddressList.First(addr => addr.AddressFamily == AddressFamily.InterNetwork); // Build URI with numeric IPv4 var uriBuilderIP4 = new UriBuilder(uri);  uriBuilderIP4.Host = ip4.ToString());  var uri4 = uriBuilder4.Uri;   // Get IPv6 address var ip6 = hostEntry.AddressList.First(addr => addr.AddressFamily == AddressFamily.InterNetworkV6);  // Build URI with numeric IPv6 var uriBuilderIP6 = new UriBuilder(uri);  uriBuilderIP6.Host = $"[{ip6}]";  var uri6 = uriBuilder6.Uri;  

For HTTPS connections numeric addresses work only with "host" header with the name of the host (not an IP address) in it. Here is the way to add it.

var client = new HttpClient();  // Add "host" header with real host name e.g. stackoverflow.com  client.DefaultRequestHeaders.Add("Host", uri.Host);  
Read More

Sunday, September 10, 2017

Keychain references in Swift used in NEVPNManager

Leave a Comment

I'm trying to connect to a VPN using Swift in Xcode. I'm using KeychainSwift to keep keychain references. My code looks like this:

    private func connectVPN(completion: @escaping () -> Void) {          let keychain = KeychainSwift()         keychain.set("<mypassword>", forKey: "passref")         keychain.set("<sharedsecretpassword>", forKey: "secretref")          NEVPNManager.shared().loadFromPreferences { error in             let vpnhost = "<11.11.11.11>"             let username = "<myusername>"              let p = NEVPNProtocolIPSec()             p.username = username             p.localIdentifier = username             p.serverAddress = vpnhost             p.remoteIdentifier = vpnhost             p.authenticationMethod = .sharedSecret             p.disconnectOnSleep = false              p.sharedSecretReference = keychain.getData("secretref")             p.passwordReference = keychain.getData("passref")              var rules = [NEOnDemandRule]()             let rule = NEOnDemandRuleConnect()             rule.interfaceTypeMatch = .any             rules.append(rule)              NEVPNManager.shared().localizedDescription = "My VPN"             NEVPNManager.shared().protocolConfiguration = p             NEVPNManager.shared().onDemandRules = rules             NEVPNManager.shared().isOnDemandEnabled = true             NEVPNManager.shared().isEnabled = true             NEVPNManager.shared().saveToPreferences { error in                 if (error != nil) {                     print(error!)                 } else {                     do {                         try NEVPNManager.shared().connection.startVPNTunnel()                         completion()                     } catch {                         print("can't connect VPN'")                     }                 }             }         }     } 

I'm using keychain.getData("secretref"), because this field needs

A persistent keychain reference to a keychain item containing the IKE shared secret.

What's more,

The persistent keychain reference must refer to a keychain item of class kSecClassGenericPassword.

I'm not really sure, if I'm doing it right. I didn't subclass kSecClassGenericPassword or use it in any way.

When I'm using this function in code, a window shows with information, that there is no shared secret for this VPN. I think it means that this keychain doesn't work as it's supposed to.

In iPhone settings, it tries to connect, moves switch towards green and instantly the switch goes back to "off" state. When I put the same data as in code manually, the connection works.

What am I doing wrong? What should I correct?

1 Answers

Answers 1

Okay, I have the answer. In the query for the SecItemCopyMatching, I had to choose kSecReturnPersistentRef with kCFBooleanTrue - not kSecReturnData.

Read More

Thursday, February 16, 2017

Android VpnService class and its builder class

Leave a Comment

I have two questions concerning the VpnService.Builder class.

Suppose I know an App communicate with its server www.somedomain.com, I know the server's IP address is 210.32.204.165.

  1. About the addAddress(String address, int prefixLength) function. In documentation, it says this function "add a network address to the VPN interface", what does that mean? Does it mean the traffic goes to that address (server IP address of an App) will go through VPN or traffic from this address (device IP?) will go through VPN?

  2. There is a function addAllowedApplication(String packageName) which specify which app's traffic go through the VPN tunnel. Is there a way or a function to force all traffic send to server www.somedomain.com go through VPN tunnel?

1 Answers

Answers 1

  1. It mean the traffic goes to that address (server IP address of an App) will go through VPN

  2. link: if this method is never called then all applications are allowed by default. If some applications are added, other, un-added applications will use networking as if the VPN wasn't running. So you can to never call this method and all applications will use VPN

Read More

Tuesday, April 5, 2016

Microsoft VPN from Linux Virtualbox

Leave a Comment

For work I need to connect to a VPN. Connecting works fine from both native Windows and native Linux. In native Linux I add it under networking and it simply works. Unfortunately my company policy obliges me to use Linux in a Virtual Machine, and I cannot get it to work there.

I have tried both NAT and Bridged networking, as well as other bridging the connection in windows.

Has anyone managed this successfully? Is there a way to see if it is the company firewall that is blocking the connection from the virtual-box OS ?

edit: Syslog output below: 58:18 x NetworkManager[2835]: <info> Starting VPN service 'pptp'... 58:18 x NetworkManager[2835]: <info> VPN service 'pptp' started (org.freedesktop.NetworkManager.pptp), PID 10117 58:18 x NetworkManager[2835]: <info> VPN service 'pptp' appeared; activating connections 58:18 x NetworkManager[2835]: <info> VPN plugin state changed: init (1) 58:18 x NetworkManager[2835]: <info> VPN connection 'PGC' (ConnectInteractive) reply received. 58:18 x NetworkManager[2835]: <info> VPN plugin state changed: starting (3) 58:18 x NetworkManager[2835]: <info> VPN connection 'PGC' (Connect) reply received. 58:18 x pppd[10120]: Plugin /usr/lib/pppd/2.4.6/nm-pptp-pppd-plugin.so loaded. 58:19 x pppd[10120]: pppd 2.4.6 started by root, uid 0 58:19 x kernel: [21373.483043] PPP generic driver version 2.4.2 58:19 x pptp[10129]: nm-pptp-service-10117 log[main:pptp.c:314]: The synchronous pptp option is NOT activated 58:19 x pppd[10120]: Using interface ppp0 58:19 x pppd[10120]: Connect: ppp0 <--> /dev/pts/3 58:20 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_rep:pptp_ctrl.c:251]: Sent control packet type is 1 'Start-Control-Connection-Request' 58:20 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_disp:pptp_ctrl.c:739]: Received Start Control Connection Reply 58:20 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_disp:pptp_ctrl.c:773]: Client connection established. 58:20 x NetworkManager[2835]: <info> (ppp0): new Generic device (driver: 'unknown' ifindex: 3) 58:20 x NetworkManager[2835]: <info> (ppp0): exported as /org/freedesktop/NetworkManager/Devices/2 58:20 x NetworkManager[2835]: <info> devices added (path: /sys/devices/virtual/net/ppp0, iface: ppp0) 58:20 x NetworkManager[2835]: <info> device added (path: /sys/devices/virtual/net/ppp0, iface: ppp0): no ifupdown configuration found. 58:21 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_rep:pptp_ctrl.c:251]: Sent control packet type is 7 'Outgoing-Call-Request' 58:21 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_disp:pptp_ctrl.c:858]: Received Outgoing Call Reply. 58:21 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_disp:pptp_ctrl.c:897]: Outgoing call established (call ID 0, peer's call ID 41). 58:27 x pptp[10136]: nm-pptp-service-10117 log[pptp_read_some:pptp_ctrl.c:544]: read returned zero, peer has closed 58:27 x pptp[10136]: nm-pptp-service-10117 log[callmgr_main:pptp_callmgr.c:258]: Closing connection (shutdown) 58:27 x pptp[10136]: nm-pptp-service-10117 log[ctrlp_rep:pptp_ctrl.c:251]: Sent control packet type is 12 'Call-Clear-Request' 58:27 x pptp[10136]: nm-pptp-service-10117 log[pptp_read_some:pptp_ctrl.c:544]: read returned zero, peer has closed 58:27 x pptp[10136]: nm-pptp-service-10117 log[call_callback:pptp_callmgr.c:79]: Closing connection (call state) 58:27 x pppd[10120]: Modem hangup 58:27 x pppd[10120]: Connection terminated. 58:27 x pppd[10120]: Exit. 58:27 x avahi-daemon[2046]: Withdrawing workstation service for ppp0. 58:27 x NetworkManager[2835]: <info> devices removed (path: /sys/devices/virtual/net/ppp0, iface: ppp0) 58:27 x NetworkManager[2835]: <warn> VPN plugin failed: connect-failed (1) 58:27 x NetworkManager[2835]: <warn> VPN plugin failed: connect-failed (1) 

0 Answers

Read More

Sunday, March 20, 2016

How to put Open VPN process in an SSIS task control flow

Leave a Comment

I'm learning how to use SSIS to create auto scheduled tasks. Everything went smoothly until the security policy has changed. Before whenever I wanted to connect to database, I just type in IP address, username and password in SSIS connection manager. Now I need to open up OpenVPN first which is still a manual process. So I really would like to put this bit into an SSIS task flow.

I had a search on Stackoverflow and over some other websites. Here is a link in stackoverflow. This example gives the idea that we can use VBA to setup the link. However that example shows we need a VPN connection name and VPN username which I don't really have and don't know where to find. Everytime I just right click on a .ovpn file and choose start openvpn on this config file. Also when I open that .ovpn file there is no command indicating which is my connection name or user name. Here is what it looks like when I open it up (there is no -auth-user-pass thing in this file and I can use this file without having to specify it):

client dev tun proto udp remote a website here 1111 resolv-retry infinite nobind persist-key persist-tun comp-lzo verb 3  ca ca.crt cert office.crt key office.key askpass login.txt 

Is it possible to find my connection name and user name? If I don't need connection name and user name, can I still write up VBA to build up an auto connection process?

I also notice there is an execute process task in SSIS. Is it possible that I can make use of it (because in my understanding obviously I use openvpn.exe to execute a .ovpn file)?

Please correct me if I'm saying something wrong since I'm new to both VPN and SSIS. And much appreciate for whoever can help me with this.

1 Answers

Answers 1

I don't know anything about OpenVPN, but I ran into a similar problem a while back with a server that insisted on an SSH tunnel using PuTTY as the only allowed connection method.

People who put these policies in place never think of the impact on SSIS, especially for unattended processing.

The solution I found was:

  1. Find the command-line equivalent to the PuTTy client (which is called plink)
  2. Have the detailed documentation to hand, because this is really difficult.
  3. Have the SSIS package start plink.exe, passing various parameters (Note: don't use the Execute Process task, as that waits for the process to exit: launch the process in a Script task, and get the handle of plink.exe's ProcessID).
  4. After a wait, have another Script task in SSIS connect to the plink process' InputBuffer, "typing in" a password. Yes, insecure and clunky, though you can store SSIS project parameters encrypted (this obviously requires SSIS 2012 or later)
    1. The SSIS package can now connect to the server and do data stuff, as if it was a normal server without stupid access requirements.
    2. At the end of the package, kill the plink.exe process.

Bear in mind, also, that on unattended execution (e.g. when scheduled in a job), your package will be running in a different security context from the one you work in to develop the package (depending on the SSIS service settings). If your access to OpenVPN has any dependency on ActiveDirectory, you may have to set up another OpenVPN account for the SSIS service's Logon user.

If this sounds horrible, nightmarishly complicated and unstable, it's because it is. Really, the TL;DR version is: tell whoever administers the server that if they really want you to access it using SSIS, they should put some more sensible access policy in place.

Read More