bytenode: Problem with JavaScript Obfuscator package

_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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16

Most upvoted comments

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!

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!

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!

If anyone also has this problem, either use a buffer to decode or just remove the stringArrayEncoding. It’s kind of useless anyway, since it only takes secs to reverse.