-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
372 lines (302 loc) · 7.45 KB
/
main.go
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"hash/crc32"
"io"
"log"
"os"
"strconv"
)
// const pngImage string = "test.png"
const IENDchunk string = "IEND"
// PngHeader
type PngHeader struct {
Header uint64
}
// ReadHeader
func (p *PngHeader) ReadHeader(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &p.Header)
}
// Validate
func (p *PngHeader) Validate() (bool, error) {
if p.Header == 0 {
return false, nil
}
h := strconv.FormatUint(p.Header, 16)
data, err := hex.DecodeString(h)
if err != nil {
return false, err
}
if string(data[1:4]) == "PNG" {
return true, nil
}
return false, nil
}
// ChunkType
type ChunkType uint32
// String
func (c ChunkType) String() (string, error) {
data, err := hex.DecodeString(strconv.FormatUint(uint64(c), 16))
if err != nil {
return "", err
}
return string(data), nil
}
// strToInt
func strToInt(n string) uint32 {
return binary.BigEndian.Uint32([]byte(n))
}
// PngMetadata structure that holds png chunk fields.
type PngMetadata struct {
Length uint32
Type ChunkType
Data []byte
CRC uint32
}
const NewChunkTypeName string = "pUNK"
// NewPngMetadata create a new png chunk function accept data as []byte and
// returns pointer to PngMetadata or an error.
func NewPngMetadata(data []byte) (*PngMetadata, error) {
chunk := &PngMetadata{
Length: uint32(len(data)),
Type: ChunkType(strToInt(NewChunkTypeName)),
Data: data,
}
if err := chunk.generateCRC(); err != nil {
return nil, err
}
return chunk, nil
}
// generateCRC generates a new CRC for the new png chunk.
func (m *PngMetadata) generateCRC() error {
buffer := &bytes.Buffer{}
if err := binary.Write(buffer, binary.BigEndian, m.Type); err != nil {
return err
}
if err := binary.Write(buffer, binary.BigEndian, m.Data); err != nil {
return err
}
m.CRC = crc32.ChecksumIEEE(buffer.Bytes())
return nil
}
func (m *PngMetadata) readLength(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &m.Length)
}
func (m *PngMetadata) readType(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &m.Type)
}
func (m *PngMetadata) readData(r io.Reader) error {
buff := make([]byte, m.Length)
if _, err := r.Read(buff); err != nil {
return err
}
m.Data = buff
return nil
}
func (m *PngMetadata) readCRC(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &m.CRC)
}
// PNG represent a png image.
type PNG struct {
Header PngHeader
Chunks []*PngMetadata
newChunkIndex int
}
// ReadChunks reads all the PNG chunks.
func (p *PNG) ReadChunks(r io.Reader) error {
stop := false
counter := 0
for {
metadata := new(PngMetadata)
if err := metadata.readLength(r); err != nil {
return err
}
if err := metadata.readType(r); err != nil {
return err
}
typAsStr, err := metadata.Type.String()
if err != nil {
return err
}
if typAsStr == IENDchunk {
stop = true
}
if err := metadata.readData(r); err != nil {
return err
}
if err := metadata.readCRC(r); err != nil {
return err
}
p.Chunks = append(p.Chunks, metadata)
if stop {
p.newChunkIndex = counter - 1
return nil
}
counter++
}
return nil
}
func (p *PNG) PrintChunks(seek io.Writer) {
for _, n := range p.Chunks {
typ, _ := n.Type.String()
fmt.Fprintf(seek, "--------------\n")
fmt.Fprintf(seek, "Length %v\n", n.Length)
fmt.Fprintf(seek, "Type %v\n", typ)
fmt.Fprintf(seek, "CRC %v\n", n.CRC)
}
}
// Marshal Chunks method used to add a new png chunk into the original one.
func (p *PNG) Marshal() ([]byte, error) {
buff := new(bytes.Buffer)
if err := binary.Write(buff, binary.BigEndian, p.Header.Header); err != nil {
return nil, err
}
for _, n := range p.Chunks {
if err := binary.Write(buff, binary.BigEndian, n.Length); err != nil {
return nil, err
}
if err := binary.Write(buff, binary.BigEndian, uint32(n.Type)); err != nil {
return nil, err
}
if err := binary.Write(buff, binary.BigEndian, n.Data); err != nil {
return nil, err
}
if err := binary.Write(buff, binary.BigEndian, n.CRC); err != nil {
return nil, err
}
}
return buff.Bytes(), nil
}
func main() {
// you can use the tool in order to store a message into a png image or
// file data . for the moment we don't support encryption of the data that
// we are hiding into the png image but that will come in the newer
// versions.
// ./stegno --encode --png <image path> --to <out file> --data <message> or --file <file path>
// ./stegno --decode --png <image path> --to <file path> or --dump true
var (
pngImage string
to string
//
encode bool
message string
file string
//
decode bool
dump bool
)
flag.BoolVar(&encode, "encode", false, "set to true to hide the data to the png file")
flag.BoolVar(&decode, "decode", false, "set to true to extract the data from png file")
flag.BoolVar(&dump, "dump", false, "set to true to dump data into stdout")
//
flag.StringVar(&pngImage, "png", "", "set the png image file path.")
flag.StringVar(&message, "message", "", "set the message that you want to hide into the png file.")
flag.StringVar(&file, "file", "", "set the file path that you want to hide it data into the png file.")
flag.StringVar(&to, "to", "", "set the output file to write")
flag.Parse()
if pngImage == "" {
log.Printf("no image specified")
flag.PrintDefaults()
return
}
png, err := readPNG(pngImage)
if err != nil {
log.Fatal(err)
}
if encode {
if message != "" && to != "" {
if err := Encode(png, []byte(message), to); err != nil {
log.Fatal(err)
}
log.Printf("[+] %s written", to)
return
}
if file != "" && to != "" {
data, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
if err := Encode(png, data, to); err != nil {
log.Fatal(err)
}
log.Printf("[+] %s written", to)
return
}
}
if decode {
data, err := Decode(png)
if err != nil {
log.Fatal(err)
}
if to != "" {
if err := os.WriteFile(to, data, 0666); err != nil {
log.Fatal(err)
}
log.Printf("[+] %s written", to)
return
}
if dump {
fmt.Printf("DATA:\n %v", string(data))
return
}
}
flag.PrintDefaults()
}
func readPNG(filename string) (*PNG, error) {
fd, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fd.Close()
header := new(PngHeader)
if err := header.ReadHeader(fd); err != nil {
return nil, err
}
ok, err := header.Validate()
if err != nil {
return nil, err
}
if !ok {
// TODO: export this error into a global variable.
return nil, fmt.Errorf("Not a PNG file")
}
png := &PNG{
Header: *header,
}
if err := png.ReadChunks(fd); err != nil {
return nil, err
}
return png, nil
}
// TODO: we still don't encrypt the data that we are hiding
func Encode(png *PNG, data []byte, outpngName string) error {
m, err := NewPngMetadata(data)
if err != nil {
return err
}
// append the new chunk into the `len(chunks)-2`
// NOTE: we can change this way of appending the new chunk in the future.
png.Chunks = append(png.Chunks[:png.newChunkIndex+1], []*PngMetadata{m, png.Chunks[len(png.Chunks)-1]}...)
// get new png data
pngdata, err := png.Marshal()
if err != nil {
return err
}
return os.WriteFile(outpngName, pngdata, 0666)
}
func Decode(png *PNG) ([]byte, error) {
c := png.Chunks[png.newChunkIndex]
typ, err := c.Type.String()
if err != nil {
return nil, err
}
if typ != NewChunkTypeName {
return nil, fmt.Errorf("couldn't find the png chunk")
}
// TODO: we need to decrypt the data here.
return c.Data, nil
}