Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. public static string csrf;
  2. CookieContainer c1 = new CookieContainer();
  3.  
  4. private void button1_Click(object sender, EventArgs e)
  5. {
  6. string PostData = String.Format("csrfmiddlewaretoken={0}&username=ra123&password=ra12345678",getToken());
  7.  
  8. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/");
  9. req.Method = "POST";
  10. req.ContentType = "application/x-www-form-urlencoded";
  11. req.KeepAlive = true;
  12. req.AllowAutoRedirect = true;
  13. req.CookieContainer = c1;
  14. byte[] byteArray = Encoding.ASCII.GetBytes(PostData);
  15. req.ContentLength = byteArray.Length;
  16. Stream dataStream = req.GetRequestStream();
  17. dataStream.Write(byteArray, 0, byteArray.Length);
  18. dataStream.Flush();
  19. dataStream.Close();
  20.  
  21. HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();
  22. Stream datastream = webResp.GetResponseStream();
  23. StreamReader reader = new StreamReader(datastream);
  24. string s = reader.ReadToEnd();
  25. MessageBox.Show(s);
  26. if (s.Contains("ra123"))
  27. {
  28.  
  29. MessageBox.Show("Loggedin");
  30.  
  31. }
  32. else
  33. {
  34. MessageBox.Show("Not");
  35. }
  36.  
  37. }
  38.  
  39.  
  40. string getToken()
  41. {
  42. string p = "<input type="hidden" name="csrfmiddlewaretoken" value="(.*)"/>";
  43. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/");
  44. req.Method = "GET";
  45. req.CookieContainer = c1;
  46.  
  47. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  48. Stream data = resp.GetResponseStream();
  49. StreamReader sr = new StreamReader(data);
  50. string src = sr.ReadToEnd();
  51.  
  52. Match m = Regex.Match(src, p);
  53. if (m.Success)
  54. {
  55. return (m.Groups[1].Value.ToString());
  56. }
  57. return false.ToString();
  58. }
  59.  
  60. WebResponse Response;
  61. HttpWebRequest Request;
  62. Uri url = new Uri("http://thewebpage.com:port/login/");
  63.  
  64. CookieContainer cookieContainer = new CookieContainer();
  65.  
  66. Request = (HttpWebRequest)WebRequest.Create(url);
  67. Request.Method = "GET";
  68. Request.CookieContainer = cookieContainer;
  69.  
  70. // Get the first response to obtain the cookie where you will find the "csrfmiddlewaretoken" value
  71. Response = Request.GetResponse();
  72.  
  73. string Parametros = "csrfmiddlewaretoken=" + cookieContainer.GetCookies(url)["csrftoken"].Value + "&username=USER&password=PASSWORD&next="; // This whill set the correct url to access
  74.  
  75. Request = (HttpWebRequest)WebRequest.Create(url); // it is important to use the same url used for the first request
  76. Request.Method = "POST";
  77. Request.ContentType = "application/x-www-form-urlencoded";
  78. Request.UserAgent = "Other";
  79. // Place the cookie container to obtain the new cookies for further access
  80. Request.CookieContainer = cookieContainer;
  81. Request.Headers.Add("Cookie",Response.Headers.Get("Set-Cookie")); // This is the most important step, you have to place the cookies at the header (without this line you will get the 403 Forbidden exception
  82.  
  83. byte[] byteArray = Encoding.UTF8.GetBytes(Parametros);
  84. Request.ContentLength = byteArray.Length;
  85.  
  86. Stream dataStream = Request.GetRequestStream();
  87. dataStream.Responseite(byteArray, 0, byteArray.Length);
  88. dataStream.Close();
  89.  
  90. Response = Request.GetResponse();
  91.  
  92. public static string csrf;
  93. CookieContainer c1 = new CookieContainer();
  94.  
  95. private void button1_Click(object sender, EventArgs e)
  96. {
  97. string PostData = String.Format("csrfmiddlewaretoken={0}&username=ra123&password=ra12345678", getToken());
  98.  
  99. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/");
  100. req.Method = "POST";
  101. req.ContentType = "application/x-www-form-urlencoded";
  102. req.KeepAlive = true;
  103. req.AllowAutoRedirect = true;
  104. req.CookieContainer = c1;
  105. byte[] byteArray = Encoding.ASCII.GetBytes(PostData);
  106. req.ContentLength = byteArray.Length;
  107. using (Stream dataStream = req.GetRequestStream())
  108. {
  109. dataStream.Write(byteArray, 0, byteArray.Length);
  110. dataStream.Flush();
  111. dataStream.Close();
  112. }
  113.  
  114. string s;
  115. using (HttpWebResponse webResp = (HttpWebResponse)req.GetResponse())
  116. {
  117. using (Stream datastream = webResp.GetResponseStream())
  118. {
  119. using (StreamReader reader = new StreamReader(datastream))
  120. {
  121. s = reader.ReadToEnd();
  122. }
  123. }
  124. }
  125. MessageBox.Show(s);
  126. if (s.Contains("ra123"))
  127. {
  128.  
  129. MessageBox.Show("Loggedin");
  130.  
  131. }
  132. else
  133. {
  134. MessageBox.Show("Not");
  135. }
  136.  
  137. }
  138.  
  139. string getToken()
  140. {
  141. string p = "<input type="hidden" name="csrfmiddlewaretoken" value="(.*)"/>";
  142. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/");
  143. req.Method = "GET";
  144. req.CookieContainer = c1;
  145.  
  146. string src;
  147. using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
  148. {
  149. using (Stream data = resp.GetResponseStream())
  150. {
  151. using (StreamReader sr = new StreamReader(data))
  152. {
  153. src = sr.ReadToEnd();
  154. }
  155. }
  156. }
  157.  
  158. Match m = Regex.Match(src, p);
  159. if (m.Success)
  160. {
  161. return (m.Groups[1].Value.ToString());
  162. }
  163. return false.ToString();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement