欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TCP端口扫描

程序员文章站 2022-05-15 22:09:58
...
~~重点代码
//扫描端口
private void NormalScan(Int32 state)
{
    Int32 port = state;
    String msg = "";
    TcpClient tcp = new TcpClient();
    try
    {
        tcp.Connect(IPAddress.Parse(tbxIP.Text), port);
        msg = port.ToString() + "端口开放";
        listBox.Items.Add(msg);
        tcp.Close();
    }
    catch
    {
        msg = port.ToString() + "端口不开放";
        listBox.Items.Add(msg);
    }
}
//开始扫描按钮事件
 private void btn_statrt_Click(object sender, EventArgs e)
 {
     String scanHost = tbxIP.Text;
     try
     {
         IPAddress ipaddr = (IPAddress)Dns.Resolve(scanHost).AddressList.GetValue(0);
         tbxIP.Text = ipaddr.ToString();
     }
     catch
     {
         tbxIP.Focus();
         MessageBox.Show("请输入正确的主机地址,该地址DNS无法进行解析", "系统提示");
         return;
     }

     Int32 threadNum = Convert.ToInt32(tbxStartPort.Text.Trim());
     Int32 end = Convert.ToInt32(tbxEndPort.Text.Trim());

     listBox.Items.Add("==扫描开始==");
     listBox.Refresh();

     for (; threadNum <= end; threadNum++)
     {
         NormalScan(threadNum);
         listBox.Refresh();
     }

     listBox.Items.Add("==扫描结束==");
     listBox.Refresh();
 }

~~全部代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace TCPScan
{
    public partial class FM_MAIN : Form
    {
        private string path = Application.StartupPath + "\\Logs";
        public FM_MAIN()
        {
            InitializeComponent();
        }

        private void tbxIP_KeyPress(object sender, KeyPressEventArgs e)
        {
            isNumber(e, sender);
        }

        private void tbxStartPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            isNumber(e, sender);
        }

        private void tbxEndPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            isNumber(e, sender);
        }
        private void btn_statrt_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxIP.Text.Trim()))
            {
                MessageBox.Show("请输入IP地址", "系统提示");
                return;
            }
            if (string.IsNullOrEmpty(tbxStartPort.Text.Trim()))
            {
                MessageBox.Show("请输入起始端口号, 系统提示");
                return;
            }
            if (string.IsNullOrEmpty(tbxEndPort.Text.Trim()))
            {
                MessageBox.Show("请输入结束端口号", "系统提示");
                return;
            }

            String scanHost = tbxIP.Text;
            try
            {
                IPAddress ipaddr = (IPAddress)Dns.Resolve(scanHost).AddressList.GetValue(0);
                tbxIP.Text = ipaddr.ToString();
            }
            catch
            {
                tbxIP.Focus();
                MessageBox.Show("请输入正确的主机地址,该地址DNS无法进行解析", "系统提示");
                return;
            }

            Int32 threadNum = Convert.ToInt32(tbxStartPort.Text.Trim());
            Int32 end = Convert.ToInt32(tbxEndPort.Text.Trim());

            listBox.Items.Add("==扫描开始==");
            listBox.Refresh();

            for (; threadNum <= end; threadNum++)
            {
                NormalScan(threadNum);
                listBox.Refresh();
            }

            listBox.Items.Add("==扫描结束==");
            listBox.Refresh();
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (listBox.Items.Count == 0)
            {
                MessageBox.Show("没有记录,不能保存!", "系统提示");
                return;
            }
            DateTime time = DateTime.Now;
            string result = "";
            result += "================================================\r\n";
            result += "IP:" + tbxIP.Text + "\r\n";
            result += "起始端口:" + tbxStartPort.Text + "\r\n";
            result += "结束端口:" + tbxEndPort.Text + "\r\n";
            result += "保存时间:" + time.ToString("yyyy-MM-dd hh:mm:ss") + "\r\n";
            result += "================================================\r\n";
            foreach (var item in listBox.Items)
            {
                result += item + "\r\n";
            }
            if (false == System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            string filePath = path + "\\" + tbxIP.Text.Trim() + "_" + tbxStartPort.Text.Trim() + "-" + tbxEndPort.Text.Trim() + "_" + time.ToString("yyyyMMdd hhmmss") + ".txt";
            StreamWriter sw = File.CreateText(filePath); //保存到指定路径
            sw.Write(result);
            sw.Close();
            MessageBox.Show("保存成功!\r\n路径:" + filePath, "系统提示");
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(path);
        }

        private void FM_MAIN_Load(object sender, EventArgs e)
        {
            tbxIP.Text = GetLocalIP();
            tbxStartPort.Text = "75"; ;
            tbxEndPort.Text = "90";
        }

        #region Function
        public string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取本机IP出错:" + ex.Message, "系统提示");
                return "";
            }
        }

        private void isNumber(KeyPressEventArgs e, object sender)
        {
            /*只能数字键、退格键、负号、小数点*/
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 &&
               (int)e.KeyChar != 45 && (int)e.KeyChar != 46) e.Handled = true;
        }

        //扫描端口
        private void NormalScan(Int32 state)
        {
            Int32 port = state;
            String msg = "";
            TcpClient tcp = new TcpClient();
            try
            {
                tcp.Connect(IPAddress.Parse(tbxIP.Text), port);
                msg = port.ToString() + "端口开放";
                listBox.Items.Add(msg);
                tcp.Close();
            }
            catch
            {
                msg = port.ToString() + "端口不开放";
                listBox.Items.Add(msg);
            }
        }
        #endregion
    }
}

运行截图

TCP端口扫描

欢迎访问作者个人技术博客:BlackvonCode(www.blackvon.top)
作者的微信公众号和小程序
TCP端口扫描

相关标签: 网络攻防 tcpip