您的位置:首页 > 编程语言 > C#

luyikk 的C# PING 工具 与 传统的不一样的是 他可以修改你的 IP地址实现伪造ICMP数据包 ,当然 XP下无法使用 2003下才可以使用

2007-04-30 04:12 1051 查看
FORM1.CS

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

namespace WindowsApplication23
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private Label label2;
private TextBox textBox2;
private TextBox textBox3;
private Label label3;
private Button button2;
private TextBox textBox4;
private Label label4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

void Application_ApplicationExit(object sender, EventArgs e)
{

Application.ExitThread();
this.vb = false;
Application.Exit();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(89, 303);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(148, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(2, 307);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 17);
this.label1.TabIndex = 1;
this.label1.Text = "请输入主机名:";
//
// listBox1
//
this.listBox1.BackColor = System.Drawing.SystemColors.WindowText;
this.listBox1.ForeColor = System.Drawing.SystemColors.Window;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(4, 10);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(400, 280);
this.listBox1.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(291, 301);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(92, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Ping";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(2, 350);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 18);
this.label2.TabIndex = 4;
this.label2.Text = "包大小:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(54, 346);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(55, 21);
this.textBox2.TabIndex = 5;
this.textBox2.Text = "40";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(182, 346);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(55, 21);
this.textBox3.TabIndex = 6;
this.textBox3.Text = "10";
//
// label3
//
this.label3.Location = new System.Drawing.Point(119, 350);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 18);
this.label3.TabIndex = 7;
this.label3.Text = "PING次数:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(291, 347);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(92, 23);
this.button2.TabIndex = 8;
this.button2.Text = "STOP";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(89, 378);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(148, 21);
this.textBox4.TabIndex = 9;
this.textBox4.Text = "127.0.0.1";
//
// label4
//
this.label4.Location = new System.Drawing.Point(6, 382);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 17);
this.label4.TabIndex = 10;
this.label4.Text = "IP头原地址:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(413, 411);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.label4);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Visual C#实现Ping";
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{

Application.Run(new Form1());

}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{

}

private void UpdateUI(object o, System.EventArgs e)
{
listBox1.Items.Add(o.ToString());

}

private void UpdateUI2(object o, System.EventArgs e)
{
this.textBox3.Text = o.ToString();

}

private void bcs()
{
Thread.Sleep(1000);

Socket Sockets = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
Sockets.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded,1);
Sockets.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);

IPHostEntry Hostinfo;

try
{
//解析主机ip入口
Hostinfo = Dns.GetHostByName(Hostclient);
}
catch (Exception)
{
//解析主机名错误。
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "没有发现此主机!");
// listBox1.Items.Add("没有发现此主机!");
return;
}
// 取服务器端主机的30号端口
EndPoint Hostpoint = (EndPoint)new IPEndPoint(Hostinfo.AddressList[0], 30);
IPHostEntry Clientinfo;
Clientinfo = Dns.GetHostByName(Hostclient);
// 取客户机端主机的30端口
EndPoint Clientpoint = (EndPoint)new IPEndPoint(Clientinfo.AddressList[0], 30);
//设置icmp报文
int Datasize = datb; // Icmp数据包大小 ;
int Packetsize = Datasize + 8;//总报文长度
const int Icmp_echo = 8;
IcmpPacket Packet = new IcmpPacket(Icmp_echo, 0, 0, 45, 0, Datasize);

Byte[] Buffer = new Byte[Packetsize];
int Index = Packet.CountByte(ref Buffer);

//报文出错
if (Index != Packetsize)
{
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "报文出现问题!");
return;
}

int Cksum_buffer_length = (int)Math.Ceiling(((Double)Index) / 2);

UInt16[] Cksum_buffer = new UInt16[Cksum_buffer_length];
int Icmp_header_buffer_index = 0;
for (int I = 0; I < Cksum_buffer_length; I++)
{
//将两个byte转化为一个uint16

Cksum_buffer[I] = (UInt16)BitConverter.ToInt16(Buffer, Icmp_header_buffer_index);
Icmp_header_buffer_index += 2;
}
//将校验和保存至报文里

Packet.CheckSum = IcmpPacket.SumOfCheck(Cksum_buffer);
// 保存校验和后,再次将报文转化为数据包

Byte[] Senddatas = new Byte[Packetsize];

Index = Packet.CountByte(ref Senddatas);

//报文出错
if (Index != Packetsize)
{
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "报文出现问题!");
return;
}
int Nbytes = 0;
//系统计时开始
int Starttime;
//发送数据包

Byte[] Senddata = getiphed(Senddatas, Packetsize, Clientinfo);

