forked from s0md3v/Photon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.py
234 lines (216 loc) · 7.33 KB
/
regex.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
import re
# regex taken from https://github.com/InQuest/python-iocextract
# 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"[>\"'\),};]"
# Get basic url format, including a few obfuscation techniques, main anchor is the uri scheme.
GENERIC_URL = 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|$)
""", re.IGNORECASE | re.VERBOSE | re.UNICODE)
# Get some obfuscated urls, main anchor is brackets around the period.
BRACKET_URL = re.compile(r"""
\b
(
[\.\:\/\\\w\[\]\(\)-]+
(?:
\x20?
[\(\[]
\x20?
\.
\x20?
[\]\)]
\x20?
\S*?
)+
)
""" + END_PUNCTUATION + r"""
(?=\s|$)
""", re.VERBOSE | re.UNICODE)
# Get some obfuscated urls, main anchor is backslash before a period.
BACKSLASH_URL = re.compile(r"""
\b
(
[\:\/\\\w\[\]\(\)-]+
(?:
\x20?
\\?\.
\x20?
\S*?
)*?
(?:
\x20?
\\\.
\x20?
\S*?
)
(?:
\x20?
\\?\.
\x20?
\S*?
)*
)
""" + END_PUNCTUATION + r"""
(?=\s|$)
""", re.VERBOSE | re.UNICODE)
# Get hex-encoded urls.
HEXENCODED_URL = 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.compile(r"""
(s?[hf]t?tps?%3A%2F%2F\w[\w%-]*?)(?:[^\w%-]|$)
""", re.IGNORECASE | re.VERBOSE)
# Get base64-encoded urls.
B64ENCODED_URL = 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]\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]?\d|1\d\d|2[0-4]\d|25[0-5])
(?:(?=[^\d\.])|$)
""", re.VERBOSE)
# Experimental IPv6 regex, will not catch everything but should be sufficent for now.
IPV6 = 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.compile(r"""
(
[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.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{32})(?:[^a-fA-F\d]|\b)")
SHA1 = re.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{40})(?:[^a-fA-F\d]|\b)")
SHA256 = re.compile(r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{64})(?:[^a-fA-F\d]|\b)")
SHA512 = re.compile(
r"(?:[^a-fA-F\d]|\b)([a-fA-F\d]{128})(?:[^a-fA-F\d]|\b)")
# YARA regex.
YARA_PARSE = 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)
CREDIT_CARD = re.compile(r"[0-9]{4}[ ]?[-]?[0-9]{4}[ ]?[-]?[0-9]{4}[ ]?[-]?[0-9]{4}")
rintels = [(GENERIC_URL, "GENERIC_URL"),
(BRACKET_URL, "BRACKET_URL"),
(BACKSLASH_URL, "BACKSLASH_URL"),
(HEXENCODED_URL, "HEXENCODED_URL"),
(URLENCODED_URL, "URLENCODED_URL"),
(B64ENCODED_URL, "B64ENCODED_URL"),
(IPV4, "IPV4"),
(IPV6, "IPV6"),
(EMAIL, "EMAIL"),
(MD5, "MD5"),
(SHA1, "SHA1"),
(SHA256, "SHA256"),
(SHA512, "SHA512"),
(YARA_PARSE, "YARA_PARSE"),
(CREDIT_CARD, "CREDIT_CARD")]
rscript = re.compile(r'<(script|SCRIPT).*(src|SRC)=([^\s>]+)')
rhref = re.compile(r'<[aA].*(href|HREF)=([^\s>]+)')
rendpoint = re.compile(r'[\'"](/.*?)[\'"]|[\'"](http.*?)[\'"]')
rentropy = re.compile(r'[\w-]{16,45}')