JavaScript filtering hazard scripting method _javascript Tips

Source: Internet
Author: User
The following are their string rules:
1, < (script|link|style|iframe) (. | \ n) *<\/\1>\s*
2, \s*on[a-z]+\s*=\s* ("[^"]+ "|") [^']+'| [^\s]+) \s* (?=>)
3, \s* (HREF|SRC) \s*=\s* ("\s* (javascript|vbscript): [^"]+ "|] \s* (javascript|vbscript): [^ ']+ ' | (javascript|vbscript): [^\s]+) \s* (?=>)
4, epression\ (. | \ n) *\);?
After understanding their rules, catching worms is a ripe move.
<textarea id= "runcode86199" > <textarea id= "bug" cols= "" "rows=" "> <button id=" Kick "> Catching worms 1</button > <script> function kickbug (str) {return str.replace/< (script|link|style|iframe) (. | \ n) *\/\1>\s*/ig, ""); } </script> <iframe></iframe> <link href= ' test.css ' ></link> <style> a {height:e Xpression (Alert (' hei ')); } </style> </textarea> <button id= "kick" > Scratch 1</button> <script> function kickbug (str) { Return Str.replace (/< (Script|link|style|iframe) (. | \ n) *\/\1>\s*/ig, ""); } if (!/msie/i.test (navigator.useragent)) {htmlelement.prototype.__definegetter__ ("innertext", function () {return This.textcontent; }); htmlelement.prototype.__definesetter__ ("InnerText", function (text) {this.textcontent = text; }); } document.getElementById ("Kick"). onclick = function () {var bug = document.getElementById ("bug"); Bug.innertext = Kickbug (Bug.innertext); } </script> </textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

<textarea id="runcode77913"><textarea id= "bug" cols= "rows=" 5 "> Test </textarea> <button id=" kick "> Catch bug 2</button> &l t;script> function Kickbug (str) {return str.replace (/<[a-z][^>]*\s*on[a-z]+\s*=[^>]+/ig,function $0,$1 {return $0.replace (/\s*on[a-z]+\s*=\s*) ("[^"]+ "|] [^']+'| [^\s]+) \s*/ig, ""); }); } if (!/msie/i.test (navigator.useragent)) {htmlelement.prototype.__definegetter__ ("innertext", function () {return This.textcontent; }); htmlelement.prototype.__definesetter__ ("InnerText", function (text) {this.textcontent = text; }); } document.getElementById ("Kick"). onclick = function () {var bug = document.getElementById ("bug"); Bug.innertext = Kickbug (Bug.innertext); } </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

<textarea id="runcode55887"><textarea id= "bug" cols= "rows=" 5 "> Test </textarea> <button id=" kick "> Catch bug 3</button> &l t;script> function Kickbug (str) {return str.replace (/<[a-z][^>]*\s* (HREF|SRC) \s*=[^>]+/ig,function ($, $ {$ = $0.replace (/&# (6[5-9]|[) 78][0-9]|9[0789]|1[01][0-9]|12[012]);? /g,function ($0,$1) {return String.fromCharCode ($);}); Return $0.replace (/\s* (HREF|SRC) \s*=\s* ("\s* (javascript|vbscript): [^"]+ "|" \s* (javascript|vbscript): [^ ']+ ' | (javascript|vbscript): [^\s]+)/ig, ""); }); } if (!/msie/i.test (navigator.useragent)) {htmlelement.prototype.__definegetter__ ("innertext", function () {return This.textcontent; }); htmlelement.prototype.__definesetter__ ("InnerText", function (text) {this.textcontent = text; }); } document.getElementById ("Kick"). onclick = function () {var bug = document.getElementById ("bug"); Bug.innertext = Kickbug (Bug.innertext); } </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

<textarea id="runcode95408"><textarea id= "bug" cols= "rows=" 5 "> Expression () Test </textarea> <button id=" kick "> Catch bug 4</ button> <script> function Kickbug (str) {return str.replace (/<[a-z][^>]*\s*style\s*=[^>]+/ig, function ($0,$1) {$ = $0.replace (/&# (6[5-9]|[ 78][0-9]|9[0789]|1[01][0-9]|12[012]);? /g,function ($0,$1) {return String.fromCharCode ($);}); Return $0.replace (/\s*style\s*=\s* ("[^"]+ (expression) [^ "]+" |] [^ ']+\2[^ ']+ ' | [^\s]+\2[^\s]+) \s*/ig, ""); }); } if (!/msie/i.test (navigator.useragent)) {htmlelement.prototype.__definegetter__ ("innertext", function () {return This.textcontent; }); htmlelement.prototype.__definesetter__ ("InnerText", function (text) {this.textcontent = text; }); } document.getElementById ("Kick"). onclick = function () {var bug = document.getElementById ("bug"); Bug.innertext = Kickbug (Bug.innertext); } </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

This call can be
K1 (K2 (K3 (K4 (str)))

This is simply filtering the script, the so-called "dangerous script" should be able to determine which belong to the "dangerous" script, not dangerous do not filter on ... That's a tricky one, the equivalent of a firewall.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.