TML转义字符:xss攻击与HTML字符的转义和反转义

xss与转义符

xss简单来说:

  • XSS 攻击是页面被注入了恶意的代码

  • XSS 漏洞是 可以让攻击者注入恶意代码可执行的漏洞

具体参看之前写的:

为了xss,常见的转义符

字符转义后的字符
& &
< &lt;
> &gt;
" &quot;
' &#x27;
/ &#x2F;

转义只是防止xss攻击的一种手段之一,更多请查看:《web开发前端安全问题总结——web前端安全问题汇总

html转义与反转义方法

html转义

lodashjs

lodashjs作为常用,集成了escape 方法

https://www.lodashjs.com/docs/lodash.escape

DOM API

转义方法
let textNode = document.createTextNode(`<script src='https://www.zhoulujun.cn/xxs.js'></script>`);
let div = document.createElement('div')
div.append(textNode);
console.log(div.innerHTML);
反转义方法
let str = `&lt;script src='let doc = new DOMParser().parseFromString(str, 'text/html'); 
console.log(doc.documentElement.textContent);

 

字符串替换处理 转义与反转义

就是正则替换,这里想不讲了,能用库太多了,去看一下源码就好了

HTML常用转义字符对照表

最常用的字符实体 Character Entities

显示说明实体名称实体编号
  半方大的空白 &ensp;  
  全方大的空白 &emsp;  
  不断行的空白格 &nbsp;  
< 小于 &lt; <
> 大于 &gt; >
& &符号 &amp; &
" 双引号 &quot; "
© 版权 &copy; ©
® 已注册商标 &reg; ®
商标(美国)
× 乘号 &times; ×
÷ 除号 &divide; ÷

ISO 8859-1 (Latin-1)字符集

HTML 4.01 支持 ISO 8859-1 (Latin-1) 字符集。

备注:为了方便起见,以下表格中,“实体名称”简称为“名称”,“实体编号”简称为“编号”

显示名称编号显示名称编号显示名称编号显示名称编号显示名称编号
  &nbsp;   ¡ &iexcl; ¡ ¢ &cent; ¢ £ &pound; £ ¤ &curren; ¤
¥ &yen; ¥ ¦ &brvbar; ¦ § &sect; § ¨ &uml; ¨ © &copy; ©
ª &ordf; ª « &laquo; « ¬ &not; ¬   &shy;   ® &reg; ®
¯ &macr; ¯ ° &deg; ° ± &plusmn; ± ² &sup2; ² ³ &sup3; ³
´ &acute; ´ µ &micro; µ &para; · &middot; · ¸ &cedil; ¸
¹ &sup1; ¹ º &ordm; º » &raquo; » ¼ &frac14; ¼ ½ &frac12; ½
¾ &frac34; ¾ ¿ &iquest; ¿ À &Agrave; À Á &Aacute; Á Â &Acirc; Â
à &Atilde; Ã Ä &Auml; Ä Å &Aring; Å Æ &AElig; Æ Ç &Ccedil; Ç
È &Egrave; È É &Eacute; É Ê &Ecirc; Ê Ë &Euml; Ë Ì &Igrave; Ì
Í &Iacute; Í Î &Icirc; Î Ï &Iuml; Ï Ð &ETH; Ð Ñ &Ntilde; Ñ
Ò &Ograve; Ò Ó &Oacute; Ó Ô &Ocirc; Ô Õ &Otilde; Õ Ö &Ouml; Ö
× &times; × Ø &Oslash; Ø Ù &Ugrave; Ù Ú &Uacute; Ú Û &Ucirc; Û
Ü &Uuml; Ü Ý &Yacute; Ý Þ &THORN; Þ ß &szlig; ß à &agrave; à
á &aacute; á â &acirc; â ã &atilde; ã ä &auml; ä å &aring; å
æ &aelig; æ ç &ccedil; ç è &egrave; è é &eacute; é ê &ecirc; ê
ë &euml; ë ì &igrave; ì í &iacute; í î &icirc; î ï &iuml; ï
ð &eth; ð ñ &ntilde; ñ ò &ograve; ò ó &oacute; ó ô &ocirc; ô
õ &otilde; õ ö &ouml; ö ÷ &divide; ÷ ø &oslash; ø ù &ugrave; ù
ú &uacute; ú û &ucirc; û ü &uuml; ü ý &yacute; ý þ &thorn; þ
ÿ &yuml; ÿ                        

数学和希腊字母标志
symbols, mathematical symbols, and Greek letters

显示名称编号显示名称编号显示名称编号显示名称编号显示名称编号
ƒ &fnof; ƒ Α &Alpha; Α Β &Beta; Β Γ &Gamma; Γ Δ &Delta; Δ
Ε &Epsilon; Ε Ζ &Zeta; Ζ Η &Eta; Η Θ &Theta; Θ Ι &Iota; Ι
Κ &Kappa; Κ Λ &Lambda; Λ Μ &Mu; Μ Ν &Nu; Ν Ξ &Xi; Ξ
Ο &Omicron; Ο Π &Pi; Π Ρ &Rho; Ρ Σ &Sigma; Σ Τ &Tau; Τ
Υ &Upsilon; Υ Φ &Phi; Φ Χ &Chi; Χ Ψ &Psi; Ψ Ω &Omega; Ω
α &alpha; α β &beta; β γ &gamma; γ δ &delta; δ ε &epsilon; ε
ζ &zeta; ζ η &eta; η θ &theta; θ ι &iota; ι κ &kappa; κ
λ &lambda; λ μ &mu; μ ν &nu; ν ξ &xi; ξ ο &omicron; ο
π &pi; π ρ &rho; ρ ς &sigmaf; ς σ &sigma; σ τ &tau; τ
υ &upsilon; υ φ &phi; φ χ &chi; χ ψ &psi; ψ ω &omega; ω
? &thetasym; ϑ ? &upsih; ϒ ? &piv; ϖ &bull; &hellip;
&prime; &Prime; &oline; &frasl; &weierp;
&image; &real; &trade; &alefsym; &larr;
&uarr; &rarr; &darr; &harr; &crarr;
&lArr; &uArr; &rArr; &dArr; &hArr;
&forall; &part; &exist; &empty; &nabla;
&isin; &notin; &ni; &prod; &sum;
&minus; &lowast; &radic; &prop; &infin;
&ang; &and; &or; &cap; &cup;
&int; &there4; &sim; &cong; &asymp;
&ne; &equiv; &le; &ge; &sub;
&sup; &nsub; &sube; &supe; &oplus;
&otimes; &perp; &sdot; ? &lceil; ? &rceil;
? &lfloor; ? &rfloor; ? &lang; ? &rang; &loz;
&spades; &clubs; &hearts; &diams;      

重要的国际标记
markup-significant and internationalization characters

显示名称编号显示名称编号显示名称编号显示名称编号显示名称编号
" &quot; " & &amp; & < &lt; < > &gt; > Œ &OElig; Œ
œ &oelig; œ Š &Scaron; Š š &scaron; š Ÿ &Yuml; Ÿ ˆ &circ; ˆ
˜ &tilde; ˜   &ensp;     &emsp;     &thinsp;   &zwnj;
&zwj; &lrm; &rlm; &ndash; &mdash;
&lsquo; &rsquo; &sbquo; &ldquo; &rdquo;
&bdquo; &dagger; &Dagger; &permil; &lsaquo;
&rsaquo; &euro;                  

JavaScript转义符

转义序列字符
\b 退格
\f 走纸换页
\n 换行
\r 回车
\t 横向跳格 (Ctrl-I)
\' 单引号
\" 双引号
\\ 反斜杠

富文本通用转义字符

字符

十进制

转义字符

描述

@

&#64

&commat

at 符号

©

&#169

&copy

版权符号

!

&#33

&excl

感叹号

¡

&#161

&iexcl

倒置感叹号

:

&#58

&colon

冒号

,

&#44

&comma

逗号

·

&#183

&middot

中间点

&#39

&apos

撇号

&#8216

&lsquo

左单引号

&#8220

&ldquo

左双引号

(

&#40

&lpar

左括号

[

&#91

&lsqb

左方括号

{

&#123

&lcub

左花括号

§

&#167

&sect

分节符号

_

&#95

&lowbar

单下划线

|

&#124

&verbar

竖线

&#8214

&Verbar

双竖线

&#8736

&ang

#

&#35

&num

数字标志

$

&#36

&dollar

美元符号

%

&#37

&percnt

百分号

&#8240

&permil

千分号

&#8241

pertenk

万分号

*

&#42

&ast

星号

/

&#47

&sol

斜线符号

+

&#43

&add

加号

×

&#215

&times

乘号

=

&#61

&equal

等于号

&#8776

&asymp

约等于

<

&#60

&lt

小于号

&#8804

&le

小于等于

¹

&#185

&sup1

上标 1

²

&#178

&sup2

上标 2

³

&#179

&sup3

上标 3

&

&#38

&amp

and 符号

®

&#174

&reg

注册商标符

?

&#63

&quest

问号

¿

&#191

&iquest

倒置问号

;

&#59

&semi

分号

.

&#46

&period

句号

&#8226

&bull

加重号

&#34

&quot

引号

&#8217

&rsquo

右单引号

&#8221

&rdquo

右双引号

)

&#41

&rpar

右括号

]

&#93

&rsqb

右方括号

}

&#125

&rcub

右花括号

&#182

&para

段落符号

__

&#818

&UnderBar

双下划线

¦

&#166

&brvbar

间断竖线

&#8230

&hellip

三点省略号

^

&#94

&Hat

hat符号

π

&#961

&pi

圆周率

¥

&#165

&yen

元符号

°

&#176

&deg

度符号

&#8730

&radic

平方根

&#8734

&infin

正无穷

±

&#177

&plusmn

加减符号

 

&#92

&bsol

反斜线符号

&#8211

&ndash

减号

÷

&#247

&divide

除号

&#8800

&ne

不等于

&#8801

&equiv

相当于

>

&#62

&gt

大于号

&#8805

&ge

大于等于

¼

&#188

&frac14

四分之一

½

&#189

&frac12

二分之一

¾

&#190

&frac34

四分之三

 

HTML特殊转义字符对照表

HTML特殊转义字符对照表
字符十进制转义字符字符十进制转义字符字符十进制转义字符
? &#161; &iexcl; Á &#193; &Aacute; á &#225; &aacute;
&#162; &cent; Â &#194; &circ; â &#226 &acirc;
&#163; &pound; Ã &#195; &Atilde; ã &#227; &atilde;
¤ &#164; &curren; Ä &#196; &Auml ä &#228; &auml;
&#165; &yen; Å &#197; &ring; å &#229; &aring;
| &#166; &brvbar; Æ &#198; &AElig; æ &#230; &aelig;
§ &#167; &sect; Ç &#199; &Ccedil; ç &#231; &ccedil;
¨ &#168; &uml; È &#200; &Egrave; è &#232; &egrave;
© &#169; &copy; É &#201; &Eacute; é &#233; &eacute;
a &#170; &ordf; Ê &#202; &Ecirc; ê &#234; &ecirc;
? &#171; &laquo; Ë &#203; &Euml; ë &#235; &euml;
? &#172; &not; Ì &#204; &Igrave; ì &#236; &igrave;
/x7f &#173; &shy; Í &#205; &Iacute; í &#237; &iacute;
® &#174; &reg; Î &#206; &Icirc; î &#238; &icirc;
ˉ &#175; &macr; Ï &#207; &Iuml; ï &#239; &iuml;
° &#176; &deg; Ð &#208; &ETH; ð &#240; &ieth;
± &#177; &plusmn; Ñ &#209; &Ntilde; ñ &#241; &ntilde;
2 &#178; &sup2; Ò &#210; &Ograve; ò &#242; &ograve;
3 &#179; &sup3; Ó &#211; &Oacute; ó &#243; &oacute;
&#180; &acute; Ô &#212; &Ocirc; ô &#244; &ocirc;
μ &#181; &micro; Õ &#213; &Otilde; õ &#245; &otilde;
? &#182; &para; Ö &#214; &Ouml; ö &#246; &ouml;
· &#183; &middot; &times; &#215; &times; ÷ &#247; &divide;
? &#184; &cedil; Ø &#216; &Oslash; ø &#248; &oslash;
1 &#185; &sup1; Ù &#217; &Ugrave; ù &#249; &ugrave;
o &#186; &ordm; Ú &#218; &Uacute; ú &#250; &uacute;
? &#187; &raquo; Û &#219; &Ucirc; û &#251; &ucirc;
? &#188; &frac14; Ü &#220; &Uuml; ü &#252; &uuml;
? &#189; &frac12; Ý &#221; &Yacute; ý &#253; &yacute;
? &#190; &frac34; Þ &#222; &THORN; þ &#254; &thorn;
? &#191; &iquest; ß &#223; &szlig; ÿ &#255; &yuml;
À &#192; &Agrave; à &#224; &agrave;      

 

 

参考文章:

巧用DOM API实现HTML字符的转义和反转义 https://www.zhangxinxu.com/wordpress/2021/01/dom-api-html-encode-decode/

java转换 HTML字符实体,java特殊字符转义字符串 https://blog.51cto.com/xionggeclub/3768494

 


转载本站文章《HTML转义字符:xss攻击与HTML字符的转义和反转义》,
请注明出处:https://www.zhoulujun.cn/html/webfront/SGML/htmlBase/2022_0617_8846.html

posted @ 2022-06-25 16:41  zhoulujun  阅读(969)  评论(0编辑  收藏  举报