What is js code obfuscation?
normal code
Let's now look at a piece of js code, the code logic is simple, it's just splicing the time return.
function formatDate(now) { var now = new Date(1230999938); var year=(); var month=()+1; var date=(); var hour=(); var minute=(); var second=(); return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; }
Run the code as shown below.
obfuscated code
I found a random online js code obfuscation site.
js code
function formatDate(mz1){var KkkGDiH2=new window["\x44\x61\x74\x65"](1230999938);var tsk3=KkkGDiH2['\x67\x65\x74\x46\x75\x6c\x6c\x59\x65\x61\x72']();var YMreyP4=KkkGDiH2['\x67\x65\x74\x4d\x6f\x6e\x74\x68']()+1;var Ozo5=KkkGDiH2['\x67\x65\x74\x44\x61\x74\x65']();var QMYEc$eD6=KkkGDiH2['\x67\x65\x74\x48\x6f\x75\x72\x73']();var JfXVV_Akq7=KkkGDiH2['\x67\x65\x74\x4d\x69\x6e\x75\x74\x65\x73']();var $mP8=KkkGDiH2['\x67\x65\x74\x53\x65\x63\x6f\x6e\x64\x73']();return tsk3+"\x2d"+YMreyP4+"\x2d"+Ozo5+" "+QMYEc$eD6+"\x3a"+JfXVV_Akq7+"\x3a"+$mP8
I really didn't write this above blindly, this is what it looks like after the confusion, look at the picture if you don't believe me.
We may have a question, js code into this thing, can still be executed? The answer is yes. Even if the js code is very messy, but still can be executed, the result with the above is the same.
This creates a problem, we are doing crawlers, if necessary, more or less may have to study the js code, and then js decryption a bit, but, but, if the code are confused into this, how to solve?
The logic doesn't make sense at all anymore, it's basically not rewritable in Python following js logic...
Coincidentally, I am also stuck here... Then I thought, if Python can execute js code is good, do not have to care about the logic inside the function, just take the function return value is good.
Python third-party package Execjs
Probably, I'm not the only one experiencing this, so the big boys developed this toolkit for executing js code.
mounting
Before installing, you need to have node environment, here is not a chestnut, the next step next step.
pip3 install PyExecJS
Execute js
Note: As the above js code will generate window object, and can not be directly executed successfully, need additional other assistance, here is a simple example of other.
Normal js code
function add(x, y) { return x + y; }
Python executes js code
import execjs ctx = (""" function add(x, y) { return x + y; } """) print(("add", 1, 2))
The result of the execution is shown below:
Obfuscating js code
function add(bi1,Pl$2){return bi1+Pl$2}
Python executes obfuscated js code
import execjs ctx = (""" function add(bi1,Pl$2){return bi1+Pl$2} """) print(("add", 1, 2))
The result of the execution is shown below:
As you can see, even if it's more obfuscated, as long as it's js code that's being executed, it's fine.
The js obfuscated code returned by the above splicing time is also executable, but it has an extra window object, which requires node to install jsdom for it to work, and I'm really sorry that I can't download the example due to a problem with the network settings.
Or use selenium to open the explorer to execute and then return also works, of course, slower.
The above is the details of python execution of js code, more information about python execution of js code please pay attention to my other related articles!