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



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

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



48
49
50
# File 'lib/origami/obfuscation.rb', line 48

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

.junk_comment(max_size = 15) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/origami/obfuscation.rb', line 17

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/origami/obfuscation.rb', line 52

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



64
65
66
# File 'lib/origami/obfuscation.rb', line 64

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

.junk_name(max_size = 8) ⇒ Object



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

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



74
75
76
# File 'lib/origami/obfuscation.rb', line 74

def self.junk_null
  Null.new
end

.junk_object(type = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/origami/obfuscation.rb', line 27

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



102
103
104
# File 'lib/origami/obfuscation.rb', line 102

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

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



106
107
108
109
110
111
# File 'lib/origami/obfuscation.rb', line 106

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



11
12
13
14
15
# File 'lib/origami/obfuscation.rb', line 11

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/origami/obfuscation.rb', line 78

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



94
95
96
97
98
99
100
# File 'lib/origami/obfuscation.rb', line 94

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

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

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