10000 Improvements of `stringArrayEncoding`: `base64` and `rc4` · sec-js/javascript-obfuscator@7eb25bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 7eb25bb

Browse files
author
sanex3339
committed
Improvements of stringArrayEncoding: base64 and rc4
1 parent e625757 commit 7eb25bb

File tree

16 files changed

+85
-53
lines changed

16 files changed

+85
-53
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22

3+
v1.3.0
4+
---
5+
* Improvements of `stringArrayEncoding`: `base64` and `rc4`
6+
37
v1.2.2
48
---
59
* Fixed performance regression of `Initializing` stage after `1.2.0`

dist/index.browser.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javascript-obfuscator",
3-
"version": "1.2.2",
3+
"version": "1.3.0",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",

src/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEn
1010
import { IOptions } from '../../interfaces/options/IOptions';
1111
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
1212

13-
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
1413
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
1514

1615
import { initializable } from '../../decorators/Initializable';
1716

1817
import { AtobTemplate } from './templates/string-array-calls-wrapper/AtobTemplate';
19-
import { GlobalVariableNoEvalTemplate } from '../common/templates/GlobalVariableNoEvalTemplate';
2018
import { Rc4Template } from './templates/string-array-calls-wrapper/Rc4Template';
2119
import { SelfDefendingTemplate } from './templates/string-array-calls-wrapper/SelfDefendingTemplate';
2220
import { StringArrayBase64DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate';
@@ -28,6 +26,12 @@ import { NodeUtils } from '../../node/NodeUtils';
2826

2927
@injectable()
3028
export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper {
29+
/**
30+
* @type {string}
31+
*/
32+
@initializable()
33+
private atobFunctionName!: string;
34+
3135
/**
3236
* @type {string}
3337
*/
@@ -76,13 +80,16 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
7680
/**
7781
* @param {string} stringArrayName
7882
* @param {string} stringArrayCallsWrapperName
83+
* @param {string} atobFunctionName
7984
*/
8085
public initialize (
8186
stringArrayName: string,
82-
stringArrayCallsWrapperName: string
87+
stringArrayCallsWrapperName: string,
88+
atobFunctionName: string
8389
): void {
8490
this.stringArrayName = stringArrayName;
8591
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
92+
this.atobFunctionName = atobFunctionName;
8693
}
8794

8895
/**
@@ -117,10 +124,12 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
117124
* @returns {string}
118125
*/
119126
private getDecodeStringArrayTemplate (): string {
120-
const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
121-
? this.getGlobalVariableTemplate()
122-
: GlobalVariableNoEvalTemplate();
123-
const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), { globalVariableTemplate });
127+
const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), {
128+
atobFunctionName: this.atobFunctionName
129+
});
130+
const rc4Polyfill: string = this.customCodeHelperFormatter.formatTemplate(Rc4Template(), {
131+
atobFunctionName: this.atobFunctionName
132+
});
124133

125134
let decodeStringArrayTemplate: string = '';
126135
let selfDefendingCode: string = '';
@@ -144,8 +153,8 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
144153
StringArrayRC4DecodeTemplate(this.randomGenerator),
145154
{
146155
atobPolyfill,
156+
rc4Polyfill,
147157
selfDefendingCode,
148-
rc4Polyfill: Rc4Template(),
149158
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
150159
}
151160
);
@@ -157,6 +166,7 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
157166
StringArrayBase64DecodeTemplate(this.randomGenerator),
158167
{
159168
atobPolyfill,
169+
atobFunctionName: this.atobFunctionName,
160170
selfDefendingCode,
161171
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
162172
}

src/custom-code-helpers/string-array/group/StringArrayCodeHelperGroup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
118118
const stringArrayName: string = this.stringArrayStorage.getStorageName();
119119
const stringArrayCallsWrapperName: string = this.stringArrayStorage.getStorageCallsWrapperName();
120120
const stringArrayRotationAmount: number = this.stringArrayStorage.getRotationAmount();
121+
const atobFunctionName: string = this.randomGenerator.getRandomString(6);
121122

122123
stringArrayCodeHelper.initialize(this.stringArrayStorage, stringArrayName);
123-
stringArrayCallsWrapperCodeHelper.initialize(stringArrayName, stringArrayCallsWrapperName);
124+
stringArrayCallsWrapperCodeHelper.initialize(stringArrayName, stringArrayCallsWrapperName, atobFunctionName);
124125
stringArrayRotateFunctionCodeHelper.initialize(stringArrayName, stringArrayRotationAmount);
125126

126127
this.customCodeHelpers.set(CustomCodeHelper.StringArray, stringArrayCodeHelper);

src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/AtobTemplate.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ import { numbersString } from '../../../../constants/NumbersString';
66
* @returns {string}
77
*/
88
export function AtobTemplate (): string {
9+
// swapped lowercase and uppercase groups of alphabet to prevent easy decode!!!!
910
return `
10-
(function () {
11-
{globalVariableTemplate}
12-
13-
const chars = '${alphabetStringUppercase}${alphabetString}${numbersString}+/=';
11+
var {atobFunctionName} = function (input) {
12+
const chars = '${alphabetString}${alphabetStringUppercase}${numbersString}+/=';
1413
15-
that.atob || (
16-
that.atob = function(input) {
17-
const str = String(input).replace(/=+$/, '');
18-
let output = '';
19-
for (
20-
let bc = 0, bs, buffer, idx = 0;
21-
buffer = str.charAt(idx++);
22-
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
23-
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
24-
) {
25-
buffer = chars.indexOf(buffer);
26-
}
27-
return output;
28-
}
29-
);
30-
})();
14+
const str = String(input).replace(/=+$/, '');
15+
let output = '';
16+
for (
17+
let bc = 0, bs, buffer, idx = 0;
18+
buffer = str.charAt(idx++);
19+
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
20+
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
21+
) {
22+
buffer = chars.indexOf(buffer);
23+
}
24+
return output;
25+
};
3126
`;
3227
}

src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/Rc4Template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function Rc4Template (): string {
66
const rc4 = function (str, key) {
77
let s = [], j = 0, x, res = '', newStr = '';
88
9-
str = atob(str);
9+
str = {atobFunctionName}(str);
1010
1111
for (let k = 0, length = str.length; k < length; k++) {
1212
newStr += '%' + ('00' + str.charCodeAt(k).toString(16)).slice(-2);

src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function StringArrayBase64DecodeTemplate (
1818
{atobPolyfill}
1919
2020
{stringArrayCallsWrapperName}.${base64DecodeFunctionIdentifier} = function (str) {
21-
const string = atob(str);
21+
const string = {atobFunctionName}(str);
2222
let newStringChars = [];
2323
2424
for (let i = 0, length = string.length; i < length; i++) {

0 commit comments

Comments
 (0)
0