Path: blob/master/spec/data/templates/pe_headers_spec.rb
59959 views
require 'spec_helper'1require 'rex/peparsey'23RSpec.describe 'PE template binaries' do4templates_dir = File.expand_path('../../../../data/templates', __FILE__)56# Architecture expectations keyed by filename pattern. Value is the expected7# Machine field from the PE file header.8ARCH_EXPECTATIONS = {9/\btemplate_x86_windows(_svc|_old|_dccw_gdiplus|_mixed_mode)?(\.256kib)?\.(exe|dll)\z/ => Rex::PeParsey::PeBase::IMAGE_FILE_MACHINE_I386,10/\btemplate_x64_windows(_svc|_dccw_gdiplus|_mixed_mode)?(\.256kib)?\.(exe|dll)\z/ => Rex::PeParsey::PeBase::IMAGE_FILE_MACHINE_AMD64,11/\btemplate_aarch64_windows\.exe\z/ => Rex::PeParsey::PeBase::IMAGE_FILE_MACHINE_ARM6412}.freeze1314# Minimum subsystem version the EXE templates must support. DLLs are not15# checked by the loader for this field, so we only enforce it on EXEs.16# x86 EXEs must run on NT 4.0 (4.0); x64 EXEs must run on Server 2003 (5.2).17EXE_VERSION_EXPECTATIONS = {18/\btemplate_x86_windows(_svc)?\.exe\z/ => [4, 0],19/\btemplate_x64_windows(_svc)?\.exe\z/ => [5, 2]20}.freeze2122templates = Dir.glob(File.join(templates_dir, 'template_*_windows*.{exe,dll}')).sort2324it 'has the expected set of PE templates present' do25expect(templates).not_to be_empty26end2728templates.each do |path|29name = File.basename(path)3031describe name do32let(:pe) { Rex::PeParsey::Pe.new_from_file(path, true) }33after { pe.close if pe.respond_to?(:close) }3435arch_pattern, expected_machine = ARCH_EXPECTATIONS.find { |re, _| name =~ re }3637if arch_pattern38it "has Machine matching its filename (0x#{expected_machine.to_s(16)})" do39expect(pe.hdr.file.Machine).to eq(expected_machine)40end41else42it 'is covered by an architecture expectation' do43fail "no architecture expectation matches #{name}; update ARCH_EXPECTATIONS"44end45end4647version_pattern, version_expect = EXE_VERSION_EXPECTATIONS.find { |re, _| name =~ re }48if version_pattern49expected_major, expected_minor = version_expect5051it "has subsystem version #{expected_major}.#{expected_minor} so it runs on legacy Windows" do52actual = [pe.hdr.opt.MajorSubsystemVersion, pe.hdr.opt.MinorSubsystemVersion]53expect(actual).to eq([expected_major, expected_minor])54end5556it "has OS version #{expected_major}.#{expected_minor}" do57actual = [pe.hdr.opt.MajorOperatingSystemVersion, pe.hdr.opt.MinorOperatingSystemVersion]58expect(actual).to eq([expected_major, expected_minor])59end60end61end62end63end646566