■ 포트 스캔하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Diagnostics;

using System.IO;

using System.Net;

using System.Net.Sockets;

using System.Threading;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 파일 경로

        /// </summary>

        private string filePath = null;

 

        /// <summary>

        /// 스캔 IP 주소

        /// </summary>

        private IPAddress scanIPAddress = null;

 

        /// <summary>

        /// 스레드

        /// </summary>

        private Thread thread = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 파일 경로 버튼 클릭시 처리하기 - filePathButton_Click(sender, e)

 

        /// <summary>

        /// 파일 경로 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void filePathButton_Click(object sender, EventArgs e)

        {

            if(this.folderBrowserDialog.ShowDialog() == DialogResult.OK)

            {

                string fileName = string.Format("포트스캔({0}).txt", this.scanIPTextBox.Text);

 

                this.filePath = Path.Combine(this.folderBrowserDialog.SelectedPath, fileName);

            }

        }

 

        #endregion

        #region 스캔 버튼 클릭시 처리하기 - scanButton_Click(sender, e)

 

        /// <summary>

        /// 스캔 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void scanButton_Click(object sender, EventArgs e)

        {

            if(this.filePath != null)

            {

                this.progressBar.Minimum = Convert.ToInt32(this.startPortTextBox.Text);

                this.progressBar.Maximum = Convert.ToInt32(this.endPortTextBox.Text  );

 

                this.scanButton.Enabled = false;

 

                this.filePathButton.Enabled = false;

 

                this.thread = new Thread(ScanPort);

 

                this.thread.IsBackground = true;

 

                this.thread.Start();

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 포트 스캔 하기 - ScanPort();

 

        /// <summary>

        /// 포트 스캔 하기

        /// </summary>

        private void ScanPort()

        {

            int currentPort;

            int startPort;

            int endPort;

 

            this.filePathLabel.Text = "생성 파일 : " + this.filePath;

 

            StreamWriter writer = new StreamWriter(this.filePath);

 

            this.scanIPAddress = IPAddress.Parse(this.scanIPTextBox.Text);

 

            startPort = Convert.ToInt32(this.startPortTextBox.Text);

            endPort   = Convert.ToInt32(this.endPortTextBox.Text  );

 

            writer.WriteLine("************ 스캔 시작 ************ " + DateTime.Now);

            writer.WriteLine();

 

            for(currentPort = startPort; currentPort <= endPort; currentPort++)

            {

                this.progressBar.Value = currentPort;

 

                try

                {

                    IPEndPoint ipEndPoint = new IPEndPoint(scanIPAddress, currentPort);

 

                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

 

                    socket.Connect(ipEndPoint);

 

                    writer.WriteLine("스캔 포트 {0} OPEN", currentPort);

 

                    this.listView.Items.Add(new ListViewItem(new string[] { currentPort.ToString(), "OPEN" }));

 

                    continue;

                }

                catch(SocketException socketException)

                {

                    if(socketException.ErrorCode != 10061)

                    {

                        writer.WriteLine("에러 : {0}", socketException.Message);

                    }

                }

 

                writer.WriteLine("스캔 포트 {0} CLOSED", currentPort);

 

                this.listView.Items.Add(new ListViewItem(new string[] { currentPort.ToString(), "CLOSED" }));

            }

 

            writer.WriteLine();

            writer.WriteLine("************ 스캔 종료 ************ " + DateTime.Now);

 

            writer.Close();

 

            this.scanButton.Enabled = true;

 

            this.filePathButton.Enabled = true;

 

            MessageBox.Show("포트 스캔을 완료하였습니다.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);

 

            Process process = new Process();

 

            process.StartInfo.FileName = filePath;

 

            process.Start();

 

            thread.Abort();

         }

 

         #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요