CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/metasploit/framework/obfuscation/crandomizer/parser.rb
Views: 11789
1
require 'metasploit/framework/obfuscation/crandomizer/utility'
2
require 'metasploit/framework/obfuscation/crandomizer/modifier'
3
4
module Metasploit
5
module Framework
6
module Obfuscation
7
module CRandomizer
8
9
class Parser
10
attr_accessor :max_random_weight
11
attr_accessor :fake_functions_collection
12
13
# Initializes a Metasploit::Framework::Obfuscation::CRandomizer::Parser instance.
14
#
15
# @param weight [Integer] Randomness of the code.
16
# @param fake_functions [Metasploit::Framework::Obfuscation::CRandomizer::CodeFactory::FakeFunctionCollection]
17
def initialize(weight, fake_functions=nil)
18
@max_random_weight = weight
19
@fake_functions_collection = fake_functions
20
end
21
22
# Returns a parser.
23
#
24
# @param template [String] Source code to parse.
25
# @return [Metasm::C::Parser]
26
def parse(template)
27
main_parser = Metasploit::Framework::Obfuscation::CRandomizer::Utility.parse(template)
28
modifier = Metasploit::Framework::Obfuscation::CRandomizer::Modifier.new(main_parser, fake_functions_collection, max_random_weight)
29
main_parser.toplevel.statements.each do |s|
30
case s.var.type
31
when Metasm::C::Function
32
# Some function objects such as declarations don't really have
33
# any statements, if we run into something like that, skip it.
34
next unless s.var.initializer.respond_to?(:statements)
35
modifier.modify_function(s)
36
end
37
end
38
39
main_parser
40
end
41
end
42
43
end
44
end
45
end
46
end
47
48