# This file is part of Metasm, the Ruby assembly manipulation suite # Copyright (C) 2006-2009 Yoann GUILLOT # # Licence is LGPL, see LICENCE in the top-level directory # This samples generates a binary loader that will load and patch a Win32 program in memory # The patch data are read from assembly files named 'patch_.asm' # The 1st mandatory argument is the name of the target binary to load # The 2nd optional argument is the name of the loader to be generated require 'metasm' target = ARGV.shift loader = ARGV.shift || "loader.exe" abort "need a target binary name to load&patch" if not target cpu = Metasm::Ia32.new # assemble the patches, to put the binary in the C source patches = Dir['patch_*.asm'].map { |f| puts " [+] assembling #{f}" addr = f[/patch_(.*)\.asm/, 1].to_i(16) sc = Metasm::Shellcode.assemble_file(cpu, f) sc.base_addr = addr raw = sc.encode_string [addr, raw] } # the C program skeleton c_src = <