forked from DidierStevens/DidierStevensSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disitool.py
259 lines (195 loc) · 8.91 KB
/
disitool.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
"""
tool to manipulate digital signatures in PE files
commands:
- delete signed-file unsigned-file
- copy signed-source-file unsigned-file signed-file
- extract signed-file signature
- add signature unsigned-file signed-file
- inject [--paddata] signed-source-file data-file signed-destination-file
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2007/12/21: added arguments
2008/01/09: code review
2008/03/26: added checksum correction
2009/01/17: V0.3 added inject, requested by H D Moore
2009/01/18: --paddata option
2020/11/27: 0.4 Python 3
requires pefile:
http://code.google.com/p/pefile/
to install: setup.py install
"""
import optparse
import pefile
from struct import *
__author__ = 'Didier Stevens (https://DidierStevens.com)'
__version__ = '0.4'
__date__ = '2020/11/27'
def Usage():
"""Displays the usage of this tool
"""
print('Usage: disitool command [options] file ...')
print(' disitool V%s %s, tool to manipulate digital signatures in PE files' % (__version__, __date__))
print(' commands:')
print(' - delete signed-file unsigned-file')
print(' - copy signed-source-file unsigned-file signed-file')
print(' - extract signed-file signature')
print(' - add signature unsigned-file signed-file')
print(' - inject [--paddata] signed-source-file data-file signed-destination-file')
print(' Source code put in the public domain by Didier Stevens, no Copyright')
print(' Use at your own risk')
print(' https://DidierStevens.com')
def DeleteDigitalSignature(SignedFile, UnsignedFile=None):
"""Deletes the digital signature from file SignedFile
When UnsignedFile is not None, writes the modified file to UnsignedFile
Returns the modified file as a PE file
"""
pe = pefile.PE(SignedFile)
address = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress
pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress = 0
pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size = 0
if address != 0:
peUnsignedFile = pefile.PE(data=pe.write()[0:address])
else:
peUnsignedFile = pefile.PE(data=pe.write())
peUnsignedFile.OPTIONAL_HEADER.CheckSum = peUnsignedFile.generate_checksum()
new_file_data = peUnsignedFile.write()
if UnsignedFile:
f = open(UnsignedFile, 'wb+')
f.write(new_file_data)
f.close()
return new_file_data
def CopyDigitalSignature(SignedSourceFile, UnsignedFile, SignedFile=None):
"""Extracts the digital signature from file SignedSourceFile and adds it to file UnsignedFile
When SignedFile is not None, writes the modified file to SignedFile
Returns the modified file as a PE file
"""
peSignedSource = pefile.PE(SignedSourceFile)
address = peSignedSource.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress
size = peSignedSource.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size
if address == 0:
print('Error: source file not signed')
return
signature = peSignedSource.write()[address:]
peUnsigned = DeleteDigitalSignature(UnsignedFile)
peSignedFileTemp = pefile.PE(data=peUnsigned + signature)
peSignedFileTemp.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress = len(peUnsigned)
peSignedFileTemp.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size = size
peSignedFile = pefile.PE(data=peSignedFileTemp.write())
peSignedFile.OPTIONAL_HEADER.CheckSum = peSignedFile.generate_checksum()
new_file_data = peSignedFile.write()
if SignedFile:
f = open(SignedFile, 'wb+')
f.write(new_file_data)
f.close()
return new_file_data
def ExtractDigitalSignature(SignedFile, SignatureFile=None):
"""Extracts the digital signature from file SignedFile
When SignatureFile is not None, writes the signature to SignatureFile
Returns the signature
"""
pe = pefile.PE(SignedFile)
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('Error: source file not signed')
return
signature = pe.write()[address+8:]
if SignatureFile:
f = open(SignatureFile, 'wb+')
f.write(signature)
f.close()
return signature
def AddDigitalSignature(SignatureFile, UnsignedFile, SignedFile=None):
"""Adds the digital signature from file SignatureFile to file UnsignedFile
When SignedFile is not None, writes the modified file to SignedFile
Returns the modified file as a PE file
"""
f = open(SignatureFile, 'rb')
signature = f.read()
f.close()
size = len(signature) + 8
peUnsigned = DeleteDigitalSignature(UnsignedFile)
peSignedFileTemp = pefile.PE(data=peUnsigned + pack('<I', size) + b'\x00\x02\x02\x00' + signature)
peSignedFileTemp.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress = len(peUnsigned)
peSignedFileTemp.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size = size
peSignedFile = pefile.PE(data=peSignedFileTemp.write())
peSignedFile.OPTIONAL_HEADER.CheckSum = peSignedFile.generate_checksum()
new_file_data = peSignedFile.write()
if SignedFile:
f = open(SignedFile, 'wb+')
f.write(new_file_data)
f.close()
return new_file_data
def InjectDataInSignedExecutable(SignedSourceFile, DataFile, SignedFile=None, paddata=False):
"""Inject data (DataFile) inside a signed executable (SignedSourceFile) without invalidating the signature
The procedure is to append the data to the digital signature and increase the size of the digital signature
When SignedFile is not None, writes the modified file to SignedFile
Returns the modified file as a PE file
"""
peSignedSource = pefile.PE(SignedSourceFile)
address = peSignedSource.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress
size = peSignedSource.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size
if address == 0:
print('Error: source file not signed')
return
f = open(DataFile, 'rb')
DataToInject = f.read()
f.close()
if len(DataToInject) % 8 != 0:
if paddata:
DataToInject = DataToInject + b'\x00' * (8 - (len(DataToInject) % 8))
print('Info: padded the data to inject')
else:
print('Warning: the length of the data to inject is not a multiple of 8')
signature = peSignedSource.write()[address:]
peUnsigned = peSignedSource.write()[:address]
peSignedFileTemp = pefile.PE(data=peUnsigned + signature + DataToInject)
peSignedFileTemp.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size = size + len(DataToInject)
peSignedFile = pefile.PE(data=peSignedFileTemp.write())
peSignedFile.OPTIONAL_HEADER.CheckSum = peSignedFile.generate_checksum()
new_file_data = peSignedFile.write()
if SignedFile:
f = open(SignedFile, 'wb+')
f.write(new_file_data)
f.close()
return new_file_data
def Main():
"""Parses the command line and executes the appropriate function
"""
oParser = optparse.OptionParser(usage='usage: %prog [options] command files...', version='%prog ' + __version__)
oParser.add_option('-p', '--paddata', action='store_true', default=False, help='pad data to a multiple of 8 bytes')
(options, args) = oParser.parse_args()
if len(args) == 0:
Usage()
elif args[0] == 'delete':
if len(args) == 3:
DeleteDigitalSignature(args[1], args[2])
else:
Usage()
elif args[0] == 'copy':
if len(args) == 4:
CopyDigitalSignature(args[1], args[2], args[3])
else:
Usage()
elif args[0] == 'extract':
if len(args) == 3:
ExtractDigitalSignature(args[1], args[2])
else:
Usage()
elif args[0] == 'add':
if len(args) == 4:
AddDigitalSignature(args[1], args[2], args[3])
else:
Usage()
elif args[0] == 'inject':
if len(args) == 4:
InjectDataInSignedExecutable(args[1], args[2], args[3], options.paddata)
else:
Usage()
else:
Usage()
if __name__ == '__main__':
Main()