Class: JSObfu

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

Overview

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.4.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Disable

included

Constructor Details

#initialize(code = nil, opts = {}) ⇒ JSObfu

Saves code for later obfuscation with #obfuscate

Parameters:

  • code (#to_s) (defaults to: nil)

    the code to obfuscate

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

    an options hash

Options Hash (opts):

  • a (JSObfu::Scope)

    pre-existing scope. This is useful for preserving variable rename maps between separate obfuscations of different scripts.



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

def initialize(code=nil, opts={})
  self.code = code
  @scope = opts.fetch(:scope) { Scope.new }
end

Instance Attribute Details

#scopeJSObfu::Scope (readonly)

Returns the global scope.

Returns:



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

def scope
  @scope
end

Instance Method Details

#<<(str) ⇒ Object

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



32
33
34
# File 'lib/jsobfu.rb', line 32

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

#astRKelly::Nodes::SourceElementsNode

Returns the abstract syntax tree.

Returns:

  • (RKelly::Nodes::SourceElementsNode)

    the abstract syntax tree



42
43
44
# File 'lib/jsobfu.rb', line 42

def ast
  @ast ||= parse
end

#code=(code) ⇒ Object

Sets the code that this obfuscator will transform

Parameters:

  • code (String)


48
49
50
51
# File 'lib/jsobfu.rb', line 48

def code=(code)
  @ast = nil # invalidate any previous parses
  @code = code
end

#obfuscate(opts = {}) ⇒ self

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`.

  • :memory_sensitive (Boolean)

    the execution environment is sensitive to changes in memory usage (e.g. a heap spray). This disables string transformations and other “noisy” obfuscation tactics. (false)

  • :preserved_identifiers (Array<String>)

    A list of identifiers to NOT obfuscate

Returns:

  • (self)

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jsobfu.rb', line 67

def obfuscate(opts={})
  return self if JSObfu.disabled?
  raise ArgumentError.new("code must be present") if @code.nil?

  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

    if @renames
      # "patch up" the renames after each iteration
      @renames.merge! (obfuscator.renames)
    else
      # on first iteration, take the renames as-is
      @renames = obfuscator.renames.dup
    end

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

  # Enter all of the renames into current scope
  @scope.renames.merge!(@renames || {})

  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



107
108
109
110
# File 'lib/jsobfu.rb', line 107

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

#to_sString

Returns the (possibly obfuscated) code.

Returns:

  • (String)

    the (possibly obfuscated) code



37
38
39
# File 'lib/jsobfu.rb', line 37

def to_s
  @code
end