safeuploads 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- safeuploads/__init__.py +92 -0
- safeuploads/config.py +842 -0
- safeuploads/enums.py +455 -0
- safeuploads/exceptions.py +468 -0
- safeuploads/file_validator.py +525 -0
- safeuploads/inspectors/__init__.py +10 -0
- safeuploads/inspectors/zip_inspector.py +411 -0
- safeuploads/protocols.py +51 -0
- safeuploads/validators/__init__.py +22 -0
- safeuploads/validators/base.py +43 -0
- safeuploads/validators/compression_validator.py +286 -0
- safeuploads/validators/extension_validator.py +97 -0
- safeuploads/validators/unicode_validator.py +132 -0
- safeuploads/validators/windows_validator.py +95 -0
- safeuploads-0.1.0.dist-info/METADATA +170 -0
- safeuploads-0.1.0.dist-info/RECORD +18 -0
- safeuploads-0.1.0.dist-info/WHEEL +4 -0
- safeuploads-0.1.0.dist-info/licenses/LICENSE.md +7 -0
safeuploads/enums.py
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
"""Enumeration classes for categorizing security threats and patterns."""
|
|
2
|
+
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DangerousExtensionCategory(Enum):
|
|
7
|
+
"""
|
|
8
|
+
File extension categories considered potentially dangerous for uploads.
|
|
9
|
+
|
|
10
|
+
Attributes:
|
|
11
|
+
WINDOWS_EXECUTABLES: Traditional Windows executable formats.
|
|
12
|
+
SCRIPT_FILES: Script files that can execute code.
|
|
13
|
+
WEB_SCRIPTS: Web server and dynamic content scripts.
|
|
14
|
+
UNIX_EXECUTABLES: Unix/Linux executables and shell scripts.
|
|
15
|
+
MACOS_EXECUTABLES: macOS specific executables and applications.
|
|
16
|
+
JAVA_EXECUTABLES: Java related executables and bytecode.
|
|
17
|
+
MOBILE_APPS: Mobile application packages.
|
|
18
|
+
BROWSER_EXTENSIONS: Browser extensions and web applications.
|
|
19
|
+
PACKAGE_FORMATS: Modern package managers and distribution formats.
|
|
20
|
+
ARCHIVE_FORMATS: Archive formats that can contain executables.
|
|
21
|
+
VIRTUALIZATION_FORMATS: Virtualization and container formats.
|
|
22
|
+
OFFICE_MACROS: Office documents with macro capabilities.
|
|
23
|
+
SYSTEM_FILES: System shortcuts and configuration files.
|
|
24
|
+
SYSTEM_DRIVERS: System drivers and low-level components.
|
|
25
|
+
WINDOWS_THEMES: Windows theme and customization files.
|
|
26
|
+
HELP_FILES: Help and documentation files that can execute code.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
# Traditional Windows executables
|
|
30
|
+
WINDOWS_EXECUTABLES = {
|
|
31
|
+
".exe",
|
|
32
|
+
".bat",
|
|
33
|
+
".cmd",
|
|
34
|
+
".com",
|
|
35
|
+
".pif",
|
|
36
|
+
".scr",
|
|
37
|
+
".msi",
|
|
38
|
+
".dll",
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
# Script files that can execute code
|
|
42
|
+
SCRIPT_FILES = {
|
|
43
|
+
".vbs",
|
|
44
|
+
".js",
|
|
45
|
+
".jse",
|
|
46
|
+
".wsf",
|
|
47
|
+
".wsh",
|
|
48
|
+
".hta",
|
|
49
|
+
".ps1",
|
|
50
|
+
".psm1",
|
|
51
|
+
".ps1xml",
|
|
52
|
+
".psc1",
|
|
53
|
+
".psd1",
|
|
54
|
+
".pssc",
|
|
55
|
+
".cdxml",
|
|
56
|
+
".xaml",
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
# Web server and dynamic content scripts
|
|
60
|
+
WEB_SCRIPTS = {
|
|
61
|
+
".jsp",
|
|
62
|
+
".php",
|
|
63
|
+
".php3",
|
|
64
|
+
".php4",
|
|
65
|
+
".php5",
|
|
66
|
+
".phtml",
|
|
67
|
+
".asp",
|
|
68
|
+
".aspx",
|
|
69
|
+
".cer",
|
|
70
|
+
".cgi",
|
|
71
|
+
".pl",
|
|
72
|
+
".py",
|
|
73
|
+
".rb",
|
|
74
|
+
".go",
|
|
75
|
+
".lua",
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
# Unix/Linux executables and shell scripts
|
|
79
|
+
UNIX_EXECUTABLES = {
|
|
80
|
+
".sh",
|
|
81
|
+
".bash",
|
|
82
|
+
".zsh",
|
|
83
|
+
".fish",
|
|
84
|
+
".csh",
|
|
85
|
+
".ksh",
|
|
86
|
+
".tcsh",
|
|
87
|
+
".run",
|
|
88
|
+
".bin",
|
|
89
|
+
".out",
|
|
90
|
+
".elf",
|
|
91
|
+
".so",
|
|
92
|
+
".a",
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
# macOS specific executables and applications
|
|
96
|
+
MACOS_EXECUTABLES = {
|
|
97
|
+
".app",
|
|
98
|
+
".dmg",
|
|
99
|
+
".pkg",
|
|
100
|
+
".mpkg",
|
|
101
|
+
".command",
|
|
102
|
+
".tool",
|
|
103
|
+
".workflow",
|
|
104
|
+
".action",
|
|
105
|
+
".dylib",
|
|
106
|
+
".bundle",
|
|
107
|
+
".framework",
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
# Java related executables and bytecode
|
|
111
|
+
JAVA_EXECUTABLES = {".jar", ".war", ".ear", ".jnlp", ".class"}
|
|
112
|
+
|
|
113
|
+
# Mobile application packages
|
|
114
|
+
MOBILE_APPS = {".apk", ".aab", ".ipa", ".appx", ".msix", ".xap"}
|
|
115
|
+
|
|
116
|
+
# Browser extensions and web applications
|
|
117
|
+
BROWSER_EXTENSIONS = {
|
|
118
|
+
".crx",
|
|
119
|
+
".xpi",
|
|
120
|
+
".safariextz",
|
|
121
|
+
".oex",
|
|
122
|
+
".nex",
|
|
123
|
+
".gadget",
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
# Modern package managers and distribution formats
|
|
127
|
+
PACKAGE_FORMATS = {
|
|
128
|
+
".deb",
|
|
129
|
+
".rpm",
|
|
130
|
+
".snap",
|
|
131
|
+
".flatpak",
|
|
132
|
+
".appimage",
|
|
133
|
+
".vsix",
|
|
134
|
+
".nupkg",
|
|
135
|
+
".gem",
|
|
136
|
+
".whl",
|
|
137
|
+
".egg",
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
# Archive formats that can contain executables
|
|
141
|
+
ARCHIVE_FORMATS = {
|
|
142
|
+
".7z",
|
|
143
|
+
".rar",
|
|
144
|
+
".cab",
|
|
145
|
+
".ace",
|
|
146
|
+
".arj",
|
|
147
|
+
".lzh",
|
|
148
|
+
".lha",
|
|
149
|
+
".zoo",
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
# Virtualization and container formats
|
|
153
|
+
VIRTUALIZATION_FORMATS = {
|
|
154
|
+
".ova",
|
|
155
|
+
".ovf",
|
|
156
|
+
".vmdk",
|
|
157
|
+
".vdi",
|
|
158
|
+
".vhd",
|
|
159
|
+
".vhdx",
|
|
160
|
+
".qcow2",
|
|
161
|
+
".docker",
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
# Office documents with macro capabilities
|
|
165
|
+
OFFICE_MACROS = {
|
|
166
|
+
".docm",
|
|
167
|
+
".dotm",
|
|
168
|
+
".xlsm",
|
|
169
|
+
".xltm",
|
|
170
|
+
".xlam",
|
|
171
|
+
".pptm",
|
|
172
|
+
".potm",
|
|
173
|
+
".ppam",
|
|
174
|
+
".sldm",
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
# System shortcuts and configuration files
|
|
178
|
+
SYSTEM_FILES = {
|
|
179
|
+
".url",
|
|
180
|
+
".website",
|
|
181
|
+
".webloc",
|
|
182
|
+
".desktop",
|
|
183
|
+
".lnk",
|
|
184
|
+
".application",
|
|
185
|
+
".manifest",
|
|
186
|
+
".deploy",
|
|
187
|
+
".msu",
|
|
188
|
+
".patch",
|
|
189
|
+
".diff",
|
|
190
|
+
".reg",
|
|
191
|
+
".inf",
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
# System drivers and low-level components
|
|
195
|
+
SYSTEM_DRIVERS = {".sys", ".drv", ".ocx", ".cpl"}
|
|
196
|
+
|
|
197
|
+
# Windows theme and customization files
|
|
198
|
+
WINDOWS_THEMES = {
|
|
199
|
+
".theme",
|
|
200
|
+
".themepack",
|
|
201
|
+
".scf",
|
|
202
|
+
".shs",
|
|
203
|
+
".shb",
|
|
204
|
+
".sct",
|
|
205
|
+
".ws",
|
|
206
|
+
".job",
|
|
207
|
+
".msc",
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
# Help and documentation files that can execute code
|
|
211
|
+
HELP_FILES = {".chm", ".hlp"}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class CompoundExtensionCategory(Enum):
|
|
215
|
+
"""
|
|
216
|
+
Categorized compound file extensions that combine multiple suffixes.
|
|
217
|
+
|
|
218
|
+
Attributes:
|
|
219
|
+
COMPRESSED_ARCHIVES: Multi-part archive formats.
|
|
220
|
+
JAVASCRIPT_VARIANTS: Specialized JavaScript files.
|
|
221
|
+
WEB_CONTENT: Minified static web assets.
|
|
222
|
+
"""
|
|
223
|
+
|
|
224
|
+
# Compressed archive formats
|
|
225
|
+
COMPRESSED_ARCHIVES = {
|
|
226
|
+
".tar.xz",
|
|
227
|
+
".tar.gz",
|
|
228
|
+
".tar.bz2",
|
|
229
|
+
".tar.lz",
|
|
230
|
+
".tar.lzma",
|
|
231
|
+
".tar.Z",
|
|
232
|
+
".tgz",
|
|
233
|
+
".tbz2",
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
# JavaScript related compound extensions
|
|
237
|
+
JAVASCRIPT_VARIANTS = {".user.js", ".backup.js", ".min.js", ".worker.js"}
|
|
238
|
+
|
|
239
|
+
# Web content compound extensions
|
|
240
|
+
WEB_CONTENT = {".min.css", ".min.html"}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class UnicodeAttackCategory(Enum):
|
|
244
|
+
"""
|
|
245
|
+
Categorized Unicode code points used in obfuscation attacks.
|
|
246
|
+
|
|
247
|
+
Attributes:
|
|
248
|
+
DIRECTIONAL_OVERRIDES: Right-to-left and directional controls.
|
|
249
|
+
ZERO_WIDTH_CHARACTERS: Zero-width and invisible characters.
|
|
250
|
+
LANGUAGE_MARKS: Language and format specific characters.
|
|
251
|
+
CONFUSING_PUNCTUATION: Punctuation that can disguise extensions.
|
|
252
|
+
"""
|
|
253
|
+
|
|
254
|
+
# Right-to-Left and directional override characters
|
|
255
|
+
DIRECTIONAL_OVERRIDES = {
|
|
256
|
+
0x202E, # U+202E RIGHT-TO-LEFT OVERRIDE
|
|
257
|
+
0x202D, # U+202D LEFT-TO-RIGHT OVERRIDE
|
|
258
|
+
0x202A, # U+202A LEFT-TO-RIGHT EMBEDDING
|
|
259
|
+
0x202B, # U+202B RIGHT-TO-LEFT EMBEDDING
|
|
260
|
+
0x202C, # U+202C POP DIRECTIONAL FORMATTING
|
|
261
|
+
0x2066, # U+2066 LEFT-TO-RIGHT ISOLATE
|
|
262
|
+
0x2067, # U+2067 RIGHT-TO-LEFT ISOLATE
|
|
263
|
+
0x2068, # U+2068 FIRST STRONG ISOLATE
|
|
264
|
+
0x2069, # U+2069 POP DIRECTIONAL ISOLATE
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
# Zero-width and invisible characters
|
|
268
|
+
ZERO_WIDTH_CHARACTERS = {
|
|
269
|
+
0x200B, # U+200B ZERO WIDTH SPACE
|
|
270
|
+
0x200C, # U+200C ZERO WIDTH NON-JOINER
|
|
271
|
+
0x200D, # U+200D ZERO WIDTH JOINER
|
|
272
|
+
0x2060, # U+2060 WORD JOINER
|
|
273
|
+
0xFEFF, # U+FEFF ZERO WIDTH NO-BREAK SPACE (BOM)
|
|
274
|
+
0x034F, # U+034F COMBINING GRAPHEME JOINER
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
# Language and format specific characters
|
|
278
|
+
LANGUAGE_MARKS = {
|
|
279
|
+
0x061C, # U+061C ARABIC LETTER MARK
|
|
280
|
+
0x180E, # U+180E MONGOLIAN VOWEL SEPARATOR
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
# Confusing punctuation that can disguise extensions
|
|
284
|
+
CONFUSING_PUNCTUATION = {
|
|
285
|
+
0x2024, # U+2024 ONE DOT LEADER
|
|
286
|
+
0x2025, # U+2025 TWO DOT LEADER
|
|
287
|
+
0x2026, # U+2026 HORIZONTAL ELLIPSIS
|
|
288
|
+
0xFF0E, # U+FF0E FULLWIDTH FULL STOP
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
class SuspiciousFilePattern(Enum):
|
|
293
|
+
"""
|
|
294
|
+
Categorized patterns used to flag potentially malicious uploads.
|
|
295
|
+
|
|
296
|
+
Attributes:
|
|
297
|
+
DIRECTORY_TRAVERSAL: Directory traversal attack patterns.
|
|
298
|
+
SUSPICIOUS_NAMES: Suspicious filename patterns.
|
|
299
|
+
EXECUTABLE_SIGNATURES: Dangerous file content signatures.
|
|
300
|
+
SUSPICIOUS_PATHS: Suspicious path components.
|
|
301
|
+
"""
|
|
302
|
+
|
|
303
|
+
# Directory traversal attack patterns
|
|
304
|
+
DIRECTORY_TRAVERSAL = {
|
|
305
|
+
"../",
|
|
306
|
+
"..\\",
|
|
307
|
+
".../",
|
|
308
|
+
"...\\",
|
|
309
|
+
"....//",
|
|
310
|
+
"....\\\\",
|
|
311
|
+
"%2e%2e%2f",
|
|
312
|
+
"%2e%2e%5c", # URL encoded ../ and ..\
|
|
313
|
+
"%252e%252e%252f",
|
|
314
|
+
"%252e%252e%255c", # Double URL encoded
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
# Suspicious filename patterns
|
|
318
|
+
SUSPICIOUS_NAMES = {
|
|
319
|
+
# Windows system files that shouldn't be in user uploads
|
|
320
|
+
"autorun.inf",
|
|
321
|
+
"desktop.ini",
|
|
322
|
+
"thumbs.db",
|
|
323
|
+
".ds_store",
|
|
324
|
+
# Common malware names
|
|
325
|
+
"install.exe",
|
|
326
|
+
"setup.exe",
|
|
327
|
+
"update.exe",
|
|
328
|
+
"patch.exe",
|
|
329
|
+
"crack.exe",
|
|
330
|
+
"keygen.exe",
|
|
331
|
+
"loader.exe",
|
|
332
|
+
"activator.exe",
|
|
333
|
+
# Hidden or system-like files
|
|
334
|
+
".htaccess",
|
|
335
|
+
".htpasswd",
|
|
336
|
+
"web.config",
|
|
337
|
+
"robots.txt",
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
# Dangerous file content signatures (magic bytes)
|
|
341
|
+
EXECUTABLE_SIGNATURES = {
|
|
342
|
+
# Windows PE executables
|
|
343
|
+
b"MZ",
|
|
344
|
+
b"PE\x00\x00",
|
|
345
|
+
# ELF executables (Linux)
|
|
346
|
+
b"\x7fELF",
|
|
347
|
+
# Mach-O executables (macOS)
|
|
348
|
+
b"\xfe\xed\xfa\xce",
|
|
349
|
+
b"\xfe\xed\xfa\xcf",
|
|
350
|
+
b"\xce\xfa\xed\xfe",
|
|
351
|
+
b"\xcf\xfa\xed\xfe",
|
|
352
|
+
# Java class files
|
|
353
|
+
b"\xca\xfe\xba\xbe",
|
|
354
|
+
# Windows shortcuts (.lnk)
|
|
355
|
+
b"L\x00\x00\x00",
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
# Suspicious path components
|
|
359
|
+
SUSPICIOUS_PATHS = {
|
|
360
|
+
# Windows system directories
|
|
361
|
+
"windows/",
|
|
362
|
+
"system32/",
|
|
363
|
+
"syswow64/",
|
|
364
|
+
"programfiles/",
|
|
365
|
+
# Unix system directories
|
|
366
|
+
"/bin/",
|
|
367
|
+
"/sbin/",
|
|
368
|
+
"/usr/bin/",
|
|
369
|
+
"/usr/sbin/",
|
|
370
|
+
"/etc/",
|
|
371
|
+
# Web server directories
|
|
372
|
+
"cgi-bin/",
|
|
373
|
+
"htdocs/",
|
|
374
|
+
"www/",
|
|
375
|
+
"wwwroot/",
|
|
376
|
+
# Development/build directories
|
|
377
|
+
".git/",
|
|
378
|
+
".svn/",
|
|
379
|
+
"node_modules/",
|
|
380
|
+
"__pycache__/",
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
class ZipThreatCategory(Enum):
|
|
385
|
+
"""
|
|
386
|
+
Categories of potentially harmful contents within ZIP archives.
|
|
387
|
+
|
|
388
|
+
Attributes:
|
|
389
|
+
NESTED_ARCHIVES: Archive format threats.
|
|
390
|
+
EXECUTABLE_FILES: Executable content threats.
|
|
391
|
+
SCRIPT_FILES: Script and code threats.
|
|
392
|
+
SYSTEM_FILES: System and configuration threats.
|
|
393
|
+
"""
|
|
394
|
+
|
|
395
|
+
# Archive format threats
|
|
396
|
+
NESTED_ARCHIVES = {
|
|
397
|
+
".zip",
|
|
398
|
+
".rar",
|
|
399
|
+
".7z",
|
|
400
|
+
".tar",
|
|
401
|
+
".gz",
|
|
402
|
+
".bz2",
|
|
403
|
+
".xz",
|
|
404
|
+
".tar.gz",
|
|
405
|
+
".tar.bz2",
|
|
406
|
+
".tar.xz",
|
|
407
|
+
".tgz",
|
|
408
|
+
".tbz2",
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
# Executable content threats
|
|
412
|
+
EXECUTABLE_FILES = {
|
|
413
|
+
".exe",
|
|
414
|
+
".com",
|
|
415
|
+
".bat",
|
|
416
|
+
".cmd",
|
|
417
|
+
".scr",
|
|
418
|
+
".pif",
|
|
419
|
+
".bin",
|
|
420
|
+
".run",
|
|
421
|
+
".app",
|
|
422
|
+
".deb",
|
|
423
|
+
".rpm",
|
|
424
|
+
".msi",
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
# Script and code threats
|
|
428
|
+
SCRIPT_FILES = {
|
|
429
|
+
".js",
|
|
430
|
+
".vbs",
|
|
431
|
+
".ps1",
|
|
432
|
+
".sh",
|
|
433
|
+
".bash",
|
|
434
|
+
".py",
|
|
435
|
+
".php",
|
|
436
|
+
".pl",
|
|
437
|
+
".rb",
|
|
438
|
+
".lua",
|
|
439
|
+
".asp",
|
|
440
|
+
".jsp",
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
# System and configuration threats
|
|
444
|
+
SYSTEM_FILES = {
|
|
445
|
+
".dll",
|
|
446
|
+
".so",
|
|
447
|
+
".dylib",
|
|
448
|
+
".sys",
|
|
449
|
+
".drv",
|
|
450
|
+
".inf",
|
|
451
|
+
".reg",
|
|
452
|
+
".cfg",
|
|
453
|
+
".conf",
|
|
454
|
+
".ini",
|
|
455
|
+
}
|