C#,多线程客户端IP端口扫描

来源:互联网 发布:小米手机 淘宝手机助手 编辑:程序博客网 时间:2024/05/17 22:08

扫描主要分s扫描,和tcp扫描,S扫描需要分析数据包,需要winpacp(c#为SharpPcap),由于这种扫描虽然快但麻烦,本人也只抓到过arp与pppoe拨号的数据包,所以没对此深入。

 

以下介绍的也只是tcp多线程,单网段扫描了(由于时间久了也懒得改了)

 

程序界面如下:

程序通过启动另外一个线程来扫描,其中判断ip另端主机是否存在,是通过ping(20ms超时)判断,端口TCP连接(200ms超时)

 

每次只扫描一个IP,开多线程扫N个端口,由于开100个线程以上系统会很卡(公司垃圾电脑。。),自己也懒得控制每次线程数量了。

 

代码如下

复制代码
using System;using System.Collections.Generic;using System.Net;using System.Net.Sockets;using System.Text.RegularExpressions;using System.Threading;using System.Windows.Forms;using System.Net.NetworkInformation;namespace WindowsFormsApplication1{    public partial class ip端口扫描 : Form    {        public ip端口扫描()        {            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;            InitializeComponent();            textBox3.Text = "10.3.1.46";            textBox4.Text = "10.3.1.53";        }        List<string> str;//结果字串        int start;        int end;        bool tag = true;//是否终止当前扫描的变量        private void button1_Click(object sender, EventArgs e)        {            this.richTextBox1.Text = "开始\n";            Regex rgx = new Regex(@"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");            if (rgx.IsMatch(textBox3.Text) && rgx.IsMatch(textBox4.Text))//匹配正确IP            {                start = Int32.Parse(textBox1.Text);                end = Int32.Parse(textBox2.Text);            }            else            {                MessageBox.Show("Jerry,填写正确IP");                return;            }            if (end - start > 100)            {                MessageBox.Show("暂不接受搜索范围超过100的端口");                return;            }            if (end < start)            {                MessageBox.Show("Jerry,别开玩笑啊!");                return;            }            tag = true;            Thread waitT = new Thread(new ThreadStart(wait));            waitT.Start();//等待所有线程执行完毕在写入textbox中        }        public void wait()        {            int startIp = Int32.Parse(textBox3.Text.Split('.')[3]);            int endIp = Int32.Parse(textBox4.Text.Split('.')[3]);            string ip = textBox3.Text.Split('.')[0] + "." + textBox3.Text.Split('.')[1] + "." + textBox3.Text.Split('.')[2] + ".";            for (int q = startIp; q <= endIp && tag == true; q++)            {                //---------------------ping                Ping ping = new Ping();                PingReply reply = ping.Send(IPAddress.Parse(ip + q), 20);                if (reply.Status == IPStatus.Success)                {                    richTextBox1.Text += ip + q + "Ping时间" + reply.RoundtripTime + "毫秒\n";                    IPHostEntry host = Dns.GetHostEntry(ip + q);                    richTextBox1.Text += "主机名为" + host.HostName + "\n";                }                else                {                    richTextBox1.Text += ip + q + "不可达\n";                    continue;                }                //---------------------end                Thread[] tharr = new Thread[end - start + 1];                str = new List<string>();                for (int i = start; i <= end; i++)                {                    Thread thread = new Thread(new ParameterizedThreadStart(Scan));                    thread.Start(new IPEndPoint(IPAddress.Parse(ip + q), i));//每扫描一个端口创建一个线程                    tharr[i - start] = thread;                }                bool iscon = true;//第一个线程等待时间                for (int i = 0; i < tharr.Length; i++)                {                    if (tharr[i] == null)                        continue;                    while (tharr[i].IsAlive && iscon)//端口超时设置时间(目前200毫秒),一直等待此ip所有线程执行完毕才扫描下个ip                    {                        Thread.Sleep(200);                        iscon = false;//第一个线程给200ms等待时间,其他线程由于同步执行的,所以没等待时间了,如果线程还没执行完,说明此端口不可达。                    }                }                str.Sort();                richTextBox1.Text += "开放端口: ";                for (int k = 0; k < str.Count; k++)                    richTextBox1.Text += str[k];                richTextBox1.Text += "\n";            }            if (tag == true)                MessageBox.Show("扫描完成");            else            {                MessageBox.Show("扫描终止");            }        }        public void Scan(object Point)        {            IPEndPoint IPPoint = (IPEndPoint)Point;            try            {                TcpClient tcp = new TcpClient();                tcp.Connect(IPPoint);                if (tcp.Connected)                    str.Add(IPPoint.Port + ",");            }            catch            {                ;            }        }        private void button2_Click(object sender, EventArgs e)        {            MessageBox.Show("目前只支持C类网段的TCP扫描\n作者Jerry", "事项");        }        private void button3_Click(object sender, EventArgs e)//终止        {            tag = false;        }    }}
复制代码

 

运行的结果图如下:

 

由于这个project还有其他功能代码,程序也确实不咋滴,很多都可以做的更好,而且主体代码全贴出来了,所以就不提供源代码下载了。

0 0
原创粉丝点击