question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with JavaScript Obfuscator package

See original GitHub issue

_Originally posted by @cyphercodes96 in https://github.com/OsamaAbbas/bytenode/issues/5#issuecomment-727723366_

Awesome and amazing work on this package - Looking forward to put it into good use

JSYK whenever I try to bytenode an obfuscated file using JavaScriptObfuscator I get the same rangeError invalid string length exception

Would be awesome if we can somehow resolve this - would add more security whenever the .js file will exist prior to .jsc on production environment ( prior to compilation / deletion of file )

const obfJs = (src, options = {}) => {
  fs.readFile(src, 'utf8', function (err, data) {
    if (err) return console.log('err obfuscation3 to file', err);
    var obfuscationResult = JavaScriptObfuscator.obfuscate(data, Object.assign({}, obfOptions, options));

    fs.writeFile(src, obfuscationResult.getObfuscatedCode(), 'utf8', function (err) {
      if (err) return console.log('err obfuscation4 to file', err);
    });
  });
};
obfJs('./libraries/secured.js');

Then on my electron main.js

const appPath = require('electron-root-path').rootPath;
const obfJs2Byte = (src, options = {}) => {
  console.log('obfuscating js 2 bye');
  try {

    let compiledFilename = bytenode.compileFile({
      filename: appPath + src,
      compileAsModule: true
    });
    console.log(compiledFilename)
  } catch (err) {
    console.log('ERR CAUGHT', err)
  }
  try {
    fs.unlink(appPath + src, (err) => {
      if (err) {
        console.error(err);
        return
      }
    })
  } catch (err) {
    console.error(err)
  }
};
console.log('LOGGING FILE RN');
const bytenode = require('bytenode');
if (!fs.existsSync(appPath + '/secured.jsc')) {
  obfJs2Byte('/secured.js');
}


const Security = require(appPath + '/secured.jsc');

Remove javascript obfuscation and it works like a charm

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
asd281533890commented, Jun 19, 2021

I think that debugProtection and self-defending options both are meant to prevent any alteration in your code. So they have to depend on Function.prototype.toString() function (in order to inspect your code), and with bytenode, you cannot in principle use .toString() on functions, because the source code is already removed for protection.

I think you are right!!! I obfuscated the code below in https://obfuscator.io/

Function.prototype._toString = Function.prototype.toString
Function.prototype.toString = function (t) {
  console.log("Function.prototype.toString is called")
  return this._toString(t)
}
function hi() {
  console.log("Hello World!");
}
hi();

No matter whether debugProtection or self-defending is checked. It outputs: Function.prototype.toString is called Hello World! (Note: Check Evaluate options in the output tab in https://obfuscator.io) Thanks very much!

1reaction
asd281533890commented, Jun 2, 2021

Barely 500 lines of code - already small source file, unless you want me to try with like 5 lines of code 😄

I don’t mean your original source, I mean after you obfuscate it, what is its size?

~I have asked this question before and I guess it could be relevant here. Please check its size (and the original file size too).~

Without a complete reproducible example, there is a little we can do. Please create a complete example that reproduce the issue as simple as npm install && npm start. Otherwise, it’s not possible for me to struggle with javascript-obfuscator, webpack, vue-cli and electron to find an issue that might be in either of them, or even in your own code after all!

A complete, simple, reproducible example please.

Barely 500 lines of code - already small source file, unless you want me to try with like 5 lines of code 😄

I don’t mean your original source, I mean after you obfuscate it, what is its size?

~I have asked this question before and I guess it could be relevant here. Please check its size (and the original file size too).~ Without a complete reproducible example, there is a little we can do. Please create a complete example that reproduce the issue as simple as npm install && npm start. Otherwise, it’s not possible for me to struggle with javascript-obfuscator, webpack, vue-cli and electron to find an issue that might be in either of them, or even in your own code after all! A complete, simple, reproducible example please.

I had the same problem. I will give a complete reproducible example later. If this problem is solved, it will help me a lot. Thank you very much.Bytenode is a great work!

I’m sorry I can’t provide a demo project, because I created a demo with vue-cli+electron-builder+javaScript-obfuscator on another computer and there is no problem. The program I had a problem with is on the intranet and could not be taken out. But I found the problem is because of the arrow function! I don’t know why there are many arrow functions in the vue code packaged in electron, even if babel is added by default. As far as I know, I usually use vue to build projects and add babel by default to transpile arrow functions into ordinary functions.

My process of creating the project: 1.vue create demo 2.vue add electron-builder

I finally solved the problem by adding the plugin “@babel/plugin-transform-arrow-functions” to the babel.config.js file in the project root directory.

// babel.config.js
module.exports = {
  presets: ['@vue/cli-plugin-babel/preset'],
  plugins: ['@babel/plugin-transform-arrow-functions'] // add this
}

However, the debugProtection option of JavaScript-obfuscator still prevents the program from starting (the application window is blank after opening).I only open the “Debug Protection” option at https://obfuscator.io/, you can see that it has inserted a piece of code.Other options of JavaScript-obfuscator are still undetermined.

Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · javascript-obfuscator/javascript-obfuscator - GitHub
A powerful obfuscator for JavaScript and Node.js. Contribute to javascript-obfuscator/javascript-obfuscator development by creating an account on GitHub.
Read more >
javascript-obfuscator - npm Package Health Analysis - Snyk
We found that javascript-obfuscator demonstrates a positive version release cadence with at least one new version released in the past 12 months. As...
Read more >
node.js - failing to package a script using pkg after obfuscating ...
i wanted to obfuscate my files with https://www.npmjs.com/package/javascript-obfuscator before compiling them to an executable to make it ...
Read more >
javascript-obfuscator - npm
Start using javascript-obfuscator in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type ...
Read more >
JavaScript Obfuscation: The Definitive Guide (2022)
As with all things related to security, obfuscation is a high-stakes process. Just like using a weak JS obfuscator can provide a wrong...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found