Class: Metasploit::Framework::Compiler::Windows

Inherits:
Object
  • Object
show all
Defined in:
lib/metasploit/framework/compiler/windows.rb

Class Method Summary collapse

Class Method Details

.compile_c(c_template, type = :exe, cpu = Metasm::Ia32.new) ⇒ String

Returns the binary of a compiled source.

Parameters:

  • c_template (String)

    The C source code to compile.

  • type (Symbol) (defaults to: :exe)

    PE type, either :exe or :dll

  • cpu (Metasm::CPU) (defaults to: Metasm::Ia32.new)

    A Metasm cpu object, for example: Metasm::Ia32.new

Returns:

  • (String)

    The compiled code.

Raises:

  • (NotImplementedError)

    If the type is not supported.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/metasploit/framework/compiler/windows.rb', line 20

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

.compile_c_to_file(out_file, c_template, type = :exe, cpu = Metasm::Ia32.new) ⇒ Integer

Saves the compiled code as a file. This is basically a wrapper of #self.compile.

Parameters:

  • out_file (String)

    The file path to save the binary as.

  • c_template (String)

    The C source code to compile.

  • type (Symbol) (defaults to: :exe)

    PE type, either :exe or :dll

  • cpu (Metasm::CPU) (defaults to: Metasm::Ia32.new)

    A Metasm cpu object, for example: Metasm::Ia32.new

Returns:

  • (Integer)

    The number of bytes written.



42
43
44
45
# File 'lib/metasploit/framework/compiler/windows.rb', line 42

def self.compile_c_to_file(out_file, c_template, type=:exe, cpu=Metasm::Ia32.new)
  pe = self.compile_c(c_template, type)
  File.write(out_file, pe, mode: 'wb')
end

.compile_random_c(c_template, opts = {}) ⇒ String

Returns the binary of a randomized and compiled source code.

Parameters:

  • c_template (String)

Returns:

  • (String)

    The compiled code.

Raises:

  • (NotImplementedError)

    If the type is not supported.



69
70
71
72
73
74
75
# File 'lib/metasploit/framework/compiler/windows.rb', line 69

def self.compile_random_c(c_template, opts={})
  type = opts[:type] || :exe
  cpu = opts[:cpu] || Metasm::Ia32.new

  random_c = self.generate_random_c(c_template, opts)
  self.compile_c(random_c, type, cpu)
end

.compile_random_c_to_file(out_file, c_template, opts = {}) ⇒ Integer

Saves the randomized compiled code as a file. This is basically a wrapper for #self.compile_random_c

Parameters:

  • out_file (String)

    The file path to save the binary as.

  • c_template (String)

    The randomized C source code to compile.

  • opts (Hash) (defaults to: {})

    Options to pass to #compile_random_c

Returns:

  • (Integer)

    The number of bytes written.



83
84
85
86
# File 'lib/metasploit/framework/compiler/windows.rb', line 83

def self.compile_random_c_to_file(out_file, c_template, opts={})
  pe = self.compile_random_c(c_template, opts)
  File.write(out_file, pe, mode: 'wb')
end

.generate_random_c(c_template, opts = {}) ⇒ String

Returns randomized c source code.

Parameters:

  • c_template (String)

Returns:

  • (String)

    The compiled code.

Raises:

  • (NotImplementedError)

    If the type is not supported.



53
54
55
56
57
58
59
60
61
# File 'lib/metasploit/framework/compiler/windows.rb', line 53

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)
  randomized_code.to_s
end