2013-07-29 118 views
0

我試圖在c#中獲取Facebook的驗證碼圖像,然後將其顯示在我的應用程序中,並將回覆發送回Facebook。我的應用程序工作正常,基本上與Facebook通信,但過了一段時間它要求驗證碼。我加入了輸入驗證碼的功能,當我使用webclient將它發送到Facebook時,我得到了它的響應。但是,當我再次嘗試查詢數據時,我又被引導至驗證碼鏈接,並且循環繼續。Facebook驗證碼

我只是無法弄清楚我是如何在隨後的請求中附加信息的,我已經輸入了captcha或從captcha響應頭返回了哪些信息,我應該附加到我的新請求中。

對不起,我英文很差。我希望我傳達了這個信息。

private bool checkNumber(string number) 
    { 
     int index = 0; 
     StreamReader streamReader; 
     NameValueCollection nameValuePairs = new NameValueCollection(); 
     nameValuePairs.Add("lsd", "AVoA7hUw"); 
     nameValuePairs.Add("email", number); 
     nameValuePairs.Add("did_submit", "Search"); 
     nameValuePairs.Add("__user", "0"); 
     nameValuePairs.Add("__a", "1"); 
     nameValuePairs.Add("__dyn", "7w"); 
     nameValuePairs.Add("__req", "1"); 
     nameValuePairs.Add("fb_dtsg", "AQCHtLfI"); 
     nameValuePairs.Add("phstamp", "1658167721167610273109"); 


     WebClient client = new WebClient(); 
     byte[] response = client.UploadValues("https://www.facebook.com/ajax/login/help/identify.php?ctx=recover", nameValuePairs); 
     char[] chars = new char[response.Length]; 
     for (int i = 0; i < response.Length; i++) 
     { 
      chars[i] = Convert.ToChar(response[i]); 
     } 
     string s = new string(chars); 
     if (s.Contains("onload")) 
     { 
      String coo = ""; 

      CookieContainer cookies = new CookieContainer(); 
      String[] cookieString = client.ResponseHeaders.Get("set-cookie").Split(';'); 

      string name = cookieString[0].Substring(0, cookieString[0].IndexOf("=")).TrimStart(); 
      string value = cookieString[0].Substring(cookieString[0].IndexOf("=") + 1).TrimEnd(';'); 
      Cookie cookie = new Cookie(name, value); 
      cookie.Domain = "www.facebook.com"; 
      cookies.Add(cookie); 
      coo += name + "=" + value + ";"; 

      Cookie newCookie = new Cookie("reg_fb_gate", "https%3A%2F%2Fwww.facebook.com%2F"); 
      newCookie.Domain = "www.facebook.com"; 
      cookies.Add(newCookie); 
      newCookie = new Cookie("reg_fb_ref", "https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover"); 
      newCookie.Domain = "www.facebook.com"; 

      coo += "reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F;reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover;"; 
      cookies.Add(newCookie); 

      name = cookieString[4].Substring(10, cookieString[4].LastIndexOf("=") - 10).TrimStart(); 
      value = cookieString[4].Substring(cookieString[4].IndexOf("=") + 1).TrimEnd(';'); 
      cookie = new Cookie(name, value); 
      cookie.Domain = "www.facebook.com"; 
      cookies.Add(cookie); 
      coo += name + "=" + value + ";"; 






      s = ""; 
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/recover/initiate"); 

      //req.Headers.Add("set-cookie", cookie); 
      req.Host = "www.facebook.com"; 
      req.CookieContainer = cookies; 
      req.Method = "GET"; 
      req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"; 
      req.KeepAlive = true; 
      WebResponse resp = req.GetResponse(); 
      streamReader = new StreamReader(resp.GetResponseStream()); 
      s = streamReader.ReadToEnd().Trim(); 
      if (s.Contains("sometext")) 
      { 
       return true; 
      } 
      while (s.Contains("captcha") || index == -1) 
      { 
       req = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/recover/initiate"); 
       req.Host = "www.facebook.com"; 
       req.CookieContainer = cookies; 
       req.Method = "GET"; 
       req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"; 
       req.KeepAlive = true; 
       resp = req.GetResponse(); 
       streamReader = new StreamReader(resp.GetResponseStream()); 
       s = streamReader.ReadToEnd().Trim(); 

       checkBox1.IsChecked = false; 

       index = s.IndexOf("https://www.facebook.com/captcha/tfbimage.php?"); 
       if (index == -1) 
        continue; 
       string uri = s.Substring(index); 
       uri = uri.Substring(0, uri.IndexOf("\\\"") - 1); 

       string captcha_persist_data = s.Substring(s.IndexOf("captcha_persist_data")); 
       captcha_persist_data = captcha_persist_data.Substring(s.IndexOf("value=\\\"") + 7); 
       captcha_persist_data = captcha_persist_data.Substring(0, s.IndexOf("\\\"") - 1); 
       client = new WebClient(); 
       coo += "wd=1920x976;act=1374854201395/3;_e_0oL1_0=%5B%220oL1%22%2C1375031079666%2C%22act%22%2C1375031079665%2C0%2C%22captcha_response%22%2C%22click%22%2C%22click%22%2C%22-%22%2C%22r%22%2C%22%2Frecover%2Finitiate%22%2C%7B%22ft%22%3A%7B%7D%2C%22gt%22%3A%7B%7D%7D%2C0%2C0%2C0%2C1903%2C16%5D;_e_0oL1_1=%5B%220oL1%22%2C1375031092251%2C%22act%22%2C1375031092250%2C1%2C%22captcha_submit%22%2C%22click%22%2C%22click%22%2C%22-%22%2C%22r%22%2C%22%2Frecover%2Finitiate%22%2C%7B%22ft%22%3A%7B%7D%2C%22gt%22%3A%7B%7D%7D%2C0%2C0%2C0%2C1903%2C16%5D"; 
       client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"); 
       client.Headers.Add("host", "www.facebook.com"); 
       client.Headers.Add("set-cookie", coo);      

       image1.Source = new BitmapImage(new Uri(uri)); 
       MessageBox.Show("Alert"); 
       streamReader = new StreamReader("D:\\abc.txt"); 
       string newStr = streamReader.ReadLine(); 
       streamReader.Close(); 



       nameValuePairs = new NameValueCollection(); 
       nameValuePairs.Add("lsd", "AVoA7hUw"); 
       /*nameValuePairs.Add("email", number); 
       nameValuePairs.Add("did_submit", "Search"); 
       nameValuePairs.Add("__user", "0"); 
       nameValuePairs.Add("__a", "1"); 
       nameValuePairs.Add("__dyn", "7w"); 
       nameValuePairs.Add("__req", "1"); 
       nameValuePairs.Add("fb_dtsg", "AQCHtLfI"); 
       nameValuePairs.Add("ttstamp", "26581656790668586"); */ 
       nameValuePairs.Add("captcha_persist_data", captcha_persist_data); 
       //nameValuePairs.Add("recaptcha_challenge_field", "");     

       nameValuePairs.Add("captcha_response", newStr); 
       //nameValuePairs.Add("confirmed", "1"); 
       nameValuePairs.Add("captcha_submit", "Submit"); 

       response = client.UploadValues("https://www.facebook.com/recover/initiate", nameValuePairs); 
       chars = new char[response.Length]; 
       for (int i = 0; i < response.Length; i++) 
       { 
        chars[i] = Convert.ToChar(response[i]); 
       } 
       s = new string(chars); 

       cookies = new CookieContainer(); 

       coo = "";      
       cookieString = client.ResponseHeaders.Get("set-cookie").Split(';'); 

       name = cookieString[0].Substring(0, cookieString[0].IndexOf("=")).TrimStart(); 
       value = cookieString[0].Substring(cookieString[0].IndexOf("=") + 1).TrimEnd(';'); 
       cookie = new Cookie(name, value); 
       cookie.Domain = "www.facebook.com"; 
       cookies.Add(cookie); 
       coo += name + "=" + value + ";"; 

       newCookie = new Cookie("reg_fb_gate", "https%3A%2F%2Fwww.facebook.com%2F"); 
       newCookie.Domain = "www.facebook.com"; 
       cookies.Add(newCookie); 
       newCookie = new Cookie("reg_fb_ref", "https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover"); 
       newCookie.Domain = "www.facebook.com"; 
       coo += "reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F;reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover;"; 


       cookies.Add(newCookie); 

       name = cookieString[4].Substring(10, cookieString[4].LastIndexOf("=") - 10).TrimStart(); 
       value = cookieString[4].Substring(cookieString[4].IndexOf("=") + 1).TrimEnd(';'); 
       cookie = new Cookie(name, value); 
       cookie.Domain = "www.facebook.com"; 
       cookies.Add(cookie); 
       coo += name + "=" + value + ";"; 
       coo+= "highContrastMode=0;wd=1920x976;"; 

       nameValuePairs = new NameValueCollection(); 
       nameValuePairs.Add("__user", "0"); 
       nameValuePairs.Add("__a", "1"); 
       nameValuePairs.Add("__dyn", "7w86i"); 
       nameValuePairs.Add("__req", "1"); 
       nameValuePairs.Add("fb_dtsg", "AQDdK6At"); 
       nameValuePairs.Add("q", "Miny1~66~%22%3A~%22%3A%22~%2C%22~%22%2C%22~.~null~0~%22%2C%7B%22~script_path_change~%5D%2C%5B%22~%22%3A%5B~time_spent~1~user~page_id~97dbjl~trigger~time~posts~%22%3A%5B%5B%22~source_path~WebRecoverInitiateController~source_token~01afab33~dest_path~dest_token~navigation~cause~%22%7D%2C~29183~time_spent_bit_array~tos_id~start_time~tos_array~%2C~%5D%2C%22~tos_len~tos_seq~tos_cum~%7D%2C~noxdt2~ods~%3A~ms~qa~www~15~4~%5B%7B%22~1375031063833~unload~1375031063~1053788547~31~21~%5D%5D%7D%2C%7B%22~1375031093606~load~1375031093~11~9597~bits~js_initialized~%5D%7D%2C~10599~%5D%5D%7D%5D~1MJxCzKxLzMxEzNw1NyOPEDQxRzSxTzUwByVwByWwByXx1OYZF-D_xLz1ww1Py1xG1Q1yC1z1Aw1Ry1BwIy1Cw1S1DZ1TJxCzKx1EzMx1F1G1HAHA1IA1JzNw1UyOPEDQwBySwByUxRzVxTzWwByXx1VY1KF-D_x1Ez1ww1Wy1xG1K1yC1z1Aw1Xy1BwCy1Cw1L1D1YF1F1G1HAHA1IA1JDHA1ZA1-GI1_2w2x"); 
       nameValuePairs.Add("ts", "1375031104207"); 
       nameValuePairs.Add("ph", "V3");  
       nameValuePairs.Add("miny_encode_ms", "6"); 

       client = new WebClient(); 
       client.Headers.Add("set-cookie", coo); 
       response = client.UploadValues("https://www.facebook.com/ajax/bz", nameValuePairs); 
       chars = new char[response.Length]; 
       for (int i = 0; i < response.Length; i++) 
       { 
        chars[i] = Convert.ToChar(response[i]); 
       } 
       s = new string(chars); 


       req = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/recover/initiate"); 


       //req.Headers.Add("set-cookie", cookie); 
       req.Host = "www.facebook.com"; 
       req.CookieContainer = cookies; 
       req.Method = "GET"; 
       req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"; 
       req.KeepAlive = true; 
       resp = req.GetResponse(); 
       streamReader = new StreamReader(resp.GetResponseStream()); 
       s = streamReader.ReadToEnd().Trim(); 
       if (s.Contains("sometext")) //example text to look for in the response 
       { 
        return true; 
       } 

      } 


     } 
     return false; 

回答

0

您應該比較您正在發送的數據與手動在瀏覽器中進行比較的情況。 我會特別檢查HTTP請求的頭字段。

+0

這就是我正在做的。我在檢查標題的同時提出了請求,然後將響應標題附加到我的下一個請求中。我正在通過firefox中的TemperData檢查標題。如果我發佈我的代碼給你,它會幫助你嗎? –

+0

是的,當然不是! –

+0

我已將代碼添加到原始帖子中。我基本上編寫了一個方法,它將一個數字作爲輸入,然後在Facebook上對其進行查詢,如果滿足'sometext'條件則返回true,如果我獲得了驗證碼,我將驗證碼圖像讀取,然後將驗證碼放入文件中我的程序讀取它並傳遞到Facebook。然後我再次發送驗證碼請求。我知道代碼太臃腫了。但現在我不想寫一個完整的應用程序,我現在只是在爲我的任務做一個PoC。 –