typedef struct _IMAGE_DOS_HEADER { // DOSµÄ.EXEÍ·²¿
USHORT e_magic; // ħÊõÊý×Ö
USHORT e_cblp; // Îļþ×îºóÒ³µÄ×Ö½ÚÊý
USHORT e_cp; // ÎļþÒ³Êý
USHORT e_crlc; // ÖØ¶¨ÒåÔªËØ¸öÊý
USHORT e_cparhdr; // Í·²¿³ß´ç£¬ÒÔ¶ÎÂäΪµ¥Î»
USHORT e_minalloc; // ËùÐèµÄ×îС¸½¼Ó¶Î
USHORT e_maxalloc; // ËùÐèµÄ×î´ó¸½¼Ó¶Î
USHORT e_ss; // ³õʼµÄSSÖµ£¨Ïà¶ÔÆ«ÒÆÁ¿£©
USHORT e_sp; // ³õʼµÄSPÖµ
USHORT e_csum; // УÑéºÍ
USHORT e_ip; // ³õʼµÄIPÖµ
USHORT e_cs; // ³õʼµÄCSÖµ£¨Ïà¶ÔÆ«ÒÆÁ¿£©
USHORT e_lfarlc; // ÖØ·ÖÅä±íÎļþµØÖ·
USHORT e_ovno; // ¸²¸ÇºÅ
USHORT e_res[4]; // ±£Áô×Ö
USHORT e_oemid; // OEM±êʶ·û£¨Ïà¶Ôe_oeminfo£©
USHORT e_oeminfo; // OEMÐÅÏ¢
USHORT e_res2[10]; // ±£Áô×Ö
LONG e_lfanew; // ÐÂexeÍ·²¿µÄÎļþµØÖ·
} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
ÉÔ΢дÁËÒ»¸ö½Å±¾²é¿´
$: << File.join(File.dirname(__FILE__), "..", "lib")
#require "dynamic-struct"
require 'core-ext'
#class DynamicStruct
# class_inheritable_accessor :fields_spec
#end
#DynamicStruct.fields_spec ||= []
class DynamicStruct
attr_accessor :fields
module ClassMethods
attr_accessor :fields_spec
def u2(symbol, options=nil)
@fields_spec ||= []
@fields_spec << [:u2, symbol, options ]
end
alias :USHORT :u2
def u4(symbol, options=nil)
@fields_spec ||= []
@fields_spec << [:u4, symbol, options ]
end
alias :ULONG :u4
end
extend ClassMethods
def read(file)
@fields ||= []
#puts self.class.fields_spec.inspect
self.class.fields_spec.each do |field_spec|
options = field_spec[2]
options||={}
if field_spec[0] == :u2
value = file.sysread(2)
value = value.unpack("s")[0] unless options[:format] == :string
@fields << [value, field_spec] #options
elsif field_spec[0] == :u4
value = file.sysread(4)
value = value.unpack("i")[0] unless options[:format] == :string
@fields << [value, field_spec] #options
end
end
end
def to_s(format=nil)
result = ""
if(format.nil?)
@fields.each do |value, field_spec|
#puts "converting #{value.inspect}"
result << "#{field_spec[1]}:#{value.to_s}\n"
end
end
result
end
end
class Image_Dos_Header < DynamicStruct
u2 :e_magic, :format=>:string;
u2 :e_cblp; #// Îļþ×îºóÒ³µÄ×Ö½ÚÊý
u2 :e_cp; #// ÎļþÒ³Êý
u2 :e_crlc; #// ÖØ¶¨ÒåÔªËØ¸öÊý
u2 :e_cparhdr; #// Í·²¿³ß´ç£¬ÒÔ¶ÎÂäΪµ¥Î»
u2 :e_minalloc; #// ËùÐèµÄ×îС¸½¼Ó¶Î
u2 :e_maxalloc; #// ËùÐèµÄ×î´ó¸½¼Ó¶Î
u2 :e_ss; #// ³õʼµÄSSÖµ£¨Ïà¶ÔÆ«ÒÆÁ¿£©
u2 :e_sp; #// ³õʼµÄSPÖµ
u2 :e_csum; #// УÑéºÍ
u2 :e_ip; #// ³õʼµÄIPÖµ
u2 :e_cs; #// ³õʼµÄCSÖµ£¨Ïà¶ÔÆ«ÒÆÁ¿£©
u2 :e_lfarlc; #// ÖØ·ÖÅä±íÎļþµØÖ·
u2 :e_ovno; #// ¸²¸ÇºÅ
u2 :e_res_0; #// ±£Áô×Ö
u2 :e_res_1; #// ±£Áô×Ö
u2 :e_res_2; #// ±£Áô×Ö
u2 :e_res_3; #// ±£Áô×Ö
u2 :e_oemid; #// OEM±êʶ·û£¨Ïà¶Ôe_oeminfo£©
u2 :e_oeminfo; #// OEMÐÅÏ¢
u2 :e_res2_0; #// ±£Áô×Ö
u2 :e_res2_1; #// ±£Áô×Ö
u2 :e_res2_2; #// ±£Áô×Ö
u2 :e_res2_3; #// ±£Áô×Ö
u2 :e_res2_4; #// ±£Áô×Ö
u2 :e_res2_5; #// ±£Áô×Ö
u2 :e_res2_6; #// ±£Áô×Ö
u2 :e_res2_7; #// ±£Áô×Ö
u2 :e_res2_8; #// ±£Áô×Ö
u2 :e_res2_9; #// ±£Áô×Ö
u4 :e_lfanew; #// ÐÂexeÍ·²¿µÄÎļþµØÖ·
#field_type :image_dos_header
end
class PEFile < DynamicStruct
#image_dos_header :dos_header
end
file = ARGV[0] || "shake.exe"
f = File.new(file, "rb")
#pefile=PEFile.new
dos_header = Image_Dos_Header.new
#dos_header.read(f)
#pefile.read(f)
f.seek 0x108
puts f.sysread(4)
class Image_File_Header < DynamicStruct
USHORT :Machine;
USHORT :NumberOfSections;
ULONG :TimeDateStamp;
ULONG :PointerToSymbolTable;
ULONG :NumberOfSymbols;
USHORT :SizeOfOptionalHeader;
USHORT :Characteristics;
end
r = Image_File_Header.new
r.read(f)
puts r
f.close
#puts dos_header.to_s
Êä³ö£º
PE
Machine:332
NumberOfSections:4
TimeDateStamp:1083547367
PointerToSymbolTable:0
NumberOfSymbols:0
SizeOfOptionalHeader:224
Characteristics:271