コード例 #1
0
    def _create_stream(self, name, timeout=None):
        try:
            req = requests.get(self.base_url + name, params={'key': self.api_key}, stream=True, timeout=timeout)
        except:
            raise exception.APIError('Unable to contact the Shodan Streaming API')

        if req.status_code != 200:
            try:
                raise exception.APIError(data.json()['error'])
            except:
                pass
            raise exception.APIError('Invalid API key or you do not have access to the Streaming API')
        return req
コード例 #2
0
    def _request(self, function, params, service='shodan', method='get'):
        """General-purpose function to create web requests to SHODAN.
        
        Arguments:
            function  -- name of the function you want to execute
            params    -- dictionary of parameters for the function
        
        Returns
            A dictionary containing the function's results.
        
        """
        # Add the API key parameter automatically
        params['key'] = self.api_key

        # Determine the base_url based on which service we're interacting with
        base_url = {
            'shodan': self.base_url,
            'exploits': self.base_exploits_url,
        }.get(service, 'shodan')

        # Send the request
        try:
            if method.lower() == 'post':
                data = requests.post(base_url + function, params)
            else:
                data = requests.get(base_url + function, params=params)
        except:
            raise exception.APIError('Unable to connect to Shodan')

        # Check that the API key wasn't rejected
        if data.status_code == 401:
            try:
                raise exception.APIError(data.json()['error'])
            except:
                pass
            raise exception.APIError('Invalid API key')

        # Parse the text into JSON
        try:
            data = data.json()
        except:
            raise exception.APIError('Unable to parse JSON response')

        # Raise an exception if an error occurred
        if type(data) == dict and data.get('error', None):
            raise exception.APIError(data['error'])

        # Return the data
        return data
コード例 #3
0
class Stream:

    base_url = 'https://stream.shodan.io'

    def __init__(self, api_key):
        self.api_key = api_key

    def _create_stream(self, name, timeout=None):
        try:
            req = requests.get(self.base_url + name,
                               params={'key': self.api_key},
                               stream=True,
                               timeout=timeout)
        except Exception, e:
            raise exception.APIError(
                'Unable to contact the Shodan Streaming API')

        if req.status_code != 200:
            try:
                data = simplejson.loads(req.text)
                raise exception.APIError(data['error'])
            except exception.APIError, e:
                raise
            except Exception, e:
                pass
コード例 #4
0
 def _create_stream(self, name, timeout=None):
     try:
         req = requests.get(self.base_url + name,
                            params={'key': self.api_key},
                            stream=True,
                            timeout=timeout)
     except Exception, e:
         raise exception.APIError(
             'Unable to contact the Shodan Streaming API')
コード例 #5
0
ファイル: stream.py プロジェクト: Pygmalion6636/shodan-python
    def alert(self, aid=None, timeout=None):
        if aid:
            stream = self._create_stream('/shodan/alert/%s' % aid,
                                         timeout=timeout)
        else:
            stream = self._create_stream('/shodan/alert', timeout=timeout)

        try:
            for line in stream.iter_lines():
                if line:
                    banner = simplejson.loads(line)
                    yield banner
        except requests.exceptions.ConnectionError, e:
            raise exception.APIError('Stream timed out')
コード例 #6
0
                               stream=True,
                               timeout=timeout)
        except Exception, e:
            raise exception.APIError(
                'Unable to contact the Shodan Streaming API')

        if req.status_code != 200:
            try:
                data = simplejson.loads(req.text)
                raise exception.APIError(data['error'])
            except exception.APIError, e:
                raise
            except Exception, e:
                pass
            raise exception.APIError(
                'Invalid API key or you do not have access to the Streaming API'
            )
        return req

    def alert(self, aid=None, timeout=None, raw=False):
        if aid:
            stream = self._create_stream('/shodan/alert/%s' % aid,
                                         timeout=timeout)
        else:
            stream = self._create_stream('/shodan/alert', timeout=timeout)

        try:
            for line in stream.iter_lines():
                if line:
                    if raw:
                        yield line