ShodanCVE.psm1

# ShodanCVE.psm1
<#
.SYNOPSIS
    PowerShell module for querying the Shodan CVE Database API.
 
.DESCRIPTION
    This module implements the current endpoints from https://cvedb.shodan.io/,
    where no API key is required or mentioned in the docs.
 
    Endpoints used:
      - /cve/<CVE_ID>
      - /cves?vendor=<vendor>
      - /cves?product=<product>
      - /cves?cpe=<cpe_string>
      - /cves?search=<keyword>
 
.VERSION
    1.0.0
#>


# ----------------------------------------------------------------------
# 1) Single CVE
function Get-ShodanCVE {
    param(
        [Parameter(Mandatory)]
        [string]$CVE_ID
    )

    # According to https://cvedb.shodan.io/docs#/
    # We just call GET /cve/<CVE_ID> with no key param.
    $url = "https://cvedb.shodan.io/cve/$CVE_ID"

    try {
        return Invoke-RestMethod -Uri $url -Method GET
    }
    catch {
        Write-Error "Error fetching CVE: $_"
    }
}

# ----------------------------------------------------------------------
# 2) Search by vendor
function Search-ShodanCVEsByVendor {
    param(
        [Parameter(Mandatory)]
        [string]$Vendor
    )

    # GET /cves?vendor=<vendor>
    $url = "https://cvedb.shodan.io/cves?vendor=$Vendor"

    try {
        return Invoke-RestMethod -Uri $url -Method GET
    }
    catch {
        Write-Error "Error searching CVEs by vendor: $_"
    }
}

# ----------------------------------------------------------------------
# 3) Search by product
function Search-ShodanCVEsByProduct {
    param(
        [Parameter(Mandatory)]
        [string]$ProductName
    )

    # GET /cves?product=<product>
    $url = "https://cvedb.shodan.io/cves?product=$ProductName"

    try {
        return Invoke-RestMethod -Uri $url -Method GET
    }
    catch {
        Write-Error "Error searching CVEs by product: $_"
    }
}

# ----------------------------------------------------------------------
# 4) Search by CPE
function Search-ShodanCVEsByCPE {
    param(
        [Parameter(Mandatory)]
        [string]$CPE
    )

    # GET /cves?cpe=<cpe_string>
    $url = "https://cvedb.shodan.io/cves?cpe=$CPE"

    try {
        return Invoke-RestMethod -Uri $url -Method GET
    }
    catch {
        Write-Error "Error searching CVEs by CPE: $_"
    }
}

# ----------------------------------------------------------------------
# 5) Search by arbitrary keyword
function Search-ShodanCVEsByKeyword {
    param(
        [Parameter(Mandatory)]
        [string]$Keyword
    )

    # GET /cves?search=<keyword>
    $url = "https://cvedb.shodan.io/cves?search=$Keyword"

    try {
        return Invoke-RestMethod -Uri $url -Method GET
    }
    catch {
        Write-Error "Error searching CVEs by keyword: $_"
    }
}

# ----------------------------------------------------------------------
# Export all relevant functions
Export-ModuleMember -Function *-ShodanCVE*