-
Notifications
You must be signed in to change notification settings - Fork 24
/
pe-carv.py
executable file
·164 lines (152 loc) · 5.12 KB
/
pe-carv.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
#!/usr/bin/env python
# Name:
# pe-carv.py
# Version:
# 0.3
# * Added Ascii Blob and overlay functionality
# Description:
# This script can be used to carve out portable executable files from a data stream.
# It relies on pefile by Ero Carrera parse the portable executable format and calculate
# the file size.
#
# Author
# alexander<dot>hanel<at>gmail<dot>com
#
# License: Free game.. Please give credit
import re
import sys
from optparse import OptionParser
import imp
try:
imp.find_module('pefile')
import pefile
except ImportError as error:
print '\t[IMPORT ERROR] %s - aborting' % error
sys.exit()
class CARVER():
def __init__(self):
self.args = sys.argv
self.buffer = ''
self.outputF = ''
self.parser = None
self.asciiBlob = False
self.location = 0
self.help = None
self.verbose = False
self.overlay = False
self.overlaySize = 512
self.callParser()
self.argumentCheck()
self.readStream()
self.convertAscii2Hex()
self.carve()
def argumentCheck(self):
'check for arguments'
if len(sys.argv) == 1:
self.parser.print_help()
sys.exit()
def readStream(self):
'read the file into a buffer'
try:
self.fileH = open(sys.argv[len(sys.argv)-1], 'rb')
self.buffer = self.fileH.read()
except:
print '\t[FILE ERROR] could not access file: %s' % sys.argv[1]
sys.exit()
def getExt(self, pe):
'returns ext of the file type using pefile'
if pe.is_dll() == True:
return 'dll'
if pe.is_driver() == True:
return 'sys'
if pe.is_exe() == True:
return 'exe'
else:
return 'bin'
def writeFile(self, count, ext, pe):
'write file to working directory'
name = ''
try:
if self.outputF != '':
out = open( str(self.outputF) + '-' + str(count)+ '.' + ext, 'wb')
else:
out = open(str(count) + '.' + ext, 'wb')
except:
print '\t[FILE ERROR] could not write file'
sys.exit()
# get size of trimmed file using PE
trimmedPE = pe.trim()
trimSize = len(trimmedPE)
if self.overlay == True:
tmpFileHandle = self.fileH
tmpFileHandle.seek(self.location)
try:
trimmedPE = tmpFileHandle.read(trimSize+self.overlaySize)
except:
trimmedPE = tmpFileHandle.read()
out.write(trimmedPE)
out.close()
def convertAscii2Hex(self):
'converts the buffer from ascii to hex. all non-hex is whitespace'
if self.asciiBlob == False:
return
from StringIO import StringIO
tmp = StringIO(self.buffer)
buff = ''
b = tmp.read(2)
while b != '':
try:
b = chr(int(b,16))
except ValueError:
b = ' '
buff += b
b = tmp.read(2)
# replace the buffer with ascii to hex version.
self.buffer = ''
self.buffer = buff
self.fileH = StringIO(buff)
def callParser(self):
'parse arguments for parser'
self.parser = OptionParser()
usage = 'usage: %prog [options] <carving.file>'
self.parser = OptionParser(usage=usage)
# command options
self.parser.add_option('-o', '--output', type='string',dest='output', help='output file name')
self.parser.add_option('-a', '--ascii_blob', action='store_true', dest='ascii', help='read as hex ascii blob')
self.parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='print MZ location')
self.parser.add_option('-l', '--overlay', action='store_true', dest='overlay', help='get overlay, default 1024 bytes')
self.parser.add_option('-s', '--size', type='int', dest='size', help='size of overlay')
(options, args) = self.parser.parse_args()
if options.output != None:
self.outputF = options.output
if options.ascii == True:
self.asciiBlob = True
if options.verbose == True:
self.verbose = True
if options.overlay == True:
self.overlay = True
if options.size != None:
self.overlaySize = options.size
def carve(self):
'carve out embeddded executables'
c = 1
# For each address that contains MZ
for y in [tmp.start() for tmp in re.finditer('\x4d\x5a',self.buffer)]:
self.location = y
self.fileH.seek(y)
try:
pe = pefile.PE(data=self.fileH.read())
except:
print "Failed to parse EXE"
continue
# determine file ext
ext = self.getExt(pe)
if self.verbose == True:
print '\t*', ext , 'found at offset', hex(y)
self.writeFile(c,ext,pe)
c += 1
ext = ''
self.fileH.seek(0)
pe.close()
if __name__== '__main__':
CARVER()