Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript uses proxy objects

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use JavaScript proxy object, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Proxy object

Proxy object is one of the most useful tools in JavaScript, which can help us understand other objects in the code, including modifying their behavior and triggering object activity in a specific environment. For example, we can create a cute object and track every document.createElemen call, and then record the relevant information:

Const handler = {/ / Our hook to keep the track apply: function (target, thisArg, args) {console.log ("Intercepted a call tocreateElement with args:" + args); return target.apply (thisArg, args)}} document.createElement= new Proxy (document.createElement, handler) / / Create our proxy object withour hook ready to interceptdocument.createElement ('div')

Next, we can record the relevant parameters and information in the console:

VM64:3 Intercepted a call to createElement with args: div

We can use this information to debug code by intercepting specific functions, but the main purpose of this article is to introduce anti-debugging techniques, so how do we detect whether the "other party" uses a proxy object? In fact, this is a cat-and-mouse game. For example, we can use the same code snippet and then try to call the toString method and catch the exception:

/ / Call a "virgin" createElement:try {document.createElement.toString ();} catch (e) {console.log ("I saw your proxy!");}

The information is as follows:

"function createElement () {[native code]}"

But when we use the proxy:

/ / Then apply the hookconsthandler = {apply: function (target, thisArg, args) {console.log ("Intercepted a call tocreateElement with args:" + args); return target.apply (thisArg, args)}} document.createElement= new Proxy (document.createElement, handler); / / Callour not-so-virgin-after-that-party createElementtry {document.createElement.toString ();} catch (e) {console.log ("I saw your proxy!");}

Yes, we can detect proxies:

VM391:13 I saw your proxy!

We can also add the toString method:

Const handler = {apply: function (target, thisArg, args) {console.log ("Intercepted a call tocreateElement with args:" + args); return target.apply (thisArg, args)}} document.createElement= new Proxy (document.createElement, handler); document.createElement= Function.prototype.toString.bind (document.createElement); / / Add toString//Callour not-so-virgin-after-that-party createElementtry {document.createElement.toString () } catch (e) {console.log ("I saw your proxy!");}

Now we can't detect it:

"function createElement () {[native code]}"

Like I said, this is a cat-and-mouse game.

These are all the contents of the article "how JavaScript uses proxy objects". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report