您好,登錄后才能下訂單哦!
下面代碼內容是關于C#進行端口掃描的的代碼,應該是對碼農們有一些好處。
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace PortScan
{
class Program
{
static void Main(string[] args)
{
IPAddress ip;
int startPort, endPort;
{
Console.ReadKey();
}
}
#region 從命令行參數 中提取端口 + static bool GetPortRange(string[] args, out int startPort, out int endPort)
private static bool GetPortRange(string[] args,out IPAddress ip, out int startPort, out int endPort)
{
ip = null;
startPort = endPort = 0;
if (args.Length != 0 && (args[0] == "/?" || args[0] == "/help"))
{
Console.WriteLine("Scan port from startPort to endPort of the host specified by the IPAddress.");
Console.WriteLine("Command Format:");
Console.WriteLine("PortScan IPAddress startPort endPort");
Console.WriteLine("For example:");
Console.WriteLine("PortScan 127.0.0.1 1 1024");
return false;
}
if (args.Length == 3)
{
if (IPAddress.TryParse(args[0],out ip) && int.TryParse(args[1], out startPort) && int.TryParse(args[2], out endPort))
{
return true;
}
else
{
Console.WriteLine("參數格式不正確!");
return false;
}
}
else
{
Console.WriteLine("參數數目不正確!");
return false;
}
}
#endregion
static void Scan(IPAddress ip, int startPort, int endPort)
{
Random rand = new Random((int)DateTime.Now.Ticks);
Console.WriteLine("Begin Scan...");
for (int port = startPort; port < endPort; port++)
{
Socket scanSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
do
{
try
{
scanSocket.Bind(new IPEndPoint(IPAddress.Any, rand.Next(65535)));
break;
}
catch
{
}
} while (true);
try
{
scanSocket.BeginConnect(new IPEndPoint(ip, port), ScanCallBack, new ArrayList() { scanSocket, port});
}
catch
{
continue;
}
}
Console.WriteLine("Port Scan Completed!");
}
static void ScanCallBack(IAsyncResult result)
{
ArrayList arrList = (ArrayList)result.AsyncState;
Socket scanSocket = (Socket)arrList[0];
int port = (int)arrList[1];
if (result.IsCompleted && scanSocket.Connected)
{
Console.WriteLine("port {0,5}tOpen.", port);
}
else
{
}
scanSocket.Close();
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。