Advertisement
Dojnaz

Not a port scanner

Jan 2nd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. //I AM NOT RESPONSIBLE FOR ANYTHING YOU DO WITH THIS CODE!
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Net.NetworkInformation;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Runtime.InteropServices;
  11. using System.Threading;
  12. using System.IO;
  13. using System.Windows.Forms;
  14.  
  15. namespace EffectivePortScan
  16. {
  17.     class Program
  18.     {
  19.         public static List<int> port = new List<int>();
  20.         public static string ip = "";
  21.         public static int portRangeMin = 0;
  22.         public static int portRangeMax = 0;
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             Console.Write("IP: ");
  27.             ip = Console.ReadLine();
  28.             Console.Write("Start from port: ");
  29.             portRangeMin = (Convert.ToInt32(Console.ReadLine()) - 1);
  30.             Console.Write("Continue until port: ");
  31.             portRangeMax = Convert.ToInt32(Console.ReadLine());
  32.             Console.Clear();
  33.  
  34.             for (int i = 0; i < portRangeMax - portRangeMin; i++)
  35.             {
  36.                 port.Add(0);
  37.             }
  38.            
  39.             for (int i = 0; i < portRangeMax - portRangeMin; i++)
  40.             {
  41.                 Thread.Sleep(250);
  42.                 Task.Run(() => scan(ip + portRangeMin, i));
  43.             }
  44.  
  45.             Console.WriteLine("Finished with scan, writing to file...");
  46.  
  47.             Thread.Sleep(2500);
  48.             TextWriter outputStream = new StreamWriter("output.txt", true);
  49.  
  50.             for (int i = 0; i < port.Count; i++)
  51.             {
  52.                 if (port[i] == 0)
  53.                     outputStream.WriteLine((portRangeMin + i) + " : is closed");
  54.                 else
  55.                     outputStream.WriteLine((portRangeMin + i) + " : is open");
  56.             }
  57.             outputStream.Close();
  58.  
  59.             Console.WriteLine("I'm done!");
  60.             Console.ReadLine();
  61.         }
  62.  
  63.         async static Task scan(string ip, int p)
  64.         {
  65.             try
  66.             {
  67.                 TcpClient ports = new TcpClient();
  68.                 ports.SendTimeout = 100;
  69.                 ports.SendBufferSize = 100;
  70.                 ports.Connect(ip, Convert.ToInt32(p));
  71.                 Console.WriteLine(p + ": Open");
  72.                 port[p] = 1;
  73.             }
  74.             catch
  75.             {
  76.                 Console.WriteLine(p + ": Closed");
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement