Advertisement
Cinder1986

KSIS2

Mar 17th, 2023
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.NetworkInformation;
  3. using System.Net.Sockets;
  4.  
  5. internal class Lab2
  6. {
  7.     static byte[] IPv4ToBytes(string address)
  8.     {
  9.         byte[] bytes = new byte[4];
  10.         string temp = "";
  11.         int i = 0;
  12.         int j = 0;
  13.         while(i < address.Length)
  14.         {
  15.             if (address[i] == '.')
  16.             {
  17.                 bytes[j++] = Convert.ToByte(temp);
  18.                 temp = "";
  19.             }
  20.             else
  21.                 temp += address[i];
  22.             i++;
  23.         }
  24.         bytes[j] = Convert.ToByte(temp);
  25.         return bytes;
  26.     }
  27.  
  28.     static string bytesToIPv4(byte[] bytes)
  29.     {
  30.         string ip = "";
  31.         for (int i = 0; i < 3; i++)
  32.         {
  33.             ip += bytes[i].ToString();
  34.             ip += '.';
  35.         }
  36.         ip += bytes[3].ToString();
  37.         return ip;
  38.     }
  39.  
  40.     static string[] calcIPDiapason(string ip, string mask)
  41.     {
  42.         string[] diapason = new string[2];
  43.         byte[] ipBytes = IPv4ToBytes(ip);
  44.         byte[] maskBytes = IPv4ToBytes(mask);
  45.         for(int i = 0;i < 4;i++)
  46.             ipBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
  47.         diapason[0] = bytesToIPv4(ipBytes);
  48.         for(int i = 0;i < 4;i++)
  49.             if (maskBytes[i] != 255)
  50.                 ipBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]);
  51.         diapason[1] = bytesToIPv4(ipBytes);
  52.         return diapason;
  53.     }
  54.    
  55.     static bool isInDiapason(IPAddress address, string[] diapason)
  56.     {
  57.         if (address.ToString().CompareTo(diapason[0]) > 0 && address.ToString().CompareTo(diapason[1]) < 0)
  58.             return true;
  59.         else
  60.             return false;
  61.     }
  62.  
  63.     static string[] getDiapasonFromInput()
  64.     {
  65.         Console.Write("IP: ");
  66.         string inputIP = Console.ReadLine();
  67.         Console.Write("Mask: ");
  68.         string inputMask = Console.ReadLine();
  69.         string[] diapason = null;
  70.         try
  71.         {
  72.             diapason = calcIPDiapason(inputIP, inputMask);
  73.         }
  74.         catch (Exception)
  75.         {
  76.             diapason = null;
  77.         }
  78.         return diapason;
  79.     }
  80.  
  81.     static string getFirstMac()
  82.     {
  83.         string mac = "";
  84.         foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
  85.         {
  86.             if (nic.OperationalStatus == OperationalStatus.Up)
  87.             {
  88.                 mac = nic.GetPhysicalAddress().ToString();
  89.                 break;
  90.             }
  91.         }
  92.         return mac;
  93.     }
  94.  
  95.     static bool checkConfig()
  96.     {
  97.         const string OS = "Microsoft Windows NT 10.0.22621.0";
  98.         const string mac = "7A791948F62B";
  99.         const string cpuName = "Intel64 Family 6 Model 142 Stepping 12, GenuineIntel";
  100.         string currOS = System.Environment.OSVersion.VersionString;
  101.         string currMac = getFirstMac();
  102.         string currCPU = System.Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").ToString();
  103.         return currOS.Equals(OS) && currMac.Equals(mac) && currCPU.Equals(cpuName);
  104.     }
  105.  
  106.     static void scanLAN(string[] diapason)
  107.     {
  108.         Console.WriteLine($"From {diapason[0]} to {diapason[1]}\n=================================");
  109.         string macAddress = "";
  110.         string ipAddress = "";
  111.         string itemName = "";
  112.         foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
  113.         {
  114.             foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
  115.             {
  116.                 if (ip.Address.AddressFamily == AddressFamily.InterNetwork && isInDiapason(ip.Address, diapason))
  117.                 {
  118.                     IPInterfaceProperties props = item.GetIPProperties();
  119.                     ipAddress = ip.Address.ToString();
  120.                     macAddress = item.GetPhysicalAddress().ToString();
  121.                     if (macAddress.Length == 0)
  122.                         macAddress = "Не распознан";
  123.                     itemName = item.Name;
  124.                     Console.WriteLine("MAC: " + macAddress + "\nIP: " + ipAddress + '\n' + itemName + "\nDNS:");
  125.                     for (int i = 0; i < props.DnsAddresses.Count; i++)
  126.                     {
  127.                         Console.WriteLine(props.DnsAddresses.ElementAt(i).ToString());
  128.                     }
  129.                     Console.WriteLine("Interface type: " + item.NetworkInterfaceType.ToString());
  130.                     Console.WriteLine($"Mask: {ip.IPv4Mask}");
  131.                     Console.WriteLine("========================");
  132.                 }
  133.             }
  134.         }
  135.     }
  136.  
  137.     private static void Main(string[] args)
  138.     {
  139.         if (checkConfig())
  140.         {
  141.             string[] diapason = getDiapasonFromInput();
  142.             if (diapason != null)
  143.                 scanLAN(diapason);
  144.             else
  145.                 Console.WriteLine("An error occured while trying to convert the ip and the mask!");
  146.         }
  147.         else
  148.             Console.WriteLine("Access denied!");
  149.  
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement