记录黑客技术中优秀的内容, 传播黑客文化,分享黑客技术精华

C# 单ip端口扫描工具 (多线程)

2014-03-31 16:05

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();
}

}
}

 

知识来源: www.2cto.com/Article/201403/289625.html

阅读:58459 | 评论:0 | 标签:扫描

想收藏或者和大家分享这篇好文章→复制链接地址

“C# 单ip端口扫描工具 (多线程)”共有0条留言

发表评论

姓名:

邮箱:

网址:

验证码:

公告

关注公众号hackdig,学习最新黑客技术

推广

工具

标签云