Class: JSObfu

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

Overview

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

Defined Under Namespace

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Disable

included

Constructor Details

- (JSObfu) initialize(code)

Saves code for later obfuscation with #obfuscate

Parameters:

  • code (#to_s)

    the code to obfuscate



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

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

Instance Attribute Details

- (JSObfu::Scope) scope (readonly)

Returns the global scope

Returns:



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

def scope
  @scope
end

Instance Method Details

- (Object) <<(str)

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



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

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

- (RKelly::Nodes::SourceElementsNode) ast

Returns the abstract syntax tree

Returns:

  • (RKelly::Nodes::SourceElementsNode)

    the abstract syntax tree



39
40
41
# File 'lib/jsobfu.rb', line 39

def ast
  @ast || parse
end

- (self) obfuscate(opts = {})

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)

  • :global (String)

    the global object to rewrite unresolved lookups to. Depending on the environment, it may be `window`, `global`, or `this`.

Returns:

  • (self)


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
78
79
80
81
82
83
84
85
86
# File 'lib/jsobfu.rb', line 53

def obfuscate(opts={})
  return self if JSObfu.disabled?

  iterations = opts.fetch(:iterations, 1).to_i
  strip_whitespace = opts.fetch(:strip_whitespace, true)

  iterations.times do |i|
    obfuscator = JSObfu::Obfuscator.new(opts.merge(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

- (Object) parse (protected)

Generate an Abstract Syntax Tree (#ast) for later obfuscation



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

def parse
  @ast = RKelly::Parser.new.parse(@code)
end

- (String) sym(sym)

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



92
93
94
95
# File 'lib/jsobfu.rb', line 92

def sym(sym)
  return sym.to_s if @renames.nil?
  @renames[sym.to_s]
end

- (String) to_s

Returns the (possibly obfuscated) code

Returns:

  • (String)

    the (possibly obfuscated) code



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

def to_s
  @code
end