-
Notifications
You must be signed in to change notification settings - Fork 0
/
orthrus.py
216 lines (198 loc) · 8.51 KB
/
orthrus.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
# Orthrus Carver
# Copyright (C) 2014 InFo-Lab
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU
# Lesser General Public License as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not,
# write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# coding=utf-8
import argparse
import cStringIO
import datetime
import FileValidators
import re
import os
# A few constants:
DEBUG_BENCHMARK = True
CONST_BUILD = 18
CONST_VER = "0.2.3"
CONST_VERSTRING = "Version %s build %s" % (CONST_VER, CONST_BUILD)
CONST_YEARS = "2014"
CONST_BANNER = """
Orthrus carver
==============
InFo-Lab prototype %s
%s
""" % (CONST_YEARS, CONST_VERSTRING)
KILO = 1024
MEGA = 1024 * KILO
CONST_BLOCKSIZE = 10 * MEGA
CONST_FILESIZE = 5 * MEGA
CONST_SECTORSIZE = 512
# And now some variables:
headers_list = [
'\xff\xd8\xff',
'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a',
'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1',
'GIF8',
]
def Carve(args):
"""
Performs bifragment gap carving on the files referenced by args.
:param args: dictionary with the arguments from the command line. Most important are the input
file and the output directory.
:return:
"""
blocksize = CONST_BLOCKSIZE
filesize = CONST_FILESIZE
sectorsize = CONST_SECTORSIZE
headers = map(re.escape, headers_list)
rex_heads = re.compile("|".join(headers))
validators = {
'\xff\xd8\xff': FileValidators.JPGValidator(),
'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a': FileValidators.PNGValidator(),
'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1': FileValidators.MSOLEValidator(),
'GIF8': FileValidators.GIFValidator(),
}
extensions = {
'\xff\xd8\xff': ".jpg",
'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a': ".png",
'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1': ".doc",
'GIF8': ".gif",
}
sois = { # Structures of Interest, it's a list that gets compiled into a regex that helps the
# carver determine the gap end position.
'\xff\xd8\xff': ['\xff\xc0', '\xff\xc1', '\xff\xc2', '\xff\xc3', '\xff\xc4', '\xff\xc5',
'\xff\xc6', '\xff\xc7', '\xff\xc8', '\xff\xc9', '\xff\xca', '\xff\xcb', '\xff\xcc',
'\xff\xcd', '\xff\xce', '\xff\xcf', '\xff\xd0', '\xff\xd1', '\xff\xd2', '\xff\xd3',
'\xff\xd4', '\xff\xd5', '\xff\xd6', '\xff\xd7', '\xff\xd9', '\xff\xda', '\xff\xdb',
'\xff\xdc', '\xff\xdd', '\xff\xde', '\xff\xdf', '\xff\xe0', '\xff\xe1', '\xff\xe2',
'\xff\xe3', '\xff\xe4', '\xff\xe5', '\xff\xe6', '\xff\xe7', '\xff\xe8', '\xff\xe9',
'\xff\xea', '\xff\xeb', '\xff\xec', '\xff\xed', '\xff\xee', '\xff\xef', '\xff\xf0',
'\xff\xf1', '\xff\xf2', '\xff\xf3', '\xff\xf4', '\xff\xf5', '\xff\xf6', '\xff\xf7',
'\xff\xf8', '\xff\xf9', '\xff\xfa', '\xff\xfb', '\xff\xfc', '\xff\xfd', '\xff\xfe'
], # this is a list of all the valid jpeg markers
'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a': ["PLTE", "IDAT", "IEND", "bKGD", "cHRM", "gAMA", "hIST",
"iCCP", "iTXt", "pHYs", "sBIT", "sPLT", "sRGB", "sTER", "tEXt", "tIME", "tRNS", "zTXt"
], # this is a list of all the standard PNG segments that could be found
'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1': [], # there's really no structure that we should/could
# look for regarding MS-OLE files, so we have to return to a standard size
'GIF8': [',', ';', '\x21\xf9', '\x21\x01', '\x21\xff', '\x21\xfe'
], # this covers all possible GIF blocks
}
# now we have to compile those lists and store the regex(s)
rex_sois = {}
for k in sois:
values = map(re.escape, sois[k])
rex_sois[k] = re.compile("|".join(values))
image = open(args.ipath, "rb")
os.mkdir(args.opath)
ostring = args.opath + os.path.sep + "%08d%s"
ext_number = 1
block = image.read(blocksize)
readbytes = 0
while block:
readbytes = float(len(block)) / MEGA
print "-> %0.2f MB read" % readbytes
newblock = image.read(blocksize)
bigblock = block + newblock
match_results = rex_heads.finditer(block)
for match in match_results:
offset = match.start()
head = match.group()
val = validators[head]
data = bigblock[offset: offset + filesize]
#obj = cStringIO.StringIO(data)
print "Testing %s at %d..." % (head.encode("hex"), offset)
valid = val.Validate(data)
if valid:
extract = True
else:
extract = False
lvb = val.GetStatus()[2] # last valid byte
gap_start = (lvb / sectorsize) + 1
gap_end = (filesize / sectorsize) - 1
if sois[head]:
rx = rex_sois[head]
end_match = rx.search(data[gap_start * sectorsize:])
if end_match:
gap_end = gap_start + (end_match.start() / sectorsize)
# lets try to broaden the spectrum and look for another SOI
new_match = rx.search(data[(gap_end + 1) * sectorsize:])
if new_match:
gap_old = gap_end
gap_end = gap_start + (new_match.start() / sectorsize)
print " got a new match, old(%d), new(%d)" % (gap_old, gap_end)
else:
continue
else:
gap_end = (filesize / sectorsize) - 1
gap_size_start = 1
print " file not valid, trying gaps... (head: %s)" % (head.encode("hex"))
for gap_pos in xrange(gap_start, gap_end):
print "\r gaps starting from %d..." % (gap_pos),
gap_size_end = gap_end - gap_pos
print "possible gap size: %d... " % (gap_size_end),
gap_size_end = min((2048, gap_size_end))
#for gap_size in xrange(gap_size_start, gap_size_end):
for gap_size in xrange(gap_size_end - 1, 0, -1):
#print "\bx",
pos1 = gap_pos * sectorsize
pos2 = (gap_pos + gap_size) * sectorsize
newdata = data[:pos1] + data[pos2:]
#new_obj = cStringIO.StringIO(newdata)
if val.Validate(newdata):
extract = True
data = newdata
print "... validated with gap %d to %d!" % (gap_pos, gap_pos + gap_size)
break
if extract:
break
if extract:
extension = extensions[head]
ext_size = val.GetStatus()[2]
print " extracted to %s, %d bytes" % (ostring % (ext_number, extension), ext_size)
fo = open(ostring % (ext_number, extension), "wb")
fo.write(data[:ext_size])
fo.close()
ext_number += 1
block = newblock
image.close()
def ArgParse():
"""
Parses the command line arguments
:return: argparse dictionary
"""
# parse command line arguments
parser = argparse.ArgumentParser(
description="orthrus: performs bifragment-gap-carving on a disk image.")
parser.add_argument("ipath",
help="Input path.")
parser.add_argument("opath",
help="Output path.")
parser.add_argument("-l",
dest="logfile",
default="orthrus-log.md",
help="Log file.")
args = parser.parse_args()
return args
def main():
print CONST_BANNER
args = ArgParse()
t1 = datetime.datetime.now()
if os.path.isfile(args.ipath) and not os.path.exists(args.opath):
Carve(args)
else:
print "ipath argument must be a valid file!"
print "opath argument must be a non-existent directory!"
dt = datetime.datetime.now() - t1
if DEBUG_BENCHMARK:
print "\nTime taken: %s" % dt
if __name__ == "__main__":
main()