JavaScriptObfuscatorCLI.ts 19 KB

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