-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteggin.sh
executable file
·167 lines (147 loc) · 5.86 KB
/
steggin.sh
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
#!/bin/bash
# ----- Just steggin' bro. -----
# version 0.1.0
# system_page - A script to accept two files, concatenate them, or deconcatonate a concatenated file.
if [ "$#" = 0 ]; then
echo "Error: We can't be steggin' without any files. Use -h or --help." >&2
exit 1
fi
# Function Definitions
function exitFunction {
echo "Error - Bad Argument: $1 not found. Use -h or --help." >&2
exit 1
}
function helpFunction {
echo "Usage: $0 [arguments...]"; echo
echo " -h| --help Show help."; echo
echo " -c| --carrier= Carrier file to be used to hide a file. Must be used with -s and -o."; echo
echo " -s| --secret= Secret file to be hidden in the carrier file. Must be used with -c and -o."; echo
echo " -e| --extract= Stegged file from which to extract a message. Must be used with -o."; echo
echo " -o| --output= Output file in which to create a stegged file. Must be used with -c and -s."
echo " or"
echo " Output file in which to extract a hidden file. Must be used with -e."; echo
exit 1
}
# while loop to parse arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--help) helpFunction; shift 1;;
-h) helpFunction; shift 1;;
--carrier=*) [[ -s "${1#*=}" ]] && carrier="${1#*=}" || exitFunction "$1"; shift 1;;
--secret=*) [[ -s "${1#*=}" ]] && secret="${1#*=}" || exitFunction "$1"; shift 1;;
--extract=*) [[ -s "${1#*=}" ]] && stegfile="${1#*=}" || exitFunction "$1"; shift 1;;
--output=*) [[ "${1#*=}" ]] && output="${1#*=}" || exitFunction "$1"; shift 1;;
--carrier|--secret|--extract|--output) echo "$1 must be set = to a filename. Ex. $1=yourFile.ext. Use -h or --help" >&2; exit 1;;
-c) [[ -s "$2" ]] && carrier="$2" || exitFunction "$1 $2"; shift 2;;
-s) [[ -s "$2" ]] && secret="$2" || exitFunction "$1 $2"; shift 2;;
-e) [[ -s "$2" ]] && stegfile="$2" || exitFunction "$1 $2"; shift 2;;
-o) [[ "$2" ]] && output="$2" || exitFunction "$1"; shift 2;;
-*) exitFunction "$1"; shift 1;;
*) exitFunction "$1"; shift 1;;
esac
done
function errorCases {
if [[ -n "$stegfile" && -z "$output" ]]; then
echo "Error - Bad Command: Specifying -e|--extract= requires an -o|--output= file. Use -h or --help." >&2
exit 1
fi
if [[ -n "$output" && -z "$carrier" && -z "$stegfile" ]]; then
echo "Error - Bad Command: Specifying -o|--output= requires either -c|--carrier= and a -s|--secret= files or an -e|--extract= file. Use -h or --help" >&2
exit 1
fi
if [[ -n "$carrier" && -n "$stegfile" ]]; then
echo "Error - Bad Command: You cannot specify both a -c|--carrier= file and an -e|--extract= file. Use -h or --help." >&2
exit 1
fi
if [[ -n "$secret" && -n "$stegfile" ]]; then
echo "Error - Bad Command: You cannot specify both a -s|--secret= file and an -e|--extract= file. Use -h or --help." >&2
exit 1
fi
if [[ -e "$output" ]]; then
echo "Error - Bad Command: A file already exists with that name. Exiting without steggin'." >&2
echo " Please rename the existing file, or specify a different -o output file name. Use -h or --help." >&2
exit 1
fi
}
function declareInitialVariables {
lastByte="$( wc -c $carrier | awk '{print $1}')"
startByte=$(($lastByte + 1))
}
function preventMultipleSteggin {
validStegFileCheck="$(tail -c 500 $carrier | grep -a 'SECSHA' | awk '{print $1}')"
if [ "$validStegFileCheck" = "SECSHA:" ]; then
echo "Error: Carrier file $carrier has been previously stegged. Exiting without steggin'." >&2
exit 1
fi
}
function concatenate {
echo concatenating "$carrier" and "$secret"
cat "$carrier" "$secret" > "$output"
}
function declareStegginVariables {
endByte="$(wc -c $output | awk '{print $1}')"
metaByte=$(($endByte + 1))
}
function getSha256Hashes {
carSha="$(shasum -a 256 $carrier | awk '{print $1}')"
secretSha="$(shasum -a 256 $secret | awk '{print $1}')"
}
function concatenateMetaDataText {
echo "CARSHA: $carSha" > metaData.txt
echo "SECSHA: $secretSha" >> metaData.txt
echo "STARTBYTE: $startByte" >> metaData.txt
echo "ENDBYTE: $endByte" >> metaData.txt
echo "METABYTE: $metaByte" >> metaData.txt
cat metaData.txt >> "$output"
}
function echoSucess {
echo Meta File:
cat metadata.txt && echo; echo
echo "!!!!! Success file $secret is hidden in the file $output !!!!!"
}
function cleanup {
rm metadata.txt
}
function readMetaDataText {
validStegFileCheck="$(tail -c 500 $stegfile | grep -a 'SECSHA' | awk '{print $1}')"
if [ "$validStegFileCheck" != "SECSHA:" ]; then
echo "Error: File $stegfile has not been previously stegged." >&2
exit 1
fi
secSha="$(tail -c 500 $stegfile | grep -a 'SECSHA' | awk '{print $2}')"
carSha="$(tail -c 500 $stegfile | grep -a 'CARSHA' | awk '{print $2}')"
startByte="$(tail -c 500 $stegfile | grep -a 'STARTBYTE' | awk '{print $2}')"
endByte="$(tail -c 500 $stegfile | grep -a 'ENDBYTE' | awk '{print $2}')"
metaByte="$(tail -c 500 $stegfile | grep -a 'METABYTE' | awk '{print $2}')"
}
function extractSecretFile {
head -c "$endByte" "$stegfile" | tail -c +"$startByte" > "$output"
extractedSha="$(shasum -a 256 "$output" | awk '{print $1}')"
echo "Original sha256: $secSha"
echo "Extracted sha256: $extractedSha"
if [ "$extractedSha" = "$secSha" ]; then
echo "SUCCESS: EXTRACTED FILE $output IS BYTE FOR BYTE IDENTICAL TO THE ORIGINALLY STEGGED FILE."
else
echo "WARNING: EXTRACTED FILE $output HAS BEEN MODIFIED SINCE ORIGINALLY STEGGED."
fi
}
# Main Code
# Don't run the code if these error cases are met.
errorCases
# Create stegfile if a carrier, secret and output file were given.
if [[ -n "$carrier" && -n "$secret" && -n "$output" ]]; then
declareInitialVariables
preventMultipleSteggin
concatenate
declareStegginVariables
getSha256Hashes
concatenateMetaDataText
echoSucess
cleanup
fi
# Split a file that hass been previously stegged.
if [[ -n "$stegfile" && -n "$output" ]]; then
readMetaDataText
extractSecretFile
fi
exit 0