Path: blob/master/lib/msf/util/exe/linux/x86.rb
36043 views
module Msf::Util::EXE::Linux::X861include Msf::Util::EXE::Common2include Msf::Util::EXE::Linux::Common345def self.included(base)6base.extend(ClassMethods)7end89module ClassMethods1011# Create a 32-bit Linux ELF containing the payload provided in +code+12# to_linux_x86_elf13#14# @param framework [Msf::Framework] The framework of you want to use15# @param code [String]16# @param opts [Hash]17# @option [String] :template18# @return [String] Returns an elf19def to_linux_x86_elf(framework, code, opts = {})20default = true unless opts[:template]2122return to_exe_elf(framework, opts, "template_x86_linux.bin", code) if default23return to_linux_x86_custom_elf(framework, code, opts)24end2526# Create a 32-bit Linux ELF containing the payload provided in +code+ with custom template27# to_linux_x86_custom_elf28#29# @param framework [Msf::Framework]30# @param code [String]31# @param opts [Hash]32# @option [String] :template33# @return [String] Returns an elf34def to_linux_x86_custom_elf(framework, code, opts = {})35# Use set_template_default to normalize the :template key. It will just end up doing36# opts[:template] = File.join(opts[:template_path], opts[:template])37# for us, check if the file exists.38set_template_default(opts, 'template_x86_linux.bin')3940# If this isn't our normal template, we have to do some fancy41# header patching to mark the .text section rwx before putting our42# payload into the entry point.4344# read in the template and parse it45e = Metasm::ELF.decode_file(opts[:template])4647# This will become a modified copy of the template's original phdr48new_phdr = Metasm::EncodedData.new49e.segments.each { |s|50# Be lazy and mark any executable segment as writable. Doing51# it this way means we don't have to care about which one52# contains .text53s.flags += [ "W" ] if s.flags.include? "X"54new_phdr << s.encode(e)55}5657# Copy the original file58elf = get_file_contents(opts[:template], "rb")5960# Replace the header with our rwx modified version61elf[e.header.phoff, new_phdr.data.length] = new_phdr.data6263# Replace code at the entrypoint with our payload64entry_off = e.addr_to_off(e.label_addr('entrypoint'))65elf[entry_off, code.length] = code66end676869# Create a 32-bit Linux ELF_DYN containing the payload provided in +code+70# to_linux_x86_elf_dll71#72# @param framework [Msf::Framework]73# @param code [String]74# @param opts [Hash]75# @option [String] :template76# @return [String] Returns an elf77def to_linux_x86_elf_dll(framework, code, opts = {})78to_exe_elf(framework, opts, "template_x86_linux_dll.bin", code)79end80end8182class << self83include ClassMethods84end8586end878889