-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpit.py
213 lines (183 loc) · 6.95 KB
/
pit.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
#!/usr/bin/python3
try:
from PIL import Image
from progress.bar import IncrementalBar
except ModuleNotFoundError:
print("\033[1;31m" + "Please install the requirements")
exit()
from common import parity_bit, is_prime, binary_to_ascii
class Pit:
def __init__(self, im, steps):
self.im = im
self.width, self.height = im.size
self.steps = steps
self.channels = ["RGB", "RBG", "GRB", "GBR", "BGR", "BRG"]
def get_secret_length(self, reversed_height, reversed_width, vertical):
"""Since PIT is based on an indicator channel determined by the size of the secret, we need to find it.
The secret length is always in the first row of the image (in the first 8 bytes (so 3 pixels - 1 byte))
"""
pixels = self.im.load()
secret_length_binary = ''
i = 0
while i < 3:
if vertical:
if reversed_width and reversed_height:
r, g, b = pixels[self.width - 1, self.height - i - 1]
elif reversed_width:
r, g, b = pixels[self.width - 1, i]
elif reversed_height:
r, g, b = pixels[0, self.height - i - 1]
else:
r, g, b = pixels[0, i]
else:
if reversed_width and reversed_height:
r, g, b = pixels[self.width - i - 1, self.height - 1]
elif reversed_width:
r, g, b = pixels[self.width - i - 1, 0]
elif reversed_height:
r, g, b = pixels[i, self.height - 1]
else:
r, g, b = pixels[i, 0]
r_binary = '{0:08b}'.format(r)
g_binary = '{0:08b}'.format(g)
b_binary = '{0:08b}'.format(b)
secret_length_binary += r_binary + g_binary + b_binary
i += 1
secret_length = secret_length_binary[:-8]
return int(secret_length, 2) // 8
def find_pattern(self, secret_length):
"""To find the pattern (so to find the indicator, the first channel & the second channel) we need to follow some
rules based on the length of the secret :
╔════════════╦══════╦═══════╦═══════╗
║ Parity bit ║ Even ║ Prime ║ Other ║
╠════════════╬══════╬═══════╬═══════╣
║ Even ║ RBG ║ BGR ║ GBR ║
╠════════════╬══════╬═══════╬═══════╣
║ Odd ║ RGB ║ BRG ║ GRB ║
╚════════════╩══════╩═══════╩═══════╝
For example, if the length of the message is 17 :
- 17 isnt even, but is a prime number --> Indicator == B
- 17 in binary is 10001 so the parity is even --> first channel is G & second channel is R
"""
if secret_length % 2 == 0:
pattern = "R"
if parity_bit(secret_length):
pattern += "BG"
else:
pattern += "GB"
elif is_prime(secret_length):
pattern = "B"
if parity_bit(secret_length):
pattern += "GR"
else:
pattern += "RG"
else:
pattern = "G"
if parity_bit(secret_length):
pattern += "BR"
else:
pattern += "RB"
return pattern
def extract_bits(self, indicator, channel1, channel2):
if indicator[-2:] == "01":
return str(channel2[-2:])
elif indicator[-2:] == "10":
return str(channel1[-2:])
elif indicator[-2:] == "11":
return str(channel1[-2:] + channel2[-2:])
else:
return ""
def main(self, height_step, width_step, reversed_height, reversed_width):
"""The first part of this function browse the image horizontally starting on :
- top left
- top right
- bottom left
- bottom right
For each direction, we get the secret_length to discover the pattern. Finally, we extracts all necessary bits and
then decode them to ASCII
"""
secret_length = self.get_secret_length(reversed_height, reversed_width, False)
pattern = self.find_pattern(secret_length)
f = open("pit.txt", "a", encoding="utf-8")
if not reversed_height:
range_height_pixel = range(1, self.height, height_step)
else:
range_height_pixel = reversed(range(0, self.height - 1, height_step))
if not reversed_width:
range_width_pixel = range(0, self.width, width_step)
else:
range_width_pixel = reversed(range(0, self.width, width_step))
pixels = self.im.load()
secret = ''
for height_pixel in range_height_pixel:
for width_pixel in range_width_pixel:
r, g, b = pixels[width_pixel, height_pixel]
r_binary = '{0:08b}'.format(r)
g_binary = '{0:08b}'.format(g)
b_binary = '{0:08b}'.format(b)
if pattern == "RGB":
secret += self.extract_bits(r_binary, g_binary, b_binary)
elif pattern == "RBG":
secret += self.extract_bits(r_binary, b_binary, g_binary)
elif pattern == "BGR":
secret += self.extract_bits(b_binary, g_binary, r_binary)
elif pattern == "BRG":
secret += self.extract_bits(b_binary, r_binary, g_binary)
elif pattern == "GBR":
secret += self.extract_bits(g_binary, b_binary, r_binary)
elif pattern == "GRB":
secret += self.extract_bits(g_binary, r_binary, b_binary)
f.write(binary_to_ascii(secret))
f.close()
"""This second part focus on browsing the image vertically and doing the same operations than before
"""
secret_length = self.get_secret_length(reversed_height, reversed_width, True)
pattern = self.find_pattern(secret_length)
f = open("pit.txt", "a", encoding="utf-8")
if not reversed_height:
range_height_pixel = range(0, self.height, height_step)
else:
range_height_pixel = reversed(range(0, self.height, height_step))
if not reversed_width:
range_width_pixel = range(1, self.width, width_step)
else:
range_width_pixel = reversed(range(0, self.width - 1, width_step))
pixels = self.im.load()
secret = ''
for width_pixel in range_width_pixel:
for height_pixel in range_height_pixel:
r, g, b = pixels[width_pixel, height_pixel]
r_binary = '{0:08b}'.format(r)
g_binary = '{0:08b}'.format(g)
b_binary = '{0:08b}'.format(b)
if pattern == "RGB":
secret += self.extract_bits(r_binary, g_binary, b_binary)
elif pattern == "RBG":
secret += self.extract_bits(r_binary, b_binary, g_binary)
elif pattern == "BGR":
secret += self.extract_bits(b_binary, g_binary, r_binary)
elif pattern == "BRG":
secret += self.extract_bits(b_binary, r_binary, g_binary)
elif pattern == "GBR":
secret += self.extract_bits(g_binary, b_binary, r_binary)
elif pattern == "GRB":
secret += self.extract_bits(g_binary, r_binary, b_binary)
f.write(binary_to_ascii(secret))
f.close()
def bruteforce(self):
""" Only bruteforcing the steps and directions. Rest is provided by the Pixel Indicator Technique itself.
"""
maximum = len(self.steps)**2 * 5
bar = IncrementalBar('Processing', max=maximum, suffix='%(percent)d%%')
for height_step in self.steps:
for width_step in self.steps:
bar.next()
self.main(height_step, width_step, False, False)
bar.next()
self.main(height_step, width_step, False, True)
bar.next()
self.main(height_step, width_step, True, False)
bar.next()
self.main(height_step, width_step, True, True)
bar.next()
bar.finish()