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 to implement function redefinition by JavaScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces JavaScript how to achieve function redefinition, the article introduces in great detail, has a certain reference value, interested friends must read it!

Function redefinition

This is one of the most basic and most commonly used code anti-debugging techniques. In JavaScript, we can redefine the function used to collect information. For example, the console.log () function can be used to collect information such as functions and variables and display them in the console. If we redefine this function, we can modify its behavior and hide specific information or display bogus information.

We can run this function directly in DevTools to understand its function:

Console.log ("HelloWorld"); var fake = function () {}; window ['console'] [' log'] = fake;console.log ("Youcan't see me!")

After running, we will see:

VM48:1 Hello World

You will find that the second message is not displayed because we have redefined the function to "disable" its original function. But we can also make it display false information. For example:

Console.log ("Normalfunction"); / / First we save a reference to the original console.log functionvar original = window ['console'] [' log']; / / Next we create our fake function//Basicly we check the argument and if match we call original function with otherparam.// If there is no match pass the argument to the original functionvar fake = function (argument) {if (argument = "Ka0labs") {original ("Spoofed!");} else {original (argument) }} / / We redefine now console.log as our fake functionwindow ['console'] [' log'] = fake;//Then we call console.log with any argumentconsole.log ("Thisis unaltered"); / / Now we should see other text in console different to "Ka0labs" console.log ("Ka0labs"); / / Aaaand everything still OKconsole.log ("Byebye!")

If everything is all right:

Normal functionVM117:11 This is unalteredVM117:9 Spoofed!VM117:11 Bye bye!

In fact, in order to control how the code is executed, we can also modify the function of the function in a smarter way. For example, we can build a code snippet based on the above code and redefine the eval function. We can pass the JavaScript code to the eval function, and then the code will be evaluated and executed. If we redefine this function, we can run different code:

/ / Just a normal evaleval ("console.log ('1337')"); / / Now we repat the process...var original = eval;var fake = function (argument) {/ / If the code to be evaluated contains1337... If (argument.indexOf ("1337")! =-1) {/ /. We just execute a different code original ("for (I = 0 Now we should see the execution of a for loop instead of what is expectedeval I < 10 Weshould see this...' +) {console.log (I);}");} else {original (argument);}} eval= fake;eval ("console.log ('Weshould see this...')"); / / Now we should see the execution of a for loop instead of what is expectedeval ("console.log (' Too1337 for youthful") ")

The running results are as follows:

1337VM146:1We should see this... VM147:10VM147:11VM147:12VM147:13VM147:14VM147:15VM147:16VM147:17VM147:18VM147:19

As mentioned earlier, although this method is very clever, it is also a very basic and common method, so it is easier to detect.

These are all the contents of the article "how to redefine functions in JavaScript". Thank you for reading! Hope to share the content to help you, more related 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