Debian Codegraph @

docs/usage-asm.rst

raw
1 ASM Usage
2 =========
3
4 The Censys ASM API provides functionality for interacting with Censys ASM endpoints such as Seeds, Assets, Logbook Events, Risks, Inventory Search, and Saved Queries.
5
6 The following API clients provided are:
7
8 - :attr:`seeds <censys.asm.Seeds>` - Provides programmatic management of seeds in the ASM platform.
9 - :attr:`assets <censys.asm.Assets>` - Returns asset data for hosts, certificates, and domains. This option also allows the user to manage tags and comments on assets.
10 - :attr:`logbook <censys.asm.logbook.Logbook>` - Returns logbook events. Can be used to execute targeted searches for events based on start id or date, and event type filters.
11 - :attr:`risks <censys.asm.risks.Risks>` - Returns risk data for hosts, certificates, and domains. This option also allows the user to get more information about a specific risk.
12 - :attr:`inventory <censys.asm.inventory.InventorySearch>` - Returns inventory data for hosts, certificates, and domains. This option also allows the user to Search for assets based on a variety of criteria.
13 - :attr:`web entities <censys.asm.assets.web_entities.WebEntitiesAssets>` - Returns web entities instances. This option also allows the user to manage tags and comments on web entities.
14 - :attr:`saved queries <censys.asm.saved_queries.SavedQueries>` - Returns saved queries. This option also allows the user to manage saved queries.
15
16 More details about each option can be found in the `Censys ASM API documentation <https://app.censys.io/api-docs>`__. Users can also test example requests from the API documentation page.
17
18 Python class objects can be used individually, but must be initialized for each resource type (Seeds, Assets, Events, Risks, Inventory, Clouds, SavedQueries).
19
20 - :attr:`Seeds <censys.asm.Seeds>`
21 - :attr:`Assets <censys.asm.Assets>`
22
23 - :attr:`CertificatesAssets <censys.asm.CertificatesAssets>`
24 - :attr:`DomainsAssets <censys.asm.DomainsAssets>`
25 - :attr:`HostsAssets <censys.asm.HostsAssets>`
26 - :attr:`ObjectStoragesAssets <censys.asm.ObjectStoragesAssets>`
27 - :attr:`SubdomainsAssets <censys.asm.SubdomainsAssets>`
28 - :attr:`WebEntitiesAssets <censys.asm.WebEntitiesAssets>`
29
30 - :attr:`Logbook <censys.asm.logbook.Logbook>`
31 - :attr:`Risks <censys.asm.risks.Risks>`
32 - :attr:`InventorySearch <censys.asm.inventory.InventorySearch>`
33 - :attr:`SavedQueries <censys.asm.saved_queries.SavedQueries>`
34
35 Alternatively, all class objects can be used together by initializing an AsmClient object. This client wraps the APIs under one object for ease of use.
36
37 - :attr:`AsmClient <censys.asm.AsmClient>`
38
39
40 ``Seeds``
41 ----------
42
43 Below we show examples for **listing seeds** from the Censys ASM platform.
44
45 .. code:: python3
46
47 from censys.asm import Seeds
48
49 s = Seeds()
50
51 # Get all seeds
52 seeds = s.get_seeds()
53 print(seeds)
54
55 # Get a specific type of seed. Optional seed types are ["IP_ADDRESS", "DOMAIN_NAME", "CIDR", "ASN"]
56 # Here we get IP address seeds.
57 seeds = s.get_seeds("IP_ADDRESS")
58 print(seeds)
59
60 # Get a single seed by its ID (here we get seed with ID=3)
61 seeds = s.get_seeds(3)
62 print(seeds)
63
64 Below we show examples for **adding seeds** to the Censys ASM platform.
65
66 .. code:: python3
67
68 from censys.asm import Seeds
69
70 s = Seeds()
71
72 # Add a list of seeds. To add a single seed, just pass a list containing one seed.
73 # Here, we add two ASN seeds.
74 seed_list = [
75 {"type": "ASN", "value": 99998, "label": "seed-test-label"},
76 {"type": "ASN", "value": 99999, "label": "seed-test-label"},
77 ]
78 s.add_seeds(seed_list)
79
80 # Add a list of seeds, replacing existing seeds with a specified label
81 # Here, all seeds with label="seed-test-label" will be removed and then
82 # Seeds 99996 and 99997 will be added.
83 seed_list = [{"type": "ASN", "value": 99996}, {"type": "ASN", "value": 99997}]
84 s.replace_seeds_by_label("seed-test-label", seed_list)
85
86 Below we show examples for **deleting seeds** from the Censys ASM platform.
87
88
89 .. code:: python3
90
91 from censys.asm import Seeds
92
93 s = Seeds()
94
95 # Delete all seeds with a specified label
96 # Here we delete all seeds with label="seed-test-label"
97 s.delete_seeds_by_label("seed-test-label")
98
99 # Delete a seed by its ID
100 # Here, a seed with ID=224 will be deleted.
101 s.delete_seed_by_id(224)
102
103
104 ``Assets``
105 ----------
106 There are four types of assets (Hosts, Certificates, Domains, and Subdomains). Each asset type shares the same API interface so we will use a mixture of asset types in the following examples.
107
108 Below we show examples for **viewing assets** on the Censys ASM platform.
109
110 .. code:: python
111
112 from censys.asm import HostsAssets
113
114 h = HostsAssets()
115
116 # Get a generator that returns hosts
117 hosts = h.get_assets()
118 print(next(hosts))
119
120 # Get a single host by ID (here we get host with ID="0.0.0.0")
121 host = h.get_asset_by_id("0.0.0.0")
122 print(host)
123
124 Below we show examples for **managing asset comments** via the ASM API.
125
126 .. code:: python
127
128 from censys.asm import DomainsAssets
129
130 d = DomainsAssets()
131
132 # Get a generator that returns all comments on a specific domain asset
133 # Here we get all comments on the domain with ID="my_domain.com"
134 comments = d.get_comments("my_domain.com")
135 print(next(comments))
136
137 # Get a single comment on a specific domain by comment ID
138 # Here we look at domain with ID="my_domain.com" and get comment with ID=3
139 comment = d.get_comment_by_id("my_domain.com", 3)
140 print(comment)
141
142 # Add a comment to a domain asset
143 # Here we add comment "hello world" to domain with ID="my_domain.com"
144 d.add_comment("my_domain.com", "hello world")
145
146 Below we show examples for **managing asset tags** via the ASM API.
147
148 .. code:: python
149
150 from censys.asm import CertificatesAssets
151
152 c = CertificatesAssets()
153 cert_sha = "0006afc1ddc8431aa57c812adf028ab4f168b25bf5f06e94af86edbafa88dfe0"
154
155 # Add a tag to a certificate asset
156 # Here we add tag "New" to certificate with ID=cert_sha
157 c.add_tag(cert_sha, "New")
158
159 # We can optionally give the tag a hexadecimal color where the default=#ffffff (white)
160 # Here we add a blue tag "New-2" to certificate with ID=cert_sha
161 c.add_tag(cert_sha, "New-2", color="#0011ff")
162
163 # Delete a tag by tag name
164 # Here we delete tag name="New" from certificate with ID=cert_sha
165 c.delete_tag(cert_sha, "New")
166
167 Below we show examples for **subdomain asset tags** via the ASM API.
168
169 .. code:: python
170
171 from censys.asm import AsmClient
172
173 client = AsmClient()
174
175 sub = client.get_subdomains("my_domain.com")
176
177 # Add a tag to a subdomain under my_domain.com
178 sub.add_tag("sub.my_domain.com", "New")
179
180 ``Logbook``
181 -----------
182
183 .. note::
184
185 Note that all timestamp fields in logbook operations use **ISO-8601** format. This is the full list of event types that can be used as filters:
186
187 - ``CERT``
188 - ``CERT_RISK``
189 - ``DOMAIN``
190 - ``DOMAIN_EXPIRATION_DATE``
191 - ``DOMAIN_MAIL_EXCHANGE_SERVER``
192 - ``DOMAIN_NAME_SERVER``
193 - ``DOMAIN_REGISTRAR``
194 - ``DOMAIN_RISK``
195 - ``DOMAIN_SUBDOMAIN``
196 - ``HOST``
197 - ``HOST_CERT``
198 - ``HOST_PORT``
199 - ``HOST_PROTOCOL``
200 - ``HOST_RISK``
201 - ``HOST_SOFTWARE``
202 - ``HOST_VULNERABILITY``
203
204 Below we show examples for **creating a logbook cursor** for retrieving filtered events.
205
206 .. code:: python
207
208 from censys.asm import Logbook
209
210 l = Logbook()
211
212 # Get a logbook cursor beginning at timestamp "2020-04-22T06:55:01.000Z"
213 cursor = l.get_cursor("2020-04-22T06:55:01.000Z")
214 print(cursor)
215
216 # Get a logbook cursor beginning at event ID=10
217 cursor = l.get_cursor(10)
218 print(cursor)
219
220 # Get a logbook cursor that filters on events of type "CERT" and "CERT_RISK"
221 cursor = l.get_cursor(filters=["CERT", "CERT_RISK"])
222 print(cursor)
223
224 # Get a logbook cursor combining previous start ID and filters
225 cursor = l.get_cursor(10, filters=["CERT", "CERT_RISK"])
226 print(cursor)
227
228 Below we show examples for **getting logbook events.**
229
230 .. code:: python
231
232 from censys.asm import Logbook
233
234 l = Logbook()
235
236 # Get a generator that returns all events
237 events = l.get_events()
238 print(next(events))
239
240 # Get events based off cursor specifications
241 events = l.get_events(cursor)
242 print(next(events))
243
244 ``Risks``
245 ---------
246
247 Below we show an example of **getting risk instances**.
248
249 .. code:: python
250
251 from censys.asm import Risks
252
253 r = Risks()
254
255 # Get risk events
256 risk_events = r.get_risk_events()
257 print(risk_events)
258
259 # Get a dict that returns all risk instances
260 risk_instances = r.get_risk_instances()
261 print(risk_instances)
262
263 # Get a single risk instance by ID
264 risk_instance = r.get_risk_instance(1)
265 print(risk_instance)
266
267 # Get risk types
268 risk_types = r.get_risk_types()
269 print(risk_types)
270
271 # Get a single risk type by ID
272 risk_type = r.get_risk_type("missing-common-security-headers")
273 print(risk_type)
274
275 ``InventorySearch``
276 -------------------
277
278 Below we show an example of **searching for assets**.
279
280 .. code:: python
281
282 from censys.asm import InventorySearch
283
284 i = InventorySearch()
285
286 # Get a dict that contains a list of hits for a search query with pagination
287 assets = i.search(workspaces=["my_workspace"], query="host.services.http.response.body: /.*test.*/")
288 print(assets)
289
290 # Aggregate search results by a field
291 aggregation = i.aggregate(workspaces=["my_workspace"], query="host.location.country_code: 'US'", aggregation={"term": {"field": "host.autonomous_system.bgp_prefix", "numberOfBuckets": 50}})
292 print(aggregation)
293
294 # Get list of all available fields
295 fields = i.fields()
296 print(fields)
297
298 ``SavedQueries``
299 ----------------
300
301 Below we show an example of **managing saved queries**.
302
303 .. code:: python
304
305 from censys.asm import SavedQueries
306
307 s = SavedQueries()
308
309 # Get a dict that contains a list of saved queries
310 saved_queries = s.get_saved_queries()
311 print(saved_queries)
312
313 # Get a single saved query by ID
314 saved_query = s.get_saved_query_by_id("query_id")
315 print(saved_query)
316
317 # Add a saved query
318 saved_query = s.add_saved_query("my_saved_query", "host.services.http.response.body: /.*test.*/")
319 print(saved_query)
320
321 # Update a saved query
322 saved_query = s.edit_saved_query_by_id("query_id", "my_updated_saved_query", "host.services.http.response.body: /.*test.*/")
323 print(saved_query)
324
325 # Delete a saved query
326 s.delete_saved_query_by_id("query_id")
327
328 ``AsmClient``
329 -------------
330
331 The Censys AsmClient wraps the Seeds, Assets, and Events classes into a single object. It can be used as a single point of interaction for all three APIs.
332
333 Below we show how to initialize the AsmClient class object as well as a couple examples of its use. Note that with the AsmClient object, all Seeds, Assets, and Event operations can be accessed in a similar way as the individual APIs above.
334
335 .. code:: python
336
337 from censys.asm import AsmClient
338
339 client = AsmClient()
340
341 # Get all seeds
342 seeds = client.seeds.get_seeds()
343 print(seeds)
344
345 # Get all domain assets
346 domains = client.domains.get_assets()
347 print(next(domains))
348
349 # Get all logbook events
350 logbook_events = client.logbook.get_events()
351 print(next(logbook_events))
352
353
354 ``Exceptions``
355 --------------
356
357 .. TODO: Add exceptions