-
Notifications
You must be signed in to change notification settings - Fork 91
/
iocextract.py
1278 lines (1057 loc) · 35.1 KB
/
iocextract.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
"""
Extract and optionally refang Indicators of Compromise (IOCs) from text.
All methods return iterator objects, not lists.
If for some reason you need a list, you can specify like so: `list(extract_iocs(my_data))`
Otherwise, you can iterate over the objects (e.g. in a `for` loop) normally. Each object yielded from the generators will be of type `str`.
"""
import io
import os
import sys
import json
import base64
import random
import string
import argparse
import requests
import binascii
import itertools
import ipaddress
import regex as re
from pathlib import Path
from string import whitespace
try:
# Python 3
from urllib.parse import urlparse, unquote
unicode = str
except ImportError:
# Python 2
from urlparse import urlparse
from urllib import unquote
# Reusable end punctuation regex
END_PUNCTUATION = r"[\.\?>\"'`\)!,}:;\u201d\u2019\uff1e\uff1c\]]*"
# Reusable regex for symbols commonly used to defang
SEPARATOR_DEFANGS = r"[\(\)\[\]{}<>\\]"
# Split URLs on some characters that may be valid, but may also be garbage
URL_SPLIT_STR = r"[>\"'\),};]"
# Checks for whitespace and trailing characters after the URL
WS_SYNTAX_RM = re.compile(r"\s+/[a-zA-Z]")
def url_re(open_punc=False):
"""
Modified URL regex based on if end puncuation is needed or not.
"""
if open_punc:
# Get basic url format, including a few obfuscation techniques, main anchor is the uri scheme
GENERIC_URL_RE = re.compile(
r"""
(
# Scheme
[fhstu]\S\S?[px]s?
# One of these delimiters/defangs
(?:
:\/\/|
:\\\\|
\[:\]\/\/|
:?__
)
# Any number of defang characters
(?:
\x20|
"""
+ SEPARATOR_DEFANGS
+ r"""
)*
# Domain/path characters
\w
\S+?
# CISCO ESA style defangs followed by domain/path characters
(?:\x20[\/\.][^\.\/\s]\S*?)*
)
"""
+ r"""
(?=\s|[^\x00-\x7F]|$)
""",
re.IGNORECASE | re.VERBOSE | re.UNICODE,
)
else:
# Get basic url format, including a few obfuscation techniques, main anchor is the uri scheme
GENERIC_URL_RE = re.compile(
r"""
(
# Scheme.
[fhstu]\S\S?[px]s?
# One of these delimiters/defangs
(?:
:\/\/|
:\\\\|
\[:\]\/\/|
:?__
)
# Any number of defang characters
(?:
\x20|
"""
+ SEPARATOR_DEFANGS
+ r"""
)*
# Domain/path characters
\w
\S+?
# CISCO ESA style defangs followed by domain/path characters
(?:\x20[\/\.][^\.\/\s]\S*?)*
)
"""
+ END_PUNCTUATION
+ r"""
(?=\s|[^\x00-\x7F]|$)
""",
re.IGNORECASE | re.VERBOSE | re.UNICODE,
)
return GENERIC_URL_RE
# Get some obfuscated urls, main anchor is brackets around the period
BRACKET_URL_RE = re.compile(
r"""
\b
(
[\.\:\/\\\w\[\]\(\)-]+
(?:
\x20?
[\(\[]
\x20?
\.
\x20?
[\]\)]
\x20?
\S*?
)+
)
"""
+ END_PUNCTUATION
+ r"""
(?=\s|[^\x00-\x7F]|$)
""",
re.VERBOSE | re.UNICODE,
)
# Get some obfuscated urls, main anchor is backslash before a period
BACKSLASH_URL_RE = re.compile(
r"""
\b
(
[\.\:\/\\\w\[\]\(\)-]+
(?:
\x20?
\\
\x20?
\.
\x20?
\S*?
)+
)
"""
+ END_PUNCTUATION
+ r"""
(?=\s|[^\x00-\x7F]|$)
""",
re.VERBOSE | re.UNICODE,
)
# Get hex-encoded urls
HEXENCODED_URL_RE = re.compile(
r"""
(
[46][86]
(?:[57]4)?
[57]4[57]0
(?:[57]3)?
3a2f2f
(?:2[356def]|3[0-9adf]|[46][0-9a-f]|[57][0-9af])+
)
(?:[046]0|2[0-2489a-c]|3[bce]|[57][b-e]|[8-f][0-9a-f]|0a|0d|09|[
\x5b-\x5d\x7b\x7d\x0a\x0d\x20
]|$)
""",
re.IGNORECASE | re.VERBOSE,
)
# Get urlencoded urls
URLENCODED_URL_RE = re.compile(
r"(s?[hf]t?tps?%3A%2F%2F\w[\w%-]*?)(?:[^\w%-]|$)", re.IGNORECASE | re.VERBOSE
)
# Get base64-encoded urls
B64ENCODED_URL_RE = re.compile(
r"""
(
# b64re '([hH][tT][tT][pP][sS]|[hH][tT][tT][pP]|[fF][tT][pP])://'
# Modified to ignore whitespace
(?:
[\x2b\x2f-\x39A-Za-z]\s*[\x2b\x2f-\x39A-Za-z]\s*[\x31\x35\x39BFJNRVZdhlptx]\s*[Gm]\s*[Vd]\s*[FH]\s*[A]\s*\x36\s*L\s*y\s*[\x2b\x2f\x38-\x39]\s*|
[\x2b\x2f-\x39A-Za-z]\s*[\x2b\x2f-\x39A-Za-z]\s*[\x31\x35\x39BFJNRVZdhlptx]\s*[Io]\s*[Vd]\s*[FH]\s*[R]\s*[Qw]\s*[O]\s*i\s*\x38\s*v\s*[\x2b\x2f-\x39A-Za-z]\s*|
[\x2b\x2f-\x39A-Za-z]\s*[\x2b\x2f-\x39A-Za-z]\s*[\x31\x35\x39BFJNRVZdhlptx]\s*[Io]\s*[Vd]\s*[FH]\s*[R]\s*[Qw]\s*[Uc]\s*[z]\s*o\s*v\s*L\s*[\x2b\x2f-\x39w-z]\s*|
[\x2b\x2f-\x39A-Za-z]\s*[\x30\x32EGUWkm]\s*[Z]\s*[\x30U]\s*[Uc]\s*[D]\s*o\s*v\s*L\s*[\x2b\x2f-\x39w-z]\s*|
[\x2b\x2f-\x39A-Za-z]\s*[\x30\x32EGUWkm]\s*[h]\s*[\x30U]\s*[Vd]\s*[FH]\s*[A]\s*\x36\s*L\s*y\s*[\x2b\x2f\x38-\x39]\s*|
[\x2b\x2f-\x39A-Za-z]\s*[\x30\x32EGUWkm]\s*[h]\s*[\x30U]\s*[Vd]\s*[FH]\s*[B]\s*[Tz]\s*[O]\s*i\s*\x38\s*v\s*[\x2b\x2f-\x39A-Za-z]\s*|
[RZ]\s*[ln]\s*[R]\s*[Qw]\s*[O]\s*i\s*\x38\s*v\s*[\x2b\x2f-\x39A-Za-z]\s*|
[Sa]\s*[FH]\s*[R]\s*[\x30U]\s*[Uc]\s*[D]\s*o\s*v\s*L\s*[\x2b\x2f-\x39w-z]\s*|
[Sa]\s*[FH]\s*[R]\s*[\x30U]\s*[Uc]\s*[FH]\s*[M]\s*\x36\s*L\s*y\s*[\x2b\x2f\x38-\x39]\s*
)
# Up to 260 characters (pre-encoding, reasonable URL length)
[A-Za-z0-9+/=\s]{1,357}
)
(?=[^A-Za-z0-9+/=\s]|$)
""",
re.VERBOSE,
)
# Get defanged https URL schemes
HTTPS_SCHEME_DEFANG_RE = re.compile("hxxps", re.IGNORECASE)
# Get some valid obfuscated ip addresses
def ipv4_len(ip_len=3):
# Monitors the octet pattern of the extracted IP addresses
if ip_len == 3:
IPV4_RE = re.compile(
r"""
(?:^|
(?![^\d\.])
)
(?:
(?:[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])
[\[\(\\]*?\.[\]\)]*?
){3}
(?:[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])
(?:(?=[^\d\.])|$)(?!\/\d{1,2}(?!\d))
""",
re.VERBOSE,
)
elif ip_len == 4:
IPV4_RE = re.compile(
r"""
(?:^|
(?![^\d\.])
)
(?:
(?:[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])
[\[\(\\]*?\.[\]\)]*?
){4}
([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])
(?:[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])
(?:(?=[^\d\.])|$)(?!\/\d{1,2}(?!\d))
""",
re.VERBOSE,
)
return IPV4_RE
# Experimental IPv6 regex, will not catch everything but should be sufficent for now
IPV6_RE = re.compile(
r"\b(?:[a-f0-9]{1,4}:|:){2,7}(?:[a-f0-9]{1,4}|:)\b", re.IGNORECASE | re.VERBOSE
)
# Capture email addresses including common defangs
EMAIL_RE = re.compile(
r"""
(
[a-z0-9_.+-]+
[\(\[{\x20]*
(?:
(?:
(?:
\x20*
"""
+ SEPARATOR_DEFANGS
+ r"""
\x20*
)*
\.
(?:
\x20*
"""
+ SEPARATOR_DEFANGS
+ r"""
\x20*
)*
|
\W+dot\W+
)
[a-z0-9-]+?
)*
[a-z0-9_.+-]+
[\(\[{\x20]*
(?:@|\Wat\W)
[\)\]}\x20]*
[a-z0-9-]+
(?:
(?:
(?:
\x20*
"""
+ SEPARATOR_DEFANGS
+ r"""
\x20*
)*
\.
(?:
\x20*
"""
+ SEPARATOR_DEFANGS
+ r"""
\x20*
)*
|
\W+dot\W+
)
[a-z0-9-]+?
)+
)
"""
+ END_PUNCTUATION
+ r"""
(?=\s|$)
""",
re.IGNORECASE | re.VERBOSE | re.UNICODE,
)
MD5_RE = re.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{32})(?:[^a-fA-F\d]|\b)")
SHA1_RE = re.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{40})(?:[^a-fA-F\d]|\b)")
SHA256_RE = re.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{64})(?:[^a-fA-F\d]|\b)")
SHA512_RE = re.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{128})(?:[^a-fA-F\d]|\b)")
# YARA regex.
YARA_PARSE_RE = re.compile(
r"""
(?:^|\s)
(
(?:
\s*?import\s+?"[^\r\n]*?[\r\n]+|
\s*?include\s+?"[^\r\n]*?[\r\n]+|
\s*?//[^\r\n]*[\r\n]+|
\s*?/\*.*?\*/\s*?
)*
(?:
\s*?private\s+|
\s*?global\s+
)*
rule\s*?
\w+\s*?
(?:
:[\s\w]+
)?
\s+\{
.*?
condition\s*?:
.*?
\s*\}
)
(?:$|\s)
""",
re.MULTILINE | re.DOTALL | re.VERBOSE,
)
TELEPHONE_RE = re.compile(r"((?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?)")
def extract_iocs(data, refang=False, strip=False):
"""
Extract all IOCs!
Results are returned as an itertools.chain iterable object which
lazily provides the results of the other extract_* generators.
:param data: Input text
:param bool refang: Refang output
:param bool strip: Strip possible garbage from the end of URLs
:rtype: :py:func:`itertools.chain`
"""
return itertools.chain(
extract_urls(data, refang=refang, strip=strip),
extract_ips(data, refang=refang),
extract_emails(data, refang=refang),
extract_hashes(data),
extract_yara_rules(data),
extract_telephone_nums(data)
)
def extract_urls(
data,
refang=False,
strip=False,
delimiter=False,
open_punc=False,
no_scheme=False,
defang=False,
):
"""
Extract URLs!
NOTE: During extraction, if IPv4 addresses are present, you may extract some of those as well.
:param data: Input text
:param bool refang: Refang output
:param bool strip: Strip possible garbage from the end of URLs
:param bool delimiter: Continue extracting even after whitespace is detected
:param bool open_punc: Disabled puncuation regex
:param bool no_scheme: Remove protocol (http, tcp, etc.) type in output
:param bool defang: Extract non-defanged IOCs
:rtype: :py:func:`itertools.chain`
"""
return itertools.chain(
extract_unencoded_urls(
data,
refang=refang,
strip=strip,
open_punc=open_punc,
no_scheme=no_scheme,
defang=defang,
),
extract_encoded_urls(data, refang=refang, strip=strip, delimiter=delimiter),
)
def extract_unencoded_urls(
data, refang=False, strip=False, open_punc=False, no_scheme=False, defang=False
):
"""
Extract only unencoded URLs!
:param data: Input text
:param bool refang: Refang output
:param bool strip: Strip possible garbage from the end of URLs
:param bool open_punc: Disabled puncuation regex
:param bool no_scheme: Remove protocol (http, tcp, etc.) type in output
:param bool defang: Extract non-defanged IOCs
:rtype: Iterator[:class:`str`]
"""
unencoded_urls = itertools.chain(
url_re(open_punc).finditer(data),
BRACKET_URL_RE.finditer(data),
BACKSLASH_URL_RE.finditer(data),
)
for url in unencoded_urls:
if refang or defang:
if refang:
url = refang_data(url.group(1), no_scheme=no_scheme)
if defang:
url = defang_data(url.group(1))
else:
url = url.group(1)
# Checks for whitespace in the string
def found_ws(s):
return True in [check_s in s for check_s in whitespace]
if strip:
if found_ws(url):
url = re.split(WS_SYNTAX_RM, url)[0]
else:
url = re.split(URL_SPLIT_STR, url)[0]
yield url
def extract_encoded_urls(
data, refang=False, strip=False, delimiter=None, parse_json=False
):
"""
Extract only encoded URLs!
:param data: Input text
:param bool refang: Refang output
:param bool strip: Strip possible garbage from the end of URLs
:param bool delimiter: Continue extracting even after whitespace is detected
:param bool parse_json: Allows you to recursively parse JSON data to locate base64 strings
:rtype: Iterator[:class:`str`]
"""
for url in HEXENCODED_URL_RE.finditer(data):
if refang:
yield binascii.unhexlify(url.group(1)).decode("utf-8")
else:
yield url.group(1)
for url in URLENCODED_URL_RE.finditer(data):
if refang:
yield unquote(url.group(1))
else:
yield url.group(1)
for url in B64ENCODED_URL_RE.finditer(data):
# Strip whitespace
url = "".join(url.group(1).split())
# Truncate the string if it's not a multiple of 3 bytes long
# We don't care about the end of the string since it's probably garbage
if len(url) % 4:
url = url[: -(len(url) % 4)]
if refang:
# Decode base64
url = base64.b64decode(url).decode("utf-8", "replace")
# Remove the first 1-2 bytes if we got back extra leading characters from the base64
# The only valid starts are "http" or "ftp", so look for h/f case insensitive
url = url[re.search("[hHfF]", url).start() :]
if delimiter:
pass
else:
# Stop at the first whitespace or non-unicode character
url = url.split("\ufffd")[0].split()[0]
if strip:
url = re.split(URL_SPLIT_STR, url)[0]
yield url
def validate_base64(b64_data):
"""
Validate a string is Base64 encoded.
:param b64_data: Input base64 string
"""
try:
if isinstance(b64_data, str):
base64_bytes = bytes(b64_data, "ascii")
elif isinstance(b64_data, bytes):
base64_bytes = b64_data
else:
raise ValueError("Data type should be a string or bytes")
return base64.b64encode(base64.b64decode(base64_bytes)) == base64_bytes
except Exception:
return False
if parse_json:
try:
try:
for json_data in json.loads(data):
for _, value in json_data.items():
if validate_base64(value):
yield base64.b64decode(value).decode("ascii")
except json.decoder.JSONDecodeError:
pass
except AttributeError:
pass
def extract_ips(data, refang=False):
"""
Extract IP addresses!
Includes both IPv4 and IPv6 addresses.
:param data: Input text
:param bool refang: Refang output
:rtype: :py:func:`itertools.chain`
"""
return itertools.chain(
extract_ipv4s(data, refang=refang),
extract_ipv6s(data),
)
def extract_ipv4s(data, refang=False):
"""
Extract IPv4 addresses!
:param data: Input text
:param bool refang: Refang output
:rtype: Iterator[:class:`str`]
"""
def ipv4_str(data):
"""
Extract schemes from IPv4 addresses.
:param data: IP addresses
"""
protocol_str = re.compile(r"https|http|ftp")
for pro in protocol_str.finditer(data):
if refang:
return refang_ipv4(pro.group(0))
else:
return pro.group(0)
for ip_address in ipv4_len().finditer(data):
# Iterates over any ip address with 4 numbers after the final (3rd) octet
for ip_address in ipv4_len(4).finditer(data):
pass
if refang:
yield refang_ipv4(ip_address.group(0))
else:
yield ip_address.group(0)
def extract_ipv6s(data):
"""
Extract IPv6 addresses!
Not guaranteed to catch all valid IPv6 addresses.
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for ip_address in IPV6_RE.finditer(data):
# Sets a minimal standard for IPv6 (0:0:0:0:0:0:0:0)
if len(ip_address.group(0)) >= 15:
yield ip_address.group(0)
def extract_emails(data, refang=False):
"""
Extract email addresses!
:param data: Input text
:param bool refang: Refang output
:rtype: Iterator[:class:`str`]
"""
for email in EMAIL_RE.finditer(data):
if refang:
email = refang_email(email.group(1))
else:
email = email.group(1)
yield email
def extract_telephone_nums(data):
"""
Extract telephone numbers!
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for tele in TELEPHONE_RE.finditer(data):
yield tele.group(1)
def extract_hashes(data):
"""
Extract MD5/SHA hashes!
Results are returned as an itertools.chain iterable object which lazily provides the results of the other extract_*_hashes generators.
:param data: Input text
:rtype: :py:func:`itertools.chain`
"""
return itertools.chain(
extract_md5_hashes(data),
extract_sha1_hashes(data),
extract_sha256_hashes(data),
extract_sha512_hashes(data),
)
def extract_md5_hashes(data):
"""
Extract MD5 hashes!
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for md5 in MD5_RE.finditer(data):
yield md5.group(1)
def extract_sha1_hashes(data):
"""
Extract SHA1 hashes!
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for sha1 in SHA1_RE.finditer(data):
yield sha1.group(1)
def extract_sha256_hashes(data):
"""
Extract SHA256 hashes!
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for sha256 in SHA256_RE.finditer(data):
yield sha256.group(1)
def extract_sha512_hashes(data):
"""
Extract SHA512 hashes!
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for sha512 in SHA512_RE.finditer(data):
yield sha512.group(1)
def extract_yara_rules(data):
"""
Extract YARA rules!
:param data: Input text
:rtype: Iterator[:class:`str`]
"""
for yara_rule in YARA_PARSE_RE.finditer(data):
yield yara_rule.group(1).strip()
def extract_custom_iocs(data, regex_list):
"""
Extract using custom regex strings!
Need help? Check out the README: https://github.com/inquest/iocextract#custom-regex
:param data: Input text
:param regex_list: List of strings to treat as regex and match against data
:rtype: Iterator[:class:`str`]
"""
# Compile all the regex strings first, so we can error out quickly
regex_objects = []
for regex_string in regex_list:
regex_objects.append(re.compile(regex_string))
# Iterate over regex objects, running each against input data
for regex_object in regex_objects:
for ioc in regex_object.finditer(data):
yield ioc.group(1)
def _is_ipv6_url(url):
"""
URL network location is an IPv6 address, not a domain.
:param url: String URL
:rtype: bool
"""
# Fix urlparse exception.
parsed = urlparse(url)
# Handle RFC 2732 IPv6 URLs with and without port, as well as non-RFC IPv6 URLs
if "]:" in parsed.netloc:
ipv6 = ":".join(parsed.netloc.split(":")[:-1])
else:
ipv6 = parsed.netloc
try:
ipaddress.IPv6Address(unicode(ipv6.replace("[", "").replace("]", "")))
except ValueError:
return False
return True
def _refang_common(ioc):
"""
Remove artifacts from common defangs!
:param ioc: String IP/Email Address or URL netloc
:rtype: str
"""
return (
ioc.replace("[dot]", ".")
.replace("(dot)", ".")
.replace("[.]", ".")
.replace("(", "")
.replace(")", "")
.replace(",", ".")
.replace(" ", "")
.replace("\u30fb", ".")
)
def refang_email(email):
"""
Refang an email address!
:param email: String email address
:rtype: str
"""
# Check for ' at ' and ' dot ' first
email = re.sub("\W[aA][tT]\W", "@", email.lower())
email = re.sub("\W*[dD][oO][tT]\W*", ".", email)
# Then do other char replaces
return (
_refang_common(email)
.replace("[", "")
.replace("]", "")
.replace("{", "")
.replace("}", "")
)
def refang_data(url, no_scheme=False):
"""
Refang a URL!
:param url: String URL
:rtype: str
"""
# First fix urlparse errors
# Fix ipv6 parsing exception
if "[." in url and "[.]" not in url:
url = url.replace("[.", "[.]")
if ".]" in url and "[.]" not in url:
url = url.replace(".]", "[.]")
if "[dot" in url and "[dot]" not in url:
url = url.replace("[dot", "[.]")
if "dot]" in url and "[dot]" not in url:
url = url.replace("dot]", "[.]")
if "[:]" in url:
url = url.replace("[:]", ":")
if "[/]" in url:
url = url.replace("[/]", "/")
# Since urlparse expects a scheme, make sure one exists
if "//" not in url:
if "__" in url[:8]:
# Support http__domain and http:__domain
if ":__" in url[:8]:
url = url.replace(":__", "://", 1)
else:
url = url.replace("__", "://", 1)
elif "\\\\" in url[:8]:
# Support http:\\domain
url = url.replace("\\\\", "//", 1)
else:
# Support no protocol
pass
# Refang (/), since it's not entirely in the netloc.
url = url.replace("(/)", "/")
# Refang some backslash-escaped characters.
url = (
url.replace("\.", ".")
.replace("\(", "(")
.replace("\[", "[")
.replace("\)", ")")
.replace("\]", "]")
)
try:
_ = urlparse(url)
except ValueError:
# Last resort on ipv6 fail
url = url.replace("[", "").replace("]", "")
# Now use urlparse and continue processing
parsed = urlparse(url)
# Handle URLs with no scheme / obfuscated scheme
# Note: ParseResult._replace is a public member, this is safe
if parsed.scheme not in ["http", "https", "ftp"]:
if parsed.scheme.strip("s") in ["ftx", "fxp"]:
scheme = "ftp"
elif HTTPS_SCHEME_DEFANG_RE.fullmatch(parsed.scheme):
scheme = "https"
else:
if no_scheme:
scheme = ""
else:
scheme = "http"
parsed = parsed._replace(scheme=scheme)
replacee = "{}:///".format(scheme)
replacement = "{}://".format(scheme)
url = parsed.geturl().replace(replacee, replacement)
try:
_ = urlparse(url)
except ValueError:
# Last resort on ipv6 fail
url = url.replace("[", "").replace("]", "")
parsed = urlparse(url)
# Remove artifacts from common defangs
parsed = parsed._replace(netloc=_refang_common(parsed.netloc))
parsed = parsed._replace(path=parsed.path.replace("[.]", "."))
# Fix example[.]com, but keep RFC 2732 URLs intact
if not _is_ipv6_url(url):
parsed = parsed._replace(netloc=parsed.netloc.replace("[", "").replace("]", ""))
if no_scheme and parsed.scheme in ['http', 'https']:
return parsed.netloc
return parsed.geturl()
def refang_ipv4(ip_address):
"""
Refang an IPv4 address!
:param ip_address: String IPv4 address
:rtype: str
"""
return (
_refang_common(ip_address).replace("[", "").replace("]", "").replace("\\", "")
)
def defang_data(ioc):
"""
Defang a URL, domain, or IPv4 address!
:param ioc: String URL, domain, or IPv4 address
:rtype: str
"""
# If it's a url, defang just the scheme and netloc
try:
parsed = urlparse(ioc)
if parsed.netloc:
parsed = parsed._replace(
netloc=parsed.netloc.replace(".", "[.]"),
scheme=parsed.scheme.replace("t", "x"),
)
return parsed.geturl()
except ValueError:
pass
# If it's a domain or IP, defang up to the first slash
split_list = ioc.split("/")
defanged = split_list[0].replace(".", "[.]")
# Include everything after the first slash without modification
if len(split_list) > 1:
defanged = "/".join([defanged] + split_list[1:])
return defanged
def main():
"""
Run as a command line interface!
Advanced Indicator of Compromise (IOC) extractor.
If no arguments are specified, the default behavior is to extract all IOCs.
"""
parser = argparse.ArgumentParser(
description="""
Advanced Indicator of Compromise (IOC) extractor.
If no arguments are specified, the default behavior is to extract all IOCs.
"""
)
parser.add_argument(
"-i",
"--input",
type=lambda x: io.open(x, "r", encoding="utf-8", errors="ignore"),
default=io.open(0, "r", encoding="utf-8", errors="ignore"),
help="default: stdin",
)
parser.add_argument(
"-o",