for (int i = 0; i < pingci; i++)
{
Thread.Sleep(500);

if (vb == false)
{
break;
}

Starttime = Environment.TickCount;

if ((Nbytes = Sockets.SendTo(Senddata,Senddata.Length, SocketFlags.None, Hostpoint)) == -1)
{
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "无法传送报文!");

}

Byte[] Receivedata = new Byte[256]; //接收数据
Nbytes = 0;

int Timeconsume = 0;

try
{
Nbytes = Sockets.ReceiveFrom(Receivedata, 256, SocketFlags.None, ref Clientpoint);

}
catch (SocketException e)
{
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), Hostinfo.AddressList[0].ToString() + "::Request timed out.");

}

if (Nbytes > 0)
{
Timeconsume = System.Environment.TickCount - Starttime;
//得到发送报文到接收报文之间花费的时间
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "Reply From " + Hostinfo.AddressList[0].ToString() + " In "
+ Timeconsume + "ms :bytes Received " + Nbytes);

}
Timeconsume = Environment.TickCount - Starttime;

this.textBox3.BeginInvoke(new System.EventHandler(UpdateUI2), (pingci - i - 1).ToString());

}

//关闭套接字
Sockets.Close();

}

private Byte[] getiphed(Byte[] buffs, int Packetsize,IPHostEntry abstads)
{

ipHeader ippacket = new ipHeader(69, 0, 28, 0, 0, 128, 2, 0, Convert.ToUInt32(abstads.AddressList[0].Address),adds);
ippacket.dates = buffs;

int ippacketlength = buffs.Length + 20;

Byte[] buff = new Byte[ippacketlength];

int index = ippacket.CountByte(ref buff);

if (index != ippacketlength)
{
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "构造IP包头错误#1");
}

ippacket.ip_checksum=ippacket.checksum_get(buff, index);

Byte[] sendipdate = new Byte[ippacketlength];

index = ippacket.CountByte(ref sendipdate);

if (index != ippacketlength)
{
this.listBox1.BeginInvoke(new System.EventHandler(UpdateUI), "构造IP包头错误#2");
}

return sendipdate;

}

private String Hostclient;

private int datb;
private int pingci;

private bool vb = true;

private UInt32 adds;

private void button1_Click(Object Sender, System.EventArgs E)
{
listBox1.Items.Clear();
Hostclient = textBox1.Text;
vb = true;

try
{
datb = int.Parse(this.textBox2.Text) - 28;
pingci = int.Parse(this.textBox3.Text);
IPHostEntry Clientinfo = Dns.GetHostByName(this.textBox4.Text);
adds = Convert.ToUInt32(Clientinfo.AddressList[0].Address);

}
catch
{
MessageBox.Show("输入的包大小错误");
}

Thread a = new Thread(new ThreadStart(bcs));
a.Start();

}

private void button2_Click(object sender, EventArgs e)
{
vb = false;
}

public class IcmpPacket
{
private Byte _type; // 类型
private Byte _subCode; // 代码
private UInt16 _checkSum; // 校验和
private UInt16 _identifier; // 识别符
private UInt16 _sequenceNumber; // 序列号
private Byte[] _data; //选项数据
//初始化ICMP报文
public IcmpPacket(Byte type, Byte subCode, UInt16 checkSum, UInt16 identifier, UInt16 sequenceNumber, int dataSize)
{
_type = type;
_subCode = subCode;
_checkSum = checkSum;
_identifier = identifier;
_sequenceNumber = sequenceNumber;
_data = new Byte[dataSize];
//在选项数据中,写入指定的数据大小
for (int i = 0; i < dataSize; i++)
{
//由于选项数据在此命令中并不重要,所以你可以改换任何你喜欢的字符
_data[i] = (byte)'x';
}
}
public UInt16 CheckSum
{
get
{
return _checkSum;
}
set
{
_checkSum = value;
}
}

//得到将整个ICMP报文信息和数据转化为Byte时的Byte数目,主要是用来判断报文的完整性
public int CountByte(ref Byte[] buffer)
{

Byte[] b_type = new Byte[1] { _type };
Byte[] b_code = new Byte[1] { _subCode };
Byte[] b_cksum = BitConverter.GetBytes(_checkSum);
Byte[] b_id = BitConverter.GetBytes(_identifier);
Byte[] b_seq = BitConverter.GetBytes(_sequenceNumber);
int i = 0;
Array.Copy(b_type, 0, buffer, i, b_type.Length);
i += b_type.Length;
Array.Copy(b_code, 0, buffer, i, b_code.Length);
i += b_code.Length;
Array.Copy(b_cksum, 0, buffer, i, b_cksum.Length);
i += b_cksum.Length;
Array.Copy(b_id, 0, buffer, i, b_id.Length);
i += b_id.Length;
Array.Copy(b_seq, 0, buffer, i, b_seq.Length);
i += b_seq.Length;
Array.Copy(_data, 0, buffer, i, _data.Length);
i += _data.Length;
return i;
}

public static UInt16 SumOfCheck(UInt16[] buffer)
{
int cksum = 0;
for (int i = 0; i < buffer.Length; i++)
cksum += (int)buffer[i];
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (UInt16)(~cksum);
}
}

