add generate_random_c method
This commit is contained in:
parent
ec9ea4ce0d
commit
3c50f3d54e
|
@ -44,35 +44,47 @@ module Metasploit
|
||||||
File.write(out_file, pe)
|
File.write(out_file, pe)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the binary of a randomized and compiled source code.
|
# Returns randomized c source code.
|
||||||
#
|
#
|
||||||
# @param c_template [String]
|
# @param c_template [String]
|
||||||
#
|
#
|
||||||
# @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_random_c(c_template, opts={})
|
def self.generate_random_c(c_template, opts={})
|
||||||
type = opts[:type] || :exe
|
|
||||||
cpu = opts[:cpu] || Metasm::Ia32.new
|
|
||||||
weight = opts[:weight] || 80
|
weight = opts[:weight] || 80
|
||||||
headers = Compiler::Headers::Windows.new
|
headers = Compiler::Headers::Windows.new
|
||||||
source_code = Compiler::Utils.normalize_code(c_template, headers)
|
source_code = Compiler::Utils.normalize_code(c_template, headers)
|
||||||
|
|
||||||
randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)
|
randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)
|
||||||
randomized_code = randomizer.parse(source_code)
|
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
|
end
|
||||||
|
|
||||||
# Saves the randomized compiled code as a file. This is basically a wrapper for #self.compile_random_c
|
# 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 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
|
# @param opts [Hash] Options to pass to #compile_random_c
|
||||||
# @return [Integer] The number of bytes written.
|
# @return [Integer] The number of bytes written.
|
||||||
def self.compile_random_c_to_file(out_file, c_template, opts={})
|
def self.compile_random_c_to_file(out_file, rand_c_template, opts={})
|
||||||
pe = self.compile_random_c(c_template, opts)
|
pe = self.compile_random_c(rand_c_template, opts)
|
||||||
File.write(out_file, pe)
|
File.write(out_file, pe)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -72,7 +72,8 @@ int main() {
|
||||||
def run
|
def run
|
||||||
vprint_line c_template
|
vprint_line c_template
|
||||||
# The randomized code allows us to generate a unique EXE
|
# 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}")
|
print_status("Compiled executable size: #{bin.length}")
|
||||||
file_create(bin)
|
file_create(bin)
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,7 +33,8 @@ elsif out_path.nil? || out_path.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
source_code = File.read(source_code_path)
|
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)
|
if File.exists?(out_path)
|
||||||
puts "File saved as #{out_path}"
|
puts "File saved as #{out_path}"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue