add generate_random_c method

This commit is contained in:
Shelby Pace 2019-10-15 12:50:58 -05:00
parent ec9ea4ce0d
commit 3c50f3d54e
No known key found for this signature in database
GPG Key ID: B2F3A8B476406857
3 changed files with 26 additions and 12 deletions

View File

@ -44,35 +44,47 @@ module Metasploit
File.write(out_file, pe)
end
# Returns the binary of a randomized and compiled source code.
# Returns randomized c 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
def self.generate_random_c(c_template, opts={})
weight = opts[:weight] || 80
headers = Compiler::Headers::Windows.new
source_code = Compiler::Utils.normalize_code(c_template, headers)
randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)
randomized_code = randomizer.parse(source_code)
self.compile_c(randomized_code.to_s, type, cpu)
randomized_code.to_s
end
# Returns the binary of a randomized and compiled source code.
#
# @param rand_c_template [String]
#
# @raise [NotImplementedError] If the type is not supported.
# @return [String] The compiled code.
def self.compile_random_c(rand_c_template, opts={})
type = opts[:type] || :exe
cpu = opts[:cpu] || Metasm::Ia32.new
self.compile_c(rand_c_template, type, cpu)
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.
# @param c_template [String] The C source code to randomize and compile.
# @param rand_c_template [String] The randomized C source code to compile.
# @param opts [Hash] Options to pass to #compile_random_c
# @return [Integer] The number of bytes written.
def self.compile_random_c_to_file(out_file, c_template, opts={})
pe = self.compile_random_c(c_template, opts)
def self.compile_random_c_to_file(out_file, rand_c_template, opts={})
pe = self.compile_random_c(rand_c_template, opts)
File.write(out_file, pe)
end
end
end
end
end
end

View File

@ -72,7 +72,8 @@ int main() {
def run
vprint_line c_template
# The randomized code allows us to generate a unique EXE
bin = Metasploit::Framework::Compiler::Windows.compile_random_c(c_template)
random_c = Metasploit::Framework::Compiler::Windows.generate_random_c(c_template)
bin = Metasploit::Framework::Compiler::Windows.compile_random_c(random_c)
print_status("Compiled executable size: #{bin.length}")
file_create(bin)
end

View File

@ -33,7 +33,8 @@ elsif out_path.nil? || out_path.empty?
end
source_code = File.read(source_code_path)
Metasploit::Framework::Compiler::Windows.compile_random_c_to_file(out_path, source_code, weight: weight.to_i)
rand_c_src = Metasploit::Framework::Compiler::Windows.generate_random_c(source_code, weight: weight.to_i)
Metasploit::Framework::Compiler::Windows.compile_random_c_to_file(out_path, rand_c_src)
if File.exists?(out_path)
puts "File saved as #{out_path}"
end
end