javascript - 如何正确阻止 Node.JS 服务器应用程序中的 IP 地址?

标签 javascript node.js security networking

假设我有一个 Node.JS 服务器应用程序,我想阻止连续 3 次登录失败的用户,正确的方法是什么?
我应该在服务器应用程序中处理阻塞部分,基本上是在用户连接但尚未登录之后,还是有一些较低级别的阶段我应该这样做,所以它甚至没有到达我的 Node.JS应用程序?

最佳答案

在你做之前
您应该考虑在以下情况下会发生什么:

  • 两个同事在同一个办公室工作,拥有相同的 IP 地址。坏同事想从不同的PC登录到他同事的帐户,连续3次登录失败。现在帐户所有者想要登录,在这种情况下您会怎么做?
  • 父亲和儿子在家,IP地址相同,儿子想从不同的PC登录父亲的账户,连续3次登录失败。现在父亲想要登录,在这种情况下你会怎么做?

  • 有些用户有静态 IP 地址(他们无法更改)。
    解决方案
    正因为如此,我会阻止帐户和 IP 地址,然后我会向帐户所有者发送一封电子邮件,其中包含一个链接,该链接将解除对帐户的封锁,而这个 没有 更改密码的要求。帐户所有者还应回答有关解除 IP 地址封锁的问题,因为他知道自己的 IP 地址。
    怎么做(阻止IP地址)
    更可取的解决方案是在较低级别阻止 IP 地址 - 在它到达 Node.JS 之前,因为所有网络都发生在操作系统内核中,并且内核可以比 Node.JS 更有效、更快地在连接中阻止它它。
    但首先要回答你的问题...
    如何使用 Node.JS 阻止它
    var blackList =
    [
        '77.88.99.1',
        '88.77.99.1'
    ];
    
    var http = require('http');
    var server = http.createServer(function(req, res)
    {
        var ip = req.ip 
                || req.connection.remoteAddress 
                || req.socket.remoteAddress 
                || req.connection.socket.remoteAddress;
    
        if(blackList.indexOf(ip) > -1)
        {
            res.end(); // exit if it is a black listed ip
        }
    
    }).listen(80, '127.0.0.1');
    
    和 Node.js http.Server有一个 connection 事件。你也可以这样做:
    server.on('connection', function(socket)
    {
        // console.log(socket.remoteAddress);
        // Put your logic here
    });
    
    如何用 Linux 阻止它(例如)

    Sometime it is necessary to block incoming connection or traffic from specific remote host. iptables is administration tool for IPv4 packet filtering and NAT under Linux kernel. Following tip will help you to block attacker or spammers IP address.

    How do I block specific incoming ip address?

    Following iptable rule will drop incoming connection from host/IP 202.54.20.22:

    iptables -A INPUT -s 202.54.20.22 -j DROP
    iptables -A OUTPUT -d 202.54.20.22 -j DROP
    

    A simple shell script to block lots of IP address

    If you have lots of IP address use the following shell script:

    A) Create a text file:

    # vi /root/ip.blocked
    

    Now append IP address:

    # Ip address block  file
    202.54.20.22
    202.54.20.1/24
    #65.66.36.87
    

    B) Create a script as follows or add following script line to existing iptables shell script:

    BLOCKDB="/root/ip.blocked"
    IPS=$(grep -Ev "^#" $BLOCKDB)
    for i in $IPS
    do
    iptables -A INPUT -s $i -j DROP
    iptables -A OUTPUT -d $i -j DROP
    done
    

    C) Save and close the file.


    © Source Linux Iptables block incoming access to selected or specific ip address

    关于javascript - 如何正确阻止 Node.JS 服务器应用程序中的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63455999/

    相关文章:

    node.js - Nodejs twitter api 不返回预期的 token

    php - 终极清洁/安全功能

    c# - Javascript 从 Asp.net GridView 取消选中复选框

    javascript - Codecademy - 我的代码返回 true,但它说我错过了其他东西?

    node.js - Docker 中的远程调试 Node 9

    node.js - 在 Node 中连接回调之外的监听上访问套接字

    MySQL 版本 : 5. 5.36 易受攻击?

    javascript - 保护 Node js 运行时或禁用调试/检查

    javascript - 如何从 JavaScript 生成 Excel xlsx 文件

    javascript - 在单击的文本元素上绘制箭头?