原代码: /Files/sherlockhua/scan.rar
实现效果图:(windows窗体版)
实现效果图:(控制台版)
端口扫描器大家都不陌生吧,用着别人的端口扫描器,虽说方便,但是如果能够做一个,当然是一个赏心悦目的事。其实,实现自己的端口扫描器并不困难。而且原理也比较简单,只要我们具备基础的网络知识,明白tcp三次握手原则即可。
C#提供一组网络编程的类,使网络编程更加简单优美。其中,Socket封装了大多数用于网络通讯的api,要实现端口扫描,我们只需使用Socket.Connect()方法。然后再判断Socket.Connected是否为真,如果为真 ,则表明该端口可以连接,也就是说该端口是开放的。所以,判断一个端口是否开放的代码如下:
public void ThreadFunc(object o)
{
int port = (int)o;
ScanSocket s = new ScanSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Port = port;
IPEndPoint ipport = new IPEndPoint(scanIP, port);
try
{
// s.BeginConnect(ipport, new AsyncCallback(EndConnect), s);
s.Connect(ipport);
if (s.Connected == true)
OnPortConnect(null, new ScanEventArg(s.Port));
}
catch (Exception e)
{
}
finally
{
lock (this)
{
finishThreads++;
}
if (OnPortThreadFinish != null)
OnPortThreadFinish(null, null);
}
}
{
int port = (int)o;
ScanSocket s = new ScanSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Port = port;
IPEndPoint ipport = new IPEndPoint(scanIP, port);
try
{
// s.BeginConnect(ipport, new AsyncCallback(EndConnect), s);
s.Connect(ipport);
if (s.Connected == true)
OnPortConnect(null, new ScanEventArg(s.Port));
}
catch (Exception e)
{
}
finally
{
lock (this)
{
finishThreads++;
}
if (OnPortThreadFinish != null)
OnPortThreadFinish(null, null);
}
}
为了实现快速扫描,不可避免的要使用多线程的。我的实现的思路是,每个线程只扫描一个端口号。扫描完了再分配一个新的端口号。直到所有的端口号扫描完为止(不知道大家有没有更好的方法呢)代码如下:
private void MainScan()
{
if (!AdjustPortAndIP(ip,ref sport, ref eport))
{
if (OnPortError != null)
OnPortError(null, null);
return;
}
if (maxThreadCount > DEFAULT_THREAD_COUNT)
{
maxThreadCount = DEFAULT_THREAD_COUNT;
}
realThreadCount = 0;
ScanThread st = new ScanThread();
st.ScanIP = this.scanIP;
st.OnPortConnect += new ScanHandler(st_OnPortConnect);
st.OnPortThreadFinish += new EventHandler(st_OnPortThreadFinish);
for (int i = sport; i <= eport; i++,realThreadCount++)
{
while (realThreadCount-st.FinishThreads > maxThreadCount)
{
Thread.Sleep(10);
}
Thread.Sleep(10);
ParameterizedThreadStart ps = new ParameterizedThreadStart(st.ThreadFunc);
Thread start = new Thread(ps);
start.Start(i);
}
while (st.FinishThreads != realThreadCount) Thread.Sleep(1000);//等到所有线程全退出为止
//Thread.Sleep(10000);
if (OnScanFinish != null) //通知客户端扫描已经完成
OnScanFinish(null, null);
}
{
if (!AdjustPortAndIP(ip,ref sport, ref eport))
{
if (OnPortError != null)
OnPortError(null, null);
return;
}
if (maxThreadCount > DEFAULT_THREAD_COUNT)
{
maxThreadCount = DEFAULT_THREAD_COUNT;
}
realThreadCount = 0;
ScanThread st = new ScanThread();
st.ScanIP = this.scanIP;
st.OnPortConnect += new ScanHandler(st_OnPortConnect);
st.OnPortThreadFinish += new EventHandler(st_OnPortThreadFinish);
for (int i = sport; i <= eport; i++,realThreadCount++)
{
while (realThreadCount-st.FinishThreads > maxThreadCount)
{
Thread.Sleep(10);
}
Thread.Sleep(10);
ParameterizedThreadStart ps = new ParameterizedThreadStart(st.ThreadFunc);
Thread start = new Thread(ps);
start.Start(i);
}
while (st.FinishThreads != realThreadCount) Thread.Sleep(1000);//等到所有线程全退出为止
//Thread.Sleep(10000);
if (OnScanFinish != null) //通知客户端扫描已经完成
OnScanFinish(null, null);
}
为了和客户段进行交互,所以在ScanPort.cs里定义了几个事件:
public event EventHandler OnPortError;
public event EventHandler OnScanFinish;
public event ScanHandler OnPortConnect;
public event EventHandler OnPortThreadFinish;
public event EventHandler OnScanFinish;
public event ScanHandler OnPortConnect;
public event EventHandler OnPortThreadFinish;
分别代表扫描已经完成,该端口是开的和该端口已经扫描完毕。好了,有这些基础,相信实现自己的扫描器也不难了,具体实现参照原代码。
如何使用:先把scandll.dll添加到工程中,然后指定ip地址和端口号即可,代码如下:
scanPort.Scan(ip,sPort,ePort,threadCount);