-
Notifications
You must be signed in to change notification settings - Fork 0
/
esteg.py
executable file
·327 lines (261 loc) · 10.3 KB
/
esteg.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
#!/usr/bin/env python3
# Copyright (C) 2021 Jithin Renji
#
# ESteg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ESteg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
import argparse
import traceback
import struct
from sys import stderr
from getpass import getpass
from Crypto import Cipher
from PIL import Image
from colorama import Fore, Back, Style
from Crypto.Protocol.KDF import scrypt
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
def get_nth_bit(val: int, n: int) -> int:
"""Get the nth bit from the left."""
return (val & (0b10000000 >> (n - 1))) >> (7 - (n - 1))
def set_nth_bit(val: int, n: int) -> int:
"""Set nth bit from the left."""
val |= 0b10000000 >> (n - 1)
return val
def unset_nth_bit(val: int, n: int) -> int:
"""Unset nth bit from the left"""
val &= ~(0b10000000 >> (n - 1))
return val
def set_nth_bit_to_bit(val: int, bit: int, n: int) -> int:
"""Set the nth bit from the left to bit."""
if get_nth_bit(val, n) == 1:
if bit == 0:
val = unset_nth_bit(val, n)
elif get_nth_bit(val, n) == 0:
if bit == 1:
val = set_nth_bit(val, n)
return val
def encrypt(msg: bytes, passphrase: bytes) -> bytes:
"""Encrypt a byte string using a given passphrase.
Arguments:
msg -- message to be encrypted
passphrase -- passphrase for encryption
"""
salt = get_random_bytes(16)
key = scrypt(passphrase, salt, 16, 2**14, r=8, p=1)
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(msg)
return salt + cipher.nonce + tag + ciphertext
def decrypt(encrypted_msg: bytes, passphrase: bytes, salt: bytes, nonce: bytes,
tag:bytes) -> bytes:
"""Decrypt a byte string.
Arguments:
encrypted_msg: encrypted message, ie. ciphertext
passphrase: passphrase which was used for encryption
salt: salt which was used for encryption
nonce: nonce which was used for encryption
tag: tag which was used for encryption
"""
key = scrypt(passphrase, salt, 16, 2**14, r=8, p=1)
cipher = AES.new(key, AES.MODE_EAX, nonce)
decrypted_msg = cipher.decrypt_and_verify(encrypted_msg, tag)
return decrypted_msg
def embed(msg_fname: str, img_fname: str) -> None:
"""Embed the contents of a given text file in an image.
Arguments:
msg_fname -- name of the file to be embedded
img_fname -- name of the image in which the file should be embedded in
"""
try:
img = Image.open(img_fname)
pixels = img.getdata()
pix_data = []
if img.format != 'PNG':
raise Exception("Only PNG images are supported.")
# Convert list of triplets to list of values
for pixel in pixels:
for val in pixel:
pix_data.append(val)
with open(msg_fname) as fhandle:
passphrase = bytes(getpass("Enter encryption passphrase: ").encode('ascii'))
if len(passphrase) == 0:
raise Exception("Encryption passphrase cannot be empty.")
confirmed_passphrase = bytes(getpass("Confirm encryption passphrase: ").encode('ascii'))
if passphrase != confirmed_passphrase:
raise Exception("Passphrases don't match.")
print(
Fore.GREEN + Style.BRIGHT +
f"\nEmbedding" +
Fore.WHITE + Style.BRIGHT +
f" '{msg_fname}'" +
Fore.GREEN + Style.BRIGHT +
f" in " +
Fore.WHITE + Style.BRIGHT +
f"'{img_fname}' " +
Fore.GREEN + Style.BRIGHT +
f"..." +
Style.RESET_ALL,
file=stderr
)
msg = encrypt(bytes(fhandle.read(), encoding='ascii'), passphrase)
# First 48 bytes returned by encrypt() contain the salt, nonce,
# and tag
msg_len = (len(msg) - 48).to_bytes(4, byteorder='big')
msg = msg_len + msg
# Each component of a pixel will store 2 bits of each byte in the
# message. So, len(msg) * 4 should be <= len(data)
if len(msg) * 4 > len(pix_data):
raise Exception("Message is too long.")
duplets = []
# Split each byte of the messege into duplets
for byte in msg:
for i in range(7, -1, -2):
duplets.append((byte & (0b11000000 >> (7 - i))) >> (i - 1))
# Change the 7th and 8th bits of each value to the 7th and 8th
# bit of each duplet.
for val, i in zip(pix_data, range(len(duplets))):
pix_data[i] = set_nth_bit_to_bit(pix_data[i], get_nth_bit(duplets[i], 7), 7)
pix_data[i] = set_nth_bit_to_bit(pix_data[i], get_nth_bit(duplets[i], 8), 8)
steg_img = Image.frombytes('RGB', (img.width, img.height), bytes(pix_data))
steg_img.save(img_fname + "_esteg.png")
print(
Fore.GREEN + Style.BRIGHT +
f"Done! Steganographic image saved to " +
Fore.WHITE + Style.BRIGHT +
f"'{img_fname + '_esteg.png'}' " +
Fore.GREEN + Style.BRIGHT +
f".",
file=stderr
)
except Exception as e:
# print(traceback.format_exc())
print(
Fore.RED + Style.BRIGHT +
"Error: " + Fore.RESET + str(e) +
Style.RESET_ALL, file=stderr
)
def read_steg_bytes(num_bytes: int, data: list) -> bytes:
"""Read the last 2 bytes from each byte in a list, concatenate them
into bytes, and return them.
Arguments:
num_bytes -- number of bytes to read from the list
data -- list containing bytes
"""
if num_bytes % 4 != 0:
raise ValueError("num_bytes must be a multiple of 4.")
byte_str = b""
duplets = []
for i in range(num_bytes):
duplet = 0
duplet = set_nth_bit_to_bit(duplet, get_nth_bit(data[i], 7), 7)
duplet = set_nth_bit_to_bit(duplet, get_nth_bit(data[i], 8), 8)
duplets.append(duplet)
if len(duplets) == 4:
byte = 0
for dup, j in zip(duplets, range(6, -1, -2)):
byte |= (dup << j)
byte_str += byte.to_bytes(1, byteorder='big')
duplets.clear()
return byte_str
def extract(img_fname: str) -> str:
"""Extract message from image.
Arguments:
img_fname -- name of the image file
"""
try:
img = Image.open(img_fname)
if img.format != 'PNG':
raise Exception("Only PNG images are supported.")
print(
Fore.GREEN + Style.BRIGHT +
f"Extracting message from " +
Fore.WHITE + Style.BRIGHT +
f"'{img_fname}' " +
Fore.GREEN + Style.BRIGHT +
f"..." +
Style.RESET_ALL, file=stderr
)
pixels = img.getdata()
pix_data = []
# Convert list of triplets to list of values
for pixel in pixels:
for val in pixel:
pix_data.append(val)
# First 16 bytes contain the length of the message.
size = read_steg_bytes(16, pix_data)
size = int.from_bytes(size, byteorder='big')
pix_data = pix_data[16:]
# Next 64 bytes contain the salt.
salt = read_steg_bytes(64, pix_data)
pix_data = pix_data[64:]
# Next 64 bytes contain the nonce.
nonce = read_steg_bytes(64, pix_data)
pix_data = pix_data[64:]
# Next 64 bytes contain the tag
tag = read_steg_bytes(64, pix_data)
pix_data = pix_data[64:]
print(
Fore.GREEN + Style.BRIGHT +
f"Probable encrypted message length: " +
Fore.WHITE + Style.BRIGHT +
f"{size}\n" +
Style.RESET_ALL, file=stderr
)
encrypted_msg = b""
duplets = []
for i in range(size * 4):
duplet = 0
duplet = set_nth_bit_to_bit(duplet, get_nth_bit(pix_data[i], 7), 7)
duplet = set_nth_bit_to_bit(duplet, get_nth_bit(pix_data[i], 8), 8)
duplets.append(duplet)
if len(duplets) == 4:
byte = 0
for dup, j in zip(duplets, range(6, -1, -2)):
byte |= (dup << j)
encrypted_msg += byte.to_bytes(1, byteorder='big')
duplets.clear()
passphrase = bytes(getpass("Enter decryption passphrase: ").encode('ascii'))
msg = decrypt(encrypted_msg, passphrase, salt, nonce, tag).decode('ascii')
print(
Fore.GREEN + Style.BRIGHT +
f"\nProbable message:" +
Style.RESET_ALL, file=stderr
)
print(msg, end="")
except Exception as e:
if str(e).lower() == "mac check failed":
print(
Fore.RED + Style.BRIGHT +
"Error: " + Fore.RESET + "Invalid passphrase." +
Style.RESET_ALL, file=stderr
)
else:
print(
Fore.RED + Style.BRIGHT +
"Error: " + Fore.RESET + str(e) +
Style.RESET_ALL, file=stderr
)
def main() -> None:
parser = argparse.ArgumentParser(description="Simple steganography program")
parser.add_argument('img_fname', metavar='IMG_FNAME', type=str,
help="name of the image file")
parser.add_argument('--version', action='version', version='ESteg v0.2',
help="show version infomation and exit")
parser.add_argument('--embed', type=str, metavar="FNAME",
help="embed FNAME in IMG_FNAME [default behavior is to extract]")
args = parser.parse_args()
if args.embed is not None:
embed(args.embed, args.img_fname)
else:
extract(args.img_fname)
if __name__ == '__main__':
main()