# 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 script compiles a source file (asm or C) into a shellcode that will # dynamically resolve the address of functions it uses # windows only, supposes the shellcode is run in the address space of a process # whose PEB allows to find all required libraries. require 'metasm' sc = Metasm::Shellcode.new(Metasm::Ia32.new) case ARGV[0] when /\.c(pp)?$/i src_c = File.read(ARGV[0]) sc.assemble 'jmp main' sc.compile_c src_c when /\.asm$/i src = File.read(ARGV[0]) sc.assemble src when nil; abort "need sourcefile" else abort "unknown srcfile extension" end # find external symbols needed by the shellcode ext_syms = sc.encoded.reloc_externals # resolver code sc.parse <> 0xd) | (hash << (32-0xd))) + char) & 0xffff_ffff } hash end def lib_name(sym) raise "unknown libname for #{sym}" if not lib = Metasm::WindowsExports::EXPORT[sym] n = lib.downcase[0, 4].unpack('C*') n[0] + (n[1]<<8) + (n[2] << 16) + (n[3] << 24) end # encode stub for each symbol ext_syms.uniq.each { |sym| next if sym == 'next_payload' sc.parse <