Add compile_random_c func && support optional func collection

GSoC/Meterpreter_Web_Console
Wei Chen 2018-06-25 16:50:05 -05:00
parent 90bc7d2294
commit 823647fbe1
4 changed files with 23 additions and 7 deletions

View File

@ -2,6 +2,7 @@ require 'metasm'
require 'erb' require 'erb'
require 'metasploit/framework/compiler/utils' require 'metasploit/framework/compiler/utils'
require 'metasploit/framework/compiler/headers/windows' require 'metasploit/framework/compiler/headers/windows'
require 'metasploit/framework/obfuscation/crandomizer'
module Metasploit module Metasploit
module Framework module Framework
@ -13,7 +14,7 @@ module Metasploit
# #
# @param c_template [String] The C source code to compile. # @param c_template [String] The C source code to compile.
# @param type [Symbol] PE type, either :exe or :dll # @param type [Symbol] PE type, either :exe or :dll
# @param cpu [Object] A Metasm cpu object, for example: Metasm::Ia32.new # @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new
# @raise [NotImplementedError] If the type is not supported. # @raise [NotImplementedError] If the type is not supported.
# @return [String] The compiled code. # @return [String] The compiled code.
def self.compile_c(c_template, type=:exe, cpu=Metasm::Ia32.new) def self.compile_c(c_template, type=:exe, cpu=Metasm::Ia32.new)
@ -36,12 +37,29 @@ module Metasploit
# @param out_file [String] The file path to save the binary as. # @param out_file [String] The file path to save the binary as.
# @param c_template [String] The C source code to compile. # @param c_template [String] The C source code to compile.
# @param type [Symbol] PE type, either :exe or :dll # @param type [Symbol] PE type, either :exe or :dll
# @param cpu [Object] A Metasm cpu object, for example: Metasm::Ia32.new # @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new
# @return [Integer] The number of bytes written. # @return [Integer] The number of bytes written.
def self.compile_c_to_file(out_file, c_template, type=:exe, cpu=Metasm::Ia32.new) def self.compile_c_to_file(out_file, c_template, type=:exe, cpu=Metasm::Ia32.new)
pe = self.compile(c_template, type) pe = self.compile(c_template, type)
File.write(out_file, pe) File.write(out_file, pe)
end end
# Returns the binary of a randomized and compiled source code.
#
# @param c_template [String]
#
# @raise [NotImplementedError] If the type is not supported.
# @return [String] The compiled code.
def self.compile_random_c(c_template, opts={})
type = opts[:type] || :exe
cpu = opts[:cpu] || Metasm::Ia32.new
fake_function_size = opts[:fake_function_size] || rand(0..3)
weight = opts[:random_weight] || 50
headers = Compiler::Headers::Windows.new
source_code = Compiler::Utils.normalize_code(c_template, headers)
randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)
randomizer.parse(source_code)
end
end end
end end

View File

@ -14,7 +14,7 @@ module Metasploit
# #
# @param weight [Integer] Randomness of the code. # @param weight [Integer] Randomness of the code.
# @param fake_functions [Metasploit::Framework::Obfuscation::CRandomizer::CodeFactory::FakeFunctionCollection] # @param fake_functions [Metasploit::Framework::Obfuscation::CRandomizer::CodeFactory::FakeFunctionCollection]
def initialize(weight, fake_functions) def initialize(weight, fake_functions=nil)
@max_random_weight = weight @max_random_weight = weight
@fake_functions_collection = fake_functions @fake_functions_collection = fake_functions
end end

View File

@ -22,7 +22,7 @@ module Metasploit
# Only generate fake function calls when the function we are modifying isn't # Only generate fake function calls when the function we are modifying isn't
# from one of those fake functions (to avoid a recursion). # from one of those fake functions (to avoid a recursion).
if s && !fake_function_collection.has_function_name?(s.var.name) if s && fake_function_collection && !fake_function_collection.has_function_name?(s.var.name)
@function_list << Proc.new { get_random_function_call } @function_list << Proc.new { get_random_function_call }
end end
end end

View File

@ -14,8 +14,6 @@ template = %Q|
void printf(const char*); void printf(const char*);
#{fake_function_collection}
void test() { void test() {
printf(MSG); printf(MSG);
} }
@ -24,6 +22,6 @@ int main() {
return 0; return 0;
}| }|
p = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(90, fake_function_collection) p = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(90)
result = p.parse(template) result = p.parse(template)
puts result puts result