-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic.py
120 lines (105 loc) · 4.2 KB
/
basic.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
#!/usr/bin/python3
try:
from PIL import Image
from progress.bar import IncrementalBar
import binascii
except ModuleNotFoundError:
print("\033[1;31m" + "Please install the requirements")
exit()
from common import rgba_to_binary, la_to_binary, binary_to_ascii
class Basic:
def __init__(self, im, bits_quantity, channels, steps):
self.im = im
self.width, self.height = im.size
self.bits_quantity = bits_quantity
self.channels = channels
self.steps = steps
def main(self, bit, channel, height_step, width_step, reversed_height, reversed_width):
""" We browse the image in every directions (bottom -> top / right -> left / ...) and for each directions, we write
the results ascii strings in the basic.txt file.
"""
range_height_pixel = range(0, self.height, height_step)
range_width_pixel = range(0, self.width, width_step)
if reversed_width:
range_width_pixel = reversed(range_width_pixel)
if reversed_height:
range_height_pixel = reversed(range_height_pixel)
# If the image is an indexed-color image, we fetch the palette
if self.im.mode == "P":
# Getting the palette
datas = self.im.palette.getdata() # Experimental method in the doc
plte = binascii.hexlify(datas[1]).decode("utf-8") # This return a long HEX string
# Splitting the HEX string into group of 6 (RGB) (if RGBA then 8)
result = []
for i in range(0, len(plte), 6):
entry = plte[i:i+6]
# Converting each group into a triple
triple = (int(entry[0:2],16), int(entry[2:4],16), int(entry[4:6],16))
result.append(triple)
plte = result
f = open("basic.txt", "a", encoding="utf-8")
# Foreach height, we browse the whole width
pixels = self.im.load()
secret = ''
for height_pixel in range_height_pixel:
for width_pixel in range_width_pixel:
if self.im.mode == "L":
l = pixels[width_pixel, height_pixel]
secret += la_to_binary(bit, channel, l)
elif self.im.mode == "LA":
l, a = pixels[width_pixel, height_pixel]
secret += la_to_binary(bit, channel, l, a)
elif self.im.mode == "RGB":
r, g, b = pixels[width_pixel, height_pixel]
secret += rgba_to_binary(bit, channel, r, g, b)
elif self.im.mode == "RGBA":
r, g, b, a = pixels[width_pixel, height_pixel]
secret += rgba_to_binary(bit, channel, r, g, b, a)
elif self.im.mode == "P":
r, g, b = plte[pixels[width_pixel, height_pixel]]
secret += rgba_to_binary(bit, channel, r, g, b)
f.write(binary_to_ascii(secret))
# Foreach width, we browse the whole height
pixels = self.im.load()
secret = ''
for width_pixel in range_width_pixel:
for height_pixel in range_height_pixel:
if self.im.mode == "L":
l = pixels[width_pixel, height_pixel]
secret += la_to_binary(bit, channel, l)
elif self.im.mode == "LA":
l, a = pixels[width_pixel, height_pixel]
secret += la_to_binary(bit, channel, l, a)
elif self.im.mode == "RGB":
r, g, b = pixels[width_pixel, height_pixel]
secret += rgba_to_binary(bit, channel, r, g, b)
elif self.im.mode == "RGBA":
r, g, b, a = pixels[width_pixel, height_pixel]
secret += rgba_to_binary(bit, channel, r, g, b, a)
elif self.im.mode == "P":
r, g, b = plte[pixels[width_pixel, height_pixel]]
secret += rgba_to_binary(bit, channel, r, g, b)
f.write(binary_to_ascii(secret))
f.close()
def bruteforce(self):
""" 1. Run trough all possibilities of bits
2. Run through every combinations possible of channel
3. Setting up the step amount to maximum 10 (vertically and horizontally)
4. Calling the basic process to browse the image in different directions
"""
maximum = len(self.bits_quantity) * len(self.channels) * len(self.steps)**2 * 5
bar = IncrementalBar('Processing', max=maximum, suffix='%(percent)d%%')
for bit in self.bits_quantity:
for channel in self.channels:
for height_step in self.steps:
for width_step in self.steps:
bar.next()
self.main(bit, channel, height_step, width_step, False, False)
bar.next()
self.main(bit, channel, height_step, width_step, False, True)
bar.next()
self.main(bit, channel, height_step, width_step, True, False)
bar.next()
self.main(bit, channel, height_step, width_step, True, True)
bar.next()
bar.finish()