ASP.NET中端口扫描
佚名 http://www.msproject.cn/

简介

这是一个用于扫描端口的ASP.NET程序,相信一定很少见吧(请不要用于恶意用途)。在这里的ASP.NET页面中输入IP地址或域名,程序就可以对对方主机进行扫描,发现已经打开的端口。

 

 

 

 

 

 

 

 

 

使用

输入主机IP或域名,输入开始结束端口。

原理

主要使用了ASP.NET 2.0的System.Net.Sockets组件。

StartPort = Convert.ToInt32(numStart.Text);
EndPort = Convert.ToInt32(numEnd.Text);
ipAdres = txtIP.Text;
Thread[] pool = new Thread[(EndPort - StartPort) + 1];
int i = 0;

DateTime start = DateTime.Now;
// Loop through the ports between start port and end port

for (int CurrPort = StartPort; CurrPort <= EndPort; CurrPort++)
{
    Thread th = 
         new Thread(new System.Threading.ParameterizedThreadStart(portAc));
    //NOTE: better to leave to system.

    // th.Priority = ThreadPriority.AboveNormal;

    th.Start(CurrPort);
    pool[i] = th;
    i++;
}
#region thread pool
int k = --i;
int retryCount = 0;
for (; i >= 0; i--)
{
    if (pool[i].IsAlive)
    {
        i = k;
        retryCount++;
        continue;
    }
    if (retryCount == 1000)
    {
        break;
    }
}
#endregion

#region httpfinger
if (http)
{
    // Create a request for the URL.         

    WebRequest request = WebRequest.Create("http://" + txtIP.Text);
    // If required by the server, set the credentials.

    request.Credentials = CredentialCache.DefaultCredentials;
    // Get the response.

    try{
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        string serverType = response.Headers["server"];
        if (serverType.Contains("IIS"))
        {
            lblServer.Text = "Windows System ";
            if (serverType.Contains("5."))
            {
                lblServer.Text += "XP/2000";
            }
            if (serverType.Contains("6."))
            {
                lblServer.Text += "2003";
            }
        }
        if (serverType.ToLower().Contains("apache"))
        {
            lblServer.Text += "probably linux";
        }
        lblServer.Text += "
" + serverType;
    }
    catch(Exception Err){
        //sometime which returns 404 and it makes a problem.

    }
}
#endregion

DateTime end = DateTime.Now;
TimeSpan sonuc = end - start;
lblzaman.Text = sonuc.TotalSeconds + " total secs";

上面是主要的代码,当然我们需要使用线程了,否则,这么多端口的扫描是非常浪费时间的。

public void portAc(object portNoObj)
{
    int portNo = (int)portNoObj;
    TcpClient TcpScan = new TcpClient();
    try
    {
        // Try to connect

        TcpScan.Connect(ipAdres, portNo);

        if (!TcpScan.Connected) return;
        // If there's no exception, we can say the port is open

        log += "Port " + portNo + " open\r\n";

        //NOTE: We may include more finger tips to here

        switch (portNo)
        {
            case 80: http = true; break;
        }

        try
        {
            DataRow dr = dt.NewRow();
            dr[0] = "http://www.portsdb.org/bin/portsdb.cgi?portnumber=" + 
                     portNo + "&protocol=ANY&String=";
            dt.Rows.Add(dr);
        } // Ends Try

        catch (Exception Err)
        {
            throw Err;
        }
    }
    catch
    {
        // An exception occured, thus the port is probably closed

    }
}


TcpClient类提供了网络连接、发送和接收的功能,不过这里我们只是使用了它的连接方法。

声明

请只用于学习,不可用于任何破坏等恶意行为,否则责任自负

CIO之家 www.ciozj.com 公众号:imciow
关联的文档
也许您喜欢