Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obfuscating transpilade babel ES5 #53

Closed
I-NOZex opened this issue May 19, 2017 · 19 comments
Closed

Obfuscating transpilade babel ES5 #53

I-NOZex opened this issue May 19, 2017 · 19 comments
Labels

Comments

@I-NOZex
Copy link

I-NOZex commented May 19, 2017

Is it possible?
I'm using the gulp task, but all i get is this error:

C:\Users\<MyProjectPath>\src\node_modules\ttf2woff2\jssrc\ttf2woff2.js:1 (function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["normalize"](filename);var ret=node Error: Line 1: Unexpected token : at ErrorHandler.constructError (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:3396:16) at ErrorHandler.createError (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:3414:27) at Parser.unexpectedTokenError (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:542:39) at Parser.throwUnexpectedToken (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:552:21) at Parser.consumeSemicolon (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:845:23) at Parser.parseExpressionStatement (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:2080:15) at Parser.parseStatement (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:2495:35) at Parser.parseStatementListItem (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:1823:31) at Parser.parseBlock (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:1835:30) at Parser.parseStatement (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:2500:39) at Parser.parseStatementListItem (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:1823:31) at Parser.parseProgram (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:3061:29) at Object.parse (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\node_modules\esprima\dist\esprima.js:117:24) at JavaScriptObfuscatorInternal.obfuscate (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\dist\webpack:\src\JavaScriptObfuscatorInternal.ts:61:54) at Function.obfuscate (C:\Users\<MyProjectPath>\src\node_modules\javascript-obfuscator\dist\webpack:\src\JavaScriptObfuscator.ts:30:85) at DestroyableTransform._transform (C:\Users\<MyProjectPath>\src\node_modules\gulp-javascript-obfuscator\index.js:19:46)

@I-NOZex I-NOZex changed the title Obfuscating transcompilade babel ES5 Obfuscating transpilade babel ES5 May 19, 2017
@sanex3339
Copy link
Member

Hi, show me your source code

@I-NOZex
Copy link
Author

I-NOZex commented May 19, 2017

Hi, thank you for your reply.
my build task is this:

'use strict';

var gulp = require('gulp');
var babel = require('gulp-babel');
var sourcemaps = require('gulp-sourcemaps');
var less = require('gulp-less');
var sass = require('gulp-sass');

var jetpack = require('fs-jetpack');

var utils = require('./utils');

var projectDir = jetpack;
var srcDir = projectDir.cwd('./app');
var destDir = projectDir.cwd('./build');

var javascriptObfuscator = require('gulp-javascript-obfuscator');

var paths = {
    jsCodeToTranspile: [
        'app/**/*.js',
        '!app/js/communication/server_files/**',
        '!app/node_modules/**',
        '!app/vendor/**'
    ],
    jsCodeToObfuscate: [
        'build/**/*.js',
        '!build/js/communication/server_files/**',
        '!build/node_modules/**',
        '!build/vendor/**'
    ],	
    copyFromAppDir: [
        //'./node_modules/**',
        './js/communication/server_files/**',
        './vendor/**',
        './fonts/**',
        './welcome/**',
        './locales/**',
        './*.html',
        './stylesheets/*.css',
        './*.rtf'
    ],
	copyFromAppDirAll: [
		'./node_modules/**',
        './js/communication/server_files/**',
        './vendor/**',
        './fonts/**',
		'./locales/**',
        './welcome/**',
        './*.html',
        './stylesheets/*.css',
        './*.rtf'
	]
}

// -------------------------------------
// Tasks
// -------------------------------------

gulp.task('clean', function(callback) {
    return destDir.dirAsync('.', { empty: true });
});



var copyTask = function () {
    projectDir.copy('resources/icon.png', destDir.path('icon.png'), { overwrite: true });

	
	function fallbackCopy(){
		return projectDir.copyAsync('app', destDir.path(), {
        overwrite: true,
        matching: paths.copyFromAppDirAll
    });
		/*projectDir.copy('app', destDir.path(), {
			overwrite: true,
			matching: ['./node_modules/**']
		});*/
	}
		
		
	if((process.platform === 'win32') && (utils.getEnvName()!=="production")){
		var vfs = require('vinyl-fs');
		vfs.src('app/node_modules/', {followSymlinks: false})
			.pipe(vfs.symlink('build/node_modules'))
			.on('error', fallbackCopy);
    } else {
      return fallbackCopy();
    }
	
	
	return projectDir.copyAsync('app', destDir.path(), {
        overwrite: true,
        matching: paths.copyFromAppDir
    });
	
	
	/*function fallbackCopy(){
		return projectDir.copyAsync('app', destDir.path(), {
			overwrite: true,
			matching: ['./node_modules/**']
		});
	}
	var vfs = require('vinyl-fs');
	vfs.src('app/node_modules/', {followSymlinks: false})
		.pipe(vfs.symlink('build/node_modules'))
		.on('error', fallbackCopy);*/
};
gulp.task('copy', ['clean'], copyTask);
gulp.task('copy-watch', copyTask);

var transpileTask = function () {
    return gulp.src(paths.jsCodeToTranspile)
    .pipe(sourcemaps.init())
    .pipe(babel({ modules: 'amd' }))
    .pipe(sourcemaps.write('.'))
    .pipe(javascriptObfuscator())	
    .pipe(gulp.dest(destDir.path())).on('end', function() { 
		require('gulp-util').beep(); 
		require('child_process').exec("msg  'Done ;)'");
	});
};
gulp.task('transpile', ['clean'], transpileTask);
gulp.task('transpile-watch', transpileTask);


var obfuscateTask = function () {
    return gulp.src(paths.jsCodeToObfuscate)
    .pipe(javascriptObfuscator())	
    .pipe(gulp.dest(destDir.path())).on('end', function() { 
		require('gulp-util').beep(); 
		require('child_process').exec("msg  'Done ;)'");
	});
};
gulp.task('obfuscateTask', ['transpile'], obfuscateTask);


/*
var lessTask = function () {
    return gulp.src('app/stylesheets/main.less')
    .pipe(less())
    .pipe(gulp.dest(destDir.path('stylesheets')));
};*/
//Minify and transpile Sass files into css
var sassTask = function () {
  gulp.src('app/stylesheets/main.scss')
    .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
	//.pipe(rename({ suffix: '.min' }))
    .pipe(gulp.dest(destDir.path('stylesheets')));
	
  gulp.src('app/vendor/hubing_ui/sass/style.scss')
    .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
	//.pipe(rename({ suffix: '.min' }))
    .pipe(gulp.dest(destDir.path('vendor/hubing_ui/stylesheets')));	
};

var less2Task = function() {
	return gulp.src('app/js/communication/server_files/css/*.less')
    .pipe(less())
    .pipe(gulp.dest(destDir.path('js/communication/server_files/css')));
}

gulp.task('sass', ['clean','copy'], sassTask);
gulp.task('sass-watch', sassTask);

gulp.task('less2', ['clean'], less2Task);
gulp.task('less2-watch',less2Task);


//generate icons// -------------------------------------
var iconfont = require('gulp-iconfont');
var runTimestamp = Math.round(Date.now()/1000);

gulp.task('Iconfont', ['clean'], function(){
	var async = require('async'),
	iconfont = require('gulp-iconfont'),
	consolidate = require('gulp-consolidate'),
	rename = require('gulp-rename');

  var iconStream = gulp.src(['app/icons/*.svg'])
    .pipe(iconfont({ fontName: 'PROJECT_NAMEfonticon', formats: ['woff'], normalize: true/*, fontHeight: 1001 */}));

  async.parallel([
    function handleGlyphs (cb) {
      iconStream.on('glyphs', function(glyphs, options) {
        gulp.src('app/icons/tpl.css')
          .pipe(consolidate('lodash', {
            glyphs: glyphs,
            fontName: 'PROJECT_NAMEfonticon',
            fontPath: '',
            className: 'icon'
          }))
		  .pipe(rename({ basename: 'PROJECT_NAMEfonticon' }))
          .pipe(gulp.dest(destDir.path('icons')))
		  .pipe(gulp.dest(destDir.path('js/communication/server_files/fonts')))

        gulp.src('app/icons/tpl.json')
          .pipe(consolidate('lodash', {
            glyphs: glyphs,
            fontName: 'PROJECT_NAMEfonticon',
            fontPath: '',
            className: 'icon'
          }))
		  .pipe(rename({ basename: 'PROJECT_NAMEfonticon' }))
          .pipe(gulp.dest(destDir.path('icons')))
		  .pipe(gulp.dest(destDir.path('js/communication/server_files/fonts')))

        gulp.src('app/icons/tpl_.json')
          .pipe(consolidate('lodash', {
            glyphs: glyphs,
            fontName: 'PROJECT_NAMEfonticon',
            fontPath: '',
            className: 'icon'
          }))
		  .pipe(rename({ basename: 'PROJECT_NAMEfonticon_' }))
          .pipe(gulp.dest(destDir.path('icons')))
		  .pipe(gulp.dest(destDir.path('js/communication/server_files/fonts')))

		if(utils.getEnvName() === 'development')
			gulp.src('app/icons/tpl.html')
			  .pipe(consolidate('lodash', {
				glyphs: glyphs,
				fontName: 'PROJECT_NAMEfonticon',
				fontPath: './',
				className: 'icon'
			  }))
			  .pipe(rename({ basename: 'PROJECT_NAMEfonticon' }))
			  .pipe(gulp.dest(destDir.path('icons')))
			  .pipe(gulp.dest(destDir.path('js/communication/server_files/fonts')))
			  .on('finish', cb);
      });
    },
    function handleFonts (cb) {
      iconStream
        .pipe(gulp.dest(destDir.path('icons')))
		.pipe(gulp.dest(destDir.path('js/communication/server_files/fonts')))
        .on('finish', cb);
    }
  ]);
});

// Add and customize OS-specyfic and target-specyfic stuff.
gulp.task('finalize', ['clean'], function () {
    var manifest = srcDir.read('package.json', 'json');
    switch (utils.getEnvName()) {
        case 'production':
            // Hide dev toolbar if doing a release.
            manifest.window.toolbar = false;
            break;
        case 'test':
            // Add "-test" suffix to name, so NW.js will write all
            // data like cookies and locaStorage into separate place.
            manifest.name += '-test';
            // Change the main entry to spec runner.
            manifest.main = 'spec.html';
            break;
        case 'development':
            // Add "-dev" suffix to name, so NW.js will write all
            // data like cookies and locaStorage into separate place.
            manifest.name += '-dev';
            break;
    }
    destDir.write('package.json', manifest);

    var configFilePath = projectDir.path('config/env_' + utils.getEnvName() + '.json');
    destDir.copy(configFilePath, 'env_config.json');
});


gulp.task('watch', function () {
    gulp.watch(paths.jsCodeToTranspile, {
        interval: 1000, // default 100
        debounceDelay: 500, // default 500
        mode: 'poll'
    },['transpile-watch']);
    gulp.watch('app/stylesheets/*.scss', ['sass-watch']);
	gulp.watch('app/js/communication/server_files/css/*.less',['less2-watch']);
    gulp.watch(paths.copyFromAppDir, { cwd: 'app' }, ['copy-watch']);
	
});


gulp.task('build', ['transpile', 'sass', 'copy', 'less2', 'Iconfont','obfuscateTask', 'finalize']);

ps: this is a NWJS app

@sanex3339
Copy link
Member

sanex3339 commented May 19, 2017

Not gulpfile.js but your source code that you obfuscating.

This file:

require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["normalize"](filename);var ret=node Error

@sanex3339
Copy link
Member

can you upload it here?
https://gist.github.com/

@I-NOZex
Copy link
Author

I-NOZex commented May 19, 2017

@sanex3339
Copy link
Member

It's strange because this code obfuscating ok with https://javascriptobfuscator.herokuapp.com/

@sanex3339
Copy link
Member

sanex3339 commented May 19, 2017

What version of javascript-obfuscator you using? (check package.json under node_modules/javascript-obfuscator or node_modules/gulp-javascript-obfuscator/node_modules/javascript-obfuscator

@I-NOZex
Copy link
Author

I-NOZex commented May 19, 2017

exactly, i've already tested...
it could be some sort of conflict with babel? :/

@sanex3339
Copy link
Member

sanex3339 commented May 19, 2017

I don't know. Right way is to use obfuscator after babel, it should accepts any valid es5/es6 code

@I-NOZex
Copy link
Author

I-NOZex commented May 19, 2017

it's what i'm doing...
the gulp obfuscator module version is "1.1.2"
obfuscator module dependency version is "0.9.4"

@sanex3339
Copy link
Member

sanex3339 commented May 19, 2017

Try to get source code after transpiling with babel and paste it here
https://javascriptobfuscator.herokuapp.com/

If it will obfuscating without errors - try to make issue here:
https://github.com/javascript-obfuscator/gulp-javascript-obfuscator/issues

@I-NOZex
Copy link
Author

I-NOZex commented May 20, 2017 via email

@sanex3339
Copy link
Member

You 100% sure that babel task was running before obfuscation task and not after?

@I-NOZex
Copy link
Author

I-NOZex commented May 22, 2017

well yes, my obfuscate task has a depedency on transpile task

var transpileTask = function () {
    return gulp.src(paths.jsCodeToTranspile)
    .pipe(sourcemaps.init())
    .pipe(babel({ modules: 'amd' }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(destDir.path())).on('end', function() { 
		require('gulp-util').beep(); 
		require('child_process').exec("msg 'Done ;)'");
	});
};
gulp.task('transpile', ['clean'], transpileTask);
gulp.task('transpile-watch', transpileTask);


var obfuscateTask = function () {
    return gulp.src(paths.jsCodeToObfuscate)
    .pipe(javascriptObfuscator())	
    .pipe(gulp.dest(destDir.path())).on('end', function() { 
		require('gulp-util').beep(); 
		require('child_process').exec("msg 'Done ;)'");
	});
};
gulp.task('obfuscateTask', ['transpile'], obfuscateTask);

@sanex3339
Copy link
Member

sanex3339 commented May 22, 2017

So it's very strange.

Line 1: Unexpected token : at ErrorHandler.constructError

Error message pointed on strange symbol : that AST parser esprima can't parse.
esprima should correctly parse any valid es5 and es2015/es2016 code, so it looks like input code for javascript-obfuscator and esprima not a valid es5/es2015/es2016 code

@I-NOZex
Copy link
Author

I-NOZex commented May 22, 2017

i've managed to make it work, i created an auxiliar folder to put the transpiled files, and the use those folder to obfuscate and copy to the final build path

@sanex3339
Copy link
Member

I can close issue then?

@I-NOZex
Copy link
Author

I-NOZex commented Jul 17, 2017

Yes, sorry didn't replied earlier

@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants