频道栏目
首页 > 安全 > 工具软件 > 正文
C# 单ip端口扫描工具 (多线程)
2014-03-31 12:04:53           
收藏   我要投稿
using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  using System.Diagnostics;
 
  namespace 端口扫描
  {
      public partial class Form1 : Form
      {
          ThreadStart threadstart;
          Thread[] thread;
          int port;         //当前正在扫描的端口
          int portmax;      //扫描端口最大值
          
          public Form1()
          {
              InitializeComponent();
          }
 
          public bool IsPortOpen(string ip, int port)       //判断某一计算机端口是否处于打开状态
          {
              try
              {
                  TcpClient client = new TcpClient();       //创建一个TcpClient实例
                  IPAddress address = IPAddress.Parse(ip);  //转化string类型的ip地址到IpAddress
                  client.Connect(address, port);            //连接服务器address的port端口
                  client.Close();                           //连接成功立即断开
                  return true;
              }
 
              catch (Exception e)
              {
                  return false;
              }
          }
 
          delegate void Show_Change(string input);
          public void ShowProc(string input)
          {
              this.lab_show.Text = input;
          }
 
          public void ScanThread()
          {
              while (true)
              {
                  int nowport;
                  lock (this)
                  {
                      if (port > portmax)
                          return;
                      else
                      {
                          //lab_show.Text = port.ToString();
                          this.BeginInvoke(new Show_Change(ShowProc), new object[] { port.ToString() });
                          nowport = port;
                          port++;
                      }
                  }
                  if(IsPortOpen(txt_ip.Text,nowport))
                      listBox_Return.Items.Add(nowport.ToString() + "端口............开放");
              }
          }
 
          private void btn_scan_Click(object sender, EventArgs e)
          {
              listBox_Return.Items.Clear();
              int ThreadCount = (int)numericUpDown_ThreadCount.Value;  //线程数量
              thread = new Thread[ThreadCount];
              threadstart = new ThreadStart(ScanThread);
              for (int i = 0; i < ThreadCount; i++)
              {
                  Thread t = new Thread(threadstart);
                  thread[i] = t;
              }
 
 
              portmax = (int)numericUpDown_Max.Value;
              port = (int)numericUpDown_Min.Value;
              for (int i = 0; i < ThreadCount; i++)
              {
                  thread[i].Start();
              }
          }
 
          private void Form1_Load(object sender, EventArgs e)
          {
              lab_show.Text = "";
          }
 
          private void Form1_FormClosed(object sender, FormClosedEventArgs e)
          {
              Application.Exit();
          }
 
          private void btn_ping_Click(object sender, EventArgs e)             //ping按钮事件
          {
              string command = @"ping "+txt_UrlOrIp.Text;
              txt_ping.Text = RunCmd(command);
          }
 
          
          public string RunCmd(string command)                                 //运行cmd命令,并返回结果字符串
          {
              Process p = new Process();
              p.StartInfo.FileName = "cmd.exe";
              p.StartInfo.Arguments = "/c" + command;
              p.StartInfo.UseShellExecute = false;
              p.StartInfo.RedirectStandardError = true;
              p.StartInfo.RedirectStandardOutput = true;
              p.StartInfo.RedirectStandardInput = true;
              p.StartInfo.CreateNoWindow = true;
              p.Start();
              return p.StandardOutput.ReadToEnd();
          }
 
 
          int[] port_array = { 21, 23, 80, 1433, 3306, 3389 };        //常用端口
          string ip;
          public void Scan()
          {
              for (int i = 0; i < port_array.Length; i++)
              {
                  if (IsPortOpen(ip, port_array[i]))
                  {
                      listBox1.Items.Add(port_array[i].ToString() + "端口.............开放");
                      listBox1.Items.Add("");
                  }
                  else
                  {
 
                      listBox1.Items.Add(port_array[i].ToString() + "端口.............关闭");
                      listBox1.Items.Add("");
                  }
              }
          }
          private void btn_Start_Click(object sender, EventArgs e)    //扫描计算机常用端口
          {
              listBox1.Items.Clear();
              ip = txt_IpAlways.Text;
              ThreadStart ts = new ThreadStart(Scan);
              Thread th = new Thread(ts);
              th.Start();
          }   
 
      }
  }
 

 

点击复制链接 与好友分享!回本站首页
上一篇:Debian安装MSF(install msf on debain)
下一篇:使用Fiddler的X5S插件查找XSS漏洞
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站