-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstegctfsolver.py
217 lines (180 loc) · 6.99 KB
/
stegctfsolver.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
214
215
216
217
#Automaticlally tries to solve steganography CTF challenges
from termcolor import colored
import magic
import argparse
import re
import string,sys,os,io,shutil
import base64
import binascii
from GPSPhoto import gpsphoto
import subprocess
import tempfile
#command line args
parser = argparse.ArgumentParser()
parser.add_argument('file', nargs='?', help='file to analyze')
parser.add_argument('-f', '--format', help='custom regex for flag format', nargs='?')
#parser.add_argument('-p', '--password', help='password if you think one was used', nargs='?')
args = parser.parse_args()
def main():
filetype = getfiletype(args.file)
print(filetype)
cwd = os.getcwd()
f = args.file.split('/')[-1]
outputdir = cwd + '/%s-stegresults' % f
if os.path.isdir(outputdir):
print('%s already exists' % outputdir)
exit()
os.mkdir(outputdir)
for s in strings(args.file):
if search(s):
print(colored(s, 'red'))
exifdata = exif(args.file)
if exifdata:
print('GPS Coordinates Found')
print('Latitude: ', exifdata['Latitude'])
print('Longitude: ', exifdata['Longitude'])
binwalk(args.file, outputdir)
foremost(args.file, outputdir)
if filetype.startswith('JPEG') or filetype.startswith('JPG'):
stegdetect(args.file)
stegoveritas(args.file, outputdir)
if filetype.startswith('PC bitmap'):
stegoveritas(args.file, outputdir)
zsteg(args.file)
if filetype.startswith('GIF'):
stegoveritas(args.file, outputdir)
extractframes(args.file, outputdir)
if filetype.startswith('MPEG ADTS') or filetype.startswith('RIFF'):
spectrogram(args.file, outputdir)
hideme(args.file)
#extension
if args.file.endswith('.png'):
#attempting to fix invalid header
#TODO make this output to a directory
if filetype == 'data':
print('This is not a valid png')
f = open(args.file, 'rb')
content = f.read()
contentnoheader = f.read(7)
header = bytes([137,80,78,71,13,10,26,10])
with open('%s/headerfix1.png' % outputdir,'wb') as outfile:
outfile.write(header)
outfile.write(content)
with open('%s/headerfix2.png' % outputdir,'wb') as outfile:
outfile.write(header)
outfile.write(contentnoheader)
f.close()
zsteg(args.file)
stegoveritas(args.file, outputdir)
pngcheck(args.file, outputdir)
#determine file type
def getfiletype(filename):
ftype = magic.from_file(filename)
return ftype
#taken directly from https://stackoverflow.com/questions/17195924/python-equivalent-of-unix-strings-utility
def strings(filename, min=7):
with open(filename, errors='ignore') as f:
result = ''
for c in f.read():
if c in string.printable:
result += c
continue
if len(result) >= min:
yield result
result = ''
if len(result) >= min: # catch result at EOF
yield result
#search string for flag
def search(s):
regex = 'flag{.*}|(ctf|CTF){.*}'
if args.format:
regex += '|%s' % args.format
flag = re.search(regex, s)
if flag:
return True
else:
return False
#exif data
def exif(filename):
data = gpsphoto.getGPSData(filename)
if ('Latitude' in data.keys()) and ('Longitude' in data.keys()):
return data
else:
return False
#binwalk -e
def binwalk(filename, outputdir):
output = subprocess.run(['binwalk', '-e', '--directory=%s' % outputdir, filename], stdout=subprocess.PIPE).stdout.decode('utf-8')
print(output)
#foremost
def foremost(filename, outputdir):
outdir = '%s/foremost' % outputdir
subprocess.run(['foremost', '-o', outdir, filename], stdout=subprocess.PIPE)
#stegdetect
def stegdetect(filename):
output = subprocess.run(['stegdetect', filename], stdout=subprocess.PIPE).stdout.decode('utf-8')
print(output)
#stegoveritas.py
def stegoveritas(filename, outputdir):
outdir = outputdir + '/stegoveritas'
os.mkdir(outdir)
print('brute forcing LSB with stegoveritas')
print('this may take a minute')
output = subprocess.run(['stegoveritas.py', filename], stdout=subprocess.PIPE).stdout.decode('utf-8')
for line in output.split(os.linesep):
if search(line):
print(colored(line, 'red'))
shutil.move('results',outdir)
trailingfile = '%s/results/trailing_data.bin' % outdir
if os.path.isfile(trailingfile):
print('FOUND TRAILING DATA')
print(getfiletype(trailingfile))
#TODO try to uncompress file and read/strings it
#zsteg -a
def zsteg(filename):
print('brute forcing LSB with zsteg')
output = subprocess.run(['zsteg', '-a', filename], stdout=subprocess.PIPE).stdout.decode('utf-8')
print(output)
for line in output.split(os.linesep):
if search(line):
print(colored(line, 'red'))
#pngcheck
def pngcheck(filename, outputdir):
#auto fix header
f = open(filename, 'rb')
#extract illegal chunk
p = subprocess.Popen(['pngcheck', '-v', filename], stdout=subprocess.PIPE)
for line in io.TextIOWrapper(p.stdout, encoding="utf-8"):
print(str(line))
if 'illegal (unless recently approved) unknown, public chunk' in line:
m = re.search('offset 0x([0-9]*[a-z]*), length ([0-9]*)', line)
if m:
print('writing content of illegal chunk to chunk.bin')
f = open(filename, 'rb')
offset = int(m.group(1), 16)
length = int(m.group(2))
f.seek(offset, 1)
chunk = f.read(length)
filechunk = open('%s/chunk.bin' % outputdir, 'wb')
filechunk.write(chunk)
f.close()
#flip file bytes
reversefile = open(outputdir + '/reversed.png', 'w')
p1 = subprocess.Popen(['xxd', '-p', '-c1', args.file], stdout=subprocess.PIPE,)
p2 = subprocess.Popen(['tac'], stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(['xxd', '-p', '-r'], stdin=p2.stdout, stdout=reversefile)
#ffmpeg audio convert waveform and save spectrogram as image in different ratios
def spectrogram(filename, outputdir):
outfile1 = '%s/spectrum1.png' % outputdir
outfile2 = '%s/spectrum2.png' % outputdir
subprocess.run(['ffmpeg', '-i', filename, '-lavfi', 'showspectrumpic', outfile1], stdout=subprocess.PIPE)
subprocess.run(['ffmpeg', '-i', outfile1, '-vf', 'scale=5000:500', outfile2], stdout=subprocess.PIPE)
def hideme(filename):
output = subprocess.run(['hideme', filename, '-f'], stdout=subprocess.PIPE).stdout
print(output)
def extractframes(filename, outputdir):
print('Extracting GIF frames')
outfile = '%s/frame%%03d.png' % outputdir
output = subprocess.run(['ffmpeg', '-i', filename, '-vsync', '0', outfile], stdout=subprocess.PIPE).stdout
#ffmpeg -i path/to/gif -vsync 0 path/to/output$03d.png
if __name__ == '__main__':
main()