基本信息
源码名称:c#实现网络嗅探器
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2014-06-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Text; namespace netpcap { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { ArrayList devlist=null; private System.Windows.Forms.Label labAdapters; private System.Windows.Forms.Label labDumpFile; private System.Windows.Forms.Button butStart; private System.Windows.Forms.Button butStartDump; private System.Windows.Forms.Button butStop; private System.Windows.Forms.ComboBox cbAdapters; private System.Windows.Forms.Button butBrowse; private System.Windows.Forms.TextBox txtDumpFile; private System.Windows.Forms.SaveFileDialog sfd; private System.Windows.Forms.Label labPacketCnt; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label labDump; private System.Windows.Forms.Button button1; long pack_count=0; private System.Windows.Forms.Button butSend; private System.Windows.Forms.RichTextBox rtb; private dotnetWinpCap.ReceivePacket rcvPack=null; [DllImport("advapi32.dll")] public static extern bool GetUserName( StringBuilder name, ref int size); dotnetWinpCap wpcap=null; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.butStart = new System.Windows.Forms.Button(); this.butStartDump = new System.Windows.Forms.Button(); this.cbAdapters = new System.Windows.Forms.ComboBox(); this.butStop = new System.Windows.Forms.Button(); this.labAdapters = new System.Windows.Forms.Label(); this.txtDumpFile = new System.Windows.Forms.TextBox(); this.labDumpFile = new System.Windows.Forms.Label(); this.butBrowse = new System.Windows.Forms.Button(); this.sfd = new System.Windows.Forms.SaveFileDialog(); this.labPacketCnt = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.labDump = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.butSend = new System.Windows.Forms.Button(); this.rtb = new System.Windows.Forms.RichTextBox(); this.SuspendLayout(); // // butStart // this.butStart.Anchor = System.Windows.Forms.AnchorStyles.Top; this.butStart.Location = new System.Drawing.Point(-17, 103); this.butStart.Name = "butStart"; this.butStart.Size = new System.Drawing.Size(125, 26); this.butStart.TabIndex = 0; this.butStart.Text = "&Start capture"; this.butStart.Click = new System.EventHandler(this.button1_Click); // // butStartDump // this.butStartDump.Anchor = System.Windows.Forms.AnchorStyles.Top; this.butStartDump.Location = new System.Drawing.Point(396, 103); this.butStartDump.Name = "butStartDump"; this.butStartDump.Size = new System.Drawing.Size(106, 26); this.butStartDump.TabIndex = 1; this.butStartDump.Text = "Start &dump"; this.butStartDump.Click = new System.EventHandler(this.butStartDump_Click); // // cbAdapters // this.cbAdapters.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.cbAdapters.Location = new System.Drawing.Point(125, 17); this.cbAdapters.Name = "cbAdapters"; this.cbAdapters.Size = new System.Drawing.Size(446, 20); this.cbAdapters.TabIndex = 2; // // butStop // this.butStop.Anchor = System.Windows.Forms.AnchorStyles.Top; this.butStop.Location = new System.Drawing.Point(127, 103); this.butStop.Name = "butStop"; this.butStop.Size = new System.Drawing.Size(125, 26); this.butStop.TabIndex = 3; this.butStop.Text = "S&top capture"; this.butStop.Click = new System.EventHandler(this.butStop_Click); // // labAdapters // this.labAdapters.Location = new System.Drawing.Point(19, 17); this.labAdapters.Name = "labAdapters"; this.labAdapters.Size = new System.Drawing.Size(67, 17); this.labAdapters.TabIndex = 4; this.labAdapters.Text = "&Adapters"; // // txtDumpFile // this.txtDumpFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txtDumpFile.Location = new System.Drawing.Point(125, 52); this.txtDumpFile.Name = "txtDumpFile"; this.txtDumpFile.Size = new System.Drawing.Size(350, 21); this.txtDumpFile.TabIndex = 5; // // labDumpFile // this.labDumpFile.Location = new System.Drawing.Point(19, 52); this.labDumpFile.Name = "labDumpFile"; this.labDumpFile.Size = new System.Drawing.Size(67, 17); this.labDumpFile.TabIndex = 7; this.labDumpFile.Text = "&Dump file"; // // butBrowse // this.butBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.butBrowse.Location = new System.Drawing.Point(499, 52); this.butBrowse.Name = "butBrowse"; this.butBrowse.Size = new System.Drawing.Size(77, 26); this.butBrowse.TabIndex = 6; this.butBrowse.Text = "&Browse"; this.butBrowse.Click = new System.EventHandler(this.butBrowse_Click); // // sfd // this.sfd.FileName = "doc1"; // // labPacketCnt // this.labPacketCnt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.labPacketCnt.Location = new System.Drawing.Point(115, 366); this.labPacketCnt.Name = "labPacketCnt"; this.labPacketCnt.Size = new System.Drawing.Size(125, 18); this.labPacketCnt.TabIndex = 9; // // label1 // this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.label1.Location = new System.Drawing.Point(10, 366); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(86, 18); this.label1.TabIndex = 10; this.label1.Text = "Packet count"; // // labDump // this.labDump.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.labDump.Location = new System.Drawing.Point(379, 366); this.labDump.Name = "labDump"; this.labDump.Size = new System.Drawing.Size(211, 18); this.labDump.TabIndex = 11; // // button1 // this.button1.Anchor = System.Windows.Forms.AnchorStyles.Top; this.button1.Location = new System.Drawing.Point(521, 103); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(105, 26); this.button1.TabIndex = 12; this.button1.Text = "St&op dump"; this.button1.Click = new System.EventHandler(this.button1_Click_1); // // butSend // this.butSend.Anchor = System.Windows.Forms.AnchorStyles.Top; this.butSend.Location = new System.Drawing.Point(271, 103); this.butSend.Name = "butSend"; this.butSend.Size = new System.Drawing.Size(106, 26); this.butSend.TabIndex = 13; this.butSend.Text = "Sen&d stuff"; this.butSend.Click = new System.EventHandler(this.butSend_Click); // // rtb // this.rtb.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.rtb.Location = new System.Drawing.Point(10, 146); this.rtb.Name = "rtb"; this.rtb.ReadOnly = true; this.rtb.Size = new System.Drawing.Size(580, 203); this.rtb.TabIndex = 14; this.rtb.Text = ""; this.rtb.WordWrap = false; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(600, 391); this.Controls.Add(this.rtb); this.Controls.Add(this.butSend); this.Controls.Add(this.button1); this.Controls.Add(this.labDump); this.Controls.Add(this.label1); this.Controls.Add(this.labPacketCnt); this.Controls.Add(this.labDumpFile); this.Controls.Add(this.butBrowse); this.Controls.Add(this.txtDumpFile); this.Controls.Add(this.labAdapters); this.Controls.Add(this.butStop); this.Controls.Add(this.cbAdapters); this.Controls.Add(this.butStartDump); this.Controls.Add(this.butStart); this.Name = "Form1"; this.Text = "Form1"; this.Closing = new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load = new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { if (wpcap==null) { wpcap=new dotnetWinpCap(); } if (wpcap.IsOpen) { wpcap.Close(); } if (!wpcap.Open( ((Device)devlist[cbAdapters.SelectedIndex]).Name,65536,1,0)) { System.Diagnostics.Debug.WriteLine(wpcap.LastError); rtb.AppendText(wpcap.LastError "\n"); } else { System.Diagnostics.Debug.WriteLine("opened successfully: " ((Device)devlist[0]).Name); rtb.AppendText("Opened successfully: " ((Device)devlist[cbAdapters.SelectedIndex]).Name "\n"); } wpcap.SetMinToCopy(100); if (rcvPack==null) { rcvPack=new dotnetWinpCap.ReceivePacket(this.ReceivePacket); wpcap.OnReceivePacket =rcvPack; } wpcap.StartListen(); rtb.AppendText("Capturing. . .\n"); } private void DumpEnded(object sender) { System.Diagnostics.Debug.WriteLine("livedump ended"); wpcap.Close(); } private void ReceivePacket(object sender, PacketHeader p, byte[] s) { this.pack_count ; rtb.Focus(); rtb.AppendText("Content of p : \n"); rtb.AppendText(" Caplength: " p.Caplength "\n"); rtb.AppendText(" Length : " p.Length "\n"); rtb.AppendText(" Timestamp: " (p.TimeStamp) "\n"); labPacketCnt.Text=Convert.ToString(this.pack_count); } private void Form1_Load(object sender, System.EventArgs e) { if (wpcap==null) { devlist=dotnetWinpCap.FindAllDevs(); for (int i=0; i<=devlist.Count-1; i ) { cbAdapters.Items.Add( ((Device)devlist[i]).Name ":" ((Device)devlist[i]).Description); } } if (cbAdapters.Items.Count>0) { cbAdapters.SelectedIndex=0; } } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (this.wpcap!=null) { wpcap.Close(); } } private void butBrowse_Click(object sender, System.EventArgs e) { sfd.FileName=""; if (sfd.ShowDialog(this)==DialogResult.OK) { txtDumpFile.Text=sfd.FileName; } } private void butStartDump_Click(object sender, System.EventArgs e) { if (wpcap!=null) { if (wpcap.StartDump(txtDumpFile.Text)==true) { labDump.Text="Dumping to " txtDumpFile.Text; } } } private void butStop_Click(object sender, System.EventArgs e) { if (wpcap!=null) { wpcap.StopListen(); wpcap.Close(); } } private void button1_Click_1(object sender, System.EventArgs e) { if (wpcap!=null) { wpcap.StopDump(); labDump.Text="Dump stopped"; } } private void butSend_Click(object sender, System.EventArgs e) { if (wpcap==null) { wpcap=new dotnetWinpCap(); } if (!wpcap.IsOpen) { if (!wpcap.Open(((Device)devlist[cbAdapters.SelectedIndex]).Name, 65536, 1,0)) { MessageBox.Show(this, "Cannot open adapter " cbAdapters.Text); return; } } byte[] b=new byte [10]; for (int i=0; i<b.Length; i ) { b[i]=(byte)i; } if (!wpcap.SendPacket(b)) { rtb.AppendText("Send unsuccessful\n"); } else { rtb.AppendText("Sent garbage: " b.ToString() "\n"); } } } }