Files
metasploit-gs/lib/metasploit/framework/compiler/windows.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
3.4 KiB
Ruby
Raw Normal View History

2018-05-11 10:27:54 -05:00
require 'metasm'
require 'erb'
require 'metasploit/framework/compiler/utils'
require 'metasploit/framework/compiler/headers/windows'
require 'metasploit/framework/obfuscation/crandomizer'
2018-05-11 10:27:54 -05:00
module Metasploit
module Framework
module Compiler
class Windows
# Returns the binary of a compiled source.
#
# @param c_template [String] The C source code to compile.
# @param type [Symbol] PE type, either :exe or :dll
# @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new
2018-05-11 10:27:54 -05:00
# @raise [NotImplementedError] If the type is not supported.
# @return [String] The compiled code.
def self.compile_c(c_template, type=:exe, cpu=Metasm::Ia32.new)
headers = Compiler::Headers::Windows.new
source_code = Compiler::Utils.normalize_code(c_template, headers)
pe = Metasm::PE.compile_c(cpu, source_code)
case type
when :exe
pe.encode
when :dll
pe.encode('dll')
else
raise NotImplementedError
end
end
# Saves the compiled code as a file. This is basically a wrapper of #self.compile.
#
# @param out_file [String] The file path to save the binary as.
# @param c_template [String] The C source code to compile.
# @param type [Symbol] PE type, either :exe or :dll
# @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new
2018-05-11 10:27:54 -05:00
# @return [Integer] The number of bytes written.
2018-05-11 10:29:24 -05:00
def self.compile_c_to_file(out_file, c_template, type=:exe, cpu=Metasm::Ia32.new)
2018-07-18 19:50:46 -05:00
pe = self.compile_c(c_template, type)
2022-03-10 18:03:35 +00:00
File.write(out_file, pe, mode: 'wb')
2018-05-11 10:27:54 -05:00
end
2019-10-15 12:50:58 -05:00
# Returns randomized c source code.
#
# @param c_template [String]
2020-03-24 18:07:18 +05:30
#
# @raise [NotImplementedError] If the type is not supported.
# @return [String] The compiled code.
2019-10-15 12:50:58 -05:00
def self.generate_random_c(c_template, opts={})
2018-06-29 00:07:40 -05:00
weight = opts[:weight] || 80
headers = Compiler::Headers::Windows.new
source_code = Compiler::Utils.normalize_code(c_template, headers)
2019-10-15 12:50:58 -05:00
randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)
2018-06-27 00:09:04 -05:00
randomized_code = randomizer.parse(source_code)
2019-10-15 12:50:58 -05:00
randomized_code.to_s
end
# Returns the binary of a randomized and compiled source code.
#
2019-10-18 08:28:25 -05:00
# @param c_template [String]
2020-03-24 18:07:18 +05:30
#
2019-10-15 12:50:58 -05:00
# @raise [NotImplementedError] If the type is not supported.
# @return [String] The compiled code.
2019-10-18 08:28:25 -05:00
def self.compile_random_c(c_template, opts={})
2019-10-15 12:50:58 -05:00
type = opts[:type] || :exe
cpu = opts[:cpu] || Metasm::Ia32.new
2019-10-18 08:28:25 -05:00
random_c = self.generate_random_c(c_template, opts)
self.compile_c(random_c, type, cpu)
2018-06-27 00:09:04 -05:00
end
# Saves the randomized compiled code as a file. This is basically a wrapper for #self.compile_random_c
#
# @param out_file [String] The file path to save the binary as.
2019-10-18 08:28:25 -05:00
# @param c_template [String] The randomized C source code to compile.
2018-06-27 00:09:04 -05:00
# @param opts [Hash] Options to pass to #compile_random_c
# @return [Integer] The number of bytes written.
2019-10-18 08:28:25 -05:00
def self.compile_random_c_to_file(out_file, c_template, opts={})
2019-10-18 11:06:40 -05:00
pe = self.compile_random_c(c_template, opts)
2022-03-10 18:03:35 +00:00
File.write(out_file, pe, mode: 'wb')
end
2018-05-11 10:27:54 -05:00
end
end
end
2019-10-15 12:50:58 -05:00
end