C#反爬虫之CSRF
2021-09-22 11:33:07

最近写爬虫遇到一个网站带有防止CSRF攻击机制,该接口为POST请求,用PostMan测试后发现需要请求头的Cookie和FormData里面的一个_token参数才能发起正确的请求,这两个参数缺一不可而且有效时间只有一天,因为爬虫是做成定时任务的,每天在一个时间点都会自己运行,这样的话每天都要更换Cookie和Token太麻烦,摸索了一段时间后发现可以破解此网页的CSRF:

第一步是Cookie会过期,所以先获取Cookie:

首先请求该网页的任意一个Get请求要能成功的,然后然后获取响应内容的SetCookie,再将SetCookie放到POST请求中的CookieContainer中即可;

public static CookieContainer CookieContainer = new CookieContainer();
public static void GetCookieAndToken()
        {
            Encoding encoding = Encoding.UTF8;
            string requestUrlString = @"https://www.pproperties.com.hk/tc";
            //向服务端请求
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.CookieContainer = new CookieContainer();
            //将请求的结果发送给客户端(界面、应用)
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            CookieContainer.Add(myResponse.Cookies);
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding);
            var htmlStr = reader.ReadToEnd();
            HtmlDocument html = new HtmlDocument();
            html.LoadHtml(htmlStr);
            HtmlNodeCollection node = html.DocumentNode.SelectNodes(@"//meta[@name='csrf-token']");
            if (node != null)
            {
                Token = node.First().Attributes["content"].Value;
            }
            else
            {
                GetCookieAndToken();
            }
        }
View Code

这里请求用的是HttpWebRequest因为HttpWebResponse拿到的Cookie能直接添加到CookieContainer,如果用HttpClient请求的话拿到的Cookie是IENumberable<string>类型的,(最终的POST请求)这里因为数据过多需要用到异步所以考虑HttpClient,而HttpClient中设置Cookie的方式为CookieContainer。

所以这里是先用HttpWebRequest获取CookieContainer然后再用得到CookieContainer添加到HttpHandler中用HttpClient发起Post请求,这么复杂是因为我还没找到将IENumberable<string>转为CookieContainer。

第二步是获取Token(代码在上面获取Cookie里面)

能破解CSRF是因为在网页找到隐藏的Token:

 

 获取Token:

 

 这里解析Html用的是HtmlAgilityPack

上面文字很绕口所以下面有图文解析:

 

 

 最后用Post请求数据:

 HttpClientHandler handler = new HttpClientHandler() { CookieContainer = CookieContainer};
                using (HttpClient client = new HttpClient(handler))
                {
                    HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, postUrl);
                    message.Headers.Add("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
                    viewdata += Token;
                    var dic = FormDataToDictionary(viewdata);
                    message.Content = new FormUrlEncodedContent(dic);
                    HttpResponseMessage response = client.SendAsync(message).Result;
                    return response.Content.ReadAsStringAsync().Result;
                }
View Code

HttpContent格式:

MultipartFormDataContent   =>  multipart/form-data

FormUrlEncodedContent   =>  application/x-www-form-urlencoded

StringContent       =>  application/json等

StreamContent        =>    binary

FormUrlEncodedContent()方法里的键值对只能一个一个加上去,这里我封装了一个方法一次性加上去:

      VS  

 

 

//viewparse转成Dictionary
        static Dictionary<string, string> FormDataToDictionary(string viewParse_Encode)
        {
            var viewParse = System.Web.HttpUtility.UrlDecode(viewParse_Encode, System.Text.Encoding.UTF8);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            var arr = viewParse.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var a in arr)
            {
                var arr2 = a.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                if (arr2.Length == 1)
                {
                    string s = string.Empty;
                    dic.Add(arr2[0], s);
                }
                else
                    dic.Add(arr2[0], arr2[1]);
            }
            return dic;
        }
View Code

 

参考:

C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用 - 黄聪 - 博客园 (cnblogs.com)

C#中使用HttpClient来Post数据的内容HttpContent的各种格式 - 齐建伟 - 博客园 (cnblogs.com)

 

本文摘自 :https://www.cnblogs.com/


更多科技新闻 ......