FC2ブログ

スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

C# Port Scanner 途中経過(悪用禁止)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace PortScanner
{
public partial class frmMain : Form
{
TcpClient TcpScan = new TcpClient();
StreamWriter sw;
protected int StartPort;
protected int EndPort;
int CurrPort;
int i = 0;
int tmax = 1024;
string filename;
string Result;


public frmMain()
{
InitializeComponent();
}

private class MYProtscan : frmMain
{



public void Run()
{
// Loop through the ports between start port and end port

try
{

// Try to connect
TcpScan.Connect(txtIP.Text, CurrPort);

// If there's no exception, we can say the port is open
txtLog.AppendText("Port " + CurrPort + " 開いてます\r\n");

sw.WriteLine(CurrPort + ":o");
}

catch
{
// An exception occured, thus the port is probably closed
txtLog.AppendText("Port " + CurrPort + " 閉じてます\r\n");

sw.WriteLine(CurrPort + ":x");
}


}
}

private void btnScan_Click(object sender, EventArgs e)
{

filename = "c:\\tmp\\" + txtIP.Text + "portscan.txt";
sw = new StreamWriter(filename, true, System.Text.Encoding.GetEncoding("Shift_JIS"));
Thread[] thread = new Thread[tmax];
MYProtscan[] myps = new MYProtscan[tmax];


// Store values from the NumericUpDown to variables
StartPort = Convert.ToInt32(numStart.Value);
EndPort = Convert.ToInt32(numEnd.Value);

// Reset the progress bar
prgScanning.Value = 0;

// Set the max value of the progress bar
prgScanning.Maximum = EndPort - StartPort + 1;

// Let the user know the application is busy
Cursor.Current = Cursors.WaitCursor;

CurrPort = StartPort;
//スレッド生成
for (i = 0 ; i <= tmax ; i++)
{
if(CurrPort <= EndPort) break;
myps[i] = new MYProtscan();
thread[i] = new Thread(new ThreadStart(myps[i].Run));
thread[i].IsBackground = true;
thread[i].Start();
CurrPort++;
}

// Increase the progress on the progress bar

prgScanning.PerformStep();

sw.Close();

// Set the cursor back to normal
Cursor.Current = Cursors.Arrow;
StreamReader sr = new StreamReader(filename, Encoding.Default);

//スキャン結果をバッファリング
Result = string.Empty;
while (sr.Peek() > 0)
{
string buffer = sr.ReadLine();
Result += buffer + Environment.NewLine;

}

sr.Close();
txtLog.AppendText(Result);
}

private void button1_Click(object sender, EventArgs e)
{
txtLog.ResetText();
prgScanning.Value = 0;
}

}
}



とりあえず、継承はうまくできたけど0番ポートしかスキャンしてくれない。。。
明日もバイトだから今日はここまでだけど、明日はなんとかインクリメントできるようにしたけど・・・

コメントの投稿

非公開コメント

プロフィール

modaconda

Author:modaconda
学生やってます。
夜間の学校通ってます。
載せるのは授業中のメモとかとか...
あとはセキュリティ関係好きだからチョコチョコと。。。
まぁ流し見してくだせぇ

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QR
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。