多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 服務器 > 網站安全攻擊和防御中的屏蔽代理服務器

網站安全攻擊和防御中的屏蔽代理服務器

來源:程序員人生   發布時間:2014-12-24 08:39:49 閱讀次數:3314次

網站安全攻擊和防御中的屏蔽代理服務器

 

網站安全1直是個重要話題,本人寫了網絡攻防的屏蔽代理服務器相干代碼,分享下:

1. 寫個網頁request類:

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication1 { public class WebRequestUtil { public static string responseBody = string.Empty; public static bool GetWeb(string uri, string proxyAddress = "", int proxyPort = 0) { string serverUri = string.Format(uri); ////set limit for supporting 200 connection ServicePointManager.DefaultConnectionLimit = 1000; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUri); if (!string.IsNullOrEmpty(proxyAddress)) { WebProxy myproxy = new WebProxy(proxyAddress, proxyPort); request.Proxy = myproxy; } ////extend timeout for decrease request timeout re-trying times request.Timeout = 60 * 1000; request.Method = @"GET"; UTF8Encoding encoding = new UTF8Encoding(); request.Headers.Set("Cache-Control", @"no-cache"); request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); bool isSent = false; int retryCount = 0; string errorStr = string.Empty; while (!isSent && retryCount <= 2) { retryCount++; try { using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseBody = stream.ReadToEnd(); } isSent = true; } catch (Exception exc) { if (!errorStr.Contains(exc.ToString())) { errorStr += exc.ToString(); } ////Re-try when operation timeout if (!exc.ToString().Contains("The operation has timed out")) { LogError(exc.ToString()); } Thread.Sleep(1000); } } if (retryCount > 100) { string err = string.Format("request.GetRequestStream try 100 times and timeout! detail error: {0}", errorStr); LogError(err); return false; } ////need to close or abort request for each call to fix timeout issue, otherwise it will fail when the 3rd call! if (request != null) { request.Abort(); } if (response.StatusCode != HttpStatusCode.OK) { string err = string.Format("Failed, error:{1}", response.ToString()); LogError(err); return false; } if (response != null) { response.Close(); } } catch (Exception exc) { LogError(exc.ToString()); return false; } return true; } public static void LogError(string content) { File.AppendAllText("log.log", "ERROR: " + content + Environment.NewLine); } } }

2. 收集代理服務器代碼:

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; using Microsoft.ServiceBus; using Microsoft.ServiceBus.Messaging; using System.Threading; using System.IO; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static int FailCount = 0; static int TotalCount = 0; const string IPRegex = @"(d{1,3}.){3}d{1,3}</td><td>d{1,4}"; static void Main() { DateTime startTime = DateTime.Now; //int i = 0; //while (DateTime.Now < startTime.AddMinutes(5)) { //WriteLog("Try " + i++ + "th round! Begin" + DateTime.Now.ToString()); //WebRequestUtil.GetWeb(@"http://edu.laliyun.com/test.php", "147.47.106.36", 1920); //File.AppendAllText(@"test.txt", WebRequestUtil.responseBody, Encoding.UTF8); string url = @"http://proxy.com.ru/gaoni/list_{0}.html"; for (int i = 1; i <= 63; i++) { WebRequestUtil.GetWeb(string.Format(url, i)); string sourceString = WebRequestUtil.responseBody; string IPs = string.Empty; var matches = Regex.Matches(sourceString, IPRegex); if (matches.Count > 0) { foreach (var m in matches) { string ip = m.ToString().Replace("</td><td>", "#").Split('#')[0]; IPs += ip + Environment.NewLine; } } File.AppendAllText(@"blacklist.txt", IPs, Encoding.UTF8); Console.WriteLine("Done " + i.ToString() + " page."); } //Test2(1); //WriteLog("Total:" + TotalCount); //WriteLog("Fail:" + FailCount); //WriteLog("Try " + i++ + "th round! End" + DateTime.Now.ToString()); } Console.WriteLine("Please press any key to end of this program! "); Console.ReadKey(); } static void WriteTotalLog(string message) { //WriteLog(message, @"C:TotalLog.txt"); } static void WriteLog(string message, string path = @"C:TestTest#log.txt") { message = "ThreadId:" + Thread.CurrentThread.ManagedThreadId + "," + message + Environment.NewLine; File.AppendAllText(path.Replace("#", Thread.CurrentThread.ManagedThreadId.ToString()), message); } static void WriteErrorLog(string message) { WriteLog(message, @"C:TestError" + Thread.CurrentThread.ManagedThreadId + "log.txt"); }


3. 多線程攻擊服務器代碼:

static void Test2(int numThreads) { ManualResetEvent resetEvent = new ManualResetEvent(false); int toProcess = numThreads; // Start workers. for (int i = 0; i < numThreads; i++) { new Thread(delegate() { test(); //Console.WriteLine(Thread.CurrentThread.ManagedThreadId); // If we're the last thread, signal if (Interlocked.Decrement(ref toProcess) == 0) resetEvent.Set(); }).Start(); } // Wait for workers. resetEvent.WaitOne(); WriteTotalLog("Done all!"); } static void test() { TotalCount++; try { WebRequestUtil.GetWeb(@"http://1111.ip138.com/ic.asp", "219.239.236.49", 8888); File.AppendAllText(@"response.html", WebRequestUtil.responseBody, Encoding.UTF8); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "pass"); } catch (Exception exc2) { FailCount++; WriteErrorLog("Error:" + exc2.ToString()); } } } }


4. Php網頁屏蔽代理服務器代碼:

<?php $page= file_get_contents("blacklist.txt"); if(!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else{ $ip = $_SERVER['REMOTE_ADDR']; } echo $ip; if(strpos($page,$ip)!== false) echo "您使用代理閱讀我們的網站,很抱歉本站出于安全斟酌屏蔽了代理,請使用非代理閱讀,謝謝!"; else echo "履行程序的正常邏輯"; ?>


演示代碼:http://edu.laliyun.com/test.php

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产精品毛片无码 | 一级毛片短视频 | 国产农村一一级特黄毛片 | 麻豆国产免费看片在线播放 | 久久久久久久国产精品 | 色人阁色五月 | 日本在线中文 | 亚洲码欧美码一区二区三区 | 国产精品国产三级国产a | 亚洲美女色 | 97av视频| 亚洲毛片免费在线观看 | 性欧美17一18sex性高清 | 久久精品无码一区二区日韩av | 一区二区精品在线观看 | 久久精品蜜芽亚洲国产a | 逼逼自拍 | 欧美一级视频在线 | 亚洲免费观看网站 | 中文字幕第315页 | 欧美h视频在线观看 | 国产精品久久久久久网站 | 中文字幕25页| 五月天伊人网 | 国产国语一级毛片在线放 | 在线亚洲欧洲福利视频 | 欧美成人亚洲国产精品 | 日韩影院在线观看 | 亚洲免费视频在线观看 | 国产精品成人扳一级aa毛片 | h国产在线观看 | 亚洲综合欧美日本另类激情 | 250pp久久新 26uuu色噜噜欧美在线播放 | a级做爰毛片视频免费看 | 最近免费中文字幕大全高清mv | 免费观看成人www精品视频在线 | 国产一区二区三区在线看 | 精品一区二区三区 不卡高清 | 精品一精品国产一级毛片 | 久久精品国产999久久久 | 亚洲图片二区 |