资源简介
用C#写的一个小型端口扫描程序,适合学习和更改。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Text.Regularexpressions;
using System.Net.Security;
namespace PortScan
{
class Program
{
static void Main(string[] args)
{
string server = null;
bool isConnected = true;
int scanPort = 0;
int scanPortFrom = 0;
int scanPortTo = 0;
if (args.Length < 2)
{
// Get Local Server
Console.WriteLine(“Usage: PortScan Hostname scanport“);
Console.WriteLine(“Usage: PortScan Hostname FromPort EndPort“);
return;
}
else
{
server = args[0];
//Console.WriteLine(“Testing Remote host: “ + args[0]);
}
//scanPort=Convert.ToInt32(args[1]); ///convert string to int have 2 methods
IPHostEntry heserver = Dns.GetHostEntry(server);
//Only do the First IP address
//Console.WriteLine(heserver.AddressList[0].ToString());//print the IP
//Console.WriteLine(heserver.AddressList[0]);//print the IP 2nd method
Console.WriteLine(server+“<==>“+heserver.AddressList[0]);
IPAddress remoteHost = heserver.AddressList[0];
if (args .Length== 2) //Scan 1 port
{
isConnected = true;
scanPort = int.Parse(args[1]);
Socket localSocket = new Socket(remoteHost.AddressFamily SocketType.Stream ProtocolType.Tcp); //.AddressFamily.maybe ipv4 maybe ipv6
IPEndPoint iep = new IPEndPoint(remoteHost scanPort);
try
{
localSocket.Connect(iep);
}
catch (SocketException sex)
{
//Console.WriteLine(sex.Message);//print Error Info
isConnected = false;
}
if (isConnected == true)
Console.WriteLine(“Port “ + scanPort + “ is open“);
else
Console.WriteLine(“Port “ + scanPort + “ is Close“);
localSocket.Close();
}
if (args.Length == 3) //Scan port in scope
{
scanPortFrom = int.Parse(args[1]);
scanPortTo = int.Parse(args[2]);
for(int i=scanPortFrom;i<=scanPortTo;i++)
{
isConnected = true;
Socket localSocket = new Socket(remoteHost.AddressFamily SocketType.Stream ProtocolType.Tcp); //.AddressFamily.maybe ipv4 maybe ipv6
IPEndPoint iep = new IPEndPoint(remoteHost i);
try
{
localSocket.Connect(i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
....... 5632 2012-03-06 15:13 PortScan\PortScan\bin\Debug\PortScan.exe
....... 13824 2012-03-06 15:13 PortScan\PortScan\bin\Debug\PortScan.pdb
....... 14328 2012-03-06 09:46 PortScan\PortScan\bin\Debug\PortScan.vshost.exe
....... 490 2007-07-21 01:33 PortScan\PortScan\bin\Debug\PortScan.vshost.exe.manifest
....... 5632 2012-03-06 15:15 PortScan\PortScan\bin\Release\PortScan.exe
....... 11776 2012-03-06 15:15 PortScan\PortScan\bin\Release\PortScan.pdb
....... 14328 2012-03-26 20:09 PortScan\PortScan\bin\Release\PortScan.vshost.exe
....... 490 2007-07-21 01:33 PortScan\PortScan\bin\Release\PortScan.vshost.exe.manifest
....... 288 2012-03-06 15:13 PortScan\PortScan\obj\Debug\PortScan.csproj.FileListAbsolute.txt
....... 5632 2012-03-06 15:13 PortScan\PortScan\obj\Debug\PortScan.exe
....... 13824 2012-03-06 15:13 PortScan\PortScan\obj\Debug\PortScan.pdb
文件 298 2012-03-26 20:09 PortScan\PortScan\obj\Release\PortScan.csproj.FileListAbsolute.txt
....... 5632 2012-03-06 15:15 PortScan\PortScan\obj\Release\PortScan.exe
....... 11776 2012-03-06 15:15 PortScan\PortScan\obj\Release\PortScan.pdb
....... 2607 2012-03-06 09:58 PortScan\PortScan\PortScan.csproj
....... 3630 2012-03-06 15:49 PortScan\PortScan\Program.cs
....... 1358 2012-03-06 09:46 PortScan\PortScan\Properties\AssemblyInfo.cs
....... 914 2012-03-06 09:46 PortScan\PortScan.sln
..A..H. 14848 2012-03-26 20:09 PortScan\PortScan.suo
目录 0 2012-03-06 09:46 PortScan\PortScan\obj\Debug\TempPE
目录 0 2012-03-06 15:15 PortScan\PortScan\obj\Release\TempPE
目录 0 2012-03-06 15:13 PortScan\PortScan\bin\Debug
目录 0 2012-03-06 15:15 PortScan\PortScan\bin\Release
目录 0 2012-03-06 15:13 PortScan\PortScan\obj\Debug
目录 0 2012-03-06 15:15 PortScan\PortScan\obj\Release
目录 0 2012-03-06 15:15 PortScan\PortScan\bin
目录 0 2012-03-06 15:15 PortScan\PortScan\obj
目录 0 2012-03-06 09:46 PortScan\PortScan\Properties
目录 0 2012-03-06 15:49 PortScan\PortScan
目录 0 2012-03-06 09:46 PortScan
............此处省略3个文件信息
评论
共有 条评论