Path: blob/master/lib/msf/util/exe/linux/x86.rb
57477 views
# -*- coding: binary -*-1module Msf::Util::EXE::Linux::X862include Msf::Util::EXE::Common3include Msf::Util::EXE::Linux::Common456def self.included(base)7base.extend(ClassMethods)8end910module ClassMethods1112# Create a 32-bit Linux ELF containing the payload provided in +code+13# to_linux_x86_elf14#15# @param framework [Msf::Framework] The framework of you want to use16# @param code [String]17# @param opts [Hash]18# @option [String] :template19# @return [String] Returns an elf20def to_linux_x86_elf(framework, code, opts = {})21default = true unless opts[:template]2223return to_exe_elf(framework, opts, "template_x86_linux.bin", code) if default24return to_linux_x86_custom_elf(framework, code, opts)25end2627# Create a 32-bit Linux ELF containing the payload provided in +code+ with custom template28# to_linux_x86_custom_elf29#30# @param framework [Msf::Framework]31# @param code [String]32# @param opts [Hash]33# @option [String] :template34# @return [String] Returns an elf35def to_linux_x86_custom_elf(framework, code, opts = {})36# Use set_template_default to normalize the :template key. It will just end up doing37# opts[:template] = File.join(opts[:template_path], opts[:template])38# for us, check if the file exists.39set_template_default(opts, 'template_x86_linux.bin')4041# If this isn't our normal template, we have to do some fancy42# header patching to mark the .text section rwx before putting our43# payload into the entry point.4445# read in the template and parse it46e = Metasm::ELF.decode_file(opts[:template])4748# This will become a modified copy of the template's original phdr49new_phdr = Metasm::EncodedData.new50e.segments.each { |s|51# Be lazy and mark any executable segment as writable. Doing52# it this way means we don't have to care about which one53# contains .text54s.flags += [ "W" ] if s.flags.include? "X"55new_phdr << s.encode(e)56}5758# Copy the original file59elf = get_file_contents(opts[:template], "rb")6061# Replace the header with our rwx modified version62elf[e.header.phoff, new_phdr.data.length] = new_phdr.data6364# Replace code at the entrypoint with our payload65entry_off = e.addr_to_off(e.label_addr('entrypoint'))66elf[entry_off, code.length] = code67end686970# Create a 32-bit Linux ELF_DYN containing the payload provided in +code+71# to_linux_x86_elf_dll72#73# @param framework [Msf::Framework]74# @param code [String]75# @param opts [Hash]76# @option [String] :template77# @return [String] Returns an elf78def to_linux_x86_elf_dll(framework, code, opts = {})79to_exe_elf(framework, opts, "template_x86_linux_dll.bin", code)80end81end8283class << self84include ClassMethods85end8687end888990