Module: Origami::Obfuscator

Defined in:
lib/origami/obfuscation.rb

Constant Summary collapse

WHITECHARS =
[ " ", "\t", "\r", "\n", "\0" ]
OBJECTS =
[ Array, Boolean, Dictionary, Integer, Name, Null, Stream, String, Real, Reference ]
MAX_INT =
0xFFFFFFFF
PRINTABLE =
("!".."9").to_a + (':'..'Z').to_a + ('['..'z').to_a + ('{'..'~').to_a
FILTERS =
[ :FlateDecode, :RunLengthDecode, :LZWDecode, :ASCIIHexDecode, :ASCII85Decode ]

Class Method Summary collapse

Class Method Details

.junk_array(max_size = 5) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/origami/obfuscation.rb', line 60

def self.junk_array(max_size = 5)
    length = rand(max_size) + 1

    ::Array.new(length) {
        obj = Obfuscator.junk_object until (not obj.nil? and not obj.is_a?(Stream)) ; obj
    }.to_o
end

.junk_booleanObject



68
69
70
# File 'lib/origami/obfuscation.rb', line 68

def self.junk_boolean
    Boolean.new(rand(2).zero?)
end

.junk_comment(max_size = 15) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/origami/obfuscation.rb', line 38

def self.junk_comment(max_size = 15)
    length = rand(max_size) + 1

    junk_comment = ::Array.new(length) {
        byte = rand(256).chr until (not byte.nil? and byte != "\n" and byte != "\r"); byte
    }.join

    "%#{junk_comment}#{EOL}"
end

.junk_dictionary(max_size = 5) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/origami/obfuscation.rb', line 72

def self.junk_dictionary(max_size = 5)
    length = rand(max_size) + 1

    hash = Hash.new
    length.times do
        obj = Obfuscator.junk_object
        hash[Obfuscator.junk_name] = obj unless obj.is_a?(Stream)
    end

    hash.to_o
end

.junk_integer(max = MAX_INT) ⇒ Object



84
85
86
# File 'lib/origami/obfuscation.rb', line 84

def self.junk_integer(max = MAX_INT)
    Integer.new(rand(max + 1))
end

.junk_name(max_size = 8) ⇒ Object



88
89
90
91
92
# File 'lib/origami/obfuscation.rb', line 88

def self.junk_name(max_size = 8)
    length = rand(max_size) + 1

    Name.new(::Array.new(length) { PRINTABLE[rand(PRINTABLE.size)] }.join)
end

.junk_nullObject



94
95
96
# File 'lib/origami/obfuscation.rb', line 94

def self.junk_null
    Null.new
end

.junk_object(type = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/origami/obfuscation.rb', line 48

def self.junk_object(type = nil)
    if type.nil?
        type = OBJECTS[rand(OBJECTS.size)]
    end

    unless type.include?(Origami::Object)
        raise TypeError, "Not a valid object type"
    end

    Obfuscator.send("junk_#{type.to_s.split('::').last.downcase}")
end

.junk_realObject



122
123
124
# File 'lib/origami/obfuscation.rb', line 122

def self.junk_real
    Real.new(rand * rand(MAX_INT + 1))
end

.junk_reference(max_no = 300, max_gen = 1) ⇒ Object



126
127
128
129
130
131
# File 'lib/origami/obfuscation.rb', line 126

def self.junk_reference(max_no = 300, max_gen = 1)
    no = rand(max_no) + 1
    gen = rand(max_gen)

    Reference.new(no, gen)
end

.junk_spaces(max_size = 3) ⇒ Object



32
33
34
35
36
# File 'lib/origami/obfuscation.rb', line 32

def self.junk_spaces(max_size = 3)
    length = rand(max_size) + 1

    ::Array.new(length) { WHITECHARS[rand(WHITECHARS.size)] }.join
end

.junk_stream(max_data_size = 200) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/origami/obfuscation.rb', line 98

def self.junk_stream(max_data_size = 200)

    chainlen = rand(2) + 1
    chain = ::Array.new(chainlen) { FILTERS[rand(FILTERS.size)] }

    length = rand(max_data_size) + 1
    junk_data = ::Array.new(length) { rand(256).chr }.join

    stm = Stream.new
    stm.dictionary = Obfuscator.junk_dictionary(5)
    stm.setFilter(chain)
    stm.data = junk_data

    stm
end

.junk_string(max_size = 10) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/origami/obfuscation.rb', line 114

def self.junk_string(max_size = 10)
    length = rand(max_size) + 1

    strtype = (rand(2).zero?) ? LiteralString : HexaString

    strtype.new(::Array.new(length) { PRINTABLE[rand(PRINTABLE.size)] }.join)
end