| 1 |
Usage v2 |
| 2 |
======== |
| 3 |
|
| 4 |
The Censys Search API provides functionality for interacting with Censys resources such as Hosts. |
| 5 |
|
| 6 |
There are three main API endpoints that this library provides access to: |
| 7 |
|
| 8 |
- :attr:`search <censys.search.v2.api.CensysSearchAPIv2.search>` - Allows searches against the Hosts index using the same search syntax as the `web app <https://search.censys.io/search/language?resource=hosts>`__. |
| 9 |
- :attr:`view <censys.search.v2.api.CensysSearchAPIv2.view>` - Returns the structured data we have about a specific Host, given the resource's natural ID. |
| 10 |
- :attr:`aggregate <censys.search.v2.api.CensysSearchAPIv2.aggregate>` - Allows you to view resources as a spectrum based on attributes of the resource, similar to the `Report Builder page <https://search.censys.io/search/report?resource=hosts>`__ on the web app. |
| 11 |
|
| 12 |
More details about each option can be found in the `Censys API documentation <https://search.censys.io/api>`__. A list of index fields can be found in the `Censys API definitions page <https://search.censys.io/api>`__. |
| 13 |
|
| 14 |
Python class objects must be initialized for each resource index (Hosts). |
| 15 |
|
| 16 |
- :attr:`CensysHosts <censys.search.v2.CensysHosts>` |
| 17 |
- :attr:`CensysCerts <censys.search.v2.CensysCerts>` |
| 18 |
|
| 19 |
|
| 20 |
``search`` |
| 21 |
---------- |
| 22 |
|
| 23 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 24 |
|
| 25 |
.. include:: ../examples/search/search_hosts.py |
| 26 |
:literal: |
| 27 |
|
| 28 |
``view`` |
| 29 |
-------- |
| 30 |
|
| 31 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 32 |
|
| 33 |
.. include:: ../examples/search/view_host.py |
| 34 |
:literal: |
| 35 |
|
| 36 |
``bulk_view`` |
| 37 |
------------- |
| 38 |
|
| 39 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 40 |
|
| 41 |
.. include:: ../examples/search/bulk_view_hosts.py |
| 42 |
:literal: |
| 43 |
|
| 44 |
``aggregate`` |
| 45 |
------------- |
| 46 |
|
| 47 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 48 |
|
| 49 |
.. include:: ../examples/search/aggregate_hosts.py |
| 50 |
:literal: |
| 51 |
|
| 52 |
``metadata`` |
| 53 |
------------- |
| 54 |
|
| 55 |
**Please note this method is only available only for the CensysHosts index.** |
| 56 |
|
| 57 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 58 |
|
| 59 |
.. include:: ../examples/search/metadata_hosts.py |
| 60 |
:literal: |
| 61 |
|
| 62 |
``view_host_names`` |
| 63 |
------------------- |
| 64 |
|
| 65 |
**Please note this method is only available only for the CensysHosts index.** |
| 66 |
|
| 67 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 68 |
|
| 69 |
.. include:: ../examples/search/view_host_names.py |
| 70 |
:literal: |
| 71 |
|
| 72 |
``view_host_events`` |
| 73 |
-------------------- |
| 74 |
|
| 75 |
**Please note this method is only available only for the CensysHosts index.** |
| 76 |
|
| 77 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 78 |
|
| 79 |
.. include:: ../examples/search/view_host_events.py |
| 80 |
:literal: |
| 81 |
|
| 82 |
``view_host_diff`` |
| 83 |
------------------ |
| 84 |
|
| 85 |
**Please note this method is only available only for the CensysHosts index.** |
| 86 |
|
| 87 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 88 |
|
| 89 |
.. include:: ../examples/search/view_host_diff.py |
| 90 |
:literal: |
| 91 |
|
| 92 |
|
| 93 |
``get_hosts_by_cert`` |
| 94 |
--------------------- |
| 95 |
|
| 96 |
**Please note this method is only available only for the CensysCerts index** |
| 97 |
|
| 98 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 99 |
|
| 100 |
.. code:: python |
| 101 |
|
| 102 |
from censys.search import CensysCerts |
| 103 |
|
| 104 |
c = CensysCerts() |
| 105 |
|
| 106 |
# Fetch a list of events for the specified IP address. |
| 107 |
hosts, links = c.get_hosts_by_cert( |
| 108 |
"fb444eb8e68437bae06232b9f5091bccff62a768ca09e92eb5c9c2cf9d17c426" |
| 109 |
) |
| 110 |
print(hosts) |
| 111 |
|
| 112 |
Comments |
| 113 |
-------- |
| 114 |
|
| 115 |
``get_comments`` |
| 116 |
^^^^^^^^^^^^^^^^ |
| 117 |
|
| 118 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 119 |
|
| 120 |
.. code:: python |
| 121 |
|
| 122 |
from censys.search import CensysCerts |
| 123 |
|
| 124 |
c = CensysCerts() |
| 125 |
|
| 126 |
# Fetch a list of comments for the specified certificate. |
| 127 |
comments = c.get_comments( |
| 128 |
"fb444eb8e68437bae06232b9f5091bccff62a768ca09e92eb5c9c2cf9d17c426" |
| 129 |
) |
| 130 |
print(comments) |
| 131 |
|
| 132 |
``add_comment`` |
| 133 |
^^^^^^^^^^^^^^^^ |
| 134 |
|
| 135 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 136 |
|
| 137 |
.. code:: python |
| 138 |
|
| 139 |
from censys.search import CensysHosts |
| 140 |
|
| 141 |
h = CensysHosts() |
| 142 |
|
| 143 |
# Add a comment to a host. |
| 144 |
comment = h.add_comment("1.1.1.1", "This is a test comment") |
| 145 |
print(comment) |
| 146 |
|
| 147 |
``update_comment`` |
| 148 |
^^^^^^^^^^^^^^^^^^ |
| 149 |
|
| 150 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 151 |
|
| 152 |
.. code:: python |
| 153 |
|
| 154 |
from censys.search import CensysHosts |
| 155 |
|
| 156 |
h = CensysHosts() |
| 157 |
|
| 158 |
# Update a comment to a host. |
| 159 |
comment = h.update_comment("1.1.1.1", 101, "This is an updated test comment") |
| 160 |
|
| 161 |
``delete_comment`` |
| 162 |
^^^^^^^^^^^^^^^^^^ |
| 163 |
|
| 164 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 165 |
|
| 166 |
.. code:: python |
| 167 |
|
| 168 |
from censys.search import CensysCerts |
| 169 |
|
| 170 |
c = CensysCerts() |
| 171 |
|
| 172 |
# Delete a comment for a certificate. |
| 173 |
c.delete_comment( |
| 174 |
"fb444eb8e68437bae06232b9f5091bccff62a768ca09e92eb5c9c2cf9d17c426", 102 |
| 175 |
) |
| 176 |
|
| 177 |
Tags |
| 178 |
---- |
| 179 |
|
| 180 |
``list_all_tags`` |
| 181 |
^^^^^^^^^^^^^^^^^ |
| 182 |
|
| 183 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 184 |
|
| 185 |
.. code:: python |
| 186 |
|
| 187 |
from censys.search import CensysHosts |
| 188 |
|
| 189 |
h = CensysHosts() |
| 190 |
|
| 191 |
# Fetch a list of all tags. |
| 192 |
tags = h.list_all_tags() |
| 193 |
print(tags) |
| 194 |
|
| 195 |
``create_tag`` |
| 196 |
^^^^^^^^^^^^^^ |
| 197 |
|
| 198 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 199 |
|
| 200 |
.. code:: python |
| 201 |
|
| 202 |
from censys.search import CensysCerts |
| 203 |
|
| 204 |
c = CensysCerts() |
| 205 |
|
| 206 |
# Create a new tag. |
| 207 |
tag = c.create_tag("test-tag") |
| 208 |
print(tag) |
| 209 |
|
| 210 |
# Optionally you can specify a color for the tag. |
| 211 |
tag = c.create_tag("test-tag", color="#00FF00") |
| 212 |
print(tag) |
| 213 |
|
| 214 |
``get_tag`` |
| 215 |
^^^^^^^^^^^ |
| 216 |
|
| 217 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 218 |
|
| 219 |
.. code:: python |
| 220 |
|
| 221 |
from censys.search import CensysHosts |
| 222 |
|
| 223 |
h = CensysHosts() |
| 224 |
|
| 225 |
# Fetch a tag. |
| 226 |
tag = h.get_tag("123") |
| 227 |
print(tag) |
| 228 |
|
| 229 |
``update_tag`` |
| 230 |
^^^^^^^^^^^^^^ |
| 231 |
|
| 232 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 233 |
|
| 234 |
.. code:: python |
| 235 |
|
| 236 |
from censys.search import CensysCerts |
| 237 |
|
| 238 |
c = CensysCerts() |
| 239 |
|
| 240 |
# Update a tag. |
| 241 |
tag = c.update_tag("123", "test-tag") |
| 242 |
print(tag) |
| 243 |
|
| 244 |
# Optionally you can specify a color for the tag. |
| 245 |
tag = c.update_tag("123", "test-tag", color="#00FF00") |
| 246 |
print(tag) |
| 247 |
|
| 248 |
``delete_tag`` |
| 249 |
^^^^^^^^^^^^^^ |
| 250 |
|
| 251 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 252 |
|
| 253 |
.. code:: python |
| 254 |
|
| 255 |
from censys.search import CensysHosts |
| 256 |
|
| 257 |
h = CensysHosts() |
| 258 |
|
| 259 |
# Delete a tag. |
| 260 |
h.delete_tag("123") |
| 261 |
|
| 262 |
``list_tags_on_document`` |
| 263 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 264 |
|
| 265 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 266 |
|
| 267 |
.. code:: python |
| 268 |
|
| 269 |
from censys.search import CensysCerts |
| 270 |
|
| 271 |
c = CensysCerts() |
| 272 |
|
| 273 |
# Fetch a list of tags for a document. |
| 274 |
tags = c.list_tags_on_document( |
| 275 |
"fb444eb8e68437bae06232b9f5091bccff62a768ca09e92eb5c9c2cf9d17c426" |
| 276 |
) |
| 277 |
print(tags) |
| 278 |
|
| 279 |
``add_tag_to_document`` |
| 280 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 281 |
|
| 282 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 283 |
|
| 284 |
.. code:: python |
| 285 |
|
| 286 |
from censys.search import CensysHosts |
| 287 |
|
| 288 |
h = CensysHosts() |
| 289 |
|
| 290 |
# Add a tag to a document. |
| 291 |
h.add_tag_to_document("123") |
| 292 |
|
| 293 |
``remove_tag_from_document`` |
| 294 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 295 |
|
| 296 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 297 |
|
| 298 |
.. code:: python |
| 299 |
|
| 300 |
from censys.search import CensysCerts |
| 301 |
|
| 302 |
c = CensysCerts() |
| 303 |
|
| 304 |
# Remove a tag from a document. |
| 305 |
c.remove_tag_from_document( |
| 306 |
"fb444eb8e68437bae06232b9f5091bccff62a768ca09e92eb5c9c2cf9d17c426" |
| 307 |
) |
| 308 |
|
| 309 |
``list_certs_with_tag`` |
| 310 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 311 |
|
| 312 |
**Please note this method is only available only for the CensysCerts index** |
| 313 |
|
| 314 |
Below we show an example using the :attr:`CensysCerts <censys.search.v2.CensysCerts>` index. |
| 315 |
|
| 316 |
.. code:: python |
| 317 |
|
| 318 |
from censys.search import CensysCerts |
| 319 |
|
| 320 |
c = CensysCerts() |
| 321 |
|
| 322 |
# Fetch a list of certs with the specified tag. |
| 323 |
certs = c.list_certs_with_tag("123") |
| 324 |
print(certs) |
| 325 |
|
| 326 |
``list_hosts_with_tag`` |
| 327 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 328 |
|
| 329 |
**Please note this method is only available only for the CensysHosts index.** |
| 330 |
|
| 331 |
Below we show an example using the :attr:`CensysHosts <censys.search.v2.CensysHosts>` index. |
| 332 |
|
| 333 |
.. code:: python |
| 334 |
|
| 335 |
from censys.search import CensysHosts |
| 336 |
|
| 337 |
h = CensysHosts() |
| 338 |
|
| 339 |
# Fetch a list of hosts with the specified tag. |
| 340 |
hosts = h.list_hosts_with_tag("123") |
| 341 |
print(hosts) |