forked from panther-labs/panther-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_helpers_test.py
executable file
·2390 lines (2159 loc) · 95.7 KB
/
global_helpers_test.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
#!/usr/bin/env python
# Unit tests for functions inside global_helpers
# pylint: disable=C0302 (too-many-lines)
import datetime
import os
import secrets
import string
import sys
import unittest
from panther_core.immutable import ImmutableCaseInsensitiveDict, ImmutableList
# pipenv run does the right thing, but IDE based debuggers may fail to import
# so noting, we append this directory to sys.path
sys.path.append(os.path.dirname(__file__))
import panther_asana_helpers as p_a_h # pylint: disable=C0413
import panther_auth0_helpers as p_auth0_h # pylint: disable=C0413
import panther_azuresignin_helpers as p_asi_h # pylint: disable=C0413
import panther_base_helpers as p_b_h # pylint: disable=C0413
import panther_cloudflare_helpers as p_cf_h # pylint: disable=C0413
import panther_greynoise_helpers as p_greynoise_h # pylint: disable=C0413
import panther_ipinfo_helpers as p_i_h # pylint: disable=C0413
import panther_lookuptable_helpers as p_l_h # pylint: disable=C0413
import panther_notion_helpers as p_notion_h # pylint: disable=C0413
import panther_oss_helpers as p_o_h # pylint: disable=C0413
import panther_snyk_helpers as p_snyk_h # pylint: disable=C0413
import panther_tailscale_helpers as p_tscale_h # pylint: disable=C0413
import panther_tines_helpers as p_tines_h # pylint: disable=C0413
import panther_tor_helpers as p_tor_h # pylint: disable=C0413
import panther_zoom_helpers as p_zoom_h # pylint: disable=C0413
# pylint: disable=too-many-lines
class TestEksPantherObjRef(unittest.TestCase):
def setUp(self):
# pylint: disable=C0301
self.event = ImmutableCaseInsensitiveDict(
{
"annotations": {
"authorization.k8s.io/decision": "allow",
"authorization.k8s.io/reason": "",
},
"apiVersion": "audit.k8s.io/v1",
"auditID": "35506555-dffc-4337-b2b1-c4af52b88e18",
"kind": "Event",
"level": "Request",
"objectRef": {
"apiVersion": "v1",
"name": "some-job-xxx1y",
"namespace": "default",
"resource": "pods",
"subresource": "log",
},
"p_any_aws_account_ids": ["123412341234"],
"p_any_aws_arns": [
"arn:aws:iam::123412341234:role/KubeAdministrator",
"arn:aws:sts::123412341234:assumed-role/KubeAdministrator/1669660343296132000",
],
"p_any_ip_addresses": ["5.5.5.5"],
"p_any_usernames": ["kubernetes-admin"],
"p_event_time": "2022-11-29 00:09:04.38",
"p_log_type": "Amazon.EKS.Audit",
"p_parse_time": "2022-11-29 00:10:25.067",
"p_row_id": "2e4ab474b0f0f7a4a8fff4f014aab32a",
"p_source_id": "4c859cd4-9406-469b-9e0e-c2dc1bee24fa",
"p_source_label": "example-cluster-eks-logs",
"requestReceivedTimestamp": "2022-11-29 00:09:04.38",
"requestURI": "/api/v1/namespaces/default/pods/kube-bench-drn4j/log?container=kube-bench",
"responseStatus": {"code": 200},
"sourceIPs": ["5.5.5.5"],
"stage": "ResponseComplete",
"stageTimestamp": "2022-11-29 00:09:04.394",
"user": {
"extra": {
"accessKeyId": ["ASIARLIVEKVNNXXXXXXX"],
"arn": [
"arn:aws:sts::123412341234:assumed-role/KubeAdministrator/1669660343296132000"
],
"canonicalArn": ["arn:aws:iam::123412341234:role/KubeAdministrator"],
"sessionName": ["1669660343296132000"],
},
"groups": ["system:masters", "system:authenticated"],
"uid": "aws-iam-authenticator:123412341234:AROARLIVEXXXXXXXXXXXX",
"username": "kubernetes-admin",
},
"userAgent": "kubectl/v1.25.4 (darwin/arm64) kubernetes/872a965",
"verb": "get",
}
)
def test_complete_event(self):
response = p_b_h.eks_panther_obj_ref(self.event)
self.assertEqual(response.get("actor", ""), "kubernetes-admin")
self.assertEqual(response.get("object", ""), "some-job-xxx1y")
self.assertEqual(response.get("ns", ""), "default")
self.assertEqual(len(response.get("sourceIPs", [])), 1)
self.assertEqual(response.get("sourceIPs", [])[0], "5.5.5.5")
self.assertEqual(response.get("resource", ""), "pods/log")
self.assertEqual(response.get("verb", ""), "get")
self.assertEqual(response.get("p_source_label", ""), "example-cluster-eks-logs")
def test_all_missing_event(self):
temp_event = self.event.to_dict()
del temp_event["user"]["username"]
del temp_event["objectRef"]
del temp_event["sourceIPs"]
del temp_event["verb"]
del temp_event["p_source_label"]
temp_event = ImmutableCaseInsensitiveDict(temp_event)
response = p_b_h.eks_panther_obj_ref(temp_event)
self.assertEqual(response.get("actor", ""), "<NO_USERNAME>")
self.assertEqual(response.get("object", ""), "<NO_OBJECT_NAME>")
self.assertEqual(response.get("ns", ""), "<NO_OBJECT_NAMESPACE>")
self.assertEqual(len(response.get("sourceIPs", [])), 1)
self.assertEqual(response.get("sourceIPs", [])[0], "0.0.0.0") # nosec
self.assertEqual(response.get("resource", ""), "<NO_OBJECT_RESOURCE>")
self.assertEqual(response.get("verb", ""), "<NO_VERB>")
self.assertEqual(response.get("p_source_label", ""), "<NO_P_SOURCE_LABEL>")
def test_missing_subresource_event(self):
temp_event = self.event.to_dict()
del temp_event["objectRef"]["subresource"]
temp_event = ImmutableCaseInsensitiveDict(temp_event)
response = p_b_h.eks_panther_obj_ref(temp_event)
self.assertEqual(response.get("resource", ""), "pods")
class TestGetValFromList(unittest.TestCase):
def setUp(self):
self.input = ImmutableList(
[
{"actor": 1, "one": 1, "select": "me"},
{"actor": 2, "two": 2, "select": "me"},
{"actor": 3, "three": 3, "select": "not_me"},
]
)
def test_input_key_exists(self):
response = p_b_h.get_val_from_list(self.input, "actor", "select", "me")
should_be = set()
should_be.add(1)
should_be.add(2)
self.assertCountEqual(response, should_be)
# This test case validate that get_val_from_list will return the
# empty set when the comparison key is not found
def test_input_notdict(self):
response = p_b_h.get_val_from_list(self.input, "actor", "doesnotexist", "noExceptionRaised")
should_be = set()
self.assertCountEqual(response, should_be)
class TestBoxParseAdditionalDetails(unittest.TestCase):
def setUp(self):
self.initial_dict = ImmutableCaseInsensitiveDict(
{"t": 10, "a": [{"b": 1, "c": 2}], "d": {"e": {"f": True}}}
)
self.initial_list = ImmutableList(["1", 2, True, False])
self.initial_bytes = b'{"t": 10, "a": [{"b": 1, "c": 2}], "d": {"e": {"f": True}}}'
self.initial_str = '{"t": 10, "a": [{"b": 1, "c": 2}], "d": {"e": {"f": true}}}'
self.initial_str_no_json = "this is a plain string"
self.initial_str_list_json = "[1, 2, 3, 4]"
def test_additional_details_string(self):
event = ImmutableCaseInsensitiveDict({"additional_details": self.initial_str})
returns = p_b_h.box_parse_additional_details(event)
self.assertEqual(returns.get("t", 0), 10)
# in the case of a byte array, we expect the empty dict
def test_additional_details_bytes(self):
event = ImmutableCaseInsensitiveDict({"additional_details": self.initial_bytes})
returns = p_b_h.box_parse_additional_details(event)
self.assertEqual(len(returns), 0)
# In the case of a list ( not a string or bytes array ), expect un-altered return
def test_additional_details_list(self):
event = ImmutableCaseInsensitiveDict({"additional_details": self.initial_list})
returns = p_b_h.box_parse_additional_details(event)
self.assertEqual(len(returns), 4)
# in the case of a dict or similar, we expect it to be returned un-altered
def test_additional_details_dict(self):
event = ImmutableCaseInsensitiveDict({"additional_details": self.initial_dict})
returns = p_b_h.box_parse_additional_details(event)
self.assertEqual(returns.get("t", 0), 10)
# If it's a string with no json object to be decoded, we expect an empty dict back
def test_additional_details_plain_str(self):
event = ImmutableCaseInsensitiveDict({"additional_details": self.initial_str_no_json})
returns = p_b_h.box_parse_additional_details(event)
self.assertEqual(len(returns), 0)
# If it's a string with a json list, we expect the list
def test_additional_details_str_list_json(self):
event = ImmutableCaseInsensitiveDict({"additional_details": self.initial_str_list_json})
returns = p_b_h.box_parse_additional_details(event)
self.assertEqual(len(returns), 4)
class TestTorExitNodes(unittest.TestCase):
def setUp(self):
self.event = ImmutableCaseInsensitiveDict(
{"p_enrichment": {"tor_exit_nodes": {"foo": {"ip": "1.2.3.4"}, "p_match": "1.2.3.4"}}}
)
# match against array field
self.event_list = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"tor_exit_nodes": {
"p_any_ip_addresses": [
{"ip": "1.2.3.4", "p_match": "1.2.3.4"},
{"ip": "1.2.3.5", "p_match": "1.2.3.5"},
]
}
}
}
)
def test_ip_address_not_found(self):
"""Should not find anything"""
tor_exit_nodes = p_tor_h.TorExitNodes({})
ip_address = tor_exit_nodes.ip_address("foo")
self.assertEqual(ip_address, None)
self.assertEqual(tor_exit_nodes.has_exit_nodes(), False)
def test_ip_address_found(self):
"""Should find enrichment"""
tor_exit_nodes = p_tor_h.TorExitNodes(self.event)
ip_address = tor_exit_nodes.ip_address("foo")
self.assertEqual(ip_address, "1.2.3.4")
self.assertEqual(tor_exit_nodes.has_exit_nodes(), True)
def test_ip_address_found_list(self):
"""Should find enrichment list"""
tor_exit_nodes = p_tor_h.TorExitNodes(self.event_list)
ip_address_list = tor_exit_nodes.ip_address("p_any_ip_addresses")
self.assertEqual(ip_address_list, ["1.2.3.4", "1.2.3.5"])
self.assertEqual(tor_exit_nodes.has_exit_nodes(), True)
def test_url(self):
"""url generation"""
tor_exit_nodes = p_tor_h.TorExitNodes(self.event)
url = tor_exit_nodes.url("foo")
today = datetime.datetime.today().strftime("%Y-%m-%d")
# pylint: disable=line-too-long
self.assertEqual(
url,
f"https://metrics.torproject.org/exonerator.html?ip=1.2.3.4×tamp={today}&lang=en",
)
def test_url_list(self):
"""url generation list"""
tor_exit_nodes = p_tor_h.TorExitNodes(self.event_list)
urls = tor_exit_nodes.url("p_any_ip_addresses")
today = datetime.datetime.today().strftime("%Y-%m-%d")
# pylint: disable=line-too-long
self.assertEqual(
urls,
[
f"https://metrics.torproject.org/exonerator.html?ip=1.2.3.4×tamp={today}&lang=en",
f"https://metrics.torproject.org/exonerator.html?ip=1.2.3.5×tamp={today}&lang=en",
],
)
def test_context(self):
"""context generation"""
tor_exit_nodes = p_tor_h.TorExitNodes(self.event)
context = tor_exit_nodes.context("foo")
today = datetime.datetime.today().strftime("%Y-%m-%d")
self.assertEqual(
context,
{
"IP": "1.2.3.4",
# pylint: disable=line-too-long
"ExoneraTorURL": f"https://metrics.torproject.org/exonerator.html?ip=1.2.3.4×tamp={today}&lang=en",
},
)
class TestGreyNoiseBasic(unittest.TestCase):
def setUp(self):
self.event = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"greynoise_noise_basic": {
"ClientIP": {
"actor": "unknown",
"classification": "malicious",
"ip": "142.93.204.250",
"p_match": "142.93.204.250",
}
}
}
}
)
def test_greynoise_object(self):
"""Should be basic"""
noise = p_greynoise_h.GetGreyNoiseObject(self.event)
self.assertEqual(noise.subscription_level(), "basic")
# Ensure that noise.lut_matches is None if there is no enrichment
# some users want to test if any greynoise enrichment exists
self.assertIsNotNone(noise.lut_matches)
noise_none = p_greynoise_h.GetGreyNoiseObject({})
self.assertIsNone(noise_none.lut_matches)
def test_greynoise_severity(self):
"""Should be CRITICAL"""
sev = p_greynoise_h.GreyNoiseSeverity(self.event, "ClientIP")
self.assertEqual(sev, "CRITICAL")
def test_subscription_level(self):
"""Should be basic"""
noise = p_greynoise_h.GreyNoiseBasic({})
self.assertEqual(noise.subscription_level(), "basic")
def test_ip_address_not_found(self):
"""Should not find anything"""
noise = p_greynoise_h.GreyNoiseBasic({})
ip_address = noise.ip_address("foo")
self.assertEqual(ip_address, None)
def test_ip_address_found(self):
"""Should find enrichment"""
noise = p_greynoise_h.GreyNoiseBasic(self.event)
ip_address = noise.ip_address("ClientIP")
self.assertEqual(ip_address, "142.93.204.250")
def test_classification(self):
"""Should classify as malicious"""
noise = p_greynoise_h.GreyNoiseBasic(self.event)
classification = noise.classification("ClientIP")
self.assertEqual(classification, "malicious")
def test_actor(self):
"""Should have unknown actor"""
noise = p_greynoise_h.GreyNoiseBasic(self.event)
actor = noise.actor("ClientIP")
self.assertEqual(actor, "unknown")
def test_url(self):
"""Should have url"""
noise = p_greynoise_h.GreyNoiseBasic(self.event)
url = noise.url("ClientIP")
self.assertEqual(url, "https://www.greynoise.io/viz/ip/142.93.204.250")
def test_context(self):
"""context generation"""
noise = p_greynoise_h.GreyNoiseBasic(self.event)
context = noise.context("ClientIP")
self.assertEqual(
context,
{
"Actor": "unknown",
"Classification": "malicious",
"GreyNoise_URL": "https://www.greynoise.io/viz/ip/142.93.204.250",
"IP": "142.93.204.250",
},
)
# pylint: disable=too-many-public-methods
class TestGreyNoiseAdvanced(unittest.TestCase):
def setUp(self):
self.event = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"greynoise_noise_advanced": {
"ClientIP": {
"p_match": "142.93.204.250",
"actor": "unknown",
"bot": False,
"classification": "malicious",
"cve": ["cve1244", "cve4567"],
"first_seen": "2022-03-19",
"ip": "142.93.204.250",
"last_seen_timestamp": "2022-04-06",
"metadata": {
"asn": "AS14061",
"category": "hosting",
"city": "North Bergen",
"country": "United States",
"country_code": "US",
"organization": "DigitalOcean, LLC",
"os": "Linux 2.2-3.x",
"rdns": "",
"region": "New Jersey",
"tor": False,
},
"raw_data": {
"hassh": [],
"ja3": [],
"scan": [{"port": 23, "protocol": "TCP"}],
"web": {},
},
"seen": True,
"spoofable": False,
"tags": ["Mirai", "ZMap Client"],
"vpn": False,
"vpn_service": "N/A",
}
}
}
}
)
self.event_list = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"greynoise_noise_advanced": {
"p_any_ip_addresses": [
{
"p_match": "142.93.204.250",
"actor": "unknown",
"bot": False,
"classification": "malicious",
"cve": ["cve1244", "cve4567"],
"first_seen": "2022-03-19",
"ip": "142.93.204.250",
"last_seen_timestamp": "2022-04-06",
"metadata": {
"asn": "AS14061",
"category": "hosting",
"city": "North Bergen",
"country": "United States",
"country_code": "US",
"organization": "DigitalOcean, LLC",
"os": "Linux 2.2-3.x",
"rdns": "",
"region": "New Jersey",
"tor": False,
},
"raw_data": {
"hassh": [],
"ja3": [],
"scan": [{"port": 23, "protocol": "TCP"}],
"web": {},
},
"seen": True,
"spoofable": False,
"tags": ["Mirai", "ZMap Client"],
"vpn": False,
"vpn_service": "N/A",
},
{
"p_match": "100.93.204.250",
"actor": "stinky rat",
"bot": True,
"classification": "malicious",
"cve": ["cve1244", "cve4567"],
"first_seen": "2022-02-19",
"ip": "100.93.204.250",
"last_seen_timestamp": "2022-03-06",
"metadata": {
"asn": "AS14461",
"category": "isp",
"city": "South Bergen",
"country": "United States",
"country_code": "US",
"organization": "DigitalOcean, LLC",
"os": "Linux 2.2-3.x",
"rdns": "",
"region": "South Hampton",
"tor": False,
},
"spoofable": False,
"vpn": False,
"vpn_service": "N/A",
},
]
}
}
}
)
def test_greynoise_object(self):
"""Should be advanced"""
noise = p_greynoise_h.GetGreyNoiseObject(self.event)
self.assertEqual(noise.subscription_level(), "advanced")
def test_greynoise_severity(self):
"""Should be CRITICAL"""
sev = p_greynoise_h.GreyNoiseSeverity(self.event, "ClientIP")
self.assertEqual(sev, "CRITICAL")
def test_greynoise_severity_list(self):
"""Should be CRITICAL (list)"""
sev = p_greynoise_h.GreyNoiseSeverity(self.event_list, "p_any_ip_addresses")
self.assertEqual(sev, "CRITICAL")
def test_subscription_level(self):
"""Should be advanced"""
noise = p_greynoise_h.GreyNoiseAdvanced({})
self.assertEqual(noise.subscription_level(), "advanced")
def test_ip_address_not_found(self):
"""Should not find anything"""
noise = p_greynoise_h.GreyNoiseAdvanced({})
ip_address = noise.ip_address("foo")
self.assertEqual(ip_address, None)
def test_ip_address_found(self):
"""Should find enrichment"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
ip_address = noise.ip_address("ClientIP")
self.assertEqual(ip_address, "142.93.204.250")
def test_classification(self):
"""Should classify as malicious"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
classification = noise.classification("ClientIP")
self.assertEqual(classification, "malicious")
def test_actor(self):
"""Should have unknown actor"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
actor = noise.actor("ClientIP")
self.assertEqual(actor, "unknown")
def test_url(self):
"""Should have url"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
url = noise.url("ClientIP")
self.assertEqual(url, "https://www.greynoise.io/viz/ip/142.93.204.250")
def test_is_bot(self):
"""Should be bot"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
is_bot = noise.is_bot("ClientIP")
self.assertEqual(is_bot, False)
def test_cve_string(self):
"""Should have cve string"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
cve_string = noise.cve_string("ClientIP")
self.assertEqual(cve_string, "cve1244 cve4567")
def test_cve_list(self):
"""Should have cve list"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
cve_list = noise.cve_list("ClientIP")
self.assertEqual(cve_list, ["cve1244", "cve4567"])
def test_first_seen(self):
"""Should have first seen"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
first_seen = noise.first_seen("ClientIP")
self.assertEqual(first_seen, datetime.datetime(2022, 3, 19, 0, 0))
def test_first_seen_list(self):
"""Should have first seen (list)"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event_list)
first_seen = noise.first_seen("p_any_ip_addresses")
self.assertEqual(first_seen, datetime.datetime(2022, 2, 19, 0, 0))
def test_last_seen(self):
"""Should have last seen"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
last_seen = noise.last_seen("ClientIP")
self.assertEqual(last_seen, datetime.datetime(2022, 4, 6, 0, 0))
def test_last_seen_list(self):
"""Should have last seen (list)"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event_list)
last_seen = noise.last_seen("p_any_ip_addresses")
self.assertEqual(last_seen, datetime.datetime(2022, 4, 6, 0, 0))
def test_asn(self):
"""Should have asn"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
asn = noise.asn("ClientIP")
self.assertEqual(asn, "AS14061")
def test_category(self):
"""Should have catgeory"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
category = noise.category("ClientIP")
self.assertEqual(category, "hosting")
def test_city(self):
"""Should have city"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
city = noise.city("ClientIP")
self.assertEqual(city, "North Bergen")
def test_country(self):
"""Should have city"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
country = noise.country("ClientIP")
self.assertEqual(country, "United States")
def test_country_code(self):
"""Should have city"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
country_code = noise.country_code("ClientIP")
self.assertEqual(country_code, "US")
def test_organization(self):
"""Should have organization"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
organization = noise.organization("ClientIP")
self.assertEqual(organization, "DigitalOcean, LLC")
def test_operating_system(self):
"""Should have os"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
operating_system = noise.operating_system("ClientIP")
self.assertEqual(operating_system, "Linux 2.2-3.x")
def test_region(self):
"""Should have region"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
region = noise.region("ClientIP")
self.assertEqual(region, "New Jersey")
def test_is_tor(self):
"""Should not have tor"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
is_tor = noise.is_tor("ClientIP")
self.assertEqual(is_tor, False)
def test_rev_dns(self):
"""Should not have rev dns"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
rev_dns = noise.rev_dns("ClientIP")
self.assertEqual(rev_dns, "")
def test_is_spoofable(self):
"""Should not be spoofable"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
is_spoofable = noise.is_spoofable("ClientIP")
self.assertEqual(is_spoofable, False)
def test_tags_string(self):
"""Should have tags string"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
tags_string = noise.tags_string("ClientIP")
self.assertEqual(tags_string, "Mirai ZMap Client")
def test_tags_list(self):
"""Should have tags list"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
tags_list = noise.tags_list("ClientIP")
self.assertEqual(tags_list, ["Mirai", "ZMap Client"])
def test_is_vpn(self):
"""Should not be vpn"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
is_vpn = noise.is_vpn("ClientIP")
self.assertEqual(is_vpn, False)
def test_vpn_service(self):
"""Should not be vpn service"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
vpn_service = noise.vpn_service("ClientIP")
self.assertEqual(vpn_service, "N/A")
def test_metadata(self):
"""Should have metadata"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
metadata = noise.metadata("ClientIP")
self.assertEqual(
metadata,
{
"asn": "AS14061",
"category": "hosting",
"city": "North Bergen",
"country": "United States",
"country_code": "US",
"organization": "DigitalOcean, LLC",
"os": "Linux 2.2-3.x",
"rdns": "",
"region": "New Jersey",
"tor": False,
},
)
def test_context(self):
"""context generation"""
noise = p_greynoise_h.GreyNoiseAdvanced(self.event)
context = noise.context("ClientIP")
self.assertEqual(
context,
{
"Actor": "unknown",
"Classification": "malicious",
"GreyNoise_URL": "https://www.greynoise.io/viz/ip/142.93.204.250",
"IP": "142.93.204.250",
"Metadata": {
"asn": "AS14061",
"category": "hosting",
"city": "North Bergen",
"country": "United States",
"country_code": "US",
"organization": "DigitalOcean, LLC",
"os": "Linux 2.2-3.x",
"rdns": "",
"region": "New Jersey",
"tor": False,
},
"VPN": "N/A",
"Tags": ["Mirai", "ZMap Client"],
"CVE": ["cve1244", "cve4567"],
},
)
class TestRIOTBasic(unittest.TestCase):
def setUp(self):
self.event = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"greynoise_riot_basic": {
"ClientIP": {
"p_match": "142.93.204.250",
"ip_cidr": "142.93.204.250/32",
"provider": {"name": "foo"},
"scan_time": "2023-05-12 05:11:04.679962983",
}
}
}
}
)
def test_greynoise_object(self):
"""Should be basic"""
riot = p_greynoise_h.GetGreyNoiseRiotObject(self.event)
self.assertEqual(riot.subscription_level(), "basic")
def test_subscription_level(self):
"""Should be basic"""
riot = p_greynoise_h.GreyNoiseRIOTBasic({})
self.assertEqual(riot.subscription_level(), "basic")
def test_greynoise_severity(self):
"""Should be INFO"""
sev = p_greynoise_h.GreyNoiseSeverity(self.event, "ClientIP")
self.assertEqual(sev, "INFO")
def test_is_riot(self):
"""Should be riot"""
riot = p_greynoise_h.GreyNoiseRIOTBasic(self.event)
is_riot = riot.is_riot("ClientIP")
self.assertEqual(is_riot, True)
def test_ip_address(self):
"""Should have ip address"""
riot = p_greynoise_h.GreyNoiseRIOTBasic(self.event)
cidr = riot.ip_address("ClientIP")
self.assertEqual(cidr, "142.93.204.250/32")
def test_name(self):
"""Should have name"""
riot = p_greynoise_h.GreyNoiseRIOTBasic(self.event)
name = riot.name("ClientIP")
self.assertEqual(name, "foo")
def test_url(self):
"""Should have url"""
riot = p_greynoise_h.GreyNoiseRIOTBasic(self.event)
url = riot.url("ClientIP")
self.assertEqual(url, "https://www.greynoise.io/viz/ip/142.93.204.250")
def test_last_updated(self):
"""Should have last_updated"""
riot = p_greynoise_h.GreyNoiseRIOTBasic(self.event)
last_udpated = riot.last_updated("ClientIP")
self.assertEqual(last_udpated, datetime.datetime(2023, 5, 12, 5, 11, 4, 679962))
def test_context(self):
"""Should have context"""
riot = p_greynoise_h.GreyNoiseRIOTBasic(self.event)
context = riot.context("ClientIP")
self.assertEqual(
context,
{
"GreyNoise_URL": "https://www.greynoise.io/viz/ip/142.93.204.250",
"IP": "142.93.204.250/32",
"Is_RIOT": True,
"Name": "foo",
},
)
class TestRIOTAdvanced(unittest.TestCase):
def setUp(self):
self.event = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"greynoise_riot_advanced": {
"ClientIP": {
"p_match": "142.93.204.250",
"ip_cidr": "142.93.204.250/32",
"provider": {
"name": "foo",
"category": "cloud",
"description": "some cloud",
"explanation": "because",
"reference": "my brother",
"trust_level": "1",
},
"scan_time": "2023-05-12 05:11:04.679962983",
}
}
}
}
)
# for testing array matches
self.event_list = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
"greynoise_riot_advanced": {
"p_any_ip_addresses": [
{
"p_match": "142.93.204.250",
"ip_cidr": "142.93.204.250/32",
"provider": {
"name": "foo",
"category": "cloud",
"description": "some cloud",
"explanation": "because",
"reference": "my brother",
"trust_level": "1",
},
"scan_time": "2023-05-12 05:11:04.679962983",
},
{
"p_match": "142.93.204.128",
"ip_cidr": "142.93.204.128/32",
"provider": {
"name": "bar",
"category": "cdn",
"description": "some some cdn",
"explanation": "because",
"reference": "my brother",
"trust_level": "2",
},
"scan_time": "2023-05-11 05:11:04.679962983",
},
]
}
}
}
)
def test_greynoise_object(self):
"""Should be advanced"""
riot = p_greynoise_h.GetGreyNoiseRiotObject(self.event)
self.assertEqual(riot.subscription_level(), "advanced")
def test_subscription_level(self):
"""Should be advanced"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced({})
self.assertEqual(riot.subscription_level(), "advanced")
def test_greynoise_severity(self):
"""Should be INFO"""
sev = p_greynoise_h.GreyNoiseSeverity(self.event, "ClientIP")
self.assertEqual(sev, "INFO")
def test_is_riot(self):
"""Should be riot"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
is_riot = riot.is_riot("ClientIP")
self.assertEqual(is_riot, True)
def test_ip_address(self):
"""Should have ip address"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
cidr = riot.ip_address("ClientIP")
self.assertEqual(cidr, "142.93.204.250/32")
def test_name(self):
"""Should have name"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
name = riot.name("ClientIP")
self.assertEqual(name, "foo")
def test_url(self):
"""Should have url"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
url = riot.url("ClientIP")
self.assertEqual(url, "https://www.greynoise.io/viz/ip/142.93.204.250")
def test_last_updated(self):
"""Should have last_updated"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
last_udpated = riot.last_updated("ClientIP")
self.assertEqual(last_udpated, datetime.datetime(2023, 5, 12, 5, 11, 4, 679962))
def test_last_updated_list(self):
"""Should have last_updated (list)"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event_list)
last_udpated = riot.last_updated("p_any_ip_addresses")
self.assertEqual(last_udpated, datetime.datetime(2023, 5, 12, 5, 11, 4, 679962))
def test_description(self):
"""Should have description"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
desc = riot.description("ClientIP")
self.assertEqual(desc, "some cloud")
def test_category(self):
"""Should have category"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
cat = riot.category("ClientIP")
self.assertEqual(cat, "cloud")
def test_explanation(self):
"""Should have explanation"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
exp = riot.explanation("ClientIP")
self.assertEqual(exp, "because")
def test_reference(self):
"""Should have reference"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
ref = riot.reference("ClientIP")
self.assertEqual(ref, "my brother")
def test_trust_level(self):
"""Should have trust_level"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
trust_level = riot.trust_level("ClientIP")
self.assertEqual(trust_level, "1")
def test_trust_level_list(self):
"""Should have trust_level (list)"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event_list)
levels = riot.trust_level("p_any_ip_addresses")
self.assertEqual(levels, ["1", "2"])
def test_context(self):
"""Should have context"""
riot = p_greynoise_h.GreyNoiseRIOTAdvanced(self.event)
context = riot.context("ClientIP")
self.assertEqual(
context,
{
"GreyNoise_URL": "https://www.greynoise.io/viz/ip/142.93.204.250",
"IP": "142.93.204.250/32",
"Is_RIOT": True,
"Name": "foo",
"Provider Data": {
"name": "foo",
"category": "cloud",
"description": "some cloud",
"explanation": "because",
"reference": "my brother",
"trust_level": "1",
},
},
)
class TestIpInfoHelpersLocation(unittest.TestCase):
def setUp(self):
self.match_field = "clientIp"
self.event = ImmutableCaseInsensitiveDict(
{
"p_enrichment": {
p_i_h.IPINFO_LOCATION_LUT_NAME: {
self.match_field: {
"p_match": "12.12.12.12",
"city": "Constantinople",
"country": "Byzantium",
"lat": "41.008610",
"lng": "28.971111",
"postal_code": "NA",
"region": "Asia Minor",
"region_code": "123",
"timezone": "GMT+03:00",
}
}
}
}
)
self.ip_info = p_i_h.get_ipinfo_location(self.event)
def test_city(self):
city = self.ip_info.city(self.match_field)
self.assertEqual(city, "Constantinople")
def test_country(self):
country = self.ip_info.country(self.match_field)
self.assertEqual(country, "Byzantium")
def test_latitude(self):
latitude = self.ip_info.latitude(self.match_field)
self.assertEqual(latitude, "41.008610")
def test_longitude(self):
longitude = self.ip_info.longitude(self.match_field)
self.assertEqual(longitude, "28.971111")