Port scan
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Add the following.
Using System.Data;
Using System.Net.Sockets;
Using System.Net;
Using System.IO;
Using System.Text;
Using System.Threading;
Namespace Iportscan
{
<summary>
Summary description for Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
This is where the public variables that the program is going to use
public string scanhost = Dns.gethostname (); Default Current native IP
Public Int32 tport = 0; Current Connection port number
Public Int32 connstate = 0; Scan status
public int portsum = 0; Port totals
public bool Endthread = FALSE; End State
Public AutoResetEvent Asyncopsaredone = new AutoResetEvent (false);
Private System.Windows.Forms.Label Label1 = new Label ();
Private System.Windows.Forms.TextBox Txthostname;
Private System.Windows.Forms.Button CmdExec;
Private System.Windows.Forms.ListBox loglist;
Public System.Windows.Forms.CheckedListBox portlist;
Private System.Windows.Forms.Label Label2;
Private System.Windows.Forms.Label label3;
Private System.Windows.Forms.NumericUpDown Snum;
Private System.Windows.Forms.NumericUpDown ENum;
Private System.Windows.Forms.CheckBox Showdie;
Private System.Windows.Forms.Label label4;
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.ToolTip toolTip1;
Private System.Windows.Forms.StatusBar statusBar1;
Private System.Windows.Forms.LinkLabel LinkLabel1;
Private System.Windows.Forms.LinkLabel LinkLabel2;
Private System.ComponentModel.IContainer components;
/*
The property settings code for each called control is omitted here
*/
static void Main ()
{
Application.Run (New Form1 ());
}
private void Cmdexec_click (object sender, System.EventArgs e)
{
Int32 Startport = (Int32) snum.value;
int32 endport = (Int32) enum.value;
if (txthostname.text.length==0)
{
MessageBox.Show ("Please enter the name of a host!") "," System hints ");
txthostname.text = scanhost.tostring ();
txthostname.focus ();
return;
}
if (startport>endport)
{
MessageBox.Show ("error, starting port must be less than end port!", "System Prompt");
startport = endPort-1;
snum.text = startport.tostring ();
snum.focus ();
return;
}
if (cmdexec.text== "&scan")
{
endthread = false;
cmdexec.text= "&stop";
}
else
{
endthread= true;
cmdexec.text= "&scan";
}
if (endthread!=true)
{
connstate = 0;
portsum = 0;
scanhost = Txthostname.text;
try
{
ipaddress ipaddr = (IPAddress) Dns.resolve (Scanhost). Addresslist.getvalue (0);
txthostname.text = ipaddr. ToString ();
}
catch
{
Txthostname.focus ();
messagebox.show ("Please enter the correct host address, this address DNS cannot resolve", "System Prompt");
return;
}
loglist.items.clear ();
for (Int32 threadnum = startport; threadnum <=endPort; threadnum++)
{
ThreadPool.QueueUserWorkItem (New WaitCallback (Startscan), threadnum);
LOGLIST.ITEMS.ADD ("Scan port:" + threadnum.tostring ());
}
}
}
public void Startscan (Object state)
{
Int32 port = (Int32) state;
String tmsg = "";
String getData = "";
int lindex = 0;
int eindex = 0;
connstate++; To determine the number of threads
if (endthread==true)
{
if (connstate== (Int32) enum.value-(Int32) snum.value))
{
Cmdexec.text = "&scan";
LOGLIST.ITEMS.ADD ("Scan completed!");
}
Else
{
Cmdexec.text = "&stop";
LOGLIST.ITEMS.ADD ("Stopping on" +port.) ToString () + "Scan thread for port");
}
LOGLIST.ITEMS.ADD ("End Thread:" +port.) ToString ());
Asyncopsaredone.close ();
}
Else
{
Try
{
TcpClient TCP = new TcpClient ();
Tcp. Connect (Scanhost,port);
If a connection error is established, the following code will not be executed ...
Portsum + +;
Lindex = PORTLIST.ITEMS.ADD (port. ToString () + "Port open", false);
Portlist.selectedindex=lindex;
Stream sm = tcp. GetStream ();
Sm. Write (Encoding.Default.GetBytes (Tmsg.tochararray ()), 0,tmsg.length);
StreamReader sr = new StreamReader (TCP. GetStream (), encoding.default);
GetData = Sr. ReadLine ();
if (lindex!=0&&getdata.length!=0)
{
tmsg = "+-" + port. ToString () + "Port data:" +getdata.tostring ();
Eindex = PortList.Items.Add (tmsg); Insert a message record
PortList.Items.Insert (LINDEX+1,TMSG);
PortList.Items.RemoveAt (Eindex);
}
Sr. Close ();
Sm. Close ();
Tcp. Close ();
}
Catch
{
Show necrotic ports
if (Showdie. Checked==true)
{
PORTLIST.ITEMS.ADD (port. ToString () + "Port cannot connect, return data is empty");
}
}
Finally
{
Thread.Sleep (0);
LOGLIST.ITEMS.ADD ("End Thread:" +port.) ToString ());
Asyncopsaredone.close ();
Statusbar1.text = "Port total:" +portsum.tostring ();
if (connstate== (Int32) enum.value-(Int32) snum.value))
{
Cmdexec.text = "&scan";
}
}
}
}
private void Button1_Click (object sender, System.EventArgs e)
{
Application.exit ();
}
}
}