-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_analyzer.py
1189 lines (1003 loc) · 47.7 KB
/
file_analyzer.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
import hashlib
import io
import os
import re
import base64
import binascii
import magic
import zipfile
import tarfile
import gzip
import bz2
import lzma
import math
import string
from io import BytesIO
from PIL import Image
import urllib.parse
import requests
from encoding_detector import detect_encoding, decode_content
from utils import determine_file_type_extension
def generate_file_hash(content):
"""Generates MD5 and SHA256 hashes for file content."""
md5_hash = hashlib.md5(content).hexdigest()
sha256_hash = hashlib.sha256(content).hexdigest()
return {'md5': md5_hash, 'sha256': sha256_hash}
def extract_base64_images_from_html(content):
"""
Extract Base64 encoded images from HTML content
Args:
content (bytes): HTML content as bytes
Returns:
list: List of extracted image files
"""
results = []
try:
# Convert bytes to string
html_str = content.decode('utf-8', errors='ignore')
# Find all base64 encoded data
# Pattern for data URIs: data:[<media type>][;base64],<data>
# More permissive pattern to catch various formats including:
# - standard data:image/png;base64,...
# - data:image/svg+xml;base64,...
# - non-standard variations with different whitespace/quotes
data_uri_pattern = r'data:image/([a-zA-Z0-9\-+.]+);base64,([A-Za-z0-9+/=\s]+)'
matches = re.findall(data_uri_pattern, html_str)
for i, (img_type, base64_data) in enumerate(matches):
try:
# Decode the base64 data
img_data = base64.b64decode(base64_data)
# Determine the file type from the content
detected_type, extension = determine_file_type_extension(img_data)
# Use the detected type if available, otherwise use the one from the data URI
if not extension:
extension = img_type
# Compare hash before adding
decoded_hash = generate_file_hash(img_data)
if decoded_hash['md5'] != generate_file_hash(content)['md5']:
# Add to results
results.append({
"name": f"decoded_image_{i+1}.{extension}",
"content": img_data,
"size": len(img_data),
"type": detected_type or f"image/{img_type}",
"source": "base64_image"
})
print(f"Extracted base64 image {i+1}: {extension} type ({len(img_data)} bytes)")
except Exception as e:
print(f"Error decoding base64 image {i+1}: {str(e)}")
# Also look for inline style background images with the same permissive pattern
style_image_pattern = r'url\(["\']?data:image/([a-zA-Z0-9\-+.]+);base64,([A-Za-z0-9+/=\s]+)["\']?\)'
style_matches = re.findall(style_image_pattern, html_str)
for i, (img_type, base64_data) in enumerate(style_matches):
try:
img_data = base64.b64decode(base64_data)
detected_type, extension = determine_file_type_extension(img_data)
if not extension:
extension = img_type
results.append({
"name": f"extracted_bg_image_{i+1}.{extension}",
"content": img_data,
"size": len(img_data),
"type": detected_type or f"image/{img_type}",
"source": "base64_background_image"
})
print(f"Extracted base64 background image {i+1}: {extension} type ({len(img_data)} bytes)")
except Exception as e:
print(f"Error decoding base64 background image {i+1}: {str(e)}")
# Look for SVG data in the HTML with more accurate validation
# Make pattern more precise to avoid false positives
svg_pattern = r'<svg[^>]*>.*?</svg>'
svg_matches = re.findall(svg_pattern, html_str, re.DOTALL)
for i, svg_content in enumerate(svg_matches):
# Basic validation to filter out false positives
# Valid SVG should have viewport attributes or viewBox
if (re.search(r'width=["\']\\d+(?:px|%|em|rem)?["\']', svg_content) and
re.search(r'height=["\']\d+(?:px|%|em|rem)?["\']', svg_content)) or \
re.search(r'viewBox=["\']\d+\s+\d+\s+\d+\s+\d+["\']', svg_content):
# Additional validation - SVG should have at least one shape or path
if re.search(r'<(?:path|rect|circle|ellipse|line|polyline|polygon|text)[^>]*>', svg_content):
results.append({
"name": f"extracted_svg_{i+1}.svg",
"content": svg_content.encode('utf-8'),
"size": len(svg_content),
"type": "image/svg+xml",
"source": "embedded_svg"
})
print(f"Extracted valid SVG image {i+1}: ({len(svg_content)} bytes)")
else:
print(f"Skipped invalid SVG match {i+1}")
except Exception as e:
print(f"Error extracting base64 images from HTML: {str(e)}")
return results
def safe_process_content(content, filename=None):
"""
Safely process content by ensuring it's only read as raw data
Args:
content (bytes): Raw content to analyze
filename (str, optional): Original filename
Returns:
bytes: Raw content for analysis
"""
try:
# Use BytesIO to ensure content stays in memory and is never executed
buffer = io.BytesIO(content)
# Read raw bytes without any execution
raw_content = buffer.read()
return raw_content
except Exception as e:
print(f"Error in safe content processing: {str(e)}")
return None
def sanitize_url(url):
"""
Sanitize and validate URL to prevent script injection
Args:
url (str): URL to validate
Returns:
tuple: (is_safe, message)
"""
try:
# Ensure URL is string
if not isinstance(url, str):
return False, "Invalid URL format"
# Basic URL structure validation
parsed = urllib.parse.urlparse(url)
# Only allow HTTP/HTTPS
if parsed.scheme not in ('http', 'https'):
return False, "Only HTTP/HTTPS URLs are allowed"
# Check for script injection in URL
dangerous_patterns = [
'javascript:', 'data:', 'vbscript:',
'<script', '<!--', '-->', '<img',
'onerror=', 'onload=', 'eval(',
'document.', 'window.'
]
url_lower = url.lower()
if any(pattern in url_lower for pattern in dangerous_patterns):
return False, "URL contains potentially dangerous content"
return True, "URL validation passed"
except Exception as e:
return False, f"URL validation error: {str(e)}"
def safe_url_fetch(url):
"""
Safely fetch URL content as raw data
Args:
url (str): Validated URL to fetch
Returns:
tuple: (content, error_message)
"""
try:
# Validate URL first
is_safe, message = sanitize_url(url)
if not is_safe:
return None, message
# Fetch content with safety measures
response = requests.get(
url,
timeout=30,
stream=True,
headers={'User-Agent': 'SecurityFileInspector/1.0'},
verify=True # Verify SSL certificates
)
# Stream content to memory buffer
content = io.BytesIO()
for chunk in response.iter_content(chunk_size=8192):
content.write(chunk)
return content.getvalue(), None
except Exception as e:
return None, f"Error fetching URL: {str(e)}"
def analyze_file(content, filename, recursion_depth=0, max_recursion=1, content_hashes=None, thresholds=None):
"""
Analyze file content safely as raw data only
"""
try:
# Process content safely
raw_content = safe_process_content(content, filename)
if raw_content is None:
return []
# If no thresholds provided, use defaults
if thresholds is None:
thresholds = {
0: {
"min_size": 1024, # 1KB
"max_size": 200 * 1024 * 1024, # 200MB
"entropy": 7.5
},
1: {
"min_size": 4096, # 4KB
"max_size": 100 * 1024 * 1024, # 100MB
"entropy": 7.0
},
2: {
"min_size": 8192, # 8KB
"max_size": 50 * 1024 * 1024, # 50MB
"entropy": 6.5
}
}
detected_files = []
# Safety check for recursion
if recursion_depth > max_recursion:
print(f"Maximum recursion depth reached ({max_recursion}), stopping further analysis")
return detected_files
# Initialize or use provided content hashes
if content_hashes is None:
content_hashes = set()
# Add the original file hash and skip if already seen
original_hash = generate_file_hash(raw_content)['md5']
if original_hash in content_hashes:
return []
content_hashes.add(original_hash)
# Add the original file to the list
result = determine_file_type_extension(raw_content)
file_type = result[0]
extension = result[1] if len(result) > 1 else ''
# If the determined extension doesn't match the original filename extension,
# use the determined extension for a more accurate file type
original_extension = os.path.splitext(filename)[1][1:] if '.' in filename else ''
if not original_extension or original_extension.lower() != extension.lower():
if extension:
filename = os.path.splitext(filename)[0] + '.' + extension
detected_files.append({
"name": filename,
"content": raw_content,
"size": len(raw_content),
"type": file_type,
"content_hash": original_hash,
"extension": extension
})
# Special handling for HTML files - look for embedded Base64 images
if file_type == 'text/html' and recursion_depth == 0:
base64_images = extract_base64_images_from_html(raw_content)
if base64_images:
detected_files.extend(base64_images)
# Only explore nested content if we haven't reached max depth
if recursion_depth < max_recursion:
# Check for archive files and extract contents
extracted = extract_from_archive(raw_content, file_type)
if extracted:
detected_files.extend(extracted)
# Check for encoded content
try:
if isinstance(raw_content, bytes):
content_str = raw_content.decode('utf-8', errors='ignore')
else:
content_str = raw_content
encoding_type = detect_encoding(content_str)
if encoding_type:
try:
print(f"Detected encoding: {encoding_type}")
decoded_content = decode_content(content_str, encoding_type)
result = determine_file_type_extension(decoded_content)
decoded_type = result[0]
decoded_ext = result[1] if len(result) > 1 else 'bin'
# Add any decoded content that we can identify
# PDF files and binary data will have a meaningful type other than text/plain
# Also check file size to ensure it's not just a tiny fragment
if len(decoded_content) > 500:
valid_decoded = False
# For PDFs, do enhanced validation
if decoded_content.startswith(b'%PDF'):
# Apply the same robust PDF validation as above
has_pages = b'/Pages' in decoded_content[:2000]
has_endobj = b'endobj' in decoded_content[:5000]
has_startxref = b'startxref' in decoded_content
has_trailer = b'trailer' in decoded_content or b'/Trailer' in decoded_content
has_eof = b'%%EOF' in decoded_content
# Only consider valid if all required markers are present
if has_pages and has_endobj and has_startxref and has_trailer and has_eof:
valid_decoded = True
decoded_type = "application/pdf"
decoded_ext = "pdf"
print("Detected PDF with valid structure in special handling")
else:
print("Rejected PDF with incomplete structure in special handling")
elif decoded_type and decoded_type not in ('text/plain', 'application/octet-stream'):
valid_decoded = True
if valid_decoded:
# Compare hash before adding
decoded_hash = generate_file_hash(decoded_content)['md5']
if decoded_hash not in content_hashes:
content_hashes.add(decoded_hash)
detected_files.append({
"name": f"decoded_{os.path.splitext(filename)[0]}.{decoded_ext}",
"content": decoded_content,
"size": len(decoded_content),
"type": decoded_type,
"encoding": encoding_type
})
# Recursively check decoded content for nested files
if recursion_depth + 1 <= max_recursion:
nested_files = extract_file_content(
decoded_content,
recursion_depth + 1,
max_recursion,
content_hashes=content_hashes,
thresholds=thresholds
)
if nested_files:
detected_files.extend(nested_files)
except Exception as e:
print(f"Error decoding content: {str(e)}")
except Exception as e:
print(f"Error during encoding detection: {str(e)}")
# Look for embedded files and hidden content at recursion level 0
if recursion_depth == 0:
# Check for known hidden content markers
hidden_markers = find_hidden_markers(raw_content)
for pos, encoding in hidden_markers:
hidden_content = extract_hidden_content(raw_content, pos, encoding)
if hidden_content:
# Generate name and analyze extracted content
hidden_name = f"hidden_{encoding}_{len(detected_files)}"
result = determine_file_type_extension(hidden_content)
hidden_type = result[0]
hidden_ext = result[1] if len(result) > 1 else ''
if hidden_ext:
hidden_name += f".{hidden_ext}"
detected_files.append({
"name": hidden_name,
"content": hidden_content,
"size": len(hidden_content),
"type": hidden_type,
"source": f"hidden_{encoding}"
})
# Look for traditionally embedded files
embedded_files = extract_file_content(raw_content, recursion_depth, max_recursion, content_hashes, thresholds)
if embedded_files:
detected_files.extend(embedded_files)
return detected_files
except Exception as e:
print(f"Error in file analysis: {str(e)}")
return []
def extract_from_archive(content, file_type):
"""
Extract files from archive formats (zip, tar, etc.)
Args:
content (bytes): Archive file content
file_type (str): MIME type of the content
Returns:
list: Extracted files with metadata
"""
extracted_files = []
# Handle ZIP files
if file_type == "application/zip" or file_type == "application/x-zip-compressed":
try:
with zipfile.ZipFile(BytesIO(content)) as zip_ref:
for file_info in zip_ref.infolist():
if file_info.file_size > 0 and not file_info.is_dir():
extracted_content = zip_ref.read(file_info.filename)
result = determine_file_type_extension(extracted_content)
extracted_type = result[0]
extension = result[1] if len(result) > 1 else ''
extracted_files.append({
"name": os.path.basename(file_info.filename),
"content": extracted_content,
"size": file_info.file_size,
"type": extracted_type,
"source": "zip"
})
except Exception as e:
# If ZIP extraction fails, continue with other checks
pass
# Handle TAR files
elif file_type in ["application/x-tar", "application/x-gtar"]:
try:
with tarfile.open(fileobj=BytesIO(content), mode="r:*") as tar_ref:
for member in tar_ref.getmembers():
if member.isfile() and member.size > 0:
extracted_content = tar_ref.extractfile(member).read()
result = determine_file_type_extension(extracted_content)
extracted_type = result[0]
extension = result[1] if len(result) > 1 else ''
extracted_files.append({
"name": os.path.basename(member.name),
"content": extracted_content,
"size": member.size,
"type": extracted_type,
"source": "tar"
})
except Exception as e:
# If TAR extraction fails, continue with other checks
pass
# Handle GZIP files
elif file_type == "application/gzip":
try:
with gzip.GzipFile(fileobj=BytesIO(content), mode="rb") as gz_ref:
extracted_content = gz_ref.read()
result = determine_file_type_extension(extracted_content)
extracted_type = result[0]
extension = result[1] if len(result) > 1 else ''
extracted_files.append({
"name": f"extracted.{extension}",
"content": extracted_content,
"size": len(extracted_content),
"type": extracted_type,
"source": "gzip"
})
except Exception as e:
# If GZIP extraction fails, continue with other checks
pass
# Handle BZ2 files
elif file_type == "application/x-bzip2":
try:
extracted_content = bz2.decompress(content)
result = determine_file_type_extension(extracted_content)
extracted_type = result[0]
extension = result[1] if len(result) > 1 else ''
extracted_files.append({
"name": f"extracted.{extension}",
"content": extracted_content,
"size": len(extracted_content),
"type": extracted_type,
"source": "bzip2"
})
except Exception as e:
# If BZ2 extraction fails, continue with other checks
pass
# Handle XZ/LZMA files
elif file_type == "application/x-xz":
try:
extracted_content = lzma.decompress(content)
result = determine_file_type_extension(extracted_content)
extracted_type = result[0]
extension = result[1] if len(result) > 1 else ''
extracted_files.append({
"name": f"extracted.{extension}",
"content": extracted_content,
"size": len(extracted_content),
"type": extracted_type,
"source": "lzma"
})
except Exception as e:
# If LZMA extraction fails, continue with other checks
pass
return extracted_files
def validate_image_content(content, extension):
"""
Strictly validate image content to reduce false positives
Args:
content (bytes): Image content to validate
extension (str): Expected image extension
Returns:
bool: True if valid image, False otherwise
"""
try:
if extension == 'jpg' or extension == 'jpeg':
# JPEG validation
if not content.startswith(b'\xFF\xD8\xFF'):
return False
# Check for proper JPEG structure
# Must have SOI (\xFF\xD8) at start and EOI (\xFF\xD9) at end
if not content.endswith(b'\xFF\xD9'):
return False
# Check for JFIF or Exif marker
if not (b'JFIF' in content[:23] or b'Exif' in content[:23]):
return False
# Minimum size for a valid JPEG (header + minimal image data)
if len(content) < 128:
return False
elif extension == 'png':
# PNG validation
if not content.startswith(b'\x89PNG\r\n\x1a\n'):
return False
# Must have IHDR chunk after signature
if b'IHDR' not in content[8:24]:
return False
# Must have IEND chunk at end
if not content.endswith(b'IEND\xaeB`\x82'):
return False
# Minimum size for a valid PNG
if len(content) < 57: # Header + IHDR + IEND
return False
elif extension == 'gif':
# GIF validation
if not content.startswith((b'GIF87a', b'GIF89a')):
return False
# Check for proper structure (must have global color table and image descriptor)
if len(content) < 38: # Minimum size for GIF header + color table
return False
# Must end with semicolon
if not content.endswith(b'\x3B'):
return False
elif extension == 'bmp':
# BMP validation
if not content.startswith(b'BM'):
return False
# Check minimum size for BMP header
if len(content) < 54: # Standard BMP header size
return False
# Validate BMP header
try:
size = int.from_bytes(content[2:6], 'little')
if size != len(content):
return False
except:
return False
# Additional general image validation
try:
# Try to open and verify the image
img = Image.open(io.BytesIO(content))
img.verify() # Verify image data
# Check reasonable dimensions
if img.size[0] < 8 or img.size[1] < 8: # Minimum 8x8 pixels
return False
if img.size[0] > 16384 or img.size[1] > 16384: # Max 16384x16384 pixels
return False
return True
except:
return False
except Exception as e:
print(f"Error validating image: {str(e)}")
return False
def extract_file_content(content, recursion_depth=0, max_recursion=1, content_hashes=None, thresholds=None):
"""
Extract embedded files from content by looking for file signatures
Args:
content (bytes): Content to analyze
recursion_depth (int): Current recursion depth for nested analysis
max_recursion (int): Maximum allowed recursion depth to prevent deep nested analysis
content_hashes (set): Set to track content hashes
thresholds (dict): Configuration for size and entropy thresholds per level
Returns:
list: Detected embedded files
"""
embedded_files = []
# Initialize set to track content hashes if not provided
if content_hashes is None:
content_hashes = set()
# Convert content to hex for signature scanning
hex_content = content.hex()
# Common file signatures in hex
signatures = {
'ffd8ff': {'ext': 'jpg', 'mime': 'image/jpeg'},
'89504e47': {'ext': 'png', 'mime': 'image/png'},
'4749463837': {'ext': 'gif', 'mime': 'image/gif'},
'4749463839': {'ext': 'gif', 'mime': 'image/gif'},
'25504446': {'ext': 'pdf', 'mime': 'application/pdf'},
'504b0304': {'ext': 'zip', 'mime': 'application/zip'},
'4d5a': {'ext': 'exe', 'mime': 'application/x-msdownload'},
'7f454c46': {'ext': 'elf', 'mime': 'application/x-executable'},
'377abcaf': {'ext': '7z', 'mime': 'application/x-7z-compressed'},
'1f8b08': {'ext': 'gz', 'mime': 'application/gzip'},
'cafebabe': {'ext': 'class', 'mime': 'application/java'},
'526172211a': {'ext': 'rar', 'mime': 'application/x-rar-compressed'},
}
# Scan for file signatures in hex content
for signature, info in signatures.items():
start_pos = 0
while True:
pos = hex_content.find(signature, start_pos)
if pos == -1:
break
# Calculate byte position
byte_pos = pos // 2
# Extract content from signature position
potential_file = content[byte_pos:]
# Validate extracted content
try:
# Get file type using python-magic
detected_type = magic.from_buffer(potential_file, mime=True)
# Additional validation based on file type
is_valid = False
if info['ext'] == 'exe' and potential_file.startswith(b'MZ'):
# Check for PE header
if len(potential_file) > 0x40:
pe_offset = int.from_bytes(potential_file[0x3C:0x40], byteorder='little')
if pe_offset < len(potential_file)-1 and potential_file[pe_offset:pe_offset+2] == b'PE':
is_valid = True
elif info['ext'] in ['jpg', 'jpeg'] and potential_file.startswith(b'\xFF\xD8\xFF'):
# Look for JPEG end marker
if b'\xFF\xD9' in potential_file:
end_pos = potential_file.find(b'\xFF\xD9') + 2
potential_file = potential_file[:end_pos]
is_valid = True
elif info['ext'] == 'png' and potential_file.startswith(b'\x89PNG\r\n\x1A\n'):
# Look for PNG IEND chunk
if b'IEND' in potential_file:
end_pos = potential_file.find(b'IEND') + 8
potential_file = potential_file[:end_pos]
is_valid = True
elif info['ext'] == 'pdf' and potential_file.startswith(b'%PDF'):
# Look for PDF EOF marker
if b'%%EOF' in potential_file:
end_pos = potential_file.find(b'%%EOF') + 5
potential_file = potential_file[:end_pos]
is_valid = True
else:
# For other types, trust the magic number
is_valid = detected_type == info['mime']
if is_valid:
embedded_files.append({
"name": f"embedded_{len(embedded_files)}_{info['ext']}",
"content": potential_file,
"size": len(potential_file),
"type": info['mime'],
"source": "hex_signature",
"extension": info['ext']
})
except Exception:
pass
start_pos = pos + len(signature)
# Only perform deep analysis if recursion depth is permitted
# For low recursion depth settings, we'll be more conservative to prevent false positives
conservative_mode = max_recursion < 2
# Common file signatures (magic numbers) and their corresponding extensions
# Using more specific/longer signatures to reduce false positives
file_signatures = [
# Images - more specific signatures
(b'\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46', 'jpg'), # JPEG/JFIF
(b'\xFF\xD8\xFF\xE1', 'jpg'), # JPEG/Exif
(b'\x89PNG\r\n\x1A\n', 'png'), # PNG - already specific
(b'GIF87a', 'gif'), # GIF87a
(b'GIF89a', 'gif'), # GIF89a
(b'BM\x76\x01', 'bmp'), # BMP with more specific header check
# Documents - more specific signatures where possible
(b'%PDF-1.', 'pdf'), # PDF with version
(b'PK\x03\x04\x14\x00\x06\x00', 'docx'), # DOCX/Office XML
(b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1', 'doc'), # DOC/XLS/PPT (OLE)
# Archives - more specific
(b'PK\x03\x04\x14\x00\x00\x00\x08\x00', 'zip'), # ZIP with specific header
# Executables - more specific signatures to avoid false positives
(b'MZ\x90\x00', 'exe'), # EXE with a more specific header check
(b'\x7FELF\x01\x01\x01', 'elf'), # ELF with more specific header
# Media - already specific
(b'\x00\x00\x00\x18ftypmp4', 'mp4'), # MP4
(b'\x1A\x45\xDF\xA3\x01\x00\x00\x00', 'mkv'), # MKV more specific
(b'ID3\x03\x00', 'mp3'), # MP3 with version check
(b'RIFF....WAVE', 'wav'), # WAV with format check (.... means any 4 bytes)
]
# Convert to bytes if it's a string
if isinstance(content, str):
content = content.encode('utf-8', errors='ignore')
# Search for file signatures in the content
for signature, extension in file_signatures:
offset = 0
while True:
pos = content.find(signature, offset)
if pos == -1:
break
# For signatures with wildcards (e.g., "RIFF....WAVE")
if b'.' in signature:
pattern_parts = signature.split(b'.')
matched = True
current_pos = pos
for part in pattern_parts:
if part: # Skip empty parts
if content[current_pos:current_pos+len(part)] != part:
matched = False
break
current_pos += len(part)
else: # For '.' wildcard
current_pos += 1
if not matched:
offset = pos + 1
continue
# Extract potential file content from signature position
try:
# Try to identify the file type from this position
file_content = content[pos:]
# Skip very small fragments - they're likely false positives
if len(file_content) < 1024: # Require at least 1KB
offset = pos + len(signature)
continue
# Use python-magic to identify file type
file_type = magic.from_buffer(file_content, mime=True)
# Enhanced validation to reduce false positives
valid_file = False
# PDF validation - Check for proper PDF structure with enhanced validation
if extension == 'pdf' and file_content.startswith(b'%PDF-1.'):
# PDFs must have ALL these specific PDF structural elements to be considered valid
has_pages = b'/Pages' in file_content[:2000]
has_endobj = b'endobj' in file_content[:3000]
has_startxref = b'startxref' in file_content
has_trailer = b'trailer' in file_content or b'/Trailer' in file_content
has_eof = b'%%EOF' in file_content
# Only consider valid if ALL required markers are present
valid_file = has_pages and has_endobj and has_startxref and has_trailer and has_eof
# EXE validation - Check for proper PE header structure
elif extension == 'exe' and file_content.startswith(b'MZ'):
# Look for PE header marker which should be present in legitimate executables
pe_header_offset = content[pos+0x3C:pos+0x40]
if len(pe_header_offset) == 4:
try:
pe_offset = int.from_bytes(pe_header_offset, byteorder='little')
# Check if PE signature exists at the calculated offset
if pe_offset < len(file_content) - 2 and file_content[pe_offset:pe_offset+2] == b'PE':
valid_file = True
except:
pass
# ZIP, DOCX validation
elif extension in ('zip', 'docx') and file_content.startswith(b'PK\x03\x04'):
# ZIP files should have at least one directory entry
if b'PK\x01\x02' in file_content[:8192]: # Central directory header
valid_file = True
# Image validation - typically they have reasonable size and proper format
elif extension in ('jpg', 'png', 'gif', 'bmp'):
# Images should have a reasonable size and matching mime type
if (('image/' in file_type) and
len(file_content) > 100 and
len(file_content) < 20 * 1024 * 1024): # 20MB max for images
valid_file = True
# For other file types, use basic mime type validation
elif file_type and file_type != "application/octet-stream" and file_type != "text/plain":
# Additional check: known extension should match detected mime type
if ((extension == 'mp4' and file_type in ('video/mp4', 'application/mp4')) or
(extension == 'mp3' and file_type in ('audio/mpeg', 'audio/mp3')) or
(extension == 'wav' and file_type == 'audio/wav') or
(extension == 'mkv' and file_type in ('video/x-matroska', 'application/x-matroska'))):
valid_file = True
# Apply additional restrictions in conservative mode
if conservative_mode:
# In conservative mode, we only allow certain file types to reduce false positives
if extension in ('exe', 'elf'):
# Disable executable detection completely in conservative mode
valid_file = False
elif extension in ('zip', 'docx'):
# Require stronger ZIP validation in conservative mode
if not (file_content.startswith(b'PK\x03\x04') and
b'PK\x01\x02' in file_content[:4096] and
b'PK\x05\x06' in file_content): # End of central directory
valid_file = False
elif extension == 'pdf':
# In conservative mode, only accept PDFs with perfect structure
if not (file_content.startswith(b'%PDF-1.') and \
b'/Pages' in file_content[:2000] and \
b'endobj' in file_content[:3000] and \
b'startxref' in file_content and \
(b'trailer' in file_content or b'/Trailer' in file_content) and \
b'%%EOF' in file_content):
valid_file = False
# Only add validated files to the results
if valid_file:
# Increase size limit to 200MB
max_size = min(200 * 1024 * 1024, len(file_content)) # 200MB or file size, whichever is smaller
# Check content hash before adding
file_hash = generate_file_hash(file_content[:max_size])['md5']
if file_hash not in content_hashes:
content_hashes.add(file_hash)
embedded_files.append({
"name": f"embedded_{len(embedded_files) + 1}.{extension}",
"content": file_content[:max_size],
"size": max_size,
"type": file_type,
"source": "embedded",
"extension": extension
})
except Exception as e:
print(f"Error processing potential embedded file: {str(e)}")
pass
# Move past this signature with a larger step to avoid overlapping detections
offset = pos + len(signature)
return embedded_files
def detect_steganography_jpeg(content):
"""
Detect potential steganography in JPEG files using signature analysis
Args:
content (bytes): JPEG file content
Returns:
bool: True if steganography is detected
"""
try:
if not content.startswith(b'\xFF\xD8\xFF'): # Verify JPEG signature
return False
# Look for steganography signatures
# Steghide specific markers
steghide_patterns = [
b'JPEG\x00\x01', # Steghide header
b'outguess', # OutGuess marker
b'jsteg', # JSteg marker
]
# Check for abnormal entropy in image sections
sections = content.split(b'\xFF')
for section in sections:
if len(section) > 64: # Minimum section size to check
entropy = calculate_entropy(section[:64])
# Abnormally high entropy in JPEG sections can indicate hidden data
if entropy > 7.9:
return True
# Check for known steganography tool signatures
for pattern in steghide_patterns:
if pattern in content:
return True
# Check for structural anomalies
try:
from PIL import Image
import io
img = Image.open(io.BytesIO(content))
# Check for unusual metadata
if hasattr(img, 'info') and img.info:
# Look for suspicious metadata sizes
for key, value in img.info.items():
if isinstance(value, bytes) and len(value) > 1024:
return True
# Check for unusual color distributions
if img.mode in ('RGB', 'RGBA'):
# Analyze LSB (Least Significant Bit) distribution
pixels = img.load()
lsb_count = 0
sample_size = min(1000, img.width * img.height)
for i in range(min(img.width, int(sample_size ** 0.5))):
for j in range(min(img.height, int(sample_size ** 0.5))):
pixel = pixels[i, j]
# Check LSB of each color channel
for value in pixel[:3]: # RGB channels
if value & 1: # Check LSB
lsb_count += 1
# If LSB distribution is too uniform (close to 50%)
# it might indicate LSB steganography
lsb_ratio = lsb_count / (sample_size * 3) # 3 channels
if 0.45 <= lsb_ratio <= 0.55:
return True
except Exception as e:
print(f"Error in image analysis: {str(e)}")
return False
except Exception as e:
print(f"Error in steganography detection: {str(e)}")
return False
def find_hidden_markers(content):
"""
Detect potential hidden content markers
"""
markers = []
try:
if isinstance(content, bytes):
# Add steganography detection for JPEG files
if content.startswith(b'\xFF\xD8\xFF'): # JPEG signature