-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathobfuscate.js
291 lines (239 loc) · 7.32 KB
/
obfuscate.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Obfuscate.js
*
* Copyright 2013 Mikko Ohtamaa, http://opensourcehacker.com
*
* License: MIT
*/
(function() {
"use strict";
var charpool = "abcdefghijklmnopqrstuvwxyz";
var digitpool = "1234567890";
var symbolpool = "-.,;:_ |<>*+(){}";
var numericpresuffixpool = "%$¥";
// http://stackoverflow.com/a/3843677/315168
function isDigit(val) {
return String(+val).charAt(0) == val;
}
function isUpperCase(myString) {
return (myString == myString.toUpperCase());
}
function isLowerCase(myString) {
return (myString == myString.toLowerCase());
}
// Match some common symbols we do not want to alter
function isSymbol(char) {
return symbolpool.indexOf(char) >= 0;
}
// Match some common symbols we do not want to alter if they appear as a suffix or prefix for numbers
function isNumericPreSuffix(char) {
return numericpresuffixpool.indexOf(char) >= 0;
}
// Match numeric "words"
function isNumeric(word) {
if (isNumericPreSuffix(word.charAt(0))) {
word = word.substr(1);
}
if (isNumericPreSuffix(word.charAt(word.length-1))) {
word = word.substr(0, word.length-1);
}
// https://github.com/documentcloud/underscore/blob/master/underscore.js#L979
word = parseFloat(word);
return isFinite(word) && !isNaN(toString.call(word) == '[object Number]' && word != + word);
}
function isWhitespace(char) {
var pat1 = /\s/;
return pat1.test(char);
}
function getRandomArrayEntry(pool) {
return pool[Math.floor(Math.random()*pool.length)];
}
/**
* Replace the old character with and obfuscated characetr.
*
* Have some heurestics to make sure the new text resembles
* the old text somehow, but still breaking the content.
*/
function randomChar(old) {
if(isDigit(old)) {
return getRandomArrayEntry(digitpool);
}
// Pass through for . , etc.
if(isSymbol(old) || isWhitespace(old)) {
return old;
}
// Replace as a letter (incl. unicode)
var r = getRandomArrayEntry(charpool);
if(isUpperCase(old)) {
return r.toUpperCase();
} else {
return r;
}
}
function getReplacingNumber(word) {
var newWord = word.replace(/[^\d]/g, '');
//get word length
var len = newWord.length;
//add randomness to word length
len += Math.floor(Math.random()*4)-2;
len = Math.max(len, 1);
newWord = Math.floor(Math.random() * Math.pow(10,len));
//replace prefix/suffix
if (isNumericPreSuffix(word.charAt(0))) {
newWord = word.charAt(0) + newWord;
}
if (isNumericPreSuffix(word.charAt(word.length-1))) {
newWord += word.charAt(word.length-1);
}
return newWord;
}
function getReplacingWord(word) {
var len = word.length;
var newChars = [];
for(var i=0; i<len; i++) {
newChars[i] = randomChar(word[i]);
}
return newChars.join("");
}
function obfuscateText(node, text) {
var words = text.split(" ");
for(var i=0; i<words.length; i++) {
if (isNumeric(words[i])) {
words[i] = getReplacingNumber(words[i]);
} else {
words[i] = getReplacingWord(words[i]);
}
}
return words.join(" ");
}
/**
* Walk the DOM tree text nodes
*
* http://stackoverflow.com/a/5817243/315168
*
* @param node
*/
function walk(node) {
var child, next;
switch (node.nodeType) {
case 3: // Text node
var newText = obfuscateText(node, node.nodeValue);
node.nodeValue = newText;
break;
case 1: // Element node
case 9: // Document node
child = node.firstChild;
while (child) {
next = child.nextSibling;
walk(child);
child = next;
}
break;
}
}
/**
* Obfuscates a target selection.
*
* If no target is given, obfuscate all the text on the page.
*/
function obfuscate(target, stylesArray)
{
if(!target)
{
target = "body";
}
// If the user has specified one or more styles to apply
if(typeof(stylesArray)=="object")
{
// obtain the correct venbdor prefix for the browser we're executing in
var prefix=getVendorPrefix();
// if we have a prefix for this browser
if(prefix)
{
// create a test HTML element to verify with (this element will never be added to the DOM)
var testElement=document.createElement("div"), res=false;
// loop through the supplied user style(s)
for(var i=0; i<stylesArray.length; i++)
{
// try applying a non-prefixed style first of all
testElement.style[stylesArray[i][0]]=stylesArray[i][1];
// test whether the non-prefixed style applied successfully (it'll return the style) or not (it'll return nothing)
res=testElement.style[stylesArray[i][0]];
// if no prefix has been applied but we need one...
if(stylesArray[i][0].indexOf(prefix)!=0 && !res)
{
// ...add the prefix to the user supplied style
stylesArray[i][0]=prefix+ucfirst(stylesArray[i][0]);
}
}
}
}
else
{
stylesArray=false;
}
var elems = document.querySelectorAll(target);
for(var i=0; i<elems.length; i++)
{
if(stylesArray)
{
for(var j=0; j<stylesArray.length; j++)
{
elems[i].style[stylesArray[j][0]]=stylesArray[j][1];
}
}
else
{
walk(elems[i]);
}
}
}
/*
* getVendorPrefix() is taken and only very slightly adapted from http://lea.verou.me/2009/02/find-the-vendor-prefix-of-the-current-browser/ by Lea Verou
*
*/
function getVendorPrefix()
{
var regex = /^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/;
var someScript = document.getElementsByTagName('script')[0];
for(var prop in someScript.style)
{
if(regex.test(prop))
{
// test is faster than match, so it's better to perform
// that on the lot and match only when necessary
return prop.match(regex)[0].toLowerCase();
}
}
// Nothing found so far? Webkit does not enumerate over the CSS properties of the style object.
// However (prop in style) returns the correct value, so we'll have to test for
// the precence of a specific property
if('WebkitOpacity' in someScript.style)
return 'webkit';
if('KhtmlOpacity' in someScript.style)
return 'khtml';
return '';
}
/*
* ucfirst taken directly from http://phpjs.org/functions/ucfirst/ credits below
*/
function ucfirst(str)
{
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + improved by: Brett Zamir (http://brett-zamir.me)
// * example 1: ucfirst('kevin van zonneveld');
// * returns 1: 'Kevin van zonneveld'
str += '';
var f = str.charAt(0).toUpperCase();
return f + str.substr(1);
}
// export
if(window) {
window.obfuscate = obfuscate;
} else {
// Execute as bookmarklet
obfuscate();
}
})();