」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 防止 JavaScript 應用程式中的中間人 (MitM) 攻擊的步驟

防止 JavaScript 應用程式中的中間人 (MitM) 攻擊的步驟

發佈於2024-08-01
瀏覽:574

Steps to Preventing Man-in-the-Middle (MitM) Attacks in JavaScript Applications

中间人 (MitM) 攻击对网络安全构成重大威胁。在这些攻击中,恶意行为者拦截客户端和服务器之间的通信,从而允许他们窃听、操纵或窃取数据。本博客将探讨 MitM 攻击如何在 JavaScript 应用程序上下文中发挥作用,并提供实际步骤来保护您的应用程序免受这些威胁。

什么是中间人攻击?

当攻击者秘密拦截并中继相信彼此直接通信的两方之间的消息时,就会发生中间人攻击。这种拦截可能会导致未经授权访问敏感数据,例如登录凭据、财务信息和个人详细信息。

中间人攻击如何运作

MitM 攻击可以通过多种方式执行,包括:

1。 DNS 欺骗: DNS 欺骗涉及更改 DNS 记录以将用户重定向到恶意网站。

例子:
假设您已在 xyz.com 上部署了 Node.js 和 React 应用程序。黑客可以操纵 DNS 记录,以便当用户尝试访问 xyz.com 时,他们会被重定向到与您的网站看起来相同的恶意网站。

防止 DNS 欺骗的步骤:

  • 使用 DNSSEC(域名系统安全扩展)添加额外的安全层。
  • 定期监控和更新您的 DNS 记录。
  • 使用信誉良好的 DNS 提供商,提供针对 DNS 欺骗的安全功能。
# Example of enabling DNSSEC on your domain using Cloudflare
# Visit your domain's DNS settings on Cloudflare
# Enable DNSSEC with a single click

2. IP 欺骗
IP 欺骗涉及冒充受信任的 IP 地址来拦截网络流量。

例子:
攻击者可能会欺骗您服务器的 IP 地址来拦截您的客户端和 Node.js 服务器之间的流量。

防止 IP 欺骗的步骤:

  • 实施 IP 白名单,仅允许受信任的 IP 地址与您的服务器通信。
  • 使用网络级安全措施,例如 VPN 和防火墙。
  • 确保服务器上的 IP 地址正确验证和过滤。
// Example of IP whitelisting in Express.js
const express = require('express');
const app = express();

const allowedIPs = ['123.45.67.89']; // Replace with your trusted IPs

app.use((req, res, next) => {
  const clientIP = req.ip;
  if (!allowedIPs.includes(clientIP)) {
    return res.status(403).send('Forbidden');
  }
  next();
});

// Your routes here

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

3. HTTPS 欺骗
HTTPS 欺骗涉及创建虚假 SSL 证书来冒充安全网站。

例子:
攻击者可以为 xyz.com 创建伪造的 SSL 证书,并设置一个看起来与您的合法服务器相同的恶意服务器。

防止 HTTPS 欺骗的步骤:

  • 使用证书透明度来监控和记录为您的域颁发的所有证书。
  • 实施 HTTP 公钥固定 (HPKP),将 Web 服务器的加密公钥与一组特定的 HTTPS 网站关联起来。
// Example of implementing HPKP in Express.js
const helmet = require('helmet');
const app = express();

app.use(helmet.hpkp({
  maxAge: 60 * 60 * 24 * 90, // 90 days
  sha256s: ['yourPublicKeyHash1', 'yourPublicKeyHash2'], // Replace with your public key hashes
  includeSubDomains: true
}));

// Your routes here

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

4。 Wi-Fi 窃听
Wi-Fi 窃听涉及拦截通过不安全的 Wi-Fi 网络传输的数据。

例子:
黑客可能会设置恶意 Wi-Fi 热点,并在用户连接到您的服务器时拦截用户和您的服务器之间传输的数据。

防止 Wi-Fi 窃听的步骤:

  • 鼓励用户仅连接到安全的 Wi-Fi 网络。
  • 实施端到端加密(E2EE)以保护客户端和服务器之间传输的数据。
  • 使用 VPN 加密客户端和服务器之间的流量。
// Example of enforcing HTTPS in Express.js
const express = require('express');
const app = express();

app.use((req, res, next) => {
  if (req.headers['x-forwarded-proto'] !== 'https') {
    return res.redirect(['https://', req.get('Host'), req.url].join(''));
  }
  next();
});

// Your routes here

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

防止 JavaScript 应用程序中的中间人攻击

1。到处使用 HTTPS
确保客户端和服务器之间的所有通信均使用 HTTPS 加密。使用 Let's Encrypt 等工具获取免费的 SSL/TLS 证书。

// Enforce HTTPS in Express.js
const express = require('express');
const app = express();

app.use((req, res, next) => {
  if (req.headers['x-forwarded-proto'] !== 'https') {
    return res.redirect(['https://', req.get('Host'), req.url].join(''));
  }
  next();
});

// Your routes here

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

2.验证 SSL/TLS 证书
对 SSL/TLS 证书使用强验证并避免在生产中使用自签名证书。

3.实施内容安全策略 (CSP)
使用 CSP 标头来限制应用程序可以加载资源的来源,从而降低恶意脚本注入的风险。

// Setting CSP headers in Express.js
const helmet = require('helmet');
app.use(helmet.contentSecurityPolicy({
  directives: {
    defaultSrc: ["'self'"],
    scriptSrc: ["'self'", 'trusted.com'],
    styleSrc: ["'self'", 'trusted.com']
  }
}));

4。使用安全 Cookie
确保 cookie 被标记为 Secure 和 HttpOnly,以防止通过客户端脚本访问它们。

// Setting secure cookies in Express.js
app.use(require('cookie-parser')());
app.use((req, res, next) => {
  res.cookie('session', 'token', { secure: true, httpOnly: true });
  next();
});

5。实施 HSTS(HTTP 严格传输安全)
使用 HSTS 强制浏览器仅通过 HTTPS 与您的服务器通信。

// Setting HSTS headers in Express.js
const helmet = require('helmet');
app.use(helmet.hsts({
  maxAge: 31536000, // 1 year
  includeSubDomains: true,
  preload: true
}));

中间人攻击可能会给 Web 应用程序带来毁灭性后果,导致数据盗窃和注入攻击。通过了解这些攻击的工作原理并实施强大的安全措施,您可以保护您的 JavaScript 应用程序并确保用户数据的安全。始终使用 HTTPS、验证 SSL/TLS 证书、实施 CSP、安全 cookie 并强制实施 HSTS 以减轻 MitM 攻击的风险。

版本聲明 本文轉載於:https://dev.to/rigalpatel001/steps-to-preventing-man-in-the-middle-mitm-attacks-in-javascript-applications-3ah4?1如有侵犯,請聯絡study_golang@163 .com刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3