CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/metasploit/framework/compiler/windows.rb
Views: 1904
1
require 'metasm'
2
require 'erb'
3
require 'metasploit/framework/compiler/utils'
4
require 'metasploit/framework/compiler/headers/windows'
5
require 'metasploit/framework/obfuscation/crandomizer'
6
7
module Metasploit
8
module Framework
9
module Compiler
10
11
class Windows
12
13
# Returns the binary of a compiled source.
14
#
15
# @param c_template [String] The C source code to compile.
16
# @param type [Symbol] PE type, either :exe or :dll
17
# @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new
18
# @raise [NotImplementedError] If the type is not supported.
19
# @return [String] The compiled code.
20
def self.compile_c(c_template, type=:exe, cpu=Metasm::Ia32.new)
21
headers = Compiler::Headers::Windows.new
22
source_code = Compiler::Utils.normalize_code(c_template, headers)
23
pe = Metasm::PE.compile_c(cpu, source_code)
24
25
case type
26
when :exe
27
pe.encode
28
when :dll
29
pe.encode('dll')
30
else
31
raise NotImplementedError
32
end
33
end
34
35
# Saves the compiled code as a file. This is basically a wrapper of #self.compile.
36
#
37
# @param out_file [String] The file path to save the binary as.
38
# @param c_template [String] The C source code to compile.
39
# @param type [Symbol] PE type, either :exe or :dll
40
# @param cpu [Metasm::CPU] A Metasm cpu object, for example: Metasm::Ia32.new
41
# @return [Integer] The number of bytes written.
42
def self.compile_c_to_file(out_file, c_template, type=:exe, cpu=Metasm::Ia32.new)
43
pe = self.compile_c(c_template, type)
44
File.write(out_file, pe, mode: 'wb')
45
end
46
47
# Returns randomized c source code.
48
#
49
# @param c_template [String]
50
#
51
# @raise [NotImplementedError] If the type is not supported.
52
# @return [String] The compiled code.
53
def self.generate_random_c(c_template, opts={})
54
weight = opts[:weight] || 80
55
headers = Compiler::Headers::Windows.new
56
source_code = Compiler::Utils.normalize_code(c_template, headers)
57
58
randomizer = Metasploit::Framework::Obfuscation::CRandomizer::Parser.new(weight)
59
randomized_code = randomizer.parse(source_code)
60
randomized_code.to_s
61
end
62
63
# Returns the binary of a randomized and compiled source code.
64
#
65
# @param c_template [String]
66
#
67
# @raise [NotImplementedError] If the type is not supported.
68
# @return [String] The compiled code.
69
def self.compile_random_c(c_template, opts={})
70
type = opts[:type] || :exe
71
cpu = opts[:cpu] || Metasm::Ia32.new
72
73
random_c = self.generate_random_c(c_template, opts)
74
self.compile_c(random_c, type, cpu)
75
end
76
77
# Saves the randomized compiled code as a file. This is basically a wrapper for #self.compile_random_c
78
#
79
# @param out_file [String] The file path to save the binary as.
80
# @param c_template [String] The randomized C source code to compile.
81
# @param opts [Hash] Options to pass to #compile_random_c
82
# @return [Integer] The number of bytes written.
83
def self.compile_random_c_to_file(out_file, c_template, opts={})
84
pe = self.compile_random_c(c_template, opts)
85
File.write(out_file, pe, mode: 'wb')
86
end
87
end
88
89
end
90
end
91
end
92
93