Class: JSObfu

Inherits:
Object
  • Object
show all
Defined in:
lib/jsobfu.rb

Overview

The primary class, used to parse and obfuscate Javascript code.

Defined Under Namespace

Modules: Utils Classes: ECMANoWhitespaceVisitor, Hoister, Obfuscator, Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ JSObfu

Saves code for later obfuscation with #obfuscate

Parameters:

  • code (#to_s)

    the code to obfuscate



17
18
19
20
# File 'lib/jsobfu.rb', line 17

def initialize(code)
  @code = code.to_s
  @scope = Scope.new
end

Instance Attribute Details

#scopeJSObfu::Scope (readonly)

Returns the global scope.

Returns:



13
14
15
# File 'lib/jsobfu.rb', line 13

def scope
  @scope
end

Instance Method Details

#<<(str) ⇒ Object

Add str to the un-obfuscated code. Calling this method after #obfuscate is undefined



24
25
26
# File 'lib/jsobfu.rb', line 24

def <<(str)
  @code << str
end

#astRKelly::Nodes::SourceElementsNode

Returns the abstract syntax tree.

Returns:

  • (RKelly::Nodes::SourceElementsNode)

    the abstract syntax tree



34
35
36
# File 'lib/jsobfu.rb', line 34

def ast
  @ast || parse
end

#obfuscate(opts = {}) ⇒ String

Parse and obfuscate

Parameters:

  • opts (Hash) (defaults to: {})

    the options hash

Options Hash (opts):

  • :strip_whitespace (Boolean)

    removes unnecessary whitespace from the output code (true)

  • :iterations (Integer)

    number of times to run the obfuscator on this code (1)

Returns:

  • (String)

    if successful



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jsobfu.rb', line 46

def obfuscate(opts={})
  iterations = opts.fetch(:iterations, 1).to_i
  strip_whitespace = opts.fetch(:strip_whitespace, true)

  iterations.times do |i|
    obfuscator = JSObfu::Obfuscator.new(scope: @scope)
    @code = obfuscator.accept(ast).to_s
    if strip_whitespace
      @code.gsub!(/(^\s+|\s+$)/, '')
      @code.delete!("\n")
      @code.delete!("\r")
    end

    new_renames = obfuscator.renames.dup
    if @renames
      # "patch up" the renames after each iteration
      @renames.each do |key, prev_rename|
        @renames[key] = new_renames[prev_rename]
      end
    else
      # on first iteration, take the renames as-is
      @renames = new_renames
    end

    unless i == iterations-1
      @scope = Scope.new
      @ast = nil # force a re-parse
    end
  end

  self
end

#sym(sym) ⇒ String

Returns the obfuscated name for the variable or function sym

Parameters:

  • sym (String)

    the name of the variable or function

Returns:

  • (String)

    the obfuscated name

Raises:

  • (RuntimeError)


83
84
85
86
# File 'lib/jsobfu.rb', line 83

def sym(sym)
  raise RuntimeError, "Must obfuscate before calling #sym" if @renames.nil?
  @renames[sym.to_s]
end

#to_sString

Returns the (possibly obfuscated) code.

Returns:

  • (String)

    the (possibly obfuscated) code



29
30
31
# File 'lib/jsobfu.rb', line 29

def to_s
  @code
end