AlienVault OTX Python SDK

repository·master·Indexed 19 days ago

https://github.com/alienvault-otx/otx-python-sdk

A programmatic interface to the AlienVault Open Threat Exchange (OTX) that allows developers to automate the retrieval of indicators of compromise (IOCs) from subscribed pulses and contribute new threat intelligence. The SDK provides the OTXv2 class for interacting with the API, supporting operations such as searching for pulses, retrieving indicator details, and creating new pulses with various supported indicator types including IPv4, IPv6, Domain, and FileHashes.

Tokens
2K
Snippets
11
Records
11
Agent score
14%

What's inside otx-python-sdk

  1. Install the OTXv2 Python SDK

    master

    You can install the SDK directly from PyPI using pip. Alternatively, you can install it from the source by cloning the repository and running the setup script.

    pip install OTXv2

    Or from source

    # From the root directory of the cloned repo
    pip install .
    # OR
    python setup.py install
  2. Set up OTX with a Python Notebook

    master

    To use the SDK within a Jupyter Notebook environment, follow these steps:

    1. Clone the repository.
    2. Install pandas and jupyter.
    3. Launch the provided notebook howto_use_python_otx_api.ipynb.
    pip install pandas
    pip install jupyter
    
    # Run the notebook

    jupyter notebook howto_use_python_otx_api.ipynb

  3. Read indicators and details from OTX

    master

    Use the OTXV2 class to interact with the Open Threat Exchange. You can retrieve all indicators associated with a specific pulse ID or fetch full details for a specific indicator type (e.g., Domain, IP, etc.).

    from OTXv2 import OTXv2
    from OTXv2 import IndicatorTypes
    
    # Initialize with your API Key
    otx = OTXv2("API_KEY")
    
    # Get all the indicators associated with a pulse
    indicators = otx.get_pulse_indicators("pulse_id")
    for indicator in indicators:
        print indicator["indicator"] + indicator["type"]
    
    # Get everything OTX knows about a specific domain
    otx.get_indicator_details_full(IndicatorTypes.DOMAIN, "google.com")
  4. Create a new pulse in OTX

    master

    You can programmatically create a new pulse by providing a name, visibility (public/private), and a list of indicators. Each indicator in the list must be a dictionary containing the indicator value and its type.

    from OTXv2 import OTXv2
    
    otx = OTXv2("API_KEY")
    name = 'Test Pulse'
    indicators = [
        {'indicator': '69.73.130.198', 'type': 'IPv4'},
        {'indicator': 'aoldaily.com', 'type': 'Domain'}
    ]
    
    # Create the pulse
    response = otx.create_pulse(name=name, public=True, indicators=indicators, tags=[], references=[])
    print str(response)
  5. Access subscribed pulses and indicators with getall()

    master

    The getall() method retrieves all pulses and their associated Indicators of Compromise (IOCs) that you are subscribed to. This includes:

    • Pulses you subscribe to directly.
    • Pulses from users you follow.
    • Pulses you created (including private ones).
    • All pulses created by AlienVault (by default for new accounts).

    Each pulse object contains metadata such as author_name, created, description, id, indicators, modified, name, references, revision, and tags.

    pulses = otx.getall()
  6. Retrieve system events since a specific time

    master

    Use getevents_since(mtime) to retrieve events occurring in the OTX system that affect your account. This is useful for reconciling local data with the server when users subscribe/unsubscribe or delete pulses.

    Event Fields:

    • id: Unique reference identifier.
    • action: One of [subscribe | unsubscribe | delete].
    • object_type: One of [pulse | user].
    • object_id: The unique ID of the pulse or author.
    • created: Timestamp of the event.
    # Example using an ISO format timestamp
    mtime = "2023-01-01T00:00:00"
    events = otx.getevents_since(mtime)
  7. Get full details for an indicator

    master

    Use get_indicator_details_full(indicator_type, indicator) to retrieve comprehensive information about a specific IOC. You must provide the indicator type using the IndicatorTypes class.

    Note: Detailed information is not available for all indicator types. Check IndicatorTypes.supported_api_types to see which types support full detail retrieval.

    from OTXv2 import IndicatorTypes
    
    # Example: Getting details for an IPv4 indicator
    indicator_value = "82.194.84.121"
    details = otx.get_indicator_details_full(IndicatorTypes.IPv4, indicator_value)
  8. Search for pulses and retrieve specific pulse details

    master

    You can search for pulses and users by keyword using search_pulses(), which allows you to find content you are not yet subscribed to. To get the full details (including all indicators) for a specific pulse found in the search, use get_pulse_details(pulse_id).

    # Search for pulses containing a keyword
    search_results = otx.search_pulses("Russian")
    
    # Get details for a specific pulse ID from the results
    # Note: search_pulses returns a dict with a 'results' key
    pulse_id = search_results["results"][0]["id"]
    
    details = otx.get_pulse_details(pulse_id)
  9. Create a new pulse

    master

    Use create_pulse() to upload a new pulse to OTX.

    Required Parameters:

    • name (string): The name of the pulse.
    • indicators (list of objects): A list of IOC dictionaries. Each dictionary must contain indicator, description, and type (e.g., "IPv4").

    Optional Parameters:

    • public (boolean): Whether the pulse is public. Defaults to True if not provided.
    • description (string): Long form description of the threat.
    • tlp (string): Traffic Light Protocol level (white, green, amber, red).
    • tags (list of strings): Keywords for the pulse.
    • references (list of strings): URLs or external references.
    indicators = [
        {"indicator": "82.194.84.121", "description": "", "type": "IPv4"},
        {"indicator": "82.194.84.122", "description": "", "type": "IPv4"}
    ]
    
    new_pulse = otx.create_pulse(
        name="IPy Notebook Test", 
        indicators=indicators, 
        public=False
    )
  10. Reference: Supported Indicator Types

    master

    The following indicator types are supported by the SDK and are defined in the IndicatorTypes class:

    • IPv4 / IPv6: IP addresses.
    • domain / hostname: Domain and host information.
    • email: Suspicious email addresses.
    • URL / URI: Web locations and paths.
    • FileHash-MD5 / FileHash-SHA1 / FileHash-SHA256 / FileHash-PEHASH / FileHash-IMPHASH: Various file hash formats.
    • CIDR: Network architecture/routing paths.
    • FilePath: File system locations.
    • Mutex: Mutex resource names.
    • CVE: Common Vulnerability and Exposure entries.
    # These are accessible via the IndicatorTypes class
    # e.g., IndicatorTypes.IPv4, IndicatorTypes.domain, etc.