-
Notifications
You must be signed in to change notification settings - Fork 574
/
client.py
661 lines (544 loc) · 20.6 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# Copyright 2017 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Timesketch API client."""
import json
import uuid
import BeautifulSoup
import requests
from requests.exceptions import ConnectionError
from .definitions import HTTP_STATUS_CODE_20X
class TimesketchApi(object):
"""Timesketch API object
Attributes:
api_root: The full URL to the server API endpoint.
session: Authenticated HTTP session.
"""
def __init__(self,
host_uri,
username,
password,
verify=True,
auth_mode=u'timesketch'):
"""Initializes the TimesketchApi object.
Args:
host_uri: URI to the Timesketch server (https://<server>/).
username: User username.
password: User password.
verify: Verify server SSL certificate.
auth_mode: The authentication mode to use. Defaults to 'timesketch'
Supported values are 'timesketch' (Timesketch login form) and
'http-basic' (HTTP Basic authentication).
"""
self._host_uri = host_uri
self.api_root = u'{0:s}/api/v1'.format(host_uri)
try:
self.session = self._create_session(
username, password, verify=verify, auth_mode=auth_mode)
except ConnectionError:
raise ConnectionError(u'Timesketch server unreachable')
def _authenticate_session(self, session, username, password):
"""Post username/password to authenticate the HTTP seesion.
Args:
session: Instance of requests.Session.
username: User username.
password: User password.
"""
# Do a POST to the login handler to set up the session cookies
data = {u'username': username, u'password': password}
session.post(u'{0:s}/login/'.format(self._host_uri), data=data)
def _set_csrf_token(self, session):
"""Retrieve CSRF token from the server and append to HTTP headers.
Args:
session: Instance of requests.Session.
"""
# Scrape the CSRF token from the response
response = session.get(self._host_uri)
soup = BeautifulSoup.BeautifulSoup(response.text)
csrf_token = soup.find(id=u'csrf_token').get(u'value')
session.headers.update({
u'x-csrftoken': csrf_token,
u'referer': self._host_uri
})
def _create_session(self, username, password, verify, auth_mode):
"""Create authenticated HTTP session for server communication.
Args:
username: User to authenticate as.
password: User password.
verify: Verify server SSL certificate.
auth_mode: The authentication mode to use. Supported values are
'timesketch' (Timesketch login form) and 'http-basic'
(HTTP Basic authentication).
Returns:
Instance of requests.Session.
"""
session = requests.Session()
session.verify = verify # Depending if SSL cert is verifiable
# If using HTTP Basic auth, add the user/pass to the session
if auth_mode == u'http-basic':
session.auth = (username, password)
# Get and set CSRF token and authenticate the session if appropriate.
self._set_csrf_token(session)
if auth_mode == u'timesketch':
self._authenticate_session(session, username, password)
return session
def fetch_resource_data(self, resource_uri):
"""Make a HTTP GET request.
Args:
resource_uri: The URI to the resource to be fetched.
Returns:
Dictionary with the response data.
"""
# TODO: Catch HTTP errors and add descriptive message string.
resource_url = u'{0:s}/{1:s}'.format(self.api_root, resource_uri)
response = self.session.get(resource_url)
return response.json()
def create_sketch(self, name, description=None):
"""Create a new sketch.
Args:
name: Name of the sketch.
description: Description of the sketch.
Returns:
Instance of a Sketch object.
"""
if not description:
description = name
resource_url = u'{0:s}/sketches/'.format(self.api_root)
form_data = {u'name': name, u'description': description}
response = self.session.post(resource_url, json=form_data)
response_dict = response.json()
sketch_id = response_dict[u'objects'][0][u'id']
return self.get_sketch(sketch_id)
def get_sketch(self, sketch_id):
"""Get a sketch.
Args:
sketch_id: Primary key ID of the sketch.
Returns:
Instance of a Sketch object.
"""
return Sketch(sketch_id, api=self)
def list_sketches(self):
"""Get list of all open sketches that the user has access to.
Returns:
List of Sketch objects instances.
"""
sketches = []
response = self.fetch_resource_data(u'sketches/')
for sketch in response[u'objects'][0]:
sketch_id = sketch[u'id']
sketch_name = sketch[u'name']
sketch_obj = Sketch(
sketch_id=sketch_id, api=self, sketch_name=sketch_name)
sketches.append(sketch_obj)
return sketches
def get_searchindex(self, searchindex_id):
"""Get a searchindex.
Args:
searchindex_id: Primary key ID of the searchindex.
Returns:
Instance of a SearchIndex object.
"""
return SearchIndex(searchindex_id, api=self)
def get_or_create_searchindex(self,
searchindex_name,
es_index_name=None,
public=False):
"""Create a new searchindex.
Args:
searchindex_name: Name of the searchindex in Timesketch.
es_index_name: Name of the index in Elasticsearch.
public: Boolean indicating if the searchindex should be public.
Returns:
Instance of a SearchIndex object and a boolean indicating if the
object was created.
"""
if not es_index_name:
es_index_name = uuid.uuid4().hex
resource_url = u'{0:s}/searchindices/'.format(self.api_root)
form_data = {
u'searchindex_name': searchindex_name,
u'es_index_name': es_index_name,
u'public': public
}
response = self.session.post(resource_url, json=form_data)
if response.status_code not in HTTP_STATUS_CODE_20X:
raise RuntimeError(u'Error creating searchindex')
response_dict = response.json()
metadata_dict = response_dict[u'meta']
created = metadata_dict.get(u'created', False)
searchindex_id = response_dict[u'objects'][0][u'id']
return self.get_searchindex(searchindex_id), created
def list_searchindices(self):
"""Get list of all searchindices that the user has access to.
Returns:
List of SearchIndex object instances.
"""
indices = []
response = self.fetch_resource_data(u'searchindices/')
for index in response[u'objects'][0]:
index_id = index[u'id']
index_name = index[u'name']
index_obj = SearchIndex(
searchindex_id=index_id, api=self, searchindex_name=index_name)
indices.append(index_obj)
return indices
class BaseResource(object):
"""Base resource object."""
def __init__(self, api, resource_uri):
"""Initialize object.
Args:
api: An instance of TimesketchApi object.
"""
self.api = api
self.resource_uri = resource_uri
self.resource_data = None
def lazyload_data(self, refresh_cache=False):
"""Load resource data once and cache the result.
Args:
refresh_cache: Boolean indicating if to update cache.
Returns:
Dictionary with resource data.
"""
if not self.resource_data or refresh_cache:
self.resource_data = self.api.fetch_resource_data(self.resource_uri)
return self.resource_data
@property
def data(self):
"""Property to fetch resource data.
Returns:
Dictionary with resource data.
"""
return self.lazyload_data()
class Sketch(BaseResource):
"""Timesketch sketch object.
A sketch in Timesketch is a collection of one or more timelines. It has
access control and its own namespace for things like labels and comments.
Attributes:
id: The ID of the sketch.
api: An instance of TimesketchApi object.
"""
def __init__(self, sketch_id, api, sketch_name=None):
"""Initializes the Sketch object.
Args:
sketch_id: Primary key ID of the sketch.
api: An instance of TimesketchApi object.
sketch_name: Name of the sketch (optional).
"""
self.id = sketch_id
self.api = api
self._sketch_name = sketch_name
self._resource_uri = u'sketches/{0:d}'.format(self.id)
super(Sketch, self).__init__(api=api, resource_uri=self._resource_uri)
@property
def name(self):
"""Property that returns sketch name.
Returns:
Sketch name as string.
"""
if not self._sketch_name:
sketch = self.lazyload_data()
self._sketch_name = sketch[u'objects'][0][u'name']
return self._sketch_name
@property
def description(self):
"""Property that returns sketch description.
Returns:
Sketch description as string.
"""
sketch = self.lazyload_data()
return sketch[u'objects'][0][u'description']
@property
def status(self):
"""Property that returns sketch status.
Returns:
Sketch status as string.
"""
sketch = self.lazyload_data()
return sketch[u'objects'][0][u'status'][0][u'status']
def list_views(self):
"""List all saved views for this sketch.
Returns:
List of views (instances of View objects)
"""
sketch = self.lazyload_data()
views = []
for view in sketch[u'meta'][u'views']:
view_obj = View(
view_id=view[u'id'],
view_name=view[u'name'],
sketch_id=self.id,
api=self.api)
views.append(view_obj)
return views
def list_timelines(self):
"""List all timelines for this sketch.
Returns:
List of timelines (instances of Timeline objects)
"""
sketch = self.lazyload_data()
timelines = []
for timeline in sketch[u'objects'][0][u'timelines']:
timeline_obj = Timeline(
timeline_id=timeline[u'id'],
sketch_id=self.id,
api=self.api,
name=timeline[u'name'],
searchindex=timeline[u'searchindex'][u'index_name'])
timelines.append(timeline_obj)
return timelines
def upload(self, timeline_name, file_path):
"""Upload a CSV, JSONL, or Plaso file to the server for indexing.
Args:
timeline_name: Name of the resulting timeline.
file_path: Path to the file to be uploaded.
Returns:
Timeline object instance.
"""
resource_url = u'{0:s}/upload/'.format(self.api.api_root)
files = {u'file': open(file_path, 'rb')}
data = {u'name': timeline_name, u'sketch_id': self.id}
response = self.api.session.post(resource_url, files=files, data=data)
response_dict = response.json()
timeline = response_dict[u'objects'][0]
timeline_obj = Timeline(
timeline_id=timeline[u'id'],
sketch_id=self.id,
api=self.api,
name=timeline[u'name'],
searchindex=timeline[u'searchindex'][u'index_name'])
return timeline_obj
def add_timeline(self, searchindex):
"""Add timeline to sketch.
Args:
searchindex: SearchIndex object instance.
Returns:
Timeline object instance.
"""
resource_url = u'{0:s}/sketches/{1:d}/timelines/'.format(
self.api.api_root, self.id)
form_data = {u'timeline': searchindex.id}
response = self.api.session.post(resource_url, json=form_data)
if response.status_code not in HTTP_STATUS_CODE_20X:
raise RuntimeError(u'Failed adding timeline')
response_dict = response.json()
timeline = response_dict[u'objects'][0]
timeline_obj = Timeline(
timeline_id=timeline[u'id'],
sketch_id=self.id,
api=self.api,
name=timeline[u'name'],
searchindex=timeline[u'searchindex'][u'index_name'])
return timeline_obj
def explore(self,
query_string=None,
query_dsl=None,
query_filter=None,
view=None):
"""Explore the sketch.
Args:
query_string: Elasticsearch query string.
query_dsl: Elasticsearch query DSL as JSON string.
query_filter: Filter for the query as JSON string.
view: View object instance (optional).
Returns:
Dictionary with query results.
"""
default_filter = {
u'time_start': None,
u'time_end': None,
u'limit': 40,
u'indices': u'_all',
u'order': u'asc'
}
if not (query_string or query_filter or query_dsl or view):
raise RuntimeError(u'You need to supply a query or view')
if not query_filter:
query_filter = default_filter
if view:
query_string = view.query_string
query_filter = json.loads(view.query_filter)
query_dsl = json.loads(view.query_dsl)
resource_url = u'{0:s}/sketches/{1:d}/explore/'.format(
self.api.api_root, self.id)
form_data = {
u'query': query_string,
u'filter': query_filter,
u'dsl': query_dsl,
}
response = self.api.session.post(resource_url, json=form_data)
return response.json()
def label_events(self, events, label_name):
"""Labels one or more events with label_name.
Args:
events: Array of JSON objects representing events.
label_name: String to label the event with.
Returns:
Dictionary with query results.
"""
form_data = {
'annotation': label_name,
'annotation_type': 'label',
'events': events
}
resource_url = u'{0:s}/sketches/{1:d}/event/annotate/'.format(
self.api.api_root, self.id)
response = self.api.session.post(resource_url, json=form_data)
return response.json()
def search_by_label(self, label_name):
"""Searches for all events containing a given label.
Args:
label_name: A string representing the label to search for.
Returns:
A dictionary with query results.
"""
query = {
"nested": {
"path": "timesketch_label",
"query": {
"bool": {
"must": [
{
"term": {
"timesketch_label.name": label_name
}
},
{
"term": {
"timesketch_label.sketch_id": self.id
}
}
]
}
}
}
}
return self.explore(query_dsl=json.dumps({'query': query}))
class SearchIndex(BaseResource):
"""Timesketch searchindex object.
Attributes:
id: The ID of the search index.
api: An instance of TimesketchApi object.
"""
def __init__(self, searchindex_id, api, searchindex_name=None):
"""Initializes the SearchIndex object.
Args:
searchindex_id: Primary key ID of the searchindex.
searchindex_name: Name of the searchindex (optional).
"""
self.id = searchindex_id
self._searchindex_name = searchindex_name
self._resource_uri = u'searchindices/{0:d}'.format(self.id)
super(SearchIndex, self).__init__(
api=api, resource_uri=self._resource_uri)
@property
def name(self):
"""Property that returns searchindex name.
Returns:
Searchindex name as string.
"""
if not self._searchindex_name:
searchindex = self.lazyload_data()
self._searchindex_name = searchindex[u'objects'][0][u'name']
return self._searchindex_name
@property
def index_name(self):
"""Property that returns Elasticsearch index name.
Returns:
Elasticsearch index name as string.
"""
searchindex = self.lazyload_data()
return searchindex[u'objects'][0][u'index_name']
class View(BaseResource):
"""Saved view object.
Attributes:
id: Primary key of the view.
name: Name of the view.
"""
def __init__(self, view_id, view_name, sketch_id, api):
"""Initializes the View object.
Args:
view_id: Primary key ID for the view.
view_name: The name of the view.
sketch_id: ID of a sketch.
api: Instance of a TimesketchApi object.
"""
self.id = view_id
self.name = view_name
resource_uri = u'sketches/{0:d}/views/{1:d}/'.format(sketch_id, self.id)
super(View, self).__init__(api, resource_uri)
@property
def query_string(self):
"""Property that returns the views query string.
Returns:
Elasticsearch query as string.
"""
view = self.lazyload_data()
return view[u'objects'][0][u'query_string']
@property
def query_filter(self):
"""Property that returns the views filter.
Returns:
Elasticsearch filter as JSON string.
"""
view = self.lazyload_data()
return view[u'objects'][0][u'query_filter']
@property
def query_dsl(self):
"""Property that returns the views query DSL.
Returns:
Elasticsearch DSL as JSON string.
"""
view = self.lazyload_data()
return view[u'objects'][0][u'query_dsl']
class Timeline(BaseResource):
"""Timeline object.
Attributes:
id: Primary key of the view.
"""
def __init__(self, timeline_id, sketch_id, api, name=None,
searchindex=None):
"""Initializes the Timeline object.
Args:
timeline_id: The primary key ID of the timeline.
sketch_id: ID of a sketch.
api: Instance of a TimesketchApi object.
name: Name of the timeline (optional)
searchindex: The Elasticsearch index name (optional)
"""
self.id = timeline_id
self._name = name
self._searchindex = searchindex
resource_uri = u'sketches/{0:d}/timelines/{1:d}/'.format(
sketch_id, self.id)
super(Timeline, self).__init__(api, resource_uri)
@property
def name(self):
"""Property that returns timeline name.
Returns:
Timeline name as string.
"""
if not self._name:
timeline = self.lazyload_data()
self._name = timeline[u'objects'][0][u'name']
return self._name
@property
def index(self):
"""Property that returns index name.
Returns:
Elasticsearch index name as string.
"""
if not self._searchindex:
timeline = self.lazyload_data()
index_name = timeline[u'objects'][0][u'searchindex'][u'index_name']
self._searchindex = index_name
return self._searchindex