Advertisement
x2Fusion

Shodan.io Scraper

Feb 7th, 2016
2,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. <?php
  2.  
  3. $query = 'ShodanQuery';
  4.  
  5. $loginArgs = array(
  6.     'username'      => 'shodan_username',
  7.     'password'      => 'shodan_password',
  8.     'grant_type'    => 'password',
  9.     'continue'      => 'https://account.shodan.io/',
  10.     'login_submit'  => 'Log+in'
  11. );
  12.  
  13. fetchUrl('https://account.shodan.io/login', $loginArgs);
  14.  
  15. $summary = fetchUrl("https://www.shodan.io/search/_summary?query=$query");
  16.  
  17. preg_match_all('/<div class="span8 name"><a title="(.*)" href=".*<\/a>/', $summary, $matches);
  18.  
  19. $countries = array(
  20.     $matches[1][0],
  21.     $matches[1][1],
  22.     $matches[1][2],
  23.     $matches[1][3],
  24.     $matches[1][4]
  25. );
  26.  
  27. $pageMax = fetchUrl("http://www.shodan.io/search?query=$query&page=6");
  28.  
  29. $pageMax = strpos($pageMax, 'more than 5 pages') ? 5 : 5000;   
  30.  
  31. foreach($countries as $country)
  32. {
  33.     for($pageNum = 1; $pageNum <= $pageMax; $pageNum++)
  34.     {
  35.         $pageData = fetchUrl("http://www.shodan.io/search?query=$query+country:$country&page=$pageNum");
  36.  
  37.         preg_match_all('/<div class="ip"><a href="(.*)">.*<\/a>.*/', $pageData, $matches); 
  38.  
  39.         if(count($matches[1]) == 0)
  40.         {
  41.             break;
  42.         }
  43.  
  44.         foreach($matches[1] as $match)
  45.         {
  46.             $return[$country][] = $match;
  47.         }
  48.     }
  49.    
  50.     if(count($matches[1]) == 0)
  51.     {
  52.         break;
  53.     }
  54. }
  55.  
  56. echo serialize($return);
  57.  
  58. // FetchUrl Function
  59. function fetchUrl($url, $data = '', $referer = '')
  60. {
  61.     $ch = curl_init();
  62.     curl_setopt($ch, CURLOPT_URL, $url);   
  63.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");
  64.     curl_setopt($ch, CURLOPT_REFERER, $referer);
  65.     curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookie');
  66.     curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookie');
  67.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  68.  
  69.     if(is_array($data))
  70.     {
  71.         $fields = '';
  72.         foreach($data as $key => $value)
  73.     {
  74.             $fields .= $key . '=' . $value . '&';
  75.         }
  76.     rtrim($fields, '&');
  77.  
  78.     curl_setopt($ch, CURLOPT_POST, count($data));
  79.         curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  80.     }
  81.    
  82.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  83.    $result = curl_exec($ch);
  84.    curl_close($ch);
  85.    
  86.    return $result;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement