How to obfuscate JavaScript using PHP? [closed]

2019-05-11 14:11发布

问题:

For example, how could I obfuscate this sample code from:

/*
The code below will write
to a heading and to a paragraph,
and will represent the start of
my homepage:
*/
document.getElementById("myH1").innerHTML="Welcome to my Homepage";
document.getElementById("myP").innerHTML="This is my first paragraph.";

into the form:

var _0xcc34=["\x69\x6E\x6E\x65\x72\x48\x54\x4D\x4C","\x6D\x79\x48\x31","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64","\x57\x65\x6C\x63\x6F\x6D\x65\x20\x74\x6F\x20\x6D\x79\x20\x48\x6F\x6D\x65\x70\x61\x67\x65","\x6D\x79\x50","\x54\x68\x69\x73\x20\x69\x73\x20\x6D\x79\x20\x66\x69\x72\x73\x74\x20\x70\x61\x72\x61\x67\x72\x61\x70\x68\x2E"];document[_0xcc34[2]](_0xcc34[1])[_0xcc34[0]]=_0xcc34[3];document[_0xcc34[2]](_0xcc34[4])[_0xcc34[0]]=_0xcc34[5];

using PHP?

I know that I can do this using http://javascriptobfuscator.com/ but I need to do that inside my PHP file, because it dynamically changes.

回答1:

OK. On theory about how to obsfucate.

  • you need to know the language, as you will need a parser for good results.
  • when you know what is what, then you can start replacing things.

for a very stupid example replace all instances of

document.getElementById('string'); with ab(cd('fgevat'); like

function cd(s) { /* ROT13 implemented here*/ } function ab(s) { return document.getElementById(s); } ab(cd('fgevat');
  • you can use eval to avoid unpacking to clear text. and then you can go

like this

function h(s) { /*implement hexdecode in an ugly way, and run eval() on the resulting string */} h('2020202066756e6374696f6e206364287329207b202f2a20524f54313320696d706c656d656e74656420686572652a2f207d2066756e6374696f6e206162287329207b2072657475726e20646f63756d656e742e676574456c656d656e74427949642873293b207d206162286364282766676576617427293b'); // which is the above code.

this is all r=1 stuff. And can be done waay better with a deeper understanding of the language, also these are absolutely trivial to reverse