Googleinurl

[SCRIPT]=> Shodan.class.php

Aug 19th, 2014
1,655
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. # https://github.com/haqqur/shodan-rest-api-wrapper/blob/master/src/shodan.class.php
  3. namespace Shodan;
  4.  
  5. class Shodan {
  6.  
  7.     private $shodan_stream;
  8.     private $shodan_shodan;
  9.     private $shodan_tools;
  10.     private $shodan_dns;
  11.     private $shodan;
  12.     private $apikey;
  13.  
  14.  
  15.     /**
  16.      * Construct.
  17.      *
  18.      * @param string $apikey;
  19.      * @return void;
  20.      */
  21.     public function __construct($apikey) {
  22.         $this->shodan_stream = 'https://stream.shodan.io/shodan';
  23.         $this->shodan_shodan = 'https://api.shodan.io/shodan';
  24.         $this->shodan_tools = 'https://api.shodan.io/tools';
  25.         $this->shodan_dns = 'https://api.shodan.io/dns';
  26.         $this->shodan = 'https://api.shodan.io';
  27.         $this->apikey = $apikey;
  28.         define('RETURN_TYPE', false); // false = object; true = array
  29.     }
  30.  
  31.     /**
  32.      * Send an HTTP request using libcurl.
  33.      *
  34.      * @param string $target;
  35.      * @return string $response;
  36.      */
  37.     public function curl_req($target) {
  38.         $curl = curl_init();
  39.         curl_setopt($curl, CURLOPT_URL, $target);
  40.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  41.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  42.         $response = curl_exec($curl);
  43.         curl_close($curl);
  44.         return $response;
  45.     }
  46.  
  47.     /**
  48.      * Returns all services that have been found on the given host IP.
  49.      *
  50.      * @param string $ip;
  51.      * @param bool $history;
  52.      * @param bool $minify;
  53.      * @return object/array $req;
  54.      */
  55.     public function host($ip, $history = false, $minify = false) {
  56.         $req = $this->curl_req("{$this->shodan_shodan}/host/{$ip}?key={$this->apikey}&history={$history}&minify={$minify}");
  57.         return json_decode($req, RETURN_TYPE);
  58.     }
  59.  
  60.     /**
  61.      * Return the total number of results that match the query and any facet information.
  62.      *
  63.      * @param string $ip;
  64.      * @param string $facets;
  65.      * @return object/array $req;
  66.      */
  67.     public function host_count($ip, $facets = false) {
  68.         if($facets) { $req = $this->curl_req("{$this->shodan_shodan}/host/count?key={$this->apikey}&query={$ip}&facets={$facets}"); }
  69.         else { $req = $this->curl_req("{$this->shodan_shodan}/host/count?key={$this->apikey}&query={$ip}"); }
  70.         return json_decode($req, RETURN_TYPE);
  71.     }
  72.  
  73.     /**
  74.      * Search Shodan using the same query syntax as the website and use facets to get summary information for different properties.
  75.      *
  76.      * @param string $query;
  77.      * @param string $facets;
  78.      * @return object/array $req;
  79.      */
  80.     public function host_search($query, $page = 1, $facets = false, $minify = false) {
  81.         if($facets) { $req = $this->curl_req("{$this->shodan_shodan}/host/search?key={$this->apikey}&query={$query}&facets={$facets}&page={$page}&minify={$minify}"); }
  82.         else { $req = $this->curl_req("{$this->shodan_shodan}/host/search?key={$this->apikey}&query={$query}"); }
  83.         return json_decode($req, RETURN_TYPE);
  84.     }
  85.  
  86.     /**
  87.      * Determine which filters are being used by the query string and what parameters were provided to the filters.
  88.      *
  89.      * @param string $query;
  90.      * @return object/array $req;
  91.      */
  92.     public function token_search($query) {
  93.         $req = $this->curl_req("{$this->shodan_shodan}/host/search/tokens?key={$this->apikey}&query={$query}");
  94.         return json_decode($req, RETURN_TYPE);
  95.     }
  96.  
  97.     /**
  98.      * Return an object containing all the services that the Shodan crawlers look at.
  99.      *
  100.      * @return object/array $req;
  101.      */
  102.     public function services() {
  103.         $req = $this->curl_req("{$this->shodan_shodan}/services?key={$this->apikey}");
  104.         return json_decode($req, RETURN_TYPE);
  105.     }
  106.  
  107.     /**
  108.      * Look up the IP address for the provided list of hostnames.
  109.      *
  110.      * @param string $hostname;
  111.      * @return object/array $req;
  112.      */
  113.     public function dns_resolve($hostname) {
  114.         $req = $this->curl_req("{$this->shodan_dns}/resolve?hostnames={$hostname}&key={$this->apikey}");
  115.         return json_decode($req, RETURN_TYPE);
  116.     }
  117.  
  118.     /**
  119.      * Look up the hostnames that have been defined for the given list of IP addresses.
  120.      *
  121.      * @param string $ip;
  122.      * @return object/array $req;
  123.      */
  124.     public function dns_reverse($ip) {
  125.         $req = $this->curl_req("{$this->shodan_dns}/reverse?ips={$ip}&key={$this->apikey}");
  126.         return json_decode($req, RETURN_TYPE);
  127.     }
  128.  
  129.     /**
  130.      * Get your current IP address as seen from the Internet.
  131.      *
  132.      * @return object/array $req;
  133.      */
  134.     public function myip() {
  135.         $req = $this->curl_req("{$this->shodan_tools}/myip?key={$this->apikey}");
  136.         return json_decode($req, RETURN_TYPE);
  137.     }
  138.  
  139.     /**
  140.      * Returns information about the API plan belonging to the given API key.
  141.      *
  142.      * @return object/array $req;
  143.      */
  144.     public function api_info() {
  145.         $req = $this->curl_req("{$this->shodan}/api-info?key={$this->apikey}");
  146.         return json_decode($req, RETURN_TYPE);
  147.     }
  148.  
  149.     /**
  150.      * Return all of the data that Shodan collects. (subscription required)
  151.      *
  152.      * @return object/array $req;
  153.      */
  154.     public function banners() {
  155.         $req = $this->curl_req("{$this->shodan_stream}/banners?key={$this->apikey}");
  156.         return json_decode($req, RETURN_TYPE);
  157.     }
  158.  
  159.     /**
  160.      *  Return geolocation data for all of the collected data by Shodan. (subscription required)
  161.      *
  162.      * @return object/array $req;
  163.      */
  164.     public function geo() {
  165.         $req = $this->curl_req("{$this->shodan_stream}/geo?key={$this->apikey}");
  166.         return json_decode($req, RETURN_TYPE);
  167.     }
  168.  
  169.     /**
  170.      *  Returns banner data for the list of specified hosts. (subscription required)
  171.      *
  172.      * @return object/array $req;
  173.      */
  174.     public function ports($port) {
  175.         $req = $this->curl_req("{$this->shodan_stream}/ports/{$port}?key={$this->apikey}");
  176.         return json_decode($req, RETURN_TYPE);
  177.     }
  178.  
  179. }
RAW Paste Data