-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_steg_data.go
321 lines (261 loc) · 9.69 KB
/
get_steg_data.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
)
// HiddenFileData: Data structure that describes the file hidden in the container file
type HiddenFileData struct {
magicNumber int64
fileName [FILENAME_LENGTH]byte
steps int64
spacing int64
}
// Unsteg: Extracts hidden file from given container file
func Unsteg(_containerFile string) (bool, string) {
var fileUnhidden bool = true
var containerFileDesc *os.File
var hiddenFileData HiddenFileData
var hiddenFileDataRead bool = false
var hiddenFile string = ""
var steps int64 = -1
var spacing int64 = -1
const START_OFFSET int64 = 2000
var offset int64 = -1
var readByte []byte
var containerFileReadCounter int64 = -1
var hiddenFileBytesRead int = 0
var containerFileReadError bool = false
var hiddenFileTotalBytesRead int = 0
const BUFFER_SIZE int64 = 1000
var hiddenFileBuffer []byte
var finalHiddenFileBuffer []byte
var hiddenFileDesc *os.File
var containerFileBytesWritten int = 0
var err error
// If file name for container file has been passed in
if len(_containerFile) > 0 {
// If container file exists
if FileExists(_containerFile) {
// Open container file
containerFileDesc, err = os.OpenFile(_containerFile, os.O_RDONLY, 0)
if err == nil {
// Get hidden file data from container file
hiddenFileDataRead, hiddenFileData = ReadHiddenFileData(containerFileDesc)
if hiddenFileDataRead == true {
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_HiddenFileData, hiddenFileData.spacing, hiddenFileData.steps, string(bytes.Trim(hiddenFileData.fileName[:], "\x00"))))
}
hiddenFile = string(bytes.Trim(hiddenFileData.fileName[:], "\x00"))
steps = hiddenFileData.steps
spacing = hiddenFileData.spacing
if len(hiddenFile) > 0 && hiddenFile != "" {
if steps != -1 && spacing != -1 {
// Set initial offset point to read hidden file from container file
offset = START_OFFSET + 1
// Create buffer to store hidden file data
hiddenFileBuffer = make([]byte, BUFFER_SIZE)
if int64(len(hiddenFileBuffer)) != BUFFER_SIZE {
fmt.Println(UI_NoBufferMemory)
fileUnhidden = false
} else {
// Check hidden file does not exist on disk
if FileExists(hiddenFile) == false {
// Create hidden file on disk
hiddenFileDesc, err = os.OpenFile(hiddenFile, os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
fmt.Println(fmt.Sprintf(UI_HiddenFileNotCreated, hiddenFile, err))
fileUnhidden = false
} else {
// Loop around container file and read the hidden file data out of it one byte at a time
for containerFileReadCounter = 0; containerFileReadCounter != steps; containerFileReadCounter++ {
if UPDATE_UI == true {
// Update UI
fmt.Printf("\r" + UI_ExtractingHiddenFile + ". ")
}
// Read a byte from the container file at the current offset point
readByte = make([]byte, 1)
hiddenFileBytesRead, err = containerFileDesc.ReadAt(readByte, offset)
if err != nil {
if err == io.EOF {
// If end of container file found, display an error and stop reading data
fmt.Println(fmt.Sprintf(UI_EOF, _containerFile))
fileUnhidden = false
containerFileReadError = true
break
} else {
// Error reading container file - display the error and stop reading data
fmt.Println(fmt.Sprintf(UI_FileReadError, _containerFile, err))
fileUnhidden = false
containerFileReadError = true
break
}
}
if UPDATE_UI == true {
// Update UI
fmt.Printf("\r" + UI_ExtractingHiddenFile + ".. ")
}
if hiddenFileBytesRead != 1 {
fmt.Println(fmt.Sprintf(UI_FileReadError, _containerFile, ""))
fileUnhidden = false
containerFileReadError = true
break
}
// Add byte read to hidden file buffer
hiddenFileBuffer[hiddenFileTotalBytesRead] = readByte[0]
hiddenFileTotalBytesRead++
// Increment offset by spacing value
offset = offset + spacing
// If buffer is full, write the contents to the hidden file on disk
if int64(hiddenFileTotalBytesRead) == BUFFER_SIZE {
containerFileBytesWritten, err = hiddenFileDesc.Write(hiddenFileBuffer)
if err != nil {
fmt.Println(fmt.Sprintf(UI_FileWriteError, hiddenFile, err))
fileUnhidden = false
containerFileReadError = true
}
if int64(containerFileBytesWritten) != BUFFER_SIZE {
fmt.Println(fmt.Sprintf(UI_FileWriteError, hiddenFile, ""))
fileUnhidden = false
containerFileReadError = true
}
// Clear hidden file buffer
hiddenFileBuffer = nil
hiddenFileBuffer = make([]byte, BUFFER_SIZE)
// Set buffer counter back to 0
hiddenFileTotalBytesRead = 0
}
} // End byte at a time for loop
if UPDATE_UI == true {
// Update UI
fmt.Printf("\r" + UI_ExtractingHiddenFile + "...")
}
if containerFileReadError == false {
// Write any data left in the buffer to the hidden file
finalHiddenFileBuffer = make([]byte, hiddenFileTotalBytesRead)
if len(finalHiddenFileBuffer) != hiddenFileTotalBytesRead {
fmt.Println(UI_NoBufferMemory)
fileUnhidden = false
} else {
for counter := 0; counter != hiddenFileTotalBytesRead; counter++ {
finalHiddenFileBuffer[counter] = hiddenFileBuffer[counter]
}
containerFileBytesWritten, err = hiddenFileDesc.Write(finalHiddenFileBuffer)
if err != nil {
fmt.Println(fmt.Sprintf(UI_FileWriteError, hiddenFile, err))
fileUnhidden = false
}
if containerFileBytesWritten != hiddenFileTotalBytesRead {
fmt.Println(fmt.Sprintf(UI_FileWriteError, hiddenFile, ""))
fileUnhidden = false
}
}
} else {
fmt.Println(fmt.Sprintf(UI_FileWriteError, hiddenFile, ""))
fileUnhidden = false
}
}
} else {
fmt.Println(UI_FileExistsError, hiddenFile)
fileUnhidden = false
}
}
if UPDATE_UI == true {
// Update UI
fmt.Printf("\r")
}
// Close down file handles for container and hidden files
containerFileDesc.Close()
hiddenFileDesc.Sync()
hiddenFileDesc.Close()
} else {
fmt.Println(UI_HiddenFileDataNotFound)
fileUnhidden = false
}
} else {
fmt.Println(UI_HiddenFileNameNotFound)
fileUnhidden = false
}
} else {
fmt.Println(UI_HiddenFileDataNotFound)
fileUnhidden = false
}
} else {
fmt.Println(UI_FileOpenError, err)
fileUnhidden = false
}
} else {
fmt.Println(UI_FileNotFound, _containerFile)
fileUnhidden = false
}
} else {
fmt.Print(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()))
fmt.Println(fmt.Sprintf(UI_Parameter, "_containerFile: "+_containerFile))
fileUnhidden = false
}
return fileUnhidden, hiddenFile
}
// ReadHiddenFileData: Reads the hidden file data stored in the container file
func ReadHiddenFileData(_containerFile *os.File) (bool, HiddenFileData) {
var hiddenFileData HiddenFileData
var readContainerFileSize []byte
var hiddenFileDataOffset int64 = -1
const READ_CONTAINER_FILE_SIZE_BUFFER int = 8
var dataRead = false
var err error
if _containerFile != nil {
// Create instance of data structure
hiddenFileData = HiddenFileData{}
// Create byte array to read data structure into
readContainerFileSize = make([]byte, READ_CONTAINER_FILE_SIZE_BUFFER)
if len(readContainerFileSize) != READ_CONTAINER_FILE_SIZE_BUFFER {
fmt.Println(UI_NoBufferMemory)
} else {
// Move to location of data structure in container file and read the data structure into the byte array
_containerFile.ReadAt(readContainerFileSize, HIDDEN_FILE_DATA_OFFSET)
hiddenFileDataOffset = int64(binary.BigEndian.Uint64(readContainerFileSize))
_containerFile.Seek(hiddenFileDataOffset, 0)
err = binary.Read(_containerFile, binary.LittleEndian, &hiddenFileData.magicNumber)
if err != nil {
fmt.Println(UI_ReadMagicNumberFailed, err)
hiddenFileData.magicNumber = -1
} else {
err = binary.Read(_containerFile, binary.LittleEndian, &hiddenFileData.fileName)
if err != nil {
fmt.Println(UI_ReadFileNameFailed, err)
} else {
err = binary.Read(_containerFile, binary.LittleEndian, &hiddenFileData.steps)
if err != nil {
fmt.Println(UI_ReadStepsFailed, err)
hiddenFileData.steps = -1
} else {
err = binary.Read(_containerFile, binary.LittleEndian, &hiddenFileData.spacing)
if err != nil {
fmt.Println(UI_ReadSpacingFailed, err)
hiddenFileData.spacing = -1
} else {
// Check if data structure contains magic number
if DEBUG == true {
fmt.Println(UI_MagicNumber, hiddenFileData.magicNumber)
}
if hiddenFileData.magicNumber == MAGIC_NUMBER {
if DEBUG == true {
fmt.Println(UI_MagicNumberFound)
}
dataRead = true
} else {
fmt.Println(UI_MagicNumberNotFound)
}
}
}
}
}
}
} else {
fmt.Print(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()))
fmt.Println(fmt.Sprintf(UI_Parameter, "containerFile: "+_containerFile.Name()))
}
return dataRead, hiddenFileData
}