-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjavascript-obfuscator.py
More file actions
executable file
·136 lines (114 loc) · 3.84 KB
/
javascript-obfuscator.py
File metadata and controls
executable file
·136 lines (114 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python2.7
import httplib, urllib, sys, re
def putFunctionsInFile(anArray, fw) :
if ( len(anArray) > 0 ):
for i in anArray:
fw.write( anArray[i] + '\n' )
fw.write('\n\n\n')
def putVariablesInFile(anArray, fw) :
if ( len(anArray) > 0 ):
begin = None
for i in anArray:
if begin is None:
fw.write( 'var ' + i + ' = ' + anArray[i] )
begin = 0
else:
fw.write( ',\n\t' + i + ' = ' + anArray[i] )
fw.write(';\n\n\n')
# Define the parameters for the POST request and encode them in
# a URL-safe format.
#params = urllib.urlencode([
# ('js_code', sys.argv[1]),
# ('compilation_level', 'WHITESPACE_ONLY'),
# ('output_format', 'text'),
# ('output_info', 'compiled_code'),
# ])
# Always use the following value for the Content-type header.
#headers = { "Content-type": "application/x-www-form-urlencoded" }
#conn = httplib.HTTPConnection('closure-compiler.appspot.com')
#conn.request('POST', '/compile', params, headers)
#response = conn.getresponse()
#data = response.read()
#print data
#conn.close()
properties = {}
refObjects = {}
functionProperties = {}
setFunctions = {}
getFunctions = {}
fr = open(sys.argv[1], 'r')
fw = open('workfile', 'w')
i = 0
for line in fr:
#if ( i > 3 ):
# break
#i = i + 1
# look for properties access
m = re.search('([a-zA-Z0-9_-]*)(\.[a-zA-Z0-9_-]*)+(\s*=\s*)([a-zA-Z0-9_-][^;]*);?(|//.*)', line)
if m is not None:
line = line.rstrip()
anObject = m.group(1).replace('.','')
aProperty = m.group(2).replace('.','')
equalSign = m.group(3)
aValue = m.group(4)
someComments = m.group(5)
#objKey = 'my' + aRefObject
#if not objKey in refObjects:
# refObjects[objKey] = '"' + aRefObject + '"'
newName = 'my' + aProperty.title()
if not newName in functionProperties:
# create the associated function
functionProperties [newName] = 'function ' + newName + '(anObj, aValue) { return anObj.' + aProperty + ' = aValue; }'
# toto.titi = 3
line = line.replace(anObject + '.' + aProperty, newName + '(' + anObject);
line = line.replace(equalSign, ','); #subtitue the equal by a comma
line = line.replace(aValue, aValue + ')');
fw.write( line + someComments + '\n' )
else:
# Loog for get function
m = re.search('([a-zA-Z0-9_-]*)(\.[a-zA-Z0-9_-]*)*\.([a-zA-Z0-9_-]*)\s*\(', line)
if m is not None:
mainObject = m.group(1)
if mainObject == '':
fw.write( line + '\n' )
else :
subObjects = m.group(2)
if subObjects is not None:
subObjects = subObjects.split('.')
#manage sub object reference
if not subObjects[1] in getFunctions:
getFunctions[subObjects[1]] = '"' + subObjects[1] + '"'
line = line.replace("." + subObjects[1], "[" + subObjects[1] + "]")
aFunction = m.group(3)
functionKey = 'my' + aFunction.title()
if not functionKey in getFunctions:
getFunctions[functionKey] = '"' + aFunction + '"'
line = line.replace('.' + aFunction, '[my' + aFunction + ']')
matchObj = re.match( '\s*loc[a-zA-Z0-9_-]*', mainObject)
# verification que c'est pas une variable locale
if matchObj is None:
# cache the object ref
objKey = 'my' + mainObject.title()
if not objKey in refObjects:
refObjects[objKey] = mainObject
line = line.replace(mainObject, objKey)
fw.write( line + '\n' )
#print line + '\n'
#print mainObject + '->///// ' + subObjects + '///// ' + aFunction + '\n'
else:
fw.write( line )
fr.closed
fw.closed
#exit(0)
fw = open('workfile.final', 'w')
#add get functions
putVariablesInFile(getFunctions, fw)
#add ref objects
putVariablesInFile(refObjects, fw)
#add properties functions
putFunctionsInFile(functionProperties, fw)
fr = open('workfile', 'r')
for line in fr:
fw.write( line )
fr.closed
fw.closed