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
67
# 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 !obj.nil? && !obj.is_a?(Stream)
    obj
  }.to_o
end

.junk_booleanObject



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

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

.junk_comment(max_size = 15) ⇒ Object



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

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

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

  "%#{junk_comment}#{$/}"
end

.junk_dictionary(max_size = 5) ⇒ Object



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

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

  hash = {}
  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



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

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

.junk_name(max_size = 8) ⇒ Object



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

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



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

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



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

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



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

def self.junk_stream(max_data_size = 200)
  chainlen = rand(1..2)
  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