JavaScriptObfuscatorCLI.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* eslint-disable max-lines */
  2. import * as commander from 'commander';
  3. import * as path from 'path';
  4. import { TInputCLIOptions } from '../types/options/TInputCLIOptions';
  5. import { TInputOptions } from '../types/options/TInputOptions';
  6. import { IFileData } from '../interfaces/cli/IFileData';
  7. import { IInitializable } from '../interfaces/IInitializable';
  8. import { IObfuscatedCode } from '../interfaces/source-code/IObfuscatedCode';
  9. import { initializable } from '../decorators/Initializable';
  10. import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  11. import { LoggingPrefix } from '../enums/logger/LoggingPrefix';
  12. import { ObfuscationTarget } from '../enums/ObfuscationTarget';
  13. import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
  14. import { RenamePropertiesMode } from '../enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
  15. import { SourceMapMode } from '../enums/source-map/SourceMapMode';
  16. import { StringArrayEncoding } from '../enums/node-transformers/string-array-transformers/StringArrayEncoding';
  17. import { StringArrayIndexesType } from '../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
  18. import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
  19. import { DEFAULT_PRESET } from '../options/presets/Default';
  20. import { ArraySanitizer } from './sanitizers/ArraySanitizer';
  21. import { BooleanSanitizer } from './sanitizers/BooleanSanitizer';
  22. import { CLIUtils } from './utils/CLIUtils';
  23. import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
  24. import { Logger } from '../logger/Logger';
  25. import { ObfuscatedCodeWriter } from './utils/ObfuscatedCodeWriter';
  26. import { SourceCodeReader } from './utils/SourceCodeReader';
  27. import { Utils } from '../utils/Utils';
  28. export class JavaScriptObfuscatorCLI implements IInitializable {
  29. /**
  30. * @type {string[]}
  31. */
  32. public static readonly availableInputExtensions: string[] = [
  33. '.js'
  34. ];
  35. /**
  36. * @type {BufferEncoding}
  37. */
  38. public static readonly encoding: BufferEncoding = 'utf8';
  39. /**
  40. * @type {string}
  41. */
  42. public static readonly obfuscatedFilePrefix: string = '-obfuscated';
  43. /**
  44. * @type {commander.CommanderStatic}
  45. */
  46. @initializable()
  47. private commands!: commander.CommanderStatic;
  48. /**
  49. * @type {TInputCLIOptions}
  50. */
  51. @initializable()
  52. private inputCLIOptions!: TInputCLIOptions;
  53. /**
  54. * @type {string}
  55. */
  56. @initializable()
  57. private inputPath!: string;
  58. /**
  59. * @type {SourceCodeReader}
  60. */
  61. @initializable()
  62. private sourceCodeReader!: SourceCodeReader;
  63. /**
  64. * @type {ObfuscatedCodeWriter}
  65. */
  66. @initializable()
  67. private obfuscatedCodeWriter!: ObfuscatedCodeWriter;
  68. /**
  69. * @type {string[]}
  70. */
  71. private readonly arguments: string[];
  72. /**
  73. * @type {string[]}
  74. */
  75. private readonly rawArguments: string[];
  76. /**
  77. * @param {string[]} argv
  78. */
  79. public constructor (argv: string[]) {
  80. this.rawArguments = argv;
  81. this.arguments = argv.slice(2);
  82. }
  83. /**
  84. * @param {TInputCLIOptions} inputOptions
  85. * @returns {TInputOptions}
  86. */
  87. private static buildOptions (inputOptions: TInputCLIOptions): TInputOptions {
  88. const inputCLIOptions: TInputOptions = JavaScriptObfuscatorCLI.filterOptions(inputOptions);
  89. const configFilePath: string | undefined = inputOptions.config;
  90. const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
  91. const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
  92. return {
  93. ...DEFAULT_PRESET,
  94. ...configFileOptions,
  95. ...inputCLIOptions
  96. };
  97. }
  98. /**
  99. * @param {TObject} options
  100. * @returns {TInputOptions}
  101. */
  102. private static filterOptions (options: TInputCLIOptions): TInputOptions {
  103. const filteredOptions: TInputOptions = {};
  104. Object
  105. .keys(options)
  106. .forEach((option: keyof TInputCLIOptions) => {
  107. if (options[option] === undefined) {
  108. return;
  109. }
  110. filteredOptions[option] = options[option];
  111. });
  112. return filteredOptions;
  113. }
  114. public initialize (): void {
  115. this.commands = <commander.CommanderStatic>(new commander.Command());
  116. this.configureCommands();
  117. this.configureHelp();
  118. this.inputPath = path.normalize(this.commands.args[0] || '');
  119. this.inputCLIOptions = JavaScriptObfuscatorCLI.buildOptions(this.commands.opts());
  120. this.sourceCodeReader = new SourceCodeReader(
  121. this.inputPath,
  122. this.inputCLIOptions
  123. );
  124. this.obfuscatedCodeWriter = new ObfuscatedCodeWriter(
  125. this.inputPath,
  126. this.inputCLIOptions
  127. );
  128. }
  129. public run (): void {
  130. const canShowHelp: boolean = !this.arguments.length || this.arguments.includes('--help');
  131. if (canShowHelp) {
  132. this.commands.outputHelp();
  133. return;
  134. }
  135. const sourceCodeData: IFileData[] = this.sourceCodeReader.readSourceCode();
  136. this.processSourceCodeData(sourceCodeData);
  137. }
  138. private configureCommands (): void {
  139. this.commands
  140. .usage('<inputPath> [options]')
  141. .version(
  142. Utils.buildVersionMessage(process.env.VERSION, process.env.BUILD_TIMESTAMP),
  143. '-v, --version'
  144. )
  145. .option(
  146. '-o, --output <path>',
  147. 'Output path for obfuscated code'
  148. )
  149. .option(
  150. '--compact <boolean>',
  151. 'Disable one line output code compacting',
  152. BooleanSanitizer
  153. )
  154. .option(
  155. '--config <boolean>',
  156. 'Name of js / json config file'
  157. )
  158. .option(
  159. '--control-flow-flattening <boolean>',
  160. 'Enables control flow flattening',
  161. BooleanSanitizer
  162. )
  163. .option(
  164. '--control-flow-flattening-threshold <number>',
  165. 'The probability that the control flow flattening transformation will be applied to the node',
  166. parseFloat
  167. )
  168. .option(
  169. '--dead-code-injection <boolean>',
  170. 'Enables dead code injection',
  171. BooleanSanitizer
  172. )
  173. .option(
  174. '--dead-code-injection-threshold <number>',
  175. 'The probability that the dead code injection transformation will be applied to the node',
  176. parseFloat
  177. )
  178. .option(
  179. '--debug-protection <boolean>',
  180. 'Disable browser Debug panel (can cause DevTools enabled browser freeze)',
  181. BooleanSanitizer
  182. )
  183. .option(
  184. '--debug-protection-interval <boolean>',
  185. 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)',
  186. BooleanSanitizer
  187. )
  188. .option(
  189. '--disable-console-output <boolean>',
  190. 'Allow console.log, console.info, console.error and console.warn messages output into browser console',
  191. BooleanSanitizer
  192. )
  193. .option(
  194. '--domain-lock <list> (comma separated, without whitespaces)',
  195. 'Allows to run the obfuscated source code only on specific domains and/or sub-domains (comma separated)',
  196. ArraySanitizer
  197. )
  198. .option(
  199. '--exclude <list> (comma separated, without whitespaces)',
  200. 'A filename or glob which indicates files to exclude from obfuscation',
  201. ArraySanitizer
  202. )
  203. .option(
  204. '--force-transform-strings <list> (comma separated, without whitespaces)',
  205. 'Enables force transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
  206. ArraySanitizer
  207. )
  208. .option(
  209. '--identifier-names-generator <string>',
  210. 'Sets identifier names generator. ' +
  211. `Values: ${CLIUtils.stringifyOptionAvailableValues(IdentifierNamesGenerator)}. ` +
  212. `Default: ${IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator}`
  213. )
  214. .option(
  215. '--identifiers-prefix <string>',
  216. 'Sets prefix for all global identifiers'
  217. )
  218. .option(
  219. '--identifiers-dictionary <list> (comma separated, without whitespaces)',
  220. 'Identifiers dictionary (comma separated) for `--identifier-names-generator dictionary` option',
  221. ArraySanitizer
  222. )
  223. .option(
  224. '--ignore-require-imports <boolean>', 'Prevents obfuscation of `require` imports',
  225. BooleanSanitizer
  226. )
  227. .option(
  228. '--log <boolean>', 'Enables logging of the information to the console',
  229. BooleanSanitizer
  230. )
  231. .option(
  232. '--numbers-to-expressions <boolean>', 'Enables numbers conversion to expressions',
  233. BooleanSanitizer
  234. )
  235. .option(
  236. '--options-preset <string>',
  237. 'Allows to set options preset. ' +
  238. `Values: ${CLIUtils.stringifyOptionAvailableValues(OptionsPreset)}. ` +
  239. `Default: ${OptionsPreset.Default}`
  240. )
  241. .option(
  242. '--reserved-names <list> (comma separated, without whitespaces)',
  243. 'Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns (comma separated)',
  244. ArraySanitizer
  245. )
  246. .option(
  247. '--reserved-strings <list> (comma separated, without whitespaces)',
  248. 'Disables transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
  249. ArraySanitizer
  250. )
  251. .option(
  252. '--rename-globals <boolean>', 'Allows to enable obfuscation of global variable and function names with declaration',
  253. BooleanSanitizer
  254. )
  255. .option(
  256. '--rename-properties <boolean>', 'UNSAFE: Enables renaming of property names. This probably MAY break your code',
  257. BooleanSanitizer
  258. )
  259. .option(
  260. '--rename-properties-mode <boolean>',
  261. 'Specify `--rename-properties` option mode. ' +
  262. `Values: ${CLIUtils.stringifyOptionAvailableValues(RenamePropertiesMode)}. ` +
  263. `Default: ${RenamePropertiesMode.Safe}`
  264. )
  265. .option(
  266. '--rotate-string-array <boolean>', 'Enable rotation of string array values during obfuscation',
  267. BooleanSanitizer
  268. )
  269. .option(
  270. '--seed <string|number>',
  271. 'Sets seed for random generator. This is useful for creating repeatable results.',
  272. parseFloat
  273. )
  274. .option(
  275. '--self-defending <boolean>',
  276. 'Disables self-defending for obfuscated code',
  277. BooleanSanitizer
  278. )
  279. .option(
  280. '--shuffle-string-array <boolean>', 'Randomly shuffles string array items',
  281. BooleanSanitizer
  282. )
  283. .option(
  284. '--simplify <boolean>', 'Enables additional code obfuscation through simplification',
  285. BooleanSanitizer
  286. )
  287. .option(
  288. '--source-map <boolean>',
  289. 'Enables source map generation',
  290. BooleanSanitizer
  291. )
  292. .option(
  293. '--source-map-base-url <string>',
  294. 'Sets base url to the source map import url when `--source-map-mode=separate`'
  295. )
  296. .option(
  297. '--source-map-file-name <string>',
  298. 'Sets file name for output source map when `--source-map-mode=separate`'
  299. )
  300. .option(
  301. '--source-map-mode <string>',
  302. 'Specify source map output mode. ' +
  303. `Values: ${CLIUtils.stringifyOptionAvailableValues(SourceMapMode)}. ` +
  304. `Default: ${SourceMapMode.Separate}`
  305. )
  306. .option(
  307. '--split-strings <boolean>',
  308. 'Splits literal strings into chunks with length of `splitStringsChunkLength` option value',
  309. BooleanSanitizer
  310. )
  311. .option(
  312. '--split-strings-chunk-length <number>',
  313. 'Sets chunk length of `splitStrings` option',
  314. parseFloat
  315. )
  316. .option(
  317. '--string-array <boolean>',
  318. 'Disables gathering of all literal strings into an array and replacing every literal string with an array call',
  319. BooleanSanitizer
  320. )
  321. .option(
  322. '--string-array-encoding <list> (comma separated, without whitespaces)',
  323. 'Encodes each string in strings array using base64 or rc4 (this option can slow down your code speed). ' +
  324. `Values: ${CLIUtils.stringifyOptionAvailableValues(StringArrayEncoding)}. ` +
  325. `Default: ${StringArrayEncoding.None}`,
  326. ArraySanitizer
  327. )
  328. .option(
  329. '--string-array-indexes-type <list> (comma separated, without whitespaces)',
  330. 'Encodes each string in strings array using base64 or rc4 (this option can slow down your code speed). ' +
  331. `Values: ${CLIUtils.stringifyOptionAvailableValues(StringArrayIndexesType)}. ` +
  332. `Default: ${StringArrayIndexesType.HexadecimalNumber}`,
  333. ArraySanitizer
  334. )
  335. .option(
  336. '--string-array-index-shift <boolean>',
  337. 'Enables additional index shift for all string array calls',
  338. BooleanSanitizer
  339. )
  340. .option(
  341. '--string-array-wrappers-count <number>',
  342. 'Sets the count of wrappers for the string array inside each root or function scope',
  343. parseInt
  344. )
  345. .option(
  346. '--string-array-wrappers-chained-calls <boolean>',
  347. 'Enables the chained calls between string array wrappers',
  348. BooleanSanitizer
  349. )
  350. .option(
  351. '--string-array-wrappers-parameters-max-count <number>',
  352. 'Allows to control the maximum number of string array wrappers parameters',
  353. parseInt
  354. )
  355. .option(
  356. '--string-array-wrappers-type <string>',
  357. 'Allows to select a type of the wrappers that are appending by the `--string-array-wrappers-count` option. ' +
  358. `Values: ${CLIUtils.stringifyOptionAvailableValues(StringArrayWrappersType)}. ` +
  359. `Default: ${StringArrayWrappersType.Variable}`
  360. )
  361. .option(
  362. '--string-array-threshold <number>',
  363. 'The probability that the literal string will be inserted into stringArray (Default: 0.8, Min: 0, Max: 1)',
  364. parseFloat
  365. )
  366. .option(
  367. '--target <string>',
  368. 'Allows to set target environment for obfuscated code. ' +
  369. `Values: ${CLIUtils.stringifyOptionAvailableValues(ObfuscationTarget)}. ` +
  370. `Default: ${ObfuscationTarget.Browser}`
  371. )
  372. .option(
  373. '--transform-object-keys <boolean>',
  374. 'Enables transformation of object keys',
  375. BooleanSanitizer
  376. )
  377. .option(
  378. '--unicode-escape-sequence <boolean>',
  379. 'Allows to enable/disable string conversion to unicode escape sequence',
  380. BooleanSanitizer
  381. )
  382. .parse(this.rawArguments);
  383. }
  384. private configureHelp (): void {
  385. this.commands.on('--help', () => {
  386. console.log(' Examples:\n');
  387. console.log(' %> javascript-obfuscator input_file_name.js --compact true --self-defending false');
  388. console.log(' %> javascript-obfuscator input_file_name.js --output output_file_name.js --compact true --self-defending false');
  389. console.log(' %> javascript-obfuscator input_directory_name --compact true --self-defending false');
  390. console.log('');
  391. });
  392. }
  393. /**
  394. * @param {IFileData[]} sourceCodeData
  395. */
  396. private processSourceCodeData (sourceCodeData: IFileData[]): void {
  397. sourceCodeData.forEach(({ filePath, content }: IFileData, index: number) => {
  398. const outputCodePath: string = this.obfuscatedCodeWriter.getOutputCodePath(filePath);
  399. try {
  400. Logger.log(
  401. Logger.colorInfo,
  402. LoggingPrefix.CLI,
  403. `Obfuscating file: ${filePath}...`
  404. );
  405. this.processSourceCode(content, filePath, outputCodePath, index);
  406. } catch (error) {
  407. Logger.log(
  408. Logger.colorInfo,
  409. LoggingPrefix.CLI,
  410. `Error in file: ${filePath}...`
  411. );
  412. throw error;
  413. }
  414. });
  415. }
  416. /**
  417. * @param {string} sourceCode
  418. * @param {string} inputCodePath
  419. * @param {string} outputCodePath
  420. * @param {number | null} sourceCodeIndex
  421. */
  422. private processSourceCode (
  423. sourceCode: string,
  424. inputCodePath: string,
  425. outputCodePath: string,
  426. sourceCodeIndex: number | null
  427. ): void {
  428. const options: TInputOptions = {
  429. ...this.inputCLIOptions,
  430. inputFileName: path.basename(inputCodePath),
  431. ...sourceCodeIndex !== null && {
  432. identifiersPrefix: Utils.getIdentifiersPrefixForMultipleSources(
  433. this.inputCLIOptions.identifiersPrefix,
  434. sourceCodeIndex
  435. )
  436. }
  437. };
  438. if (options.sourceMap) {
  439. this.processSourceCodeWithSourceMap(sourceCode, outputCodePath, options);
  440. } else {
  441. this.processSourceCodeWithoutSourceMap(sourceCode, outputCodePath, options);
  442. }
  443. }
  444. /**
  445. * @param {string} sourceCode
  446. * @param {string} outputCodePath
  447. * @param {TInputOptions} options
  448. */
  449. private processSourceCodeWithoutSourceMap (
  450. sourceCode: string,
  451. outputCodePath: string,
  452. options: TInputOptions
  453. ): void {
  454. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(sourceCode, options).getObfuscatedCode();
  455. this.obfuscatedCodeWriter.writeFile(outputCodePath, obfuscatedCode);
  456. }
  457. /**
  458. * @param {string} sourceCode
  459. * @param {string} outputCodePath
  460. * @param {TInputOptions} options
  461. */
  462. private processSourceCodeWithSourceMap (
  463. sourceCode: string,
  464. outputCodePath: string,
  465. options: TInputOptions
  466. ): void {
  467. const outputSourceMapPath: string = this.obfuscatedCodeWriter.getOutputSourceMapPath(
  468. outputCodePath,
  469. options.sourceMapFileName ?? ''
  470. );
  471. options = {
  472. ...options,
  473. sourceMapFileName: path.basename(outputSourceMapPath)
  474. };
  475. const obfuscatedCode: IObfuscatedCode = JavaScriptObfuscator.obfuscate(sourceCode, options);
  476. this.obfuscatedCodeWriter.writeFile(outputCodePath, obfuscatedCode.getObfuscatedCode());
  477. if (options.sourceMapMode === SourceMapMode.Separate && obfuscatedCode.getSourceMap()) {
  478. this.obfuscatedCodeWriter.writeFile(outputSourceMapPath, obfuscatedCode.getSourceMap());
  479. }
  480. }
  481. }