0

I have to basically create a anti-hack system which detects the user's IP address using Unity API. Is there any easy way to deal with it?

Also I need to check whether the player is using a VPN connection or not. All these has to be done in Unity and C#. Help will be appreciated. Thank you

dragon003
  • 1
  • 3
  • Okay thanks for the help, I came across many documentation but its very messy. – dragon003 Sep 22 '21 at 05:21
  • For the IP, the answer can be found [`here`](https://stackoverflow.com/questions/21155352/get-ip-address-of-client-machine). The accepted answer is the server IP, not the local machine IP so read through all of them. – TEEBQNE Sep 22 '21 at 05:21
  • Also just for clarification - I deleted my original comment as I linked the wrong post. – TEEBQNE Sep 22 '21 at 05:34
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 29 '21 at 05:21

2 Answers2

1

You can use the below example to expand on it, and adjust it. Not all VPN services may name their interface with "vpn" in it but most will. You might create a Hyper-V or virtual machine (or rent a VPS server) and install some of the VPN apps to see what they name their interfaces.

To get the Public IP, there are some free web APIs that you can call from unity using WWWForm or UnityWebRequest and they return you the public IP in JSON. An example would be at https://www.ipify.org/ or https://www.bigdatacloud.com/client-info-apis/public-ip-address-api

public IEnumerator ScanNetworkInterfaces()
{
    while(Application.isPlaying && SM.CurrentState == GameManagerState.Intro)
    {
        bool foundVPN = false;

        yield return new WaitForSeconds(2);
        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (ni.OperationalStatus == OperationalStatus.Up)
            {
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                    {
                        if (ip != null && ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            // My addresses
                            if (ni.Description.ToLower().Contains("vpn"))
                            {
                                foundVPN = true;
                            }
                        }

                        yield return null;
                    }
                }
            }

            yield return null;
        }

        if (foundVPN)
            MainMenuPanel.Instance.ShowWarning("Possibly detected a VPN - you may need to disable it or game will disconnect immediately!");
        else
            MainMenuPanel.Instance.HideWarning();
    }
}
Michael Urvan
  • 492
  • 3
  • 8
0

Send a unique request from your application to your server and see from what IP you see the request on the server.

For the second issue: you can only compare the IP to a list of known proxies.

Buda Florin
  • 624
  • 2
  • 12
  • 30