2

I'm in search of a solution to stop devices that are connected to the same WIFI network programmatically.

Usecase:

Hypothetically assume that an intruder already has access to my local WIFI network and injects malicious programs/attacks over to the connected devices in the Same WIFI network. At the same time, assume you cannot change the local WIFI password.

What I'm looking for is a .net core-based solution to find unfamiliar devices which are connected to a WIFI network by providing the username and password of that WIFI network (which I already know) and disabling the unfamiliar devices in that WIFI network.

So far as the working sample I have is the suggestion posted by @Melvyn here as a partial solution that does not contain login into the local WIFI network

SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
    if (((string)item["NetConnectionId"]) == "Local Network Connection")
    {
       item.InvokeMethod("Disable", null);
    }
}

As an alternative approach Im okay to try out the PowerShell mechanism as well which can be invoke via a .net core application

Appreciate it if anyone can guide me to a workable solution.

Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42

1 Answers1

1

I'm not sure if this is what you want, but as Hans Passant mentioned, if you want to do this via PowerShell, And, you can do something like this. And, this is just to get a rough idea. This PowerShell script is not working properly, if anyone knows the correct one please put a comment. Please be noted, This just a suggestion.

static void Main(string[] args) {
  
  string macAddress = "2A:DB:DD:9B:5A:CA";
  //string powershellScript = $"Disconnect-WirelessNetwork -MACAddress '{macAddress}'";
  string powershellScript = $ "netsh wlan disconnect interface=\"Wi-Fi\" bssid={macAddress}";

  ExecutePowershellScript(powershellScript);

  Console.ReadLine();
}

static void ExecutePowershellScript(string script) {
  try {
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "powershell";
    psi.Arguments = $ "-Command \"{script}\"";
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.UseShellExecute = false;

    using(Process process = new Process()) {
      process.StartInfo = psi;
      process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
      process.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);

      process.Start();
      process.BeginOutputReadLine();
      process.BeginErrorReadLine();
      process.WaitForExit();
    }
  } catch (Exception ex) {

    throw;
  }
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68