I'm working on a simple javascript code obfuscator.
Say you have a 2D square matrix of NxN
javascript functions. For n=3:
var m = [
[function(){return 1}, function(){return 2}, function(){return 3}],
[function(){return 4}, function(){return 5}, function(){return 6}],
[function(){return 7}, function(){return 8}, function(){return 9}]
]
Now, in the example, each one of those functions would actually contain chunks of code from the code to be obfuscated, and call that in. A typical obfuscated code would look like this:
(function(){ m[1][2](m[2][0](m[0][1]))(m[1][0])(m[2][2])(m[1][1](m[1][2](m[2][0]))); }());
The above translates into this:
(function(){ 6( 7(2) )( 4 )( 9 )( 5( 6( 7 ) ) ); }());
Of course, assuming that a typical m[x][y]
returns functions or whatever as necessary, this would all be very easily decipherable. So after every m[x][y]
call, m[x][y]
would switch position with m[x+1][y-1]
which would switch at his turn position with m[0][2]
and basically I want to mixed them up so bad you wouldn't even dare attempt to decipher. Probably using random generated math, and that goes as per function.
The point is that the obfuscator needs to know this formula beforehand, so it knows which m[x][y]
to call in, and at which point, and decide the order in which to bandwagon the functions. A working implementation might even result in:
(function( m[0][0](m[0][0](m[0][0]))(m[0][0]) ))
And still work. So here are my questions:
How can I split javascript code into smaller functions? I know compilers basically do that, but I also seen some on-line JS beautifiers, and syntax highlighters being able to distinguish code and do their thing properly. I mean can basic regex do the job?
This doesn't work and it should:
window.a = function(x){ var r = x*2; window.a =alert; // redefines itself after first call return r; }; a('2 * 2 = ' + a(2)); // doesn't work. it should've alerted "2 * 2 = 4"
This on the other hand does work:
var i = 0; window.a = function(x){ if(i===1){ i=0; return alert(x); } i=1; return x*2; }; a('2 * 2='+a(2));
Why?
Are there any similar on-going projects? Maybe I can get some notes or reference to them.
First of all congrats on what you are doing. It is a bit of insane, but it's almost brilliant.
Question 1:
I do not know how you could do this. I've spent some time thinking about the same principle.
Question 2:
Your first example doesn't work because the first call for a it is actually a( "2 * 2 = " + x )
where x would be the second call, so when the first a is called you get a NaN
because you give it a String
Question 3:
Similar projects? Hmm let me think... I guess everybody has it's own project for something like this, because the code is visible. Actual sources i do not know.
Proposal:
You could consider that square 2D matrix a 3D Rubik's Cube this way you could shuffle it and solve it. The interesting part is that you can combine that a series of moves to actually be a big function for doing something. Anyway probably the best solution for this kind of things IMHO would be to duplicate and then erase the main objects of the JS language and store it in a complex graph inside a anonymous function where nobody can enter to mess around, as for the code to be unreadable, well i think this is an ART.
来源:https://stackoverflow.com/questions/9936626/javascript-how-to-bandwagon-functions-and-change-their-purpose-after-each-call