In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how python executes js code". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how python executes js code.
What is js code obfuscation?
Normal code
Now let's look at a piece of js code. The logic of the code is very simple: the splicing time is returned.
Function formatDate (now) {var now = new Date (1230999938); var year=now.getFullYear (); var month=now.getMonth () + 1; var date=now.getDate (); var hour=now.getHours (); var minute=now.getMinutes (); var second=now.getSeconds (); return year+ "-" + month+ "-" + date+ "" + hour+ ":" + minute+ ":" + second;} obfuscation code
I randomly found an online js code confusion site.
Js code
Function formatDate (mz1) {var KkkGDiH2=new window ["x44x61x74x65"] (1230999938); var tsk3=KkkGDiH2 ['x67x65x74x46x75cx6cx59x61x72'] (); var YMreyP4=KkkGDiH2 [' x67x65x74x4dx6fx6ex74x68'] () + 1transfervar Ozo5=KkkGDiH2 ['x67x65x74x44x61x74x65'] (); var QMYEc$eD6=KkkGDiH2 [' x67x65x74x48x6fx75x72x73'] (); var JfXVV_Akq7=KkkGDiH2 ['x67x65x74x4dx69x675x4x65x675x4x73'] (); var $mP8=KkkGDiH2 [' x67x65x7x6x53x63f6x6x6x73'] (return tsk3+); "xd" YMreyP4+ + "x2xd + Ozo5+" (); var JfXVV_Akq7=KkkGDiH2 ['x67x65x74x4dx69x675x4x6x73'] (); var $var [' x67x65x7x6x63x6x6x6x6x73'] (); var JfXVV_Akq7=KkkGDiH2 ['x67x65x74x4x72x73'] (); var JfXVV_Akq7=KkkGDiH2 [' x67x65x74x4dx69x675x4x65x6x7x73'] (); var JfXVV_Akq7=KkkGDiH2 ['x67x65x74x74x46x6cx69x6x69x6x6x65x6fx6ex74x73'] (); var JfXVV_Akq7=KkkGDiH2 [' x67x65x74x46x75cx6cx59x61x7x73'] (); var JfXVV_Akq7=KkkGDiH2 ['x67x65x74x4x46x75cx6cx59x6x65x6fx6ex74x73'] (); var JfXVV_Akq7=KkkGDiH2 [' x67x65x74x46x75c
The above is really not written by me blindly, it is like this after confusion, do not believe to look at the picture.
We may have a question, js code has become this thing, can it still be executed? The answer is yes. Even if the js code is very messy, it can still be executed, and the result is the same as the above.
This creates a problem: when we are doing crawlers, we may have to study the js code more or less if necessary, and then decrypt it with js, but how can we solve it if the code is confused with this?
It doesn't make sense at all. Basically, it can't be rewritten with Python according to js logic.
Coincidentally, the editor is also stuck here. Then I thought, if only Python could execute the js code, never mind the logic in the function, and just take the function to return the value.
Python third Party package Execjs
Maybe. I'm not the only one in this situation, so the bosses developed this toolkit to execute js code.
Installation
Before installation, you need to have a node environment, so there will be no chestnuts here, and the next step will be fine.
Pip3 install PyExecJS executes js
Note: since the above js code generates window objects, which cannot be successfully executed directly, additional assistance is needed. Here are some other examples.
Normal js code
Function add (x, y) {return x + y;}
Python executes js code
Import execjsctx = execjs.compile ("function add (x, y) {return x + y;}") print (ctx.call (" add ", 1,2))
The execution result is shown in the following figure:
Confuse js code
Function add (bi1,Pl$2) {return bi1+Pl$2}
Python executes obfuscation js code
Import execjsctx = execjs.compile ("" function add (bi1,Pl$2) {return bi1+Pl$2} "") print (ctx.call ("add", 1,2))
As you can see, even if there is more confusion, there is no problem as long as the js code is executed.
The js obfuscation code returned by the above stitching time can also be executed, but it has an extra window object and requires node to install jsdom. I'm sorry that I can't download an example because of a problem with the network setting.
Or you can use selenium to open the browser and then go back, of course, more slowly.
At this point, I believe you have a deeper understanding of "how python executes js code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.