public class ipHeader
{
public Byte ip_verlen; //4位首部长度+4位IP版本号
public Byte ip_tos; //8位服务类型TOS
public UInt16 ip_totallength; //16位数据包总长度(字节)
public UInt16 ip_id; //16位标识
public UInt16 ip_offset; //3位标志位
public Byte ip_ttl; //8位生存时间 TTL
public Byte ip_protocol; //8位协议(TCP, UDP, ICMP, Etc.) (1->ICMP, 2->IGMP, 6->TCP, 17->UDP)
public UInt16 ip_checksum; //16位IP首部校验和
public uint ip_srcaddr; //32位源IP地址
public uint ip_destaddr; //32位目的IP地址
public Byte[] dates; //其他协议数据包

public ipHeader(Byte ipverlen, Byte iptos, UInt16 iptotallength, UInt16 ipid, UInt16 ipoffset, Byte ipttl, Byte ipprotocol, UInt16 ipchecksum, uint ipsrcaddr,uint ipdestaddr)
{
ip_verlen = ipverlen;
ip_tos = iptos;
ip_totallength = iptotallength;
ip_id = ipid;
ip_offset = ipoffset;
ip_ttl = ipttl;
ip_protocol=ipprotocol;
ip_checksum=ipchecksum;
ip_srcaddr=ipsrcaddr;
ip_destaddr = ipdestaddr;

}

public UInt16 checksum_get(byte[] buffer, int size)
{

int Cksum_buffer_length = (int)Math.Ceiling(((Double)size) / 2);

UInt16[] Cksum_buffer = new UInt16[Cksum_buffer_length];
int Icmp_header_buffer_index = 0;
for (int I = 0; I < Cksum_buffer_length; I++)
{
//将两个byte转化为一个uint16

Cksum_buffer[I] = (UInt16)BitConverter.ToInt16(buffer, Icmp_header_buffer_index);
Icmp_header_buffer_index += 2;
}

int cksum = 0;
for (int i = 0; i < Cksum_buffer.Length; i++)
{
cksum += (int)buffer[i];
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);

return (UInt16)(~cksum);

}

public int CountByte(ref Byte[] buffer)
{
Byte[] _ip_verlen = new Byte[1] { ip_verlen };

Byte[] _ip_tos = new Byte[1] { ip_tos };

Byte[] _ip_totallength = BitConverter.GetBytes(ip_totallength);

Byte[] _ip_id = BitConverter.GetBytes(ip_id);

Byte[] _ip_offset = BitConverter.GetBytes(ip_offset);

Byte[] _ip_ttl = new Byte[1] { ip_ttl };

Byte[] _ip_protocol = new Byte[1] { ip_protocol };

Byte[] _ip_checksum = BitConverter.GetBytes(ip_checksum);

Byte[] _ip_srcaddr = BitConverter.GetBytes(ip_srcaddr);

Byte[] _ip_destaddr = BitConverter.GetBytes(ip_destaddr);

int i = 0;
Array.Copy(_ip_verlen, 0, buffer, i, _ip_verlen.Length);
i += _ip_verlen.Length;
Array.Copy(_ip_tos, 0, buffer, i, _ip_tos.Length);
i += _ip_tos.Length;
Array.Copy(_ip_totallength, 0, buffer, i, _ip_totallength.Length);
i += _ip_totallength.Length;
Array.Copy(_ip_id, 0, buffer, i, _ip_id.Length);
i += _ip_id.Length;
Array.Copy(_ip_offset, 0, buffer, i, _ip_offset.Length);
i += _ip_offset.Length;
Array.Copy(_ip_ttl, 0, buffer, i, _ip_ttl.Length);
i += _ip_ttl.Length;
Array.Copy(_ip_protocol, 0, buffer, i, _ip_protocol.Length);
i += _ip_protocol.Length;
Array.Copy(_ip_checksum, 0, buffer, i, _ip_checksum.Length);
i += _ip_checksum.Length;
Array.Copy(_ip_srcaddr, 0, buffer, i, _ip_srcaddr.Length);
i += _ip_srcaddr.Length;
Array.Copy(_ip_destaddr, 0, buffer, i, _ip_destaddr.Length);
i += _ip_destaddr.Length;
Array.Copy(dates, 0, buffer, i, dates.Length);
i += dates.Length;

return i;

}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