Advertisement
Guest User

curl gue

a guest
Aug 4th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <?php
  2. /*$url = "http://localhost:8080/Default.aspx";
  3. $ckfile = tempnam("/tmp", "CURLCOOKIE");
  4. $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';
  5.  
  6. $username = "gianrachmat@gmail.com";
  7. $password = "st21it1p0";
  8.  
  9.  
  10. $f = fopen('log.txt', 'w'); // file to write request header for debug purpose
  11.  
  12. /**
  13. Get __VIEWSTATE & __EVENTVALIDATION
  14.  
  15. $ch = curl_init($url);
  16. curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
  17. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  20.  
  21. $html = curl_exec($ch);
  22.  
  23. curl_close($ch);
  24.  
  25. preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
  26. preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
  27.  
  28. $viewstate = $viewstate[1];
  29. $eventValidation = $eventValidation[1];
  30.  
  31.  
  32.  
  33. /**
  34. Start Login process
  35.  
  36. $ch = curl_init();
  37.  
  38. curl_setopt($ch, CURLOPT_URL, $url);
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  41. curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
  42. curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
  43. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  44. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  45. curl_setopt($ch, CURLOPT_REFERER, $url);
  46. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  47. curl_setopt($ch, CURLOPT_STDERR, $f);
  48. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  49.  
  50. // Collecting all POST fields
  51. $postfields = array();
  52. $postfields['__EVENTTARGET'] = "";
  53. $postfields['__EVENTARGUMENT'] = "";
  54. $postfields['__VIEWSTATE'] = $viewstate;
  55. $postfields['__EVENTVALIDATION'] = $eventValidation;
  56. $postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInEmail'] = $username;
  57. $postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPasswordTextNormal'] = "Password";
  58. $postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$txtMPTopSignInPassword'] = $password;
  59. $postfields['ctl00$ctl00$ucMarketPlaceSupportNavigation$btnSigninTop'] = 'Sign in';
  60. $postfields['ctl00$ctl00$cplhMain$cplhContent$txtEmail'] = 'Email address';
  61. $postfields['ctl00$ctl00$cplhMain$cplhContent$rdlPasswordYes'] = 'Password';
  62. $postfields['ctl00$ctl00$cplhMain$cplhContent$txtPassword'] = '';
  63. $postfields['ctl00$ctl00$cplhMain$cplhContent$hdnEmailDefault'] = 'Email address';
  64. $postfields['ctl00$ctl00$cplhMain$cplhContent$hdnPasswordDefault'] = 'Password';
  65.  
  66. curl_setopt($ch, CURLOPT_POST, 1);
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  68. $ret = curl_exec($ch); // Get result after login page.
  69.  
  70. print $ret;*/
  71. function get_headers_from_curl_response($headerContent)
  72. {
  73.  
  74. $headers = array();
  75.  
  76. // Split the string on every "double" new line.
  77. $arrRequests = explode("\r\n\r\n", $headerContent);
  78.  
  79. // Loop of response headers. The "count() -1" is to
  80. //avoid an empty row for the extra line break before the body of the response.
  81. for ($index = 0; $index < count($arrRequests) -1; $index++) {
  82.  
  83. foreach (explode("\r\n", $arrRequests[$index]) as $i => $line)
  84. {
  85. if ($i === 0)
  86. $headers[$index]['http_code'] = $line;
  87. else
  88. {
  89. list ($key, $value) = explode(': ', $line);
  90. $headers[$index][$key] = $value;
  91. }
  92. }
  93. }
  94.  
  95. return $headers;
  96. }
  97. function regexExtract($text, $regex, $regs, $nthValue)
  98. {
  99. if (preg_match($regex, $text, $regs)) {
  100. $result = $regs[$nthValue];
  101. }
  102. else {
  103. $result = "";
  104. }
  105. return $result;
  106. }
  107. $regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
  108. $regexEventVal = '/__EVENTVALIDATION\" value=\"(.*)\"/i';
  109.  
  110. $ch = curl_init("http://localhost:8080/Default.aspx");
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  112. curl_setopt($ch, CURLOPT_HEADER, 1);
  113. curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
  114.  
  115. $response = curl_exec($ch);
  116. curl_close($ch);
  117.  
  118. $viewstate = regexExtract($response,$regexViewstate,$regs,1);
  119. $eventval = regexExtract($response, $regexEventVal,$regs,1);
  120.  
  121. $params = array(
  122. '__EVENTTARGET' => '',
  123. '__EVENTARGUMENT' => '',
  124. '__VIEWSTATE' => $viewstate,
  125. '__EVENTVALIDATION' => $eventval,
  126. 'ctl00$txtUsername' => 'gianrachmat@gmail.com',
  127. 'ctl00$txtPassword' => 'st21it1p0',
  128. 'ctl00$ImgLogin.x' => '0',
  129. 'ctl00$ImgLogin.y' => '0',
  130. );
  131.  
  132. $ch2 = curl_init("http://localhost:8080/Default.aspx");
  133. curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  134. curl_setopt($ch2, CURLOPT_HEADER, 1);
  135. curl_setopt ($ch2, CURLOPT_POST, true);
  136. curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
  137. curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($params));
  138. curl_setopt ($ch2, CURLOPT_COOKIE,'cookies.txt');
  139. curl_setopt($ch2,CURLOPT_COOKIEJAR,'cookies2.txt');
  140.  
  141. $response2 = curl_exec($ch2);
  142. curl_close($ch2);
  143.  
  144. foreach(get_headers_from_curl_response($response2) as $value)
  145. {
  146. foreach($value as $key => $value2)
  147. {
  148. echo $key . ": " .$value2 . "<br />";
  149. }
  150. }
  151. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement