-
Notifications
You must be signed in to change notification settings - Fork 33
/
outguess-png-imager.pl
225 lines (163 loc) · 5.97 KB
/
outguess-png-imager.pl
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
#!/usr/bin/perl
# Author: Trizen
# Date: 07 February 2022
# https://github.com/trizen
# Hide arbitrary data into the pixels of a PNG image, storing 3 bits in each pixel color.
# Concept inspired by outguess:
# https://github.com/resurrecting-open-source-projects/outguess
# https://uncovering-cicada.fandom.com/wiki/OutGuess
# Q: How does it work?
# A: The script uses the Imager library to read the RGB color values of each pixel.
# Then it changes the last bit of each value to one bit from the data to be encoded.
# Q: How does the decoding work?
# A: The first 32 bits from the first 32 pixels of the image, form the length of the encoded data.
# Then the remaining bits (3 bits from each pixel) are collected to form the encoded data.
# The script also does transparent Deflate compression and decompression of the encoded data.
use 5.020;
use strict;
use warnings;
no warnings 'once';
use Imager;
use Getopt::Long qw(GetOptions);
use experimental qw(signatures);
binmode(STDIN, ':raw');
binmode(STDOUT, ':raw');
sub encode_data ($data, $img_file) {
my $image = Imager->new(file => $img_file)
or die Imager->errstr();
require IO::Compress::RawDeflate;
IO::Compress::RawDeflate::rawdeflate(\$data, \my $compressed_data)
or die "rawdeflate failed: $IO::Compress::RawDeflate::RawDeflateError\n";
$data = $compressed_data;
my $bin = unpack("B*", $data);
my $width = $image->getwidth();
my $height = $image->getheight();
my $maximum_data_size = 3 * (($width * $height - 32) >> 3);
my $data_size = length($bin) >> 3;
if ($data_size == 0) {
die sprintf("No data was given!\n");
}
if ($data_size > $maximum_data_size) {
die sprintf(
"Data is too large (%s bytes) for this image (exceeded by %.2f%%).\n"
. "Maximum data size for this image is %s bytes.\n",
$data_size, 100 - ($maximum_data_size / $data_size * 100),
$maximum_data_size
);
}
warn sprintf("Compressed data size: %s bytes (%.2f%% out of max %s bytes)\n",
$data_size, $data_size / $maximum_data_size * 100,
$maximum_data_size);
my $length_bin = unpack("B*", pack("N*", $data_size));
$bin = reverse($length_bin . $bin);
my $size = length($bin);
OUTER: foreach my $y (0 .. $height - 1) {
my $x = 0;
foreach my $color ($image->getscanline(x => 0, y => $y, width => $width)) {
if ($size > 0) {
my ($red, $green, $blue, $alpha) = $color->rgba;
$color->set((map { (($_ >> 1) << 1) | (chop($bin) || 0) } ($red, $green, $blue)), $alpha);
$size -= 3;
}
else {
last OUTER;
}
$image->setpixel(x => $x++, y => $y, color => $color);
}
}
return $image;
}
sub decode_data ($img_file) {
my $image = Imager->new(file => $img_file)
or die Imager->errstr();
my $width = $image->getwidth;
my $height = $image->getheight;
my $bin = '';
my $size = 0;
my $length = $width * $height;
my $find_length = 1;
my $max_data_size = 3 * ($length - 4);
OUTER: foreach my $y (0 .. $height - 1) {
foreach my $color ($image->getscanline(x => 0, y => $y, width => $width)) {
if ($size < $length) {
my ($red, $green, $blue) = $color->rgba;
$bin .= join('', map { $_ & 1 } ($red, $green, $blue));
$size += 3;
if ($find_length and $size >= 32) {
$length = unpack("N*", pack("B*", substr($bin, 0, 32)));
$find_length = 0;
$size = length($bin) - 32;
$bin = substr($bin, 32);
if ($length > $max_data_size or $length == 0) {
die "No hidden data was found in this image!\n";
}
warn sprintf("Compressed data size: %s bytes\n", $length);
$length <<= 3;
}
}
else {
last OUTER;
}
}
}
my $data = pack("B*", substr($bin, 0, $length));
require IO::Uncompress::RawInflate;
IO::Uncompress::RawInflate::rawinflate(\$data, \my $uncompressed)
or die "rawinflate failed: $IO::Uncompress::RawInflate::RawInflateError\n";
warn sprintf("Uncompressed data size: %s bytes\n", length($uncompressed));
return $uncompressed;
}
sub help ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [input] [output]
options:
-z [file] : encode a given data file
example:
# Encode
perl $0 -z=data.txt input.jpg encoded.png
# Decode
perl $0 encoded.png decoded-data.txt
EOT
exit($exit_code);
}
my $data_file;
GetOptions("z|f|encode=s" => \$data_file,
"h|help" => sub { help(0) },)
or die("Error in command line arguments\n");
if (defined($data_file)) {
my $input_image = shift(@ARGV) // help(2);
my $output_image = shift(@ARGV);
open my $fh, '<:raw', $data_file
or die "Can't open file <<$data_file>> for reading: $!";
my $data = do {
local $/;
<$fh>;
};
close $fh;
my $img = encode_data($data, $input_image);
if (defined($output_image)) {
if ($output_image !~ /\.png\z/i) {
die "The output image must have the '.png' extension!\n";
}
$img->write(file => $output_image)
or die $img->errstr;
}
else {
$img->write(fh => \*STDOUT, type => 'png')
or die $img->errstr;
}
}
else {
my $input_image = shift(@ARGV) // help(2);
my $output_file = shift(@ARGV);
my $data = decode_data($input_image);
if (defined($output_file)) {
open my $fh, '>:raw', $output_file
or die "Can't open file <<$output_file>> for writing: $!";
print $fh $data;
close $fh;
}
else {
print $data;
}
}