CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/lib/metasploit/framework/compiler/windows.rb
Views: 1904
require 'metasm'1require 'erb'2require 'metasploit/framework/compiler/utils'3require 'metasploit/framework/compiler/headers/windows'4require 'metasploit/framework/obfuscation/crandomizer'56module Metasploit7module Framework8module Compiler910class Windows1112# Returns the binary of a compiled source.13#14# @param c_template [String] The C source code to compile.15# @param type [Symbol] PE type, either :exe or :dll16# @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new17# @raise [NotImplementedError] If the type is not supported.18# @return [String] The compiled code.19def self.compile_c(c_template, type=:exe, cpu=Metasm::Ia32.new)20headers = Compiler::Headers::Windows.new21source_code = Compiler::Utils.normalize_code(c_template, headers)22pe = Metasm::PE.compile_c(cpu, source_code)2324case type25when :exe26pe.encode27when :dll28pe.encode('dll')29else30raise NotImplementedError31end32end3334# Saves the compiled code as a file. This is basically a wrapper of #self.compile.35#36# @param out_file [String] The file path to save the binary as.37# @param c_template [String] The C source code to compile.38# @param type [Symbol] PE type, either :exe or :dll39# @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new40# @return [Integer] The number of bytes written.41def self.compile_c_to_file(out_file, c_template, type=:exe, cpu=Metasm::Ia32.new)42pe = self.compile_c(c_template, type)43File.write(out_file, pe, mode: 'wb')44end4546# Returns randomized c source code.47#48# @param c_template [String]49#50# @raise [NotImplementedError] If the type is not supported.51# @return [String] The compiled code.52def self.generate_random_c(c_template, opts={})53weight = opts[:weight] || 8054headers = Compiler::Headers::Windows.new55source_code = Compiler::Utils.normalize_code(c_template, headers)5657randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)58randomized_code = randomizer.parse(source_code)59randomized_code.to_s60end6162# Returns the binary of a randomized and compiled source code.63#64# @param c_template [String]65#66# @raise [NotImplementedError] If the type is not supported.67# @return [String] The compiled code.68def self.compile_random_c(c_template, opts={})69type = opts[:type] || :exe70cpu = opts[:cpu] || Metasm::Ia32.new7172random_c = self.generate_random_c(c_template, opts)73self.compile_c(random_c, type, cpu)74end7576# Saves the randomized compiled code as a file. This is basically a wrapper for #self.compile_random_c77#78# @param out_file [String] The file path to save the binary as.79# @param c_template [String] The randomized C source code to compile.80# @param opts [Hash] Options to pass to #compile_random_c81# @return [Integer] The number of bytes written.82def self.compile_random_c_to_file(out_file, c_template, opts={})83pe = self.compile_random_c(c_template, opts)84File.write(out_file, pe, mode: 'wb')85end86end8788end89end90end919293