Portscan

Description

J'ai trouvé un debut de source buggée sur un news Group et je l'ai réecrite.
Elle utilise : delegate, event, enum, struct, classe (héritage), socket, thread.

Il reste des améliorations à faire ... et surtout il faut commenter le bazar, dès que j'ai le temps promis je le fait !! ;-)

Source / Exemple :


public class PortScan
{
	public class PortScanEvent : EventArgs
	{
		public PortScanEvent(string ip, long port, EventType eventType)
		{
			this.ip = ip;
			this.port = port;
			this.eventType = eventType;
		}
		public PortScanEvent(string ip, long port, EventType eventType, SocketException socketException)
		{
			this.ip = ip;
			this.port = port;
			this.eventType = eventType;
			this.socketException = socketException;
		}
		public string ip;
		public long port;
		public EventType eventType;
		public SocketException socketException;
	}

	[Flags] public enum EventType : ushort
	{
		Open,
		Close,
		Error
	}
	public struct ParamFunct
	{
		public ParamFunct(string ip, int port)
		{
			this.ip = ip;
			this.port = port;
		}
		public string ip;
		public int port;
	}

	public int count = 0;
	public int maxCount = 0;
	public ManualResetEvent eventDone = new ManualResetEvent(false);

	public delegate void PortScanHandler(object sender, PortScanEvent e);
	public event PortScanHandler OnPortScanEvent;

	public EventType eventTypeSouscrib = EventType.Open;

	public void Scan(object portArg)
	{
		ParamFunct paramFunctScan = (ParamFunct)portArg;
		TcpClient objTCP = new TcpClient();
		try
		{
			objTCP.Connect(paramFunctScan.ip, paramFunctScan.port);
			objTCP.Close();
			if(eventTypeSouscrib == EventType.Open && OnPortScanEvent != null)
				OnPortScanEvent(this, new PortScanEvent(paramFunctScan.ip, paramFunctScan.port, EventType.Open));
		}
		catch(SocketException e)
		{
			if (e.ErrorCode == 11001 || e.ErrorCode == 10060)
			{
				if(eventTypeSouscrib == EventType.Error && OnPortScanEvent != null)
					OnPortScanEvent(this, new PortScanEvent(paramFunctScan.ip, paramFunctScan.port, EventType.Error, e));
			}
			else
			{
				if(eventTypeSouscrib == EventType.Close && OnPortScanEvent != null)
					OnPortScanEvent(this, new PortScanEvent(paramFunctScan.ip, paramFunctScan.port, EventType.Close, e));
			}
		}
		finally
		{
			Interlocked.Increment(ref count);
			if (count == maxCount) 
			{
				eventDone.Set();
			}
		}
	}
}

Conclusion :


fonctionnement :
Usage: portscan host startport endport

host - IP/hostname of machine to scan
startport - port number to start scan from [0-65536]
endport - port number to end scan on [0-65536]

Codes Sources

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.