-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload.py
More file actions
1321 lines (1106 loc) · 50.7 KB
/
Copy pathload.py
File metadata and controls
1321 lines (1106 loc) · 50.7 KB
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 python3
"""
Advanced Image Payload Injection Tool
A security testing tool for injecting payloads into image files with support
for multiple formats, mutation types, and comprehensive reporting.
WARNING: This tool is for authorized security testing only. Use only on systems
you own or have explicit written permission to test.
Developed By:
- X: @CYFARELABS
- https://cyfare.net/
- t.me/CYFARELABS
- https://github.com/cyfare/IXLoader
"""
import argparse
import os
import shutil
import signal
import concurrent.futures
import multiprocessing
from PIL import Image, PngImagePlugin, ExifTags, TiffImagePlugin
import io
import struct
import time
import traceback
from tqdm import tqdm
import sys
import json
import csv
import hashlib
import random
import string
import uuid
import re
import zlib
import logging
from contextlib import contextmanager
from pathlib import Path
from typing import Dict, List, Tuple, Optional, Callable, Any, Union
from dataclasses import dataclass, asdict, field
from enum import Enum
import glob
try:
import psutil
except ImportError:
psutil = None
# --- Type Definitions ---
class InjectionType(Enum):
HEADER = "header"
BODY = "body"
TRAILER = "trailer"
EXIF = "exif"
XMP = "xmp"
IPTC = "iptc"
ICC = "icc"
TEXT_CHUNK = "text_chunk"
class DosType(Enum):
PIXEL_FLOOD = "pixel_flood"
LONG_BODY = "long_body"
DECOMPRESSION_BOMB = "decompression_bomb"
COLOR_PROFILE = "color_profile"
ICCP_DOS = "iccp_dos"
@dataclass
class MutationResult:
input_path: str
payload_idx: int
mutation: str
output_path: str
sha256: str
size: int
status: str
parses: Optional[bool] = None
error: Optional[str] = None
@dataclass
class ProcessingSummary:
total_tasks: int
successful_mutations: int
failed_mutations: int
by_format: Dict[str, Dict[str, int]]
by_injection_type: Dict[str, Dict[str, int]]
processing_time: float
# --- Constants ---
SUPPORTED_IMAGE_EXTENSIONS: set = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp', '.avif', '.heic', '.ico', '.svg', '.jp2'}
DEFAULT_OUTPUT_DIR: str = 'loaded'
DEFAULT_WORKERS: int = 4
DEFAULT_TASK_TIMEOUT: int = 300
DEFAULT_PNG_TEXT_KEYWORD: str = 'Comment'
@contextmanager
def _max_image_pixels(limit):
"""Temporarily override Pillow's MAX_IMAGE_PIXELS limit."""
prev = Image.MAX_IMAGE_PIXELS
Image.MAX_IMAGE_PIXELS = limit
try:
yield
finally:
Image.MAX_IMAGE_PIXELS = prev
# --- Error Handling (defined early for forward references) ---
class ImageProcessingError(Exception):
"""Custom exception for image processing errors."""
pass
# Format header sizes for correct injection points
FORMAT_HEADERS: Dict[str, int] = {
'png': 8, # PNG signature
'jpg': 2, # SOI marker
'jpeg': 2, # SOI marker
'gif': 6, # GIF87a/GIF89a
'bmp': 2, # BM signature
'tiff': 4, # II/MM + magic
'webp': 12, # RIFF header + WEBP
'ico': 6, # ICONDIR structure
}
# JPEG Markers
JPEG_MARKERS = {
'SOI': b'\xFF\xD8',
'EOI': b'\xFF\xD9',
'APP0': b'\xFF\xE0',
'APP1': b'\xFF\xE1',
'APP13': b'\xFF\xED',
'COM': b'\xFF\xFE',
'SOS': b'\xFF\xDA',
}
# --- Logging Setup ---
def setup_logging(verbose: int, log_file: Optional[str] = None) -> logging.Logger:
"""Configure logging with appropriate verbosity."""
logger = logging.getLogger('image_injector')
logger.setLevel(logging.DEBUG if verbose >= 2 else logging.INFO if verbose >= 1 else logging.WARNING)
# Console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG if verbose >= 2 else logging.INFO if verbose >= 1 else logging.WARNING)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# File handler
if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
# --- Legal Banner ---
def print_legal_banner(logger: logging.Logger, dos_mode: bool = False) -> bool:
"""Print legal warning and require acknowledgment for DoS mode."""
banner = """
╔════════════════════════════════════════════════════════════════╗
║ AUTHORIZED SECURITY TESTING ONLY ║
║ ║
║ This tool is designed for authorized security testing and ║
║ research purposes only. Using this tool against systems ║
║ without explicit written permission is illegal and unethical. ║
║ ║
║ You are responsible for complying with all applicable laws ║
║ and regulations. Misuse of this tool can result in criminal ║
║ charges and civil liability. ║
╚════════════════════════════════════════════════════════════════╝
"""
logger.warning(banner)
if dos_mode:
logger.warning("⚠️ DoS mode selected. This can generate resource-intensive files.")
return False # Require --i-understand flag
return True
# --- Safety Checks ---
def check_output_safety(output_dir: str, force: bool = False, logger: logging.Logger = None) -> bool:
"""Ensure output directory operation is safe."""
if os.path.exists(output_dir):
# Check if directory is empty
contents = os.listdir(output_dir)
if contents and not force:
if logger:
logger.error(f"Output directory '{output_dir}' is not empty. Use --force to overwrite or specify empty directory.")
return False
# Additional safety: prevent wiping root or current directory
abs_path = os.path.abspath(output_dir)
dangerous_paths = ['/', '\\', os.path.expanduser('~'), '/home', '/root', 'C:\\', 'C:/']
if abs_path.rstrip('/') in [p.rstrip('/') for p in dangerous_paths] or abs_path == os.path.abspath('.') and not force:
if logger:
logger.error(f"Refusing to operate on potentially dangerous path: '{output_dir}'. Use --force if you understand the risks.")
return False
return True
_psutil_warned = False
def check_memory_available(required_mb: int, logger: logging.Logger = None) -> bool:
"""Check if sufficient memory is available. Permissive when psutil is unavailable."""
global _psutil_warned
if psutil is None:
if logger and not _psutil_warned:
logger.info("psutil not installed; memory checks disabled")
_psutil_warned = True
return True
try:
available_mb = psutil.virtual_memory().available / (1024 * 1024)
if available_mb < required_mb:
if logger:
logger.warning(f"Insufficient memory: {available_mb:.0f}MB available, {required_mb}MB required")
return False
return True
except Exception:
return True # If psutil fails, proceed with caution
# --- Payload Handling ---
def _tpl_rand(n) -> str:
return ''.join(random.choices(string.ascii_letters + string.digits, k=int(n)))
def _tpl_uuid() -> str:
return str(uuid.uuid4())
def _tpl_timestamp() -> str:
return str(int(time.time()))
class PayloadProcessor:
"""Handle payload parsing, decoding, and templating."""
def __init__(self, format_type: str = 'text', logger: logging.Logger = None):
self.format_type = format_type
self.logger = logger
self.template_vars = {
'RAND': _tpl_rand,
'UUID': _tpl_uuid,
'TIMESTAMP': _tpl_timestamp,
}
def process_payload(self, payload: str, context: Dict = None) -> bytes:
"""Process payload string into bytes with templating support."""
# Apply templating
processed = self._apply_templates(payload, context or {})
# Decode based on format
if self.format_type == 'hex':
return bytes.fromhex(processed.replace(' ', ''))
elif self.format_type == 'base64':
import base64
return base64.b64decode(processed)
elif self.format_type == 'file':
try:
with open(processed, 'rb') as f:
return f.read()
except IOError as e:
if self.logger:
self.logger.error(f"Failed to read payload file {processed}: {e}")
return b''
else: # text
return processed.encode('utf-8', errors='ignore')
def _apply_templates(self, payload: str, context: Dict) -> str:
"""Apply template variables to payload."""
result = payload
# Replace {{FILE}} with context file path if available
if '{{FILE}}' in result and 'file' in context:
result = result.replace('{{FILE}}', context['file'])
# Replace {{DIMS}} with image dimensions if available
if '{{DIMS}}' in result and 'dims' in context:
result = result.replace('{{DIMS}}', context['dims'])
# Replace {{RAND:n}} with random string
for match in re.finditer(r'{{RAND:(\d+)}}', result):
n = int(match.group(1))
replacement = self.template_vars['RAND'](n)
result = result.replace(match.group(0), replacement, 1)
# Replace {{UUID}}
if '{{UUID}}' in result:
result = result.replace('{{UUID}}', self.template_vars['UUID']())
return result
def load_payloads(payloads_path: str, format_type: str = 'text', logger: logging.Logger = None) -> List[str]:
"""Load and return payloads from file."""
try:
with open(payloads_path, 'r', encoding='utf-8', errors='ignore') as f:
if format_type == 'file':
# For file mode, each line is a file path
return [line.strip() for line in f if line.strip()]
else:
return [line.strip() for line in f if line.strip()]
except IOError as e:
if logger:
logger.error(f"Failed to load payloads from {payloads_path}: {e}")
return []
# --- CRC Calculation ---
def calculate_crc32(chunk_type: bytes, chunk_data: bytes) -> int:
"""Calculate CRC32 for PNG chunk."""
return zlib.crc32(chunk_type + chunk_data) & 0xffffffff
def create_png_chunk(chunk_type: bytes, chunk_data: bytes) -> bytes:
"""Create a valid PNG chunk with proper CRC."""
chunk_len = struct.pack('>I', len(chunk_data))
chunk_crc = struct.pack('>I', calculate_crc32(chunk_type, chunk_data))
return chunk_len + chunk_type + chunk_data + chunk_crc
# --- Format Handlers Registry ---
class FormatHandler:
"""Base class for format-specific injection handlers."""
def __init__(self, logger: logging.Logger = None, **handler_kwargs):
self.logger = logger
def inject_header(self, image_data: bytes, payload: bytes) -> bytes:
raise NotImplementedError
def inject_body(self, image_data: bytes, payload: bytes) -> bytes:
raise NotImplementedError
def inject_trailer(self, image_data: bytes, payload: bytes) -> bytes:
return image_data + payload
def inject_exif(self, image_data: bytes, payload: bytes) -> bytes:
return image_data # Default: not supported
def inject_xmp(self, image_data: bytes, payload: bytes) -> bytes:
return image_data # Default: not supported
def inject_text_chunk(self, image_data: bytes, payload: bytes) -> bytes:
return image_data # Default: not supported
class PNGHandler(FormatHandler):
"""PNG-specific injection handler with proper chunk support."""
def __init__(self, logger: logging.Logger = None, png_text_keyword: str = DEFAULT_PNG_TEXT_KEYWORD, **handler_kwargs):
super().__init__(logger)
self.png_text_keyword = png_text_keyword
def inject_header(self, image_data: bytes, payload: bytes) -> bytes:
if len(image_data) < 8:
raise ImageProcessingError("PNG file too short for header injection")
return image_data[:8] + payload + image_data[8:]
def inject_body(self, image_data: bytes, payload: bytes) -> bytes:
# Find first IDAT chunk and insert before it
idat_pos = image_data.find(b'IDAT', 8)
if idat_pos > 8:
insertion_point = idat_pos - 4 # Before length field
return image_data[:insertion_point] + payload + image_data[insertion_point:]
# Fallback: midpoint
mid = len(image_data) // 2
return image_data[:mid] + payload + image_data[mid:]
def inject_text_chunk(self, image_data: bytes, payload: bytes) -> bytes:
# Create tEXt chunk with proper CRC
# tEXt format: keyword (1-79 bytes) + null + text
keyword = self.png_text_keyword.encode('latin-1', errors='replace')
text_data = keyword + b'\x00' + payload[:65535] # Limit size
chunk = create_png_chunk(b'tEXt', text_data)
# Insert before IDAT
idat_pos = image_data.find(b'IDAT', 8)
if idat_pos > 8:
insertion_point = idat_pos - 4
return image_data[:insertion_point] + chunk + image_data[insertion_point:]
return self.inject_body(image_data, chunk)
def inject_icc(self, image_data: bytes, payload: bytes) -> bytes:
# Create iCCP chunk
profile_name = b'ICC\x00'
compressed = zlib.compress(payload) if payload else b'\x78\x9c'
chunk_data = profile_name + b'\x00' + compressed
chunk = create_png_chunk(b'iCCP', chunk_data)
idat_pos = image_data.find(b'IDAT', 8)
if idat_pos > 8:
insertion_point = idat_pos - 4
return image_data[:insertion_point] + chunk + image_data[insertion_point:]
return self.inject_body(image_data, chunk)
class JPEGHandler(FormatHandler):
"""JPEG-specific injection handler with COM/APPn wrapper support."""
def inject_header(self, image_data: bytes, payload: bytes) -> bytes:
if len(image_data) < 2:
raise ImageProcessingError("JPEG file too short for header injection")
return image_data[:2] + payload + image_data[2:]
def inject_body(self, image_data: bytes, payload: bytes) -> bytes:
# Wrap payload in COM segment if not already wrapped
if not payload.startswith(b'\xFF\xFE'):
com_len = min(len(payload) + 2, 65535)
payload = b'\xFF\xFE' + struct.pack('>H', com_len) + payload[:com_len-2]
# Find SOS marker or use midpoint
sos_pos = image_data.find(JPEG_MARKERS['SOS'])
if sos_pos > 0:
if sos_pos + 4 > len(image_data):
raise ImageProcessingError("Truncated JPEG: SOS segment length unreadable")
# Insert after SOS marker and length field
segment_len = struct.unpack('>H', image_data[sos_pos+2:sos_pos+4])[0]
insertion_point = sos_pos + 2 + segment_len
return image_data[:insertion_point] + payload + image_data[insertion_point:]
mid = len(image_data) // 2
return image_data[:mid] + payload + image_data[mid:]
def inject_exif(self, image_data: bytes, payload: bytes) -> bytes:
# Create APP1 segment for EXIF
exif_header = b'Exif\x00\x00'
segment_data = exif_header + payload
segment_len = len(segment_data) + 2
if segment_len > 65535:
segment_data = segment_data[:65533]
segment_len = 65535
segment = b'\xFF\xE1' + struct.pack('>H', segment_len) + segment_data
return self.inject_header(image_data, segment)
def inject_xmp(self, image_data: bytes, payload: bytes) -> bytes:
# Create APP1 segment for XMP
xmp_header = b'http://ns.adobe.com/xap/1.0/\x00'
segment_data = xmp_header + payload
segment_len = min(len(segment_data) + 2, 65535)
segment = b'\xFF\xE1' + struct.pack('>H', segment_len) + segment_data[:segment_len-2]
return self.inject_header(image_data, segment)
class GIFHandler(FormatHandler):
"""GIF-specific injection handler with extension block support."""
def inject_header(self, image_data: bytes, payload: bytes) -> bytes:
if len(image_data) < 6:
raise ImageProcessingError("GIF file too short for header injection")
return image_data[:6] + payload + image_data[6:]
def inject_body(self, image_data: bytes, payload: bytes) -> bytes:
# Create Comment Extension block
# Extension Introducer (0x21) + Comment Label (0xFE) + Sub-blocks + Block Terminator (0x00)
comment_block = b'\x21\xFE'
# Split payload into sub-blocks (max 255 bytes each)
pos = 0
while pos < len(payload):
block_size = min(255, len(payload) - pos)
comment_block += bytes([block_size]) + payload[pos:pos+block_size]
pos += block_size
comment_block += b'\x00'
# Compute search start past header (6) + Logical Screen Descriptor (7) + GCT if present.
# Byte at offset 10 is the packed byte: bit 7 = GCT flag, bits 0-2 = GCT size exponent.
search_start = 13
if len(image_data) >= 11:
packed = image_data[10]
if packed & 0x80: # GCT flag set
gct_size = 3 * (2 ** ((packed & 0x07) + 1))
search_start = 13 + gct_size
img_sep = image_data.find(b'\x2C', search_start) # Image separator, skipping GCT
if img_sep > search_start - 1:
return image_data[:img_sep] + comment_block + image_data[img_sep:]
mid = len(image_data) // 2
return image_data[:mid] + comment_block + image_data[mid:]
class BMPHandler(FormatHandler):
"""BMP-specific injection handler."""
def inject_header(self, image_data: bytes, payload: bytes) -> bytes:
if len(image_data) < 2:
raise ImageProcessingError("BMP file too short")
# BMP header is 14 bytes, DIB header follows
# Insert after file header (14 bytes)
header_size = 14
if len(image_data) < header_size:
return image_data[:2] + payload + image_data[2:]
return image_data[:header_size] + payload + image_data[header_size:]
def inject_body(self, image_data: bytes, payload: bytes) -> bytes:
# Use reserved fields or midpoint
mid = len(image_data) // 2
return image_data[:mid] + payload + image_data[mid:]
# --- Handler Registry ---
HANDLERS: Dict[str, FormatHandler] = {}
def register_handler(ext: str, handler_class: type):
"""Register a format handler."""
HANDLERS[ext.lower()] = handler_class
def get_handler(ext: str, logger: logging.Logger = None, **handler_kwargs) -> FormatHandler:
"""Get appropriate handler for file extension. Extra kwargs are format-specific."""
ext = ext.lower().lstrip('.')
handler_class = HANDLERS.get(ext, FormatHandler)
return handler_class(logger, **handler_kwargs)
# Register handlers
register_handler('png', PNGHandler)
register_handler('jpg', JPEGHandler)
register_handler('jpeg', JPEGHandler)
register_handler('gif', GIFHandler)
register_handler('bmp', BMPHandler)
# --- Injection Functions ---
def inject_payload(image_path: str, payload_bytes: bytes, output_path: str,
injection_type: str, logger: logging.Logger = None,
**handler_kwargs) -> MutationResult:
"""Injects payload into an image using appropriate format handler."""
try:
ext = os.path.splitext(image_path)[1].lower().lstrip('.')
handler = get_handler(ext, logger, **handler_kwargs)
with open(image_path, 'rb') as f:
image_data = f.read()
# Select injection method
injection_method = getattr(handler, f'inject_{injection_type}', None)
if injection_method is None:
injection_method = handler.inject_body # Fallback
new_data = injection_method(image_data, payload_bytes)
# Write output
dirname = os.path.dirname(output_path)
if dirname:
os.makedirs(dirname, exist_ok=True)
with open(output_path, 'wb') as f:
f.write(new_data)
# Calculate hash and size
sha256 = hashlib.sha256(new_data).hexdigest()
size = len(new_data)
return MutationResult(
input_path=image_path,
payload_idx=0, # Set by caller
mutation=injection_type,
output_path=output_path,
sha256=sha256,
size=size,
status='success'
)
except Exception as e:
error_msg = f"Injection failed: {str(e)}"
if logger:
logger.error(error_msg)
return MutationResult(
input_path=image_path,
payload_idx=0,
mutation=injection_type,
output_path=output_path,
sha256='',
size=0,
status='failed',
error=error_msg
)
def validate_output(image_path: str, logger: logging.Logger = None, deep: bool = False) -> bool:
"""Validate that output image can be opened by Pillow. Optionally force pixel decode."""
try:
with _max_image_pixels(None):
with Image.open(image_path) as img:
img.verify()
if deep:
# verify() closes the underlying file; reopen for decode.
with Image.open(image_path) as img:
img.load()
return True
except Exception as e:
if logger:
logger.debug(f"Validation failed for {image_path}: {e}")
return False
# --- DoS Image Creation Functions ---
class DosImageCreator:
"""Create various DoS test images."""
def __init__(self, logger: logging.Logger = None):
self.logger = logger
def create_pixel_flood(self, output_path: str) -> bool:
"""Create large dimension image."""
dims = [(10000, 10000), (5000, 5000), (2000, 2000)]
# Check memory before attempting
required_mb = (dims[0][0] * dims[0][1] * 3) / (1024 * 1024)
if not check_memory_available(required_mb + 500, self.logger):
return False
with _max_image_pixels(None):
for width, height in dims:
try:
if self.logger:
self.logger.info(f"Attempting pixel flood: {width}x{height}")
img = Image.new('RGB', (width, height), color='white')
img.save(output_path)
if self.logger:
self.logger.info(f"Created pixel flood: {output_path}")
return True
except (MemoryError, ValueError) as e:
if self.logger:
self.logger.warning(f"Failed pixel flood {width}x{height}: {e}")
continue
return False
def create_long_body(self, output_path: str) -> bool:
"""Create image with large metadata."""
try:
img = Image.new('RGB', (100, 100), color='white')
ext = os.path.splitext(output_path)[1].lower()
if ext == '.png':
meta = PngImagePlugin.PngInfo()
comment_size = 100 * 1024
num_comments = 100
comment_len = comment_size // num_comments
for i in range(num_comments):
meta.add_text(f"Comment{i}", "A" * comment_len, zip=False)
img.save(output_path, pnginfo=meta)
elif ext in ('.jpg', '.jpeg'):
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=95)
img_data = buffer.getvalue()
# Create large COM segment
com_marker = b'\xFF\xFE'
comment_payload = b'A' * 65533
comment_len_bytes = struct.pack('>H', len(comment_payload) + 2)
insertion_point = 2
new_data = img_data[:insertion_point] + com_marker + comment_len_bytes + comment_payload + img_data[insertion_point:]
with open(output_path, 'wb') as f:
f.write(new_data)
else:
img.save(output_path)
return True
except Exception as e:
if self.logger:
self.logger.error(f"Failed long body creation: {e}")
return False
def create_decompression_bomb(self, output_path: str) -> bool:
"""Create PNG with mismatched declared dimensions."""
try:
width, height = 1, 1
with _max_image_pixels(None):
img = Image.new('RGB', (width, height), color='white')
buffer = io.BytesIO()
img.save(buffer, format="PNG")
img_data = bytearray(buffer.getvalue())
# Find IHDR
ihdr_start = 8
if len(img_data) < ihdr_start + 25:
return False
if img_data[ihdr_start+4:ihdr_start+8] != b'IHDR':
return False
# Modify dimensions
declared_width, declared_height = 30000, 30000
struct.pack_into('>II', img_data, ihdr_start + 8, declared_width, declared_height)
# Recalculate CRC
ihdr_data = bytes(img_data[ihdr_start+8:ihdr_start+8+13])
new_crc = calculate_crc32(b'IHDR', ihdr_data)
struct.pack_into('>I', img_data, ihdr_start + 8 + 13, new_crc)
with open(output_path, 'wb') as f:
f.write(img_data)
return True
except Exception as e:
if self.logger:
self.logger.error(f"Failed decompression bomb: {e}")
return False
def create_iccp_dos(self, output_path: str) -> bool:
"""Create PNG with large iCCP chunk."""
try:
# Check memory
if not check_memory_available(250, self.logger):
return False
img = Image.new('RGB', (100, 100), color='white')
buffer = io.BytesIO()
img.save(buffer, format="PNG")
img_data = buffer.getvalue()
# Find IDAT
idat_pos = img_data.find(b'IDAT')
if idat_pos == -1:
return False
insertion_point = idat_pos - 4
# Create iCCP chunk with proper CRC
profile_name = b'LargeProfile\x00'
compression_method = b'\x00'
# Large but compressible data
large_data = b'\x00' * (200 * 1024 * 1024) # 200MB of zeros
compressed = zlib.compress(large_data, level=9)
chunk_data = profile_name + compression_method + compressed
chunk = create_png_chunk(b'iCCP', chunk_data)
new_data = img_data[:insertion_point] + chunk + img_data[insertion_point:]
with open(output_path, 'wb') as f:
f.write(new_data)
return True
except Exception as e:
if self.logger:
self.logger.error(f"Failed iCCP DoS: {e}")
return False
# --- Worker and Task Management ---
def get_optimal_workers(preferred: Optional[str] = None) -> int:
"""Determine optimal worker count based on workload type."""
cpu_cores = os.cpu_count() or DEFAULT_WORKERS
if preferred == 'process':
return max(1, cpu_cores)
elif preferred == 'thread':
return max(1, cpu_cores * 2)
else:
# Auto: use processes for CPU-bound work
return max(1, min(32, cpu_cores * 2))
def collect_input_files(input_path: str, pattern: str = '*',
recursive: bool = False,
logger: logging.Logger = None) -> List[str]:
"""Collect input files with glob and recursive support."""
files = []
if os.path.isfile(input_path):
if any(input_path.lower().endswith(ext) for ext in SUPPORTED_IMAGE_EXTENSIONS):
files.append(input_path)
elif os.path.isdir(input_path):
if recursive:
for root, _, filenames in os.walk(input_path):
for filename in filenames:
if any(filename.lower().endswith(ext) for ext in SUPPORTED_IMAGE_EXTENSIONS):
files.append(os.path.join(root, filename))
else:
glob_pattern = os.path.join(input_path, pattern)
for filepath in glob.glob(glob_pattern):
if os.path.isfile(filepath) and any(filepath.lower().endswith(ext) for ext in SUPPORTED_IMAGE_EXTENSIONS):
files.append(filepath)
if logger:
logger.info(f"Collected {len(files)} input files")
return files
def generate_safe_filename(base_name: str, ext: str, payload_idx: int,
mutation: str, output_dir: str,
existing: set = None) -> str:
"""Generate safe, unique filename."""
# Preserve original name better
safe_base = re.sub(r'[^\w\-\.]', '_', base_name)
# Add hash suffix to prevent collisions
unique_str = f"{safe_base}_{payload_idx}_{mutation}"
hash_suffix = hashlib.md5(unique_str.encode()).hexdigest()[:8]
filename = f"{safe_base}_p{payload_idx}_m{mutation}_{hash_suffix}{ext}"
output_path = os.path.join(output_dir, filename)
# Handle collisions with counter
if existing and output_path in existing:
counter = 1
while output_path in existing:
filename = f"{safe_base}_p{payload_idx}_m{mutation}_{hash_suffix}_{counter}{ext}"
output_path = os.path.join(output_dir, filename)
counter += 1
return output_path
# --- Worker Configuration ---
@dataclass(frozen=True)
class WorkerConfig:
"""Picklable configuration passed to each worker process."""
verbose: int
log_file: Optional[str]
task_timeout: int
png_text_keyword: str
validate: bool
validate_deep: bool
payload_format: str
resume: bool
# --- Worker-Process State (reset per fork) ---
_worker_logger: Optional[logging.Logger] = None
_worker_payload_processor: Optional[PayloadProcessor] = None
_worker_sigalrm_installed: bool = False
def _timeout_handler(signum, frame):
raise TimeoutError("task exceeded task-timeout seconds")
def _worker_init(cfg: WorkerConfig) -> logging.Logger:
"""Lazy per-worker initialization. Idempotent within a single process."""
global _worker_logger, _worker_payload_processor, _worker_sigalrm_installed
if _worker_logger is None:
_worker_logger = setup_logging(cfg.verbose, cfg.log_file)
if _worker_payload_processor is None:
_worker_payload_processor = PayloadProcessor(cfg.payload_format, _worker_logger)
if cfg.task_timeout > 0 and not _worker_sigalrm_installed:
signal.signal(signal.SIGALRM, _timeout_handler)
_worker_sigalrm_installed = True
return _worker_logger
# --- Task Processing ---
def mutations_for_ext(requested: List[str], ext: str) -> List[str]:
"""Filter the requested mutations to those compatible with a given extension."""
ext = ext.lower()
mutations = []
if 'header' in requested:
mutations.append('header')
if 'body' in requested:
mutations.append('body')
if 'trailer' in requested:
mutations.append('trailer')
if 'exif' in requested and ext in ('.jpg', '.jpeg', '.tiff'):
mutations.append('exif')
if 'xmp' in requested and ext in ('.jpg', '.jpeg'):
mutations.append('xmp')
if 'text_chunk' in requested and ext == '.png':
mutations.append('text_chunk')
return mutations
def process_task(task: Tuple, cfg: WorkerConfig) -> List[MutationResult]:
"""Process a single image-payload task using pre-computed (mutation, output_path) pairs."""
img_path, payload_str, payload_idx, mutation_outputs = task
logger = _worker_init(cfg)
# Build context and process payload once per task
context = {'file': img_path}
try:
with Image.open(img_path) as img:
context['dims'] = f"{img.width}x{img.height}"
except Exception:
pass
payload_bytes = _worker_payload_processor.process_payload(payload_str, context)
results = []
for mutation, output_path in mutation_outputs:
# Skip if resuming and file exists
if cfg.resume and os.path.exists(output_path):
logger.debug(f"Skipping existing file: {output_path}")
continue
try:
if cfg.task_timeout > 0:
signal.alarm(cfg.task_timeout)
result = inject_payload(
img_path, payload_bytes, output_path, mutation, logger,
png_text_keyword=cfg.png_text_keyword,
)
result.payload_idx = payload_idx
if cfg.validate and result.status == 'success':
result.parses = validate_output(output_path, logger, deep=cfg.validate_deep)
except TimeoutError:
logger.warning(f"Timeout on {img_path} mutation={mutation}")
result = MutationResult(
input_path=img_path,
payload_idx=payload_idx,
mutation=mutation,
output_path=output_path,
sha256='',
size=0,
status='failed',
error='timeout',
)
finally:
if cfg.task_timeout > 0:
signal.alarm(0)
results.append(result)
return results
# --- Reporting ---
def save_manifest(results: List[MutationResult], output_dir: str,
format_type: str = 'json', logger: logging.Logger = None):
"""Save results manifest in specified format."""
manifest_path = os.path.join(output_dir, f'manifest.{format_type}')
if format_type == 'json':
data = [asdict(r) for r in results]
with open(manifest_path, 'w') as f:
json.dump(data, f, indent=2)
elif format_type == 'csv':
if results:
with open(manifest_path, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=asdict(results[0]).keys())
writer.writeheader()
for r in results:
writer.writerow(asdict(r))
if logger:
logger.info(f"Manifest saved to {manifest_path}")
def print_summary(summary: ProcessingSummary, logger: logging.Logger):
"""Print formatted summary."""
print("\n" + "="*70)
print("PROCESSING SUMMARY")
print("-"*70)
print(f"Total tasks: {summary.total_tasks}")
print(f"Successful mutations: {summary.successful_mutations}")
print(f"Failed mutations: {summary.failed_mutations}")
print(f"Processing time: {summary.processing_time:.2f}s")
print("-"*70)
print("By format:")
for fmt, stats in summary.by_format.items():
print(f" {fmt:10s}: {stats.get('success', 0)} success, {stats.get('fail', 0)} fail")
print("-"*70)
print("By injection type:")
for inj_type, stats in summary.by_injection_type.items():
print(f" {inj_type:12s}: {stats.get('success', 0)} success, {stats.get('fail', 0)} fail")
print("="*70)
# --- Command Implementations ---
def cmd_inject(args, logger: logging.Logger) -> int:
"""Execute payload injection command."""
start_time = time.time()
# Load payloads
if not args.payloads:
logger.error("Payload file required for injection mode")
return 1
payloads = load_payloads(args.payloads, args.payload_format, logger)
if not payloads:
logger.error("No payloads loaded")
return 1
logger.info(f"Loaded {len(payloads)} payloads")
# File-mode pre-flight: all payload paths must exist before we start work
if args.payload_format == 'file':
missing = [p for p in payloads if not os.path.isfile(p)]
if missing:
for p in missing:
logger.error(f"Payload file not found: {p}")
logger.error(f"{len(missing)} missing payload file(s); aborting before task execution.")
return 1
# Collect input files
image_paths = collect_input_files(args.input, args.pattern, args.recursive, logger)
if not image_paths:
logger.error("No input files found")
return 1
# Prepare tasks: mutation filtering + filename generation happen in the main process
# so the collision set is populated correctly and workers never call generate_safe_filename.
tasks = []
existing: set = set()
skipped_no_mutations = 0
for img_path in image_paths:
ext = os.path.splitext(img_path)[1].lower()
base_name = os.path.splitext(os.path.basename(img_path))[0]
mutations = mutations_for_ext(args.mutations, ext)
if not mutations:
skipped_no_mutations += 1
logger.debug(f"No applicable mutations for {img_path} (ext={ext})")
continue
for i, payload in enumerate(payloads):
payload_idx = i + 1
mutation_outputs = []
for mutation in mutations:
output_path = generate_safe_filename(
base_name, ext, payload_idx, mutation, args.output, existing=existing
)
existing.add(output_path)
mutation_outputs.append((mutation, output_path))