-
Notifications
You must be signed in to change notification settings - Fork 27
/
pecarver.py
128 lines (92 loc) · 3.53 KB
/
pecarver.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
#-------------------------------------------------------------------------------
# Name: PECarver
# Purpose: Dump An Executable from a DataStream
#
# Author: Giuseppe 'evilcry' Bonfa
#
# Created: 12/01/2011
# Copyright: (c) evilcry 2011
# Licence: GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import sys
import re
from struct import unpack
from optparse import OptionParser
def main():
usage = "%Prog stream_file\n"
description = "TEST\n"
parser = OptionParser(usage = usage, description = description,
version = "0.1")
(options, args) = parser.parse_args()
if len(args) < 1:
print("Specify the stream file where to carve executable\n")
sys.exit(-1)
else:
try:
fmap = open(args[0],'rb')
mappedFile = fmap.read()
fmap.close()
startMZ = embd_PE_File(mappedFile)
if startMZ != 0:
#dump executable
print("[*] Executable Revealed at Offset= " + hex(startMZ))
exeToCarve = mappedFile[startMZ:]
if dumpExecutable(exeToCarve) is True:
print("[*] Executable Dumped")
else:
print("[-] Not Dumped")
else:
print("[-] No Executable Revealed")
sys.exit(1)
except IOError:
print("IO Error While Opening Stream File")
sys.exit(-1)
except:
print("Generic Error Occurred While Processing Stream")
sys.exit(-1)
sys.exit(1)
def embd_PE_File(mappedFile):
match = re.search(b'MZ', mappedFile)
if match is not None:
startPEOffset = match.start()
match = re.search(b'PE', mappedFile)
if match is not None:
match = re.search(b'This program ', mappedFile)
if match is not None:
return startPEOffset
else:
return 0
else:
return 0
return 0
def dumpExecutable(exeToCarve):
e_lfanew = unpack('B',exeToCarve[0x3C])[0]
numberOfSections = unpack('B',exeToCarve[e_lfanew + 0x6])[0]
offset_sizeOfOptHeader = e_lfanew + 0x14
sizeOfOptionalHeader = unpack('B',exeToCarve[offset_sizeOfOptHeader])[0]
startSectionHeader = (sizeOfOptionalHeader + 0x4) + offset_sizeOfOptHeader
offset_sizeOfImage = offset_sizeOfOptHeader + 0x3C
sizeOfImage = unpack('I', exeToCarve[offset_sizeOfImage:offset_sizeOfImage+4])[0]
i = 0
while ( i <= numberOfSections ):
sectRawAddress = unpack('I',exeToCarve[startSectionHeader+0x14:startSectionHeader+0x18])[0]
sectRawSize = unpack('I',exeToCarve[startSectionHeader+0x10:startSectionHeader+0x14])[0]
nSection = sectRawSize + sectRawAddress
if nSection > sizeOfImage:
sizeOfImage = nSection
i += 1
startSectionHeader += 0x28
continue
carved = exeToCarve[:nSection]
try:
fexe = open('dumped.exe','wb')
fexe.write(carved)
fexe.close()
except IOError:
print("Cannot Dump the Executable")
return False
return True
if __name__ == '__main__':
main()