-
Notifications
You must be signed in to change notification settings - Fork 44
/
extract_sig.py
34 lines (27 loc) · 954 Bytes
/
extract_sig.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
#!/usr/bin/python
import argparse
import pefile
import os
import sys
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Extract PE Signature')
parser.add_argument('PEFILE', help='PE File')
parser.add_argument('--output', '-o', help='Output file')
args = parser.parse_args()
if not os.path.isfile(args.PEFILE):
print("Invalid path")
sys.exit(-1)
if args.output:
output = args.output
else:
output = args.PEFILE + '.sig'
pe = pefile.PE(args.PEFILE)
address = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress
size = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size
if address == 0:
print('Source file not signed')
sys.exit(0)
signature = pe.write()[address+8:]
f = open(output, 'wb+')
f.write(signature)
f.close()