-
Notifications
You must be signed in to change notification settings - Fork 4
/
gift-cli.py
163 lines (144 loc) · 6.24 KB
/
gift-cli.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
from gift import Gif
import argparse
def main():
parser = argparse.ArgumentParser(description='Hide and Recover Files in GIFs')
parser.add_argument('mode', choices=['hide', 'recover', 'analyze', 'spread', 'gather'],
help='Mode of operation: "hide" to hide files inside a GIF or "recover" to extract them.')
parser.add_argument('--source', type=str, help='Path to the source GIF file.', required=False)
parser.add_argument('--dest', type=str, help='Path to the destination GIF file.', required=False)
parser.add_argument('filenames', type=str, nargs='+',
help='Arbitrary list of filenames to hide or recover based on the mode.')
args = parser.parse_args()
if args.mode == 'hide':
if not args.source:
parser.error('The --source argument is required for "hide" mode.')
if not args.dest:
parser.error('The --dest argument is required for "hide" mode.')
hide_files(args.source, args.dest, args.filenames)
elif args.mode == 'recover':
if not args.source:
parser.error('The --source argument is required for "hide" mode.')
recover_files(args.source, args.filenames)
elif args.mode == 'analyze':
analyze(args.filenames[0])
elif args.mode == 'spread':
if not args.source:
parser.error('The --source argument is required for "spread" mode.')
if not args.dest:
parser.error('The --dest argument is required for "spread" mode.')
spread_data(args.source, args.dest, args.filenames)
elif args.mode == 'gather':
if not args.source:
parser.error('The --source argument is required for "gather" mode.')
gather_data(args.source, args.filenames)
def hide_files(source_gif, destination_gif, filenames):
print(f'Hiding files in {source_gif} and writing to {destination_gif}')
payloads = []
for filename in filenames:
print(f"We will hide: {filename}")
with open(filename, "rb") as fh:
payload = fh.read()
payloads.append(payload)
with open(source_gif, "rb") as fh, open(destination_gif, "wb") as oh:
print("Doing magic...")
g = Gif(file_handle=fh, hide=True, blobs=payloads)
print(f"Done...now writing to {destination_gif}")
oh.write(g.buffer)
def recover_files(source_gif, filenames):
print(f'Recovering files from {source_gif}')
with open(source_gif, "rb") as fh:
g = Gif(file_handle=fh, recover=True)
output_file_index = 0
for filename in filenames:
print(f'Recovering {filename}')
if len(g.blobs) > output_file_index:
with open(filename, "wb") as oh:
oh.write(g.blobs[output_file_index])
else:
print(f"We don't have that many recovered blobs")
output_file_index += 1
def analyze(gif_file):
with open(gif_file, "rb") as fh:
g = Gif(file_handle=fh)
print("---")
print("GIF INFO")
print("---")
print(f"header = {g.header.decode()}")
print(f"frames = {g.frames}")
print("---")
print("LOGICAL SCREEN DESCRIPTOR")
print("---")
for attr, value in vars(g.LogicalScreenDescriptor).items():
if not callable(value) and not attr.startswith("__"):
print(f"{attr} = {value}")
print("---")
print("GLOBAL COLOR TABLE")
print("---")
for attr, value in vars(g.GlobalColorTable).items():
if not callable(value) and not attr.startswith("__"):
print(f"{attr} = {value}")
print("---")
print("APPLICATION EXTENSIONS")
print("---")
for application_extension in g.application_extensions:
for attr, value in vars(application_extension).items():
if not callable(value) and not attr.startswith("__"):
if attr == "sub_blocks":
for sub_block in value:
print(f"sub_block_size: {sub_block.sub_block_size}")
print(f"sub_block_data: {sub_block.sub_block_data}")
else:
print(f"{attr} = {value}")
print("---")
print("---")
print("IMAGE DESCRIPTORS")
print("---")
for image_descriptor in g.frame_image_descriptors:
for attr, value in vars(image_descriptor).items():
if not callable(value) and not attr.startswith("__"):
print(f"{attr} = {value}")
print("---")
print("---")
print("DUMP FRAMES")
print("---")
g.render_images()
def split_bytearray(data, num_chunks):
chunk_size, remainder = divmod(len(data), num_chunks)
sizes = [chunk_size + (1 if i < remainder else 0) for i in range(num_chunks)]
chunks = []
start = 0
for size in sizes:
chunks.append(data[start:start + size])
start += size
return chunks
def spread_data(source_gif, destination_gif, filenames):
print(f'Hiding file across frames of {source_gif} and writing to {destination_gif}')
payloads = []
filename = filenames[0]
print(f"We will hide: {filename}")
with open(filename, "rb") as fh:
payload = fh.read()
with open(source_gif, "rb") as fh:
gif_info = Gif(file_handle=fh)
number_frames = gif_info.frames
payloads = split_bytearray(payload, number_frames)
print(f"We have split {filename} into {len(payloads)}")
for chunk in payloads:
print(f"Chunk of size {len(chunk)}")
with open(source_gif, "rb") as fh, open(destination_gif, "wb") as oh:
print("Doing magic...")
g = Gif(file_handle=fh, hide=True, blobs=payloads)
print(f"Done...now writing to {destination_gif}")
oh.write(g.buffer)
def gather_data(source_gif, filenames):
print(f'Recovering files from {source_gif}')
with open(source_gif, "rb") as fh:
g = Gif(file_handle=fh, recover=True)
output_filename = filenames[0]
print(f'Recovering {output_filename}')
with open(output_filename, "wb") as oh:
for blob in g.blobs:
print(f"Writing recovered blob of size {len(blob)} to {output_filename}")
oh.write(blob)
if __name__ == '__main__':
main()