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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
# File 'lib/fileshunter/Decoders/EXE.rb', line 48
def decode(offset)
ending_offset = nil
cursor = offset + BinData::Uint32le.read(@data[offset+60..offset+63])
progress(cursor)
invalid_data("@#{cursor} - Invalid PE/NE header") if ((@data[cursor..cursor+3] != PE_SIGNATURE) and (@data[cursor..cursor+1] != NE_SIGNATURE))
if (@data[cursor..cursor+1] == NE_SIGNATURE)
ne_offset = cursor
cursor += 2
entry_table_offset = BinData::Uint16le.read(@data[cursor+2..cursor+3])
entry_table_size = BinData::Uint16le.read(@data[cursor+4..cursor+5])
nbr_segments = BinData::Uint16le.read(@data[cursor+26..cursor+27])
nbr_module_reference = BinData::Uint16le.read(@data[cursor+28..cursor+29])
non_resident_names_table_size = BinData::Uint16le.read(@data[cursor+30..cursor+31])
segment_table_offset = BinData::Uint16le.read(@data[cursor+32..cursor+33])
resource_table_offset = BinData::Uint16le.read(@data[cursor+34..cursor+35])
resident_names_table_offset = BinData::Uint16le.read(@data[cursor+36..cursor+37])
module_reference_table_offset = BinData::Uint16le.read(@data[cursor+38..cursor+39])
imported_names_table_offset = BinData::Uint16le.read(@data[cursor+40..cursor+41])
non_resident_names_table_offset = BinData::Uint32le.read(@data[cursor+42..cursor+45])
cursor += 62
progress(cursor)
log_debug "@#{cursor} - NE header: entry_table_offset=#{entry_table_offset} entry_table_size=#{entry_table_size} nbr_segments=#{nbr_segments} nbr_module_reference=#{nbr_module_reference} non_resident_names_table_size=#{non_resident_names_table_size} segment_table_offset=#{segment_table_offset} resource_table_offset=#{resource_table_offset} resident_names_table_offset=#{resident_names_table_offset} module_reference_table_offset=#{module_reference_table_offset} imported_names_table_offset=#{imported_names_table_offset} non_resident_names_table_offset=#{non_resident_names_table_offset}"
log_debug "@#{cursor} - Segment table"
invalid_data("@#{cursor} - Segment table offset (#{segment_table_offset}) should have been set here (#{cursor-ne_offset}).") if (segment_table_offset != cursor-ne_offset)
segment_data = {}
nbr_segments.times do |idx_segment|
segment_data_offset = BinData::Uint16le.read(@data[cursor..cursor+1])
if (segment_data_offset > 0)
segment_data_size = BinData::Uint16le.read(@data[cursor+2..cursor+3])
segment_data_size = 65536 if (segment_data_size == 0)
segment_data[segment_data_offset] = segment_data_size
end
cursor += 8
end
progress(cursor)
max_cursor = cursor
log_debug "@#{cursor} - Resource table"
invalid_data("@#{cursor} - Resource table offset (#{resource_table_offset}) should have been set here (#{cursor-ne_offset}).") if (resource_table_offset != cursor-ne_offset)
alignment_shift_count = BinData::Uint16le.read(@data[cursor..cursor+1])
offset_factor = (1 << alignment_shift_count)
resource_data = {}
type_id = BinData::Uint16le.read(@data[cursor+2..cursor+3])
cursor += 4
while (type_id != 0)
if ((type_id & 0b10000000_00000000) == 0)
str_offset = ne_offset + resource_table_offset + type_id
end_str_cursor = str_offset + 1 + @data[str_offset].ord
max_cursor = end_str_cursor if (max_cursor < end_str_cursor)
end
nbr_resources_for_this_type = BinData::Uint16le.read(@data[cursor..cursor+1])
reserved = BinData::Uint32le.read(@data[cursor+2..cursor+5])
invalid_data("@#{cursor} - Reserved data should have been null: #{reserved}") if (reserved != 0)
cursor += 6
nbr_resources_for_this_type.times do |idx_resource_for_this_type|
resource_offset = BinData::Uint16le.read(@data[cursor..cursor+1])
resource_size = BinData::Uint16le.read(@data[cursor+2..cursor+3])
resource_flags = BinData::Uint16le.read(@data[cursor+4..cursor+5])
resource_id = BinData::Uint16le.read(@data[cursor+6..cursor+7])
real_offset = resource_offset * offset_factor
real_size = resource_size * offset_factor
log_debug "@#{cursor} - Found resource ##{resource_id} @#{real_offset} of size #{real_size} with flags #{resource_flags}"
reserved = BinData::Uint32le.read(@data[cursor+8..cursor+11])
invalid_data("@#{cursor} - Reserved data should have been null: #{reserved}") if (reserved != 0)
resource_data[real_offset] = real_size
if ((resource_id & 0b10000000_00000000) == 0)
str_offset = ne_offset + resource_table_offset + resource_id
end_str_cursor = str_offset + 1 + @data[str_offset].ord
max_cursor = end_str_cursor if (max_cursor < end_str_cursor)
end
cursor += 12
progress(cursor)
end
type_id = BinData::Uint16le.read(@data[cursor..cursor+1])
cursor += 2
progress(cursor)
end
progress(cursor)
max_cursor = cursor if (max_cursor < cursor)
cursor = ne_offset + resident_names_table_offset
log_debug "@#{cursor} - Resident names table"
next_string_length = @data[cursor].ord
cursor += 1
while (next_string_length > 0)
str_name = @data[cursor..cursor+next_string_length-1]
log_debug "@#{cursor} - Found resident name string: #{str_name.inspect}"
cursor += next_string_length
next_string_length = @data[cursor+2].ord
cursor += 3
progress(cursor)
end
max_cursor = cursor if (max_cursor < cursor)
cursor = ne_offset + module_reference_table_offset
log_debug "@#{cursor} - Module reference table"
nbr_module_reference.times do |idx_module_reference|
cursor += 2
end
progress(cursor)
max_cursor = cursor if (max_cursor < cursor)
cursor = ne_offset + imported_names_table_offset
log_debug "@#{cursor} - Imported names table"
next_string_length = @data[cursor].ord
cursor += 1
while (next_string_length > 0)
str_name = @data[cursor..cursor+next_string_length-1]
log_debug "@#{cursor} - Found imported name string: #{str_name.inspect}"
cursor += next_string_length
next_string_length = @data[cursor].ord
cursor += 1
progress(cursor)
end
max_cursor = cursor if (max_cursor < cursor)
cursor = ne_offset + entry_table_offset
log_debug "@#{cursor} - Entry table"
nbr_entries_in_bundle = @data[cursor].ord
cursor += 2
while (nbr_entries_in_bundle > 0)
invalid_data("@#{cursor} - Cannot decode entry tables from NE executable files yet. Sorry.")
nbr_entries_in_bundle = @data[cursor].ord
end
invalid_data("@#{cursor} - Declared entry table size (#{entry_table_size}) is smaller than what has been read: #{cursor-ne_offset-entry_table_offset}") if (entry_table_size < cursor-ne_offset-entry_table_offset)
cursor = ne_offset + entry_table_offset + entry_table_size
max_cursor = cursor if (max_cursor < cursor)
cursor = offset + non_resident_names_table_offset
log_debug "@#{cursor} - Non resident name table"
next_string_length = @data[cursor].ord
cursor += 1
while (next_string_length > 0)
str_name = @data[cursor..cursor+next_string_length-1]
log_debug "@#{cursor} - Found non resident name string: #{str_name.inspect}"
cursor += next_string_length
next_string_length = @data[cursor+2].ord
cursor += 3
progress(cursor)
end
invalid_data("@#{cursor} - Declared non resident name table size (#{non_resident_names_table_size}) is smaller than what has been read: #{cursor-offset-non_resident_names_table_offset}") if (non_resident_names_table_size < cursor-offset-non_resident_names_table_offset)
cursor = offset + non_resident_names_table_offset + non_resident_names_table_size
max_cursor = cursor if (max_cursor < cursor)
segment_data.merge(resource_data).each do |data_offset, data_size|
cursor = offset + data_offset
cursor += data_size
max_cursor = cursor if (max_cursor < cursor)
end
ending_offset = max_cursor
found_relevant_data(:fon)
else
cursor += 4
target_machine = @data[cursor..cursor+1]
invalid_data("@#{cursor} - Invalid machine type: #{target_machine.inspect}") if (!KNOWN_MACHINE_TYPES.include?(target_machine))
nbr_sections = BinData::Uint16le.read(@data[cursor+2..cursor+3])
symbol_table_offset = BinData::Uint32le.read(@data[cursor+8..cursor+11])
nbr_symbols = BinData::Uint32le.read(@data[cursor+12..cursor+15])
= BinData::Uint16le.read(@data[cursor+16..cursor+17])
characteristics = BinData::Uint16le.read(@data[cursor+18..cursor+19])
invalid_data("@#{cursor+18} - Invalid characteristics #{characteristics}: bits should be 0") if ((characteristics & 80) != 0)
file_type = ((characteristics & 8192) == 0) ? :exe : (( == 0) ? :obj : :dll)
found_relevant_data((file_type == :exe) ? [ :exe, :sys ] : ((file_type == :obj) ? :obj : [ :dll, :drv, :ocx ]))
metadata(
:target_machine => target_machine,
:nbr_sections => nbr_sections,
:symbol_table_offset => symbol_table_offset,
:nbr_symbols => nbr_symbols,
:optional_header_size => ,
:characteristics => characteristics
)
cursor += 20
progress(cursor)
file_alignment = nil
certificate_table_offset = nil
certificate_table_size = nil
= cursor +
if ( > 0)
c_1 = @data[cursor+1]
invalid_data("@#{cursor} - Invalid optional header") if ((@data[cursor] != OPTIONAL_HEADER_ID) or (!KNOWN_OPTIONAL_HEADER_TYPES.include?(c_1)))
mode_pe32 = (c_1 == PE32_ID)
file_alignment = BinData::Uint32le.read(@data[cursor+36..cursor+39])
win32_version = BinData::Uint32le.read(@data[cursor+52..cursor+55])
invalid_data("@#{cursor+52} - Invalid Win32 version: #{win32_version}") if (win32_version != 0)
= BinData::Uint32le.read(@data[cursor+60..cursor+64])
subsystem = BinData::Uint16le.read(@data[cursor+68..cursor+69])
if (subsystem == 1)
case file_type
when :dll
file_type = :drv
found_relevant_data(:drv)
when :exe
file_type = :sys
found_relevant_data(:sys)
end
end
dll_characteristics = BinData::Uint16le.read(@data[cursor+70..cursor+71])
invalid_data("@#{cursor+70} - Invalid DLL characteristics #{dll_characteristics}: bits should be 0") if ((dll_characteristics & 4111) != 0)
nbr_rva_and_sizes = nil
if mode_pe32
nbr_rva_and_sizes = BinData::Uint32le.read(@data[cursor+92..cursor+95])
cursor += 96
else
nbr_rva_and_sizes = BinData::Uint32le.read(@data[cursor+108..cursor+111])
cursor += 112
end
if (nbr_rva_and_sizes >= 5)
certificate_table_offset = BinData::Uint32le.read(@data[cursor+32..cursor+35])
certificate_table_size = BinData::Uint32le.read(@data[cursor+36..cursor+39])
end
cursor += 8*nbr_rva_and_sizes
progress(cursor)
log_debug "@#{cursor} - Extended header: mode_pe32=#{mode_pe32} win32_version=#{win32_version} headers_size=#{} subsystem=#{subsystem} dll_characteristics=#{dll_characteristics} nbr_rva_and_sizes=#{nbr_rva_and_sizes}"
invalid_data("@#{cursor} - Optional headers end at #{cursor} but were supposed to end at #{}") if (cursor != )
metadata(
:mode_pe32 => mode_pe32,
:file_alignment => file_alignment,
:win32_version => win32_version,
:headers_size => ,
:subsystem => subsystem,
:dll_characteristics => dll_characteristics,
:nbr_rva_and_sizes => nbr_rva_and_sizes,
:certificate_table_offset => certificate_table_offset,
:certificate_table_size => certificate_table_size
)
end
log_debug "@#{cursor} - PE Header: nbr_sections=#{nbr_sections} file_alignment=#{file_alignment} symbol_table_offset=#{symbol_table_offset} nbr_symbols=#{nbr_symbols} certificate_table_offset=#{certificate_table_offset} certificate_table_size=#{certificate_table_size}"
log_debug "@#{cursor} - Beginning of section headers"
sections = {}
line_numbers = {}
text_section_offset = nil
nbr_sections.times do |idx_section|
name = @data[cursor..cursor+7]
raw_data_size = BinData::Uint32le.read(@data[cursor+16..cursor+19])
raw_data_offset = BinData::Uint32le.read(@data[cursor+20..cursor+23])
line_numbers_offset = BinData::Uint32le.read(@data[cursor+28..cursor+31])
nbr_line_numbers = BinData::Uint16le.read(@data[cursor+34..cursor+35])
log_debug "@#{cursor} - Found section #{name}: raw_data_offset=#{raw_data_offset} raw_data_size=#{raw_data_size}"
text_section_offset = raw_data_offset if (name == SECTION_TEXT_ID)
cursor += 40
progress(cursor)
sections[raw_data_offset] = raw_data_size if (raw_data_size > 0)
line_numbers[line_numbers_offset] = nbr_line_numbers if (nbr_line_numbers > 0)
end
cursor = offset + if ( != nil)
progress(cursor)
max_cursor = cursor
log_debug "@#{cursor} - Beginning of sections"
sections.keys.sort.each_with_index do |section_offset, idx_section|
rest = (file_alignment == nil) ? 0 : ((cursor-offset) % file_alignment)
cursor += file_alignment - rest if (rest > 0)
if ((cursor-offset) != section_offset)
log_debug("@#{cursor} - Section #{idx_section} should have been at offset #{cursor-offset}, but is declared at #{section_offset}")
cursor = offset + section_offset
end
if ((text_section_offset == section_offset) and
(file_type == :dll) and
(@data.index('DllRegisterServer', cursor) != nil))
file_type = :ocx
found_relevant_data(:ocx)
end
cursor += sections[section_offset]
max_cursor = cursor if (cursor > max_cursor)
progress(cursor)
end
log_debug "@#{cursor} - Beginning of COFF line numbers"
line_numbers.keys.sort.each do |line_number_offset|
if ((cursor-offset) != line_number_offset)
log_debug("@#{cursor} - COFF line number section should have been at offset #{cursor-offset}, but is declared at #{line_number_offset}")
cursor = offset + line_number_offset
end
cursor += 6*line_numbers[line_number_offset]
max_cursor = cursor if (cursor > max_cursor)
progress(cursor)
end
log_debug "@#{cursor} - Beginning of symbol tables"
if (symbol_table_offset > 0)
if ((cursor-offset) != symbol_table_offset)
log_debug("@#{cursor} - Symbol table should have been at offset #{cursor-offset}, but is declared at #{symbol_table_offset}")
cursor = offset + symbol_table_offset
end
nbr_symbols.times do |idx_symbol|
cursor += 18
progress(cursor)
log_debug "@#{cursor} - Finished decoding symbol \##{idx_symbol}"
end
log_debug "@#{cursor} - Beginning of COFF string table"
coff_string_table_size = BinData::Uint32le.read(@data[cursor..cursor+3])
invalid_data("@#{cursor} - COFF string table size should be >= 4 (#{coff_string_table_size})") if (coff_string_table_size < 4)
cursor += coff_string_table_size
max_cursor = cursor if (cursor > max_cursor)
progress(cursor)
end
log_debug "@#{cursor} - Beginning of certificate table"
if ((certificate_table_offset != nil) and
(certificate_table_offset > 0))
if ((cursor-offset) != certificate_table_offset)
log_debug("@#{cursor} - Certificate table should have been at offset #{cursor-offset}, but is declared at #{certificate_table_offset}")
cursor = offset + certificate_table_offset
end
while (cursor < offset + certificate_table_offset + certificate_table_size)
log_debug "@#{cursor} - Read certificate"
certificate_size = BinData::Uint32le.read(@data[cursor..cursor+3])
log_debug "@#{cursor} - Found certificate of size #{certificate_size}"
cursor += certificate_size
rest = (cursor-offset) % 8
cursor += 8 - rest if (rest > 0)
progress(cursor)
end
max_cursor = cursor if (cursor > max_cursor)
end
ending_offset = max_cursor
end
return ending_offset
end
|