-
Notifications
You must be signed in to change notification settings - Fork 13
/
Manager.py
1648 lines (1324 loc) · 55.3 KB
/
Manager.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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Detect if this is Python 2 or 3
import sys
_IS_PYTHON_2 = False
if sys.version_info[ 0 ] < 3:
_IS_PYTHON_2 = True
if _IS_PYTHON_2:
from urllib2 import HTTPError
from urllib2 import Request as URLRequest
from urllib2 import urlopen
from urllib import urlencode
from urllib import quote as urlescape
else:
from urllib.error import HTTPError
from urllib.request import Request as URLRequest
from urllib.request import urlopen
from urllib.parse import urlencode
from urllib.parse import quote as urlescape
import uuid
import json
import traceback
import cmd
import zlib
import base64
import time
from functools import wraps
from .Sensor import Sensor
from .Spout import Spout
from .utils import LcApiException
from .utils import GET
from .utils import POST
from .utils import DELETE
from .Jobs import Job
from limacharlie import GLOBAL_OID
from limacharlie import GLOBAL_UID
from limacharlie import GLOBAL_API_KEY
from limacharlie import _getEnvironmentCreds
ROOT_URL = 'https://api.limacharlie.io'
API_VERSION = 'v1'
API_TO_JWT_URL = 'https://jwt.limacharlie.io'
HTTP_UNAUTHORIZED = 401
HTTP_TOO_MANY_REQUESTS = 429
HTTP_GATEWAY_TIMEOUT = 504
HTTP_OK = 200
class Manager( object ):
'''General interface to a limacharlie.io Organization.'''
def __init__( self, oid = None, secret_api_key = None, environment = None, inv_id = None, print_debug_fn = None, is_interactive = False, extra_params = {}, jwt = None, uid = None, onRefreshAuth = None, isRetryQuotaErrors = False ):
'''Create a session manager for interaction with limacharlie.io, much of the Python API relies on this object.
Args:
oid (str): a limacharlie.io organization ID, default environment if unset.
secret_api_key (str): an API key for the organization, as provided by limacharlie.io, default environment if unset.
environment (str): an environment name as defined in "limacharlie login" to use.
inv_id (str): an investigation ID that will be used/propagated to other APIs using this Manager instance.
print_debug_fn (function(message)): a callback function that will receive detailed debug messages.
is_interactive (bool): if True, the manager will provide a root investigation and Spout so that tasks sent to Sensors can be tracked in realtime automatically; requires an inv_id to be set.
extra_params (dict): optional key / values passed to interactive spout.
jwt (str): optionally specify a single JWT to use for authentication.
uid (str): a limacharlie.io user ID, if present authentication will be based on it instead of organization ID, set to False to override the current environment.
onRefreshAuth (func): if provided, function is called whenever a JWT would be refreshed using the API key.
isRetryQuotaErrors (bool): if True, the Manager will attempt to retry queries when it gets an out-of-quota error (HTTP 429).
'''
# If an environment is specified, try to get its creds.
if environment is not None:
oid, uid, secret_api_key = _getEnvironmentCreds( environment )
if secret_api_key is None or ( oid is None and uid is None ):
raise LcApiException( 'LimaCharlie environment not configured, use "limacharlie login".')
else:
# Otherwise, try to take the values in parameter. But if
# they are not present, use the GLOBAL values.
if uid is None:
if GLOBAL_UID is not None:
uid = GLOBAL_UID
if oid is None:
if GLOBAL_OID is None and uid is None:
raise LcApiException( 'LimaCharlie "default" environment not set, please use "limacharlie login".' )
oid = GLOBAL_OID
if secret_api_key is None and jwt is None:
if GLOBAL_API_KEY is None:
raise LcApiException( 'LimaCharlie "default" environment not set, please use "limacharlie login".' )
secret_api_key = GLOBAL_API_KEY
try:
if oid is not None:
uuid.UUID( oid )
except:
raise LcApiException( 'Invalid oid, should be in UUID format.' )
try:
uuid.UUID( secret_api_key )
except:
if jwt is None:
raise LcApiException( 'Invalid secret API key, should be in UUID format.' )
self._oid = oid
self._uid = uid if uid else None
self._onRefreshAuth = onRefreshAuth
self._secret_api_key = secret_api_key
self._jwt = jwt
self._debug = print_debug_fn
self._lastSensorListContinuationToken = None
self._inv_id = inv_id
self._spout = None
self._is_interactive = is_interactive
self._extra_params = extra_params
self._isRetryQuotaErrors = isRetryQuotaErrors
if self._is_interactive:
if not self._inv_id:
raise LcApiException( 'Investigation ID must be set for interactive mode to be enabled.' )
self._refreshSpout()
def _unwrap( self, data, isRaw = False ):
if isRaw:
return zlib.decompress( base64.b64decode( data ), 16 + zlib.MAX_WBITS )
else:
return json.loads( zlib.decompress( base64.b64decode( data ), 16 + zlib.MAX_WBITS ).decode() )
def _refreshSpout( self ):
if not self._is_interactive:
return
if self._spout is not None:
self._spout.shutdown()
self._spout = None
self._spout = Spout( self, 'event', is_parse = True, inv_id = self._inv_id, extra_params = self._extra_params )
def _printDebug( self, msg ):
if self._debug is not None:
self._debug( msg )
def _refreshJWT( self, expiry = None ):
try:
if self._secret_api_key is None:
raise Exception( 'No API key set' )
authData = { "secret" : self._secret_api_key }
if self._uid is not None:
authData[ 'uid' ] = self._uid
if self._oid is not None:
authData[ 'oid' ] = self._oid
if expiry is not None:
authData[ 'expiry' ] = int( expiry )
request = URLRequest( API_TO_JWT_URL,
urlencode( authData ).encode(),
headers = { "Content-Type": "application/x-www-form-urlencoded" } )
request.get_method = lambda: "POST"
u = urlopen( request )
self._jwt = json.loads( u.read().decode() )[ 'jwt' ]
u.close()
except Exception as e:
self._jwt = None
raise LcApiException( 'Failed to get JWT from API key oid=%s uid=%s: %s' % ( self._oid, self._uid, e, ) )
def _restCall( self, url, verb, params, altRoot = None, queryParams = None, rawBody = None, contentType = None, isNoAuth = False, timeout = None ):
try:
resp = None
if not isNoAuth:
headers = { "Authorization" : "bearer %s" % self._jwt }
else:
headers = {}
if altRoot is None:
url = '%s/%s/%s' % ( ROOT_URL, API_VERSION, url )
else:
url = '%s/%s' % ( altRoot, url )
if queryParams is not None:
url = '%s?%s' % ( url, urlencode( queryParams ) )
request = URLRequest( url,
rawBody if rawBody is not None else urlencode( params, doseq = True ).encode(),
headers = headers )
request.get_method = lambda: verb
request.add_header( 'User-Agent', 'lc-py-api' )
if contentType is not None:
request.add_header( 'Content-Type', contentType )
u = urlopen( request, timeout = timeout )
try:
data = u.read()
if 0 != len( data ):
resp = json.loads( data.decode() )
else:
resp = {}
except ValueError as e:
LcApiException( "Failed to decode data from API: %s" % e )
u.close()
ret = ( 200, resp )
except HTTPError as e:
errorBody = e.read()
try:
ret = ( e.getcode(), json.loads( errorBody.decode() ) )
except:
ret = ( e.getcode(), errorBody )
self._printDebug( "%s: %s ( %s ) ==> %s ( %s )" % ( verb, url, str( params ), ret[ 0 ], str( ret[ 1 ] ) ) )
return ret
def _apiCall( self, url, verb, params = {}, altRoot = None, queryParams = None, rawBody = None, contentType = None, isNoAuth = False, nMaxTotalRetries = 3, timeout = 60 * 10 ):
hasAuthRefreshed = False
nRetries = 0
# If no JWT is ready, prime it.
if not isNoAuth and self._jwt is None:
if self._onRefreshAuth is not None:
self._onRefreshAuth( self )
else:
self._refreshJWT()
while nRetries < nMaxTotalRetries:
nRetries += 1
code, data = self._restCall( url, verb, params, altRoot = altRoot, queryParams = queryParams, rawBody = rawBody, contentType = contentType, isNoAuth = isNoAuth, timeout = timeout )
if code == HTTP_UNAUTHORIZED:
if hasAuthRefreshed:
# We already renewed the JWT once.
break
elif not isNoAuth:
# Do our one JWT renew attempt.
hasAuthRefreshed = True
if self._onRefreshAuth is not None:
self._onRefreshAuth( self )
else:
self._refreshJWT()
continue
else:
# Auth failed, can't renew.
break
if code == HTTP_TOO_MANY_REQUESTS and self._isRetryQuotaErrors:
# Out of quota, wait a bit and retry.
time.sleep( 10 )
continue
if code == HTTP_GATEWAY_TIMEOUT:
# The API gateway timed out talking to the
# backend, we'll give it another shot.
continue
# Some other status code, including 200, so we're done.
break
if code != HTTP_OK:
raise LcApiException( 'Api failure (%s): %s' % ( code, str( data ) ) )
return data
def shutdown( self ):
'''Shut down any active mechanisms like interactivity.
'''
if self._spout is not None:
self._spout.shutdown()
self._spout = None
def make_interactive( self ):
'''Enables interactive mode on this instance if it was not created with is_interactive.
'''
if self._is_interactive:
return
if not self._inv_id:
raise LcApiException( 'Investigation ID must be set for interactive mode to be enabled.' )
self._is_interactive = True
self._refreshSpout()
def testAuth( self, permissions = [] ):
'''Tests authentication with limacharlie.io.
Args:
permissions (list): optional list of permissions validate we have.
Returns:
a boolean indicating whether authentication succeeded.
'''
try:
perms = None
# First make sure we have an API key or JWT.
if self._secret_api_key is not None:
try:
if self._onRefreshAuth is not None:
self._onRefreshAuth( self )
else:
self._refreshJWT()
except:
return False
elif self._jwt is not None:
try:
perms = self.whoAmI()
except:
return False
else:
return False
# If there are no permissions to check, we're good since
# the previous part of this check made sure our auth was
# at least valid.
if permissions is None or 0 == len( permissions ):
return True
# We need to check the permissions we have.
if perms is None:
perms = self.whoAmI()
if 'user_perms' in perms:
# This is from a user token with permissions to multiple
# organizations.
effective = perms[ 'user_perms' ].get( self._oid, [] )
else:
# This is a machine token. Check the current OID is in there.
if self._oid in perms.get( 'orgs', [] ):
effective = perms.get( 'perms', [] )
else:
effective = []
# Now just check if we have them all.
for p in permissions:
if p not in effective:
return False
return True
except:
return False
def whoAmI( self ):
'''Query the API to see which organizations we are authenticated for.
Returns:
A list of organizations and permissions, or a dictionary of organizations with the related permissions.
'''
resp = self._apiCall( 'who', GET, {}, altRoot = "%s/%s" % ( ROOT_URL, API_VERSION ) )
return resp
def userAccessibleOrgs( self ):
'''Query the API with a User API to see which organizations the user has access to.
Returns:
A dict with org OIDs and names.
'''
resp = self._apiCall( '/user_key_info', POST, {}, queryParams = {
'uid' : self._uid,
'secret' : self._secret_api_key,
'with_names' : True,
}, altRoot = 'https://app.limacharlie.io/', isNoAuth = True )
return resp
def sensor( self, sid, inv_id = None, detailedInfo = None ):
'''Get a Sensor object for the specific Sensor ID.
The sensor may or may not be online.
Args:
sid (uuid str): the Sensor ID to represent.
inv_id (str): investigation ID to add to all actions done using this object.
Returns:
a Sensor object.
'''
s = Sensor( self, sid, detailedInfo = detailedInfo )
if inv_id is not None:
s.setInvId( inv_id )
elif self._inv_id is not None:
s.setInvId( self._inv_id )
return s
def sensors( self, inv_id = None, selector = None, limit = None, with_ip = None, with_hostname_prefix = None ):
'''Gets all Sensors in the Organization.
The sensors may or may not be online.
Args:
inv_id (str): investigation ID to add to all actions done using these objects.
selector (str): sensor selector expression to use as filter.
limit (int): max number of sensors per page of result.
with_ip (str): list sensors with the specific internal or external ip.
with_hostname_prefix (str): list sensors with the specific hostname prefix.
Returns:
a generator of Sensor objects.
'''
continuationToken = None
while True:
params = {}
if continuationToken is not None:
params[ 'continuation_token' ] = continuationToken
if selector is not None:
params[ 'selector' ] = selector
if limit is not None:
params[ 'limit' ] = limit
if with_ip is not None:
params[ 'with_ip' ] = with_ip
if with_hostname_prefix is not None:
params[ 'with_hostname_prefix' ] = with_hostname_prefix
resp = self._apiCall( 'sensors/%s' % self._oid, GET, queryParams = params )
if inv_id is None:
inv_id = self._inv_id
for s in resp[ 'sensors' ]:
yield self.sensor( s[ 'sid' ], inv_id, detailedInfo = s )
continuationToken = resp.get( 'continuation_token', None )
if continuationToken is None:
break
def sensorsWithTag( self, tag ):
'''Get a list of sensors that have the matching tag.
Args:
tag (str): a tag to look for.
Returns:
a list of Sensor objects.
'''
resp = self._apiCall( 'tags/%s/%s' % ( self._oid, urlescape( tag, '' ) ), GET, queryParams = {} )
return [ Sensor( self, sid ) for sid in resp.keys() ]
def getAllTags( self ):
'''Get a list of tags in use by sensors.
Returns:
a list of tags.
'''
return self._apiCall( 'tags/%s' % ( self._oid, ), GET, queryParams = {} )[ 'tags' ]
def getAllOnlineSensors( self, onlySIDs = [] ):
'''Get a list of all online sensors.
Args:
onlySIDs (list of str): optional list of SIDs to check.
Returns:
a list of SIDs.
'''
req = {}
if onlySIDs:
req[ 'sids' ] = onlySIDs
return list( k for k, v in self._apiCall( 'online/%s' % ( self._oid, ), POST, req, queryParams = {} ).items() if v )
def outputs( self ):
'''Get the list of all Outputs configured for the Organization.
Returns:
a list of Output descriptions (JSON).
'''
resp = self._apiCall( 'outputs/%s' % self._oid, GET )
return resp.get( self._oid, {} )
def del_output( self, name ):
'''Remove an Output from the Organization.
Args:
name (str): the name of the Output to remove.
Returns:
the REST API response (JSON).
'''
return self._apiCall( 'outputs/%s' % self._oid, DELETE, { 'name' : name } )
def add_output( self, name, module, type, **kwargs ):
'''Add an Output to the Organization.
For detailed explanation and possible Output module parameters
see the official documentation, naming is the same as for the
REST interface.
Args:
name (str): name to give to the Output.
module (str): name of the Output module to use.
type (str): type of Output stream.
**kwargs: arguments specific to the Output module, see official doc.
Returns:
the REST API response (JSON).
'''
req = { 'name' : name, 'module' : module, 'type' : type }
for k, v in kwargs.items():
req[ k ] = v
return self._apiCall( 'outputs/%s' % self._oid, POST, req )
def hosts( self, hostname_expr, as_dict = False ):
'''Get the Sensor objects for hosts matching a hostname expression.
Args:
hostname_expr (str): hostname prefix to look for.
Returns:
a list of Sensor IDs matching the hostname expression.
'''
return self.getSensorsWithHostname( hostname_expr, as_dict = as_dict )
def rules( self, namespace = None ):
'''Get the list of all Detection & Response rules for the Organization.
Args:
namespace (str): optional namespace to operator on, defaults to "general".
Returns:
a list of D&R rules (JSON).
'''
req = {}
if namespace is not None:
req[ 'namespace' ] = namespace
resp = self._apiCall( 'rules/%s' % self._oid, GET, queryParams = req )
return resp
def del_rule( self, name, namespace = None ):
'''Remove a Rule from the Organization.
Args:
name (str): the name of the Rule to remove.
namespace (str): optional namespace to operator on, defaults to "general".
Returns:
the REST API response (JSON).
'''
req = {
'name' : name,
}
if namespace is not None:
req[ 'namespace' ] = namespace
return self._apiCall( 'rules/%s' % self._oid, DELETE, req )
def add_rule( self, name, detection, response, isReplace = False, namespace = None, isEnabled = True, ttl = None ):
'''Add a Rule to the Organization.
For detailed explanation and possible Rules parameters
see the official documentation, naming is the same as for the
REST interface.
Args:
name (str): name to give to the Rule.
namespace (str): optional namespace to operator on, defaults to "general".
isReplace (boolean): if True, replace existing Rule with the same name.
detection (dict): dictionary representing the detection component of the Rule.
response (list): list representing the response component of the Rule.
isEnabled (boolean): if True (default), the rule is enabled.
ttl (int): number of seconds before the rule should be auto-deleted.
Returns:
the REST API response (JSON).
'''
expireOn = None
if ttl is not None:
expireOn = str( int( time.time() ) + int( ttl ) )
req = {
'name' : name,
'is_replace' : 'true' if isReplace else 'false',
'detection' : json.dumps( detection ),
'response' : json.dumps( response ),
'is_enabled' : 'true' if isEnabled else 'false',
}
if expireOn is not None:
req[ 'expire_on' ] = expireOn
if namespace is not None:
req[ 'namespace' ] = namespace
return self._apiCall( 'rules/%s' % self._oid, POST, req )
def fps( self ):
'''Get the list of all False Positive rules for the Organization.
Returns:
a list of False Positive rules (JSON).
'''
req = {}
resp = self._apiCall( 'fp/%s' % self._oid, GET, queryParams = req )
return resp
def del_fp( self, name ):
'''Remove a False Positive rule from the Organization.
Args:
name (str): the name of the rule to remove.
Returns:
the REST API response (JSON).
'''
req = {
'name' : name,
}
return self._apiCall( 'fp/%s' % self._oid, DELETE, req )
def add_fp( self, name, rule, isReplace = False, ttl = None ):
'''Add a False Positive rule to the Organization.
For detailed explanation and possible rules parameters
see the official documentation, naming is the same as for the
REST interface.
Args:
name (str): name to give to the rule.
isReplace (boolean): if True, replace existing rule with the same name.
detection (dict): dictionary representing the False Positive rule content.
ttl (int): number of seconds before the rule should be auto-deleted.
Returns:
the REST API response (JSON).
'''
expireOn = None
if ttl is not None:
expireOn = str( int( time.time() ) + int( ttl ) )
req = {
'name' : name,
'is_replace' : 'true' if isReplace else 'false',
'rule' : json.dumps( rule ),
}
if expireOn is not None:
req[ 'expire_on' ] = expireOn
return self._apiCall( 'fp/%s' % self._oid, POST, req )
def isInsightEnabled( self ):
'''Check to see if Insight (retention) is enabled on this organization.
Returns:
True if Insight is enabled.
'''
data = self._apiCall( 'insight/%s' % ( self._oid, ), GET )
if data.get( 'insight_bucket', None ):
return True
return False
def getHistoricDetections( self, start, end, limit = None, cat = None ):
'''Get the detections for this organization between the two times, requires Insight (retention) enabled.
Args:
start (int): start unix (seconds) timestamp to fetch detects from.
end (int): end unix (seconds) timestamp to feth detects to.
limit (int): maximum number of detects to return.
cat (str): return dects only from this category.
Returns:
a generator of detects.
'''
cursor = '-'
start = int( start )
end = int( end )
if limit is not None:
limit = int( limit )
req = {
'start' : start,
'end' : end,
'is_compressed' : 'true',
}
if limit is not None:
req[ 'limit' ] = limit
if cat is not None:
req[ 'cat' ] = cat
nReturned = 0
while cursor:
req[ 'cursor' ] = cursor
data = self._apiCall( 'insight/%s/detections' % ( self._oid, ), GET, queryParams = req )
cursor = data.get( 'next_cursor', None )
for detect in self._unwrap( data[ 'detects' ] ):
yield detect
nReturned += 1
if limit is not None and limit <= nReturned:
break
if limit is not None and limit <= nReturned:
break
def getAuditLogs( self, start, end, limit = None, event_type = None, sid = None ):
'''Get the audit logs for the organization.
Args:
start (int): start unix (seconds) timestamp to fetch detects from.
end (int): end unix (seconds) timestamp to feth detects to.
limit (int): maximum number of detects to return.
event_type (str): only return this audit event.
sid (str): only return audit logs relating to this sensor id.
Returns:
a generator of detects.
'''
cursor = '-'
start = int( start )
end = int( end )
if limit is not None:
limit = int( limit )
req = {
'start' : start,
'end' : end,
'is_compressed' : 'true',
}
if limit is not None:
req[ 'limit' ] = limit
if event_type is not None:
req[ 'event_type' ] = event_type
if sid is not None:
req[ 'sid' ] = sid
nReturned = 0
while cursor:
req[ 'cursor' ] = cursor
data = self._apiCall( 'insight/%s/audit' % ( self._oid, ), GET, queryParams = req )
cursor = data.get( 'next_cursor', None )
for detect in self._unwrap( data[ 'events' ] ):
yield detect
nReturned += 1
if limit is not None and limit <= nReturned:
break
if limit is not None and limit <= nReturned:
break
def getHistoricDetectionByID( self, detect_id ):
'''Get the detection with a specific detect_id.
Args:
detect_id (str): the ID (detect_id) of the detection to fetch.
Returns:
a detection.
'''
return self._apiCall( 'insight/%s/detections/%s' % ( self._oid, detect_id, ), GET )
def getObjectInformation( self, objType, objName, info, isCaseSensitive = True, isWithWildcards = False, limit = None, isPerObject = None ):
'''Get information about an object (indicator) using Insight (retention) data.
Args:
objType (str): the object type to query for, one of: user, domain, ip, hash, file_path, file_name.
objName (str): the name of the object to query for, like "cmd.exe".
info (str): the type of information to query for, one of: summary, locations.
isCaseSensitive (bool): False to ignore case in the object name.
isWithWildcards (bool): True to enable use of "%" wildcards in the object name.
limit (int): optional maximum number of sensors/logs to report, or None for LimaCharlie default.
isPerObject (bool): if set, specifies if the results should be groupped per object when a wildcard is present.
Returns:
a dict with the requested information.
'''
infoTypes = ( 'summary', 'locations' )
objTypes = ( 'user', 'domain', 'ip', 'file_hash', 'file_path', 'file_name', 'service_name', 'package_name' )
if info not in infoTypes:
raise Exception( 'invalid information type: %s, choose one of %s' % ( info, infoTypes ) )
if objType not in objTypes:
raise Exception( 'invalid object type: %s, choose one of %s' % ( objType, objTypes ) )
perObject = isPerObject
if perObject is None:
perObject = 'true' if ( isWithWildcards and 'summary' == info ) else 'false'
else:
perObject = 'true' if perObject else 'false'
req = {
'name' : objName,
'info' : info,
'case_sensitive' : 'true' if isCaseSensitive else 'false',
'with_wildcards' : 'true' if isWithWildcards else 'false',
'per_object' : perObject,
}
if limit is not None:
req[ 'limit' ] = str( limit )
data = self._apiCall( 'insight/%s/objects/%s' % ( self._oid, objType ), GET, queryParams = req )
return data
def getBatchObjectInformation( self, objects, isCaseSensitive = True ):
'''Get object prevalence information in a batch.
Args:
objects (dict): dictionary of object type to list of object names to query for (objects["file_name"] = ["a.exe", "b.exe"]).
isCaseSensitive (bool): False to ignore case in the object name.
Returns:
a dict with keys as time ranges and values are maps of object types to object name lists.
'''
for objType, objNames in objects.items():
objects[ objType ] = list( objNames )
req = {
'objects' : json.dumps( objects ),
'case_sensitive' : 'true' if isCaseSensitive else 'false',
}
data = self._apiCall( 'insight/%s/objects' % ( self._oid, ), POST, req )
return data
def getInsightHostCountPerPlatform( self ):
'''Get the number of hosts for each platform for which we have long term Insight data.
Returns:
a dict with "mac", "linux" and "windows" and their count tuples [1,7,30].
'''
macBin = 'launchd'
winBin = 'ntdll.dll'
data = self.getBatchObjectInformation( {
'file_name' : [
macBin,
winBin,
]
} )
if data is None:
return data
return {
'mac' : ( data.get( 'last_1_days', {} ).get( 'file_name', {} ).get( macBin, 0 ), data.get( 'last_7_days', {} ).get( 'file_name', {} ).get( macBin, 0 ), data.get( 'last_30_days', {} ).get( 'file_name', {} ).get( macBin, 0 ) ),
'windows' : ( data.get( 'last_1_days', {} ).get( 'file_name', {} ).get( winBin, 0 ), data.get( 'last_7_days', {} ).get( 'file_name', {} ).get( winBin, 0 ), data.get( 'last_30_days', {} ).get( 'file_name', {} ).get( winBin, 0 ) ),
'linux' : ( None, None, None ),
}
def getSensorsWithHostname( self, hostnamePrefix, as_dict = False ):
'''Get the list of sensor IDs and hostnames that match the given prefix.
Args:
hostnamePrefix (str): a hostname prefix to search for.
Returns:
List of (sid, hostname).
'''
data = self._apiCall( 'hostnames/%s' % ( self._oid, ), GET, queryParams = {
'hostname' : hostnamePrefix,
'as_dict' : 'true' if as_dict else 'false',
} )
return data.get( 'sid', None )
def getSensorsWithIp( self, ip, start, end ):
'''Get the list of sensor IDs that used the given IP during the time range.
Args:
ip (str): the IP address used.
start (int): beginning of the time range to look for.
end (int): end of the time range to look for.
Returns:
List of sid.
'''
data = self._apiCall( 'ips/%s' % ( self._oid, ), GET, queryParams = {
'ip' : str( ip ),
'start' : int( start ),
'end' : int( end ),
} )
return data.get( 'sid', None )
def serviceRequest( self, serviceName, data, isAsynchronous = False, isImpersonate = False ):
'''Issue a request to a Service.
Args:
serviceName (str): the name of the Service to task.
data (dict): JSON data to send to the Service as a request.
isAsynchronous (bool): if set to False, wait for data from the Service and return it.
isImpersonate (bool): if set to True, request the Service impersonate the caller.
Returns:
Dict with general success, or data from Service if isSynchronous.
'''
req = {
'request_data' : base64.b64encode( json.dumps( data ).encode() ),
'is_async' : isAsynchronous,
}
if isImpersonate:
# To make sure we have as fresh a JWT as possible,
# always do a refresh.
self._refreshJWT()
req[ 'jwt' ] = self._jwt
data = self._apiCall( 'service/%s/%s' % ( self._oid, serviceName ), POST, req )
return data
def replicantRequest( self, *args, **kwargs ):
# Maintained for backwards compatibility post rename replicant => service.
return self.serviceRequest( *args, **kwargs )
def extensionRequest( self, extensionName, action, data, isImpersonate = False ):
'''Issue a request to an Extension.
Args:
extensionName (str): the name of the Extension to task.
data (dict): JSON data to send to the Extension as a request.
isImpersonate (bool): if set to True, request the Service impersonate the caller.
Returns:
Dict with general success.
'''
from limacharlie.Extensions import Extension
return Extension( self ).request( extensionName, action, data, isImpersonated = isImpersonate )
def getAvailableServices( self ):
'''Get the list of Services currently available.
Returns:
List of Service names.
'''
data = self._apiCall( 'service/%s' % ( self._oid, ), GET )
return data.get( 'replicants', None )
def getAvailableReplicants( self ):
# Maintained for backwards compatibility post rename replicant => service.
return self.getAvailableServices()
def getOrgConfig( self, configName ):
'''Get the value of a per-organization config.
Args:
configName (str): name of the config to get.
Returns:
String value of the configuration.
'''
data = self._apiCall( 'configs/%s/%s' % ( self._oid, configName ), GET )
return data.get( 'value', None )
def setOrgConfig( self, configName, value ):
'''Set the value of a per-organization config.
Args:
configName (str): name of the config to get.
value (str): value of the config to set.
'''
data = self._apiCall( 'configs/%s/%s' % ( self._oid, configName ), POST, {
'value' : value,
} )
return data
def getOrgURLs( self ):
'''Get the URLs used by various resources in the organization.
Returns:
Dictionary of resource types to URLs.
'''
data = self._apiCall( 'orgs/%s/url' % ( self._oid, ), GET, isNoAuth = True )
return data.get( 'url', None )
def getIngestionKeys( self ):
'''Get the Ingestion keys associated to this organization.
Returns:
Dictionary of the Ingestion keys.
'''
data = self._apiCall( 'insight/%s/ingestion_keys' % ( self._oid, ), GET )
return data.get( 'keys', None )
def setIngestionKey( self, name ):
'''Set (or reset) an Ingestion key.
Args:
name (str): name of the Ingestion key to set.
Returns:
Dictionary with the key name and value.
'''
data = self._apiCall( 'insight/%s/ingestion_keys' % ( self._oid, ), POST, {
'name' : name,
} )
return data
def delIngestionKey( self, name ):
'''Delete an Ingestion key.
Args:
name (str): name of the Ingestion key to delete.
'''
data = self._apiCall( 'insight/%s/ingestion_keys?name=%s' % ( self._oid, name ), DELETE, {} )
return data
def configureUSPKey( self, name, parse_hint = '', format_re = '' ):
'''Set the USP configuration of an Ingestion key.
Args:
name (str): name of the Ingestion key to configure.