{"id":54006,"date":"2024-01-26T02:01:11","date_gmt":"2024-01-25T18:01:11","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=54006"},"modified":"2024-01-26T02:01:11","modified_gmt":"2024-01-25T18:01:11","slug":"fail2ban-ssh-crack","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/fail2ban-ssh-crack.html","title":{"rendered":"\u4f7f\u7528 fail2ban \u9632\u5fa1 SSH \u670d\u52a1\u5668\u7684\u66b4\u529b\u7834\u89e3"},"content":{"rendered":"
\u5bf9\u4e8eSSH\u670d\u52a1\u7684\u5e38\u89c1\u7684\u653b\u51fb\u5c31\u662f\u66b4\u529b\u7834\u89e3\u653b\u51fb\u2014\u2014\u8fdc\u7a0b\u653b\u51fb\u8005\u901a\u8fc7\u4e0d\u540c\u7684\u5bc6\u7801\u6765\u65e0\u9650\u6b21\u5730\u8fdb\u884c\u767b\u5f55\u5c1d\u8bd5\u3002\u5f53\u7136SSH\u53ef\u4ee5\u8bbe\u7f6e\u4f7f\u7528\u975e\u5bc6\u7801\u9a8c\u8bc1\u9a8c\u8bc1\u65b9\u5f0f\u6765\u5bf9\u6297\u8fd9\u79cd\u653b\u51fb\uff0c\u4f8b\u5982\u516c\u94a5\u9a8c\u8bc1<\/a>\u6216\u8005\u53cc\u91cd\u9a8c\u8bc1<\/a>\u3002\u5c06\u4e0d\u540c\u7684\u9a8c\u8bc1\u65b9\u6cd5\u7684\u4f18\u52a3\u5904\u5148\u653e\u5728\u4e00\u8fb9\uff0c\u5982\u679c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u5bc6\u7801\u9a8c\u8bc1\u65b9\u5f0f\u600e\u4e48\u529e\uff1f\u4f60\u662f\u5982\u4f55\u4fdd\u62a4\u4f60\u7684 SSH \u670d\u52a1\u5668\u514d\u906d\u66b4\u529b\u7834\u89e3\u653b\u51fb\u7684\u5462\uff1f<\/p>\n <\/p>\n fail2ban<\/a> \u662f Linux \u4e0a\u7684\u4e00\u4e2a\u8457\u540d\u7684\u5165\u4fb5\u4fdd\u62a4\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5b83\u4f1a\u76d1\u63a7\u591a\u4e2a\u7cfb\u7edf\u7684\u65e5\u5fd7\u6587\u4ef6\uff08\u4f8b\u5982\uff1a\/var\/log\/auth.log \u6216\u8005 \/var\/log\/secure\uff09\u5e76\u6839\u636e\u68c0\u6d4b\u5230\u7684\u4efb\u4f55\u53ef\u7591\u7684\u884c\u4e3a\u81ea\u52a8\u89e6\u53d1\u4e0d\u540c\u7684\u9632\u5fa1\u52a8\u4f5c\u3002\u4e8b\u5b9e\u4e0a\uff0cfail2ban \u5728\u9632\u5fa1\u5bf9SSH\u670d\u52a1\u5668\u7684\u66b4\u529b\u5bc6\u7801\u7834\u89e3\u4e0a\u975e\u5e38\u6709\u7528\u3002<\/p>\n \u5728\u8fd9\u7bc7\u6307\u5bfc\u6559\u7a0b\u4e2d\uff0c\u6211\u4f1a\u6f14\u793a\u5982\u4f55\u5b89\u88c5\u5e76\u914d\u7f6e fail2ban \u6765\u4fdd\u62a4 SSH \u670d\u52a1\u5668\u4ee5\u907f\u514d\u6765\u81ea\u8fdc\u7a0bIP\u5730\u5740\u7684\u66b4\u529b\u653b\u51fb<\/strong>\u3002<\/p>\n \u4e3a\u4e86\u5728CentOS \u6216 RHEL\u4e0a\u5b89\u88c5fail2ban,\u9996\u5148\u8bbe\u7f6eEPEL\u4ed3\u5e93<\/a>\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n \u5728Fedora\u4e0a\u5b89\u88c5fail2ban\uff0c\u7b80\u5355\u5730\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n \u5728ubuntu\uff0cDebian \u6216 Linux Mint\u4e0a\u5b89\u88c5fail2ban\uff1a<\/p>\n \u73b0\u5728\u4f60\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u901a\u8fc7\u914d\u7f6e fail2ban \u6765\u52a0\u5f3a\u4f60\u7684SSH\u670d\u52a1\u5668\u3002\u4f60\u9700\u8981\u7f16\u8f91\u5176\u914d\u7f6e\u6587\u4ef6 \/etc\/fail2ban\/jail.conf\u3002 \u5728\u914d\u7f6e\u6587\u4ef6\u7684\u201c[DEFAULT]\u201d\u533a\uff0c\u4f60\u53ef\u4ee5\u5728\u6b64\u5b9a\u4e49\u6240\u6709\u53d7\u76d1\u63a7\u7684\u670d\u52a1\u7684\u9ed8\u8ba4\u53c2\u6570\uff0c\u53e6\u5916\u5728\u7279\u5b9a\u670d\u52a1\u7684\u914d\u7f6e\u90e8\u5206\uff0c\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u670d\u52a1\uff08\u4f8b\u5982SSH\uff0cApache\u7b49\uff09\u8bbe\u7f6e\u7279\u5b9a\u7684\u914d\u7f6e\u6765\u8986\u76d6\u9ed8\u8ba4\u7684\u53c2\u6570\u914d\u7f6e\u3002<\/p>\n \u5728\u9488\u5bf9\u670d\u52a1\u7684\u76d1\u72f1\u533a\uff08\u5728[DEFAULT]\u533a\u540e\u9762\u7684\u5730\u65b9\uff09\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a[ssh-iptables]\u533a\uff0c\u8fd9\u91cc\u7528\u6765\u5b9a\u4e49SSH\u76f8\u5173\u7684\u76d1\u72f1\u914d\u7f6e\u3002\u771f\u6b63\u7684\u7981\u6b62IP\u5730\u5740\u7684\u64cd\u4f5c\u662f\u901a\u8fc7iptables\u5b8c\u6210\u7684\u3002<\/p>\n \u4e0b\u9762\u662f\u4e00\u4e2a\u5305\u542b\u201cssh-iptables\u201d\u76d1\u72f1\u914d\u7f6e\u7684\/etc\/fail2ban\/jail.conf\u7684\u6587\u4ef6\u6837\u4f8b\u3002\u5f53\u7136\u6839\u636e\u4f60\u7684\u9700\u8981\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u5176\u4ed6\u7684\u5e94\u7528\u76d1\u72f1\u3002<\/p>\n \u6839\u636e\u4e0a\u8ff0\u914d\u7f6e\uff0cfail2ban\u4f1a\u81ea\u52a8\u7981\u6b62\u5728\u6700\u8fd110\u5206\u949f\u5185\u6709\u8d85\u8fc73\u6b21\u8bbf\u95ee\u5c1d\u8bd5\u5931\u8d25\u7684\u4efb\u610fIP\u5730\u5740\u3002\u4e00\u65e6\u88ab\u7981\uff0c\u8fd9\u4e2aIP\u5730\u5740\u5c06\u4f1a\u572824\u5c0f\u65f6\u5185\u4e00\u76f4\u88ab\u7981\u6b62\u8bbf\u95ee SSH \u670d\u52a1\u3002\u8fd9\u4e2a\u4e8b\u4ef6\u4e5f\u4f1a\u901a\u8fc7sendemail\u53d1\u9001\u90ae\u4ef6\u901a\u77e5\u3002<\/p>\n \u4e00\u65e6\u914d\u7f6e\u6587\u4ef6\u51c6\u5907\u5c31\u7eea\uff0c\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u91cd\u542ffail2ban\u670d\u52a1\u3002<\/p>\n \u5728 Debian, Ubuntu \u6216 CentOS\/RHEL 6:<\/p>\n \u5728 Fedora \u6216 CentOS\/RHEL 7:<\/p>\n \u4e3a\u4e86\u9a8c\u8bc1fail2ban\u6210\u529f\u8fd0\u884c\uff0c\u4f7f\u7528\u53c2\u6570'ping'\u6765\u8fd0\u884cfail2ban-client \u547d\u4ee4\u3002 \u5982\u679cfail2ban\u670d\u52a1\u6b63\u5e38\u8fd0\u884c\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u201cpong\uff08\u562d\uff09\u201d\u4f5c\u4e3a\u54cd\u5e94\u3002<\/p>\n \u4e3a\u4e86\u6d4b\u8bd5fail2ban\u662f\u5426\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u5c1d\u8bd5\u901a\u8fc7\u4f7f\u7528\u9519\u8bef\u7684\u5bc6\u7801\u6765\u7528SSH\u8fde\u63a5\u5230\u670d\u52a1\u5668\u6a21\u62df\u4e00\u4e2a\u66b4\u529b\u7834\u89e3\u653b\u51fb\u3002\u4e0e\u6b64\u540c\u65f6\uff0c\u76d1\u63a7 \/var\/log\/fail2ban.log\uff0c\u8be5\u6587\u4ef6\u8bb0\u5f55\u5728fail2ban\u4e2d\u53d1\u751f\u7684\u4efb\u4f55\u654f\u611f\u4e8b\u4ef6\u3002<\/p>\n <\/p>\n \u6839\u636e\u4e0a\u8ff0\u7684\u65e5\u5fd7\u6587\u4ef6\uff0cFail2ban\u901a\u8fc7\u68c0\u6d4bIP\u5730\u5740\u7684\u591a\u6b21\u5931\u8d25\u767b\u5f55\u5c1d\u8bd5\uff0c\u7981\u6b62\u4e86\u4e00\u4e2aIP\u5730\u5740192.168.1.8\u3002<\/p>\n \u7531\u4e8efail2ban\u7684\u201cssh-iptables\u201d\u76d1\u72f1\u4f7f\u7528iptables\u6765\u963b\u585e\u95ee\u9898IP\u5730\u5740\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u68c0\u6d4b\u5f53\u524diptables\u6765\u9a8c\u8bc1\u7981\u6b62\u89c4\u5219\u3002<\/p>\n \u5982\u679c\u4f60\u60f3\u8981\u4ecefail2ban\u4e2d\u89e3\u9501\u67d0\u4e2aIP\u5730\u5740\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528iptables\u547d\u4ee4\uff1a<\/p>\n \u5f53\u7136\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u7684iptables\u547d\u4ee4\u624b\u52a8\u5730\u68c0\u9a8c\u548c\u7ba1\u7406fail2ban\u7684IP\u963b\u585e\u5217\u8868\uff0c\u4f46\u5b9e\u9645\u4e0a\u6709\u4e00\u4e2a\u9002\u5f53\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528fail2ban-client\u547d\u4ee4\u884c\u5de5\u5177\u3002\u8fd9\u4e2a\u547d\u4ee4\u4e0d\u4ec5\u5141\u8bb8\u4f60\u5bf9\"ssh-iptables\"\u76d1\u72f1\u8fdb\u884c\u7ba1\u7406\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u547d\u4ee4\u884c\u63a5\u53e3\uff0c\u53ef\u4ee5\u7ba1\u7406\u5176\u4ed6\u7c7b\u578b\u7684fail2ban\u76d1\u72f1\u3002<\/p>\n \u4e3a\u4e86\u68c0\u9a8cfail2ban\u72b6\u6001\uff08\u4f1a\u663e\u793a\u51fa\u5f53\u524d\u6d3b\u52a8\u7684\u76d1\u72f1\u5217\u8868\uff09\uff1a<\/p>\n \u4e3a\u4e86\u68c0\u9a8c\u4e00\u4e2a\u7279\u5b9a\u76d1\u72f1\u7684\u72b6\u6001\uff08\u4f8b\u5982ssh-iptables):<\/p>\n \u4e0a\u9762\u7684\u547d\u4ee4\u4f1a\u663e\u793a\u51fa\u88ab\u7981\u6b62IP\u5730\u5740\u5217\u8868\u3002<\/p>\n <\/p>\n \u4e3a\u4e86\u89e3\u9501\u7279\u5b9a\u7684IP\u5730\u5740\uff1a<\/p>\n <\/p>\n \u6ce8\u610f\uff0c\u5982\u679c\u4f60\u505c\u6b62\u4e86Fail2ban \u670d\u52a1\uff0c\u90a3\u4e48\u6240\u6709\u7684IP\u5730\u5740\u90fd\u4f1a\u88ab\u89e3\u9501\u3002\u5f53\u4f60\u91cd\u542f Fail2ban\uff0c\u5b83\u4f1a\u4ece\/etc\/log\/secure(\u6216 \/var\/log\/auth.log)\u4e2d\u627e\u5230\u5f02\u5e38\u7684IP\u5730\u5740\u5217\u8868\uff0c\u5982\u679c\u8fd9\u4e9b\u5f02\u5e38\u5730\u5740\u7684\u53d1\u751f\u65f6\u95f4\u4ecd\u7136\u5728\u7981\u6b62\u65f6\u95f4\u5185\uff0c\u90a3\u4e48Fail2ban\u4f1a\u91cd\u65b0\u5c06\u8fd9\u4e9bIP\u5730\u5740\u7981\u6b62\u3002<\/p>\n \u4e00\u65e6\u4f60\u6210\u529f\u5730\u6d4b\u8bd5\u4e86fail2ban\u4e4b\u540e\uff0c\u6700\u540e\u4e00\u4e2a\u6b65\u9aa4\u5c31\u662f\u5728\u4f60\u7684\u670d\u52a1\u5668\u4e0a\u8ba9\u5176\u5728\u5f00\u673a\u65f6\u81ea\u52a8\u542f\u52a8\u3002\u5728\u57fa\u4e8eDebian\u7684\u53d1\u884c\u7248\u4e2d\uff0cfail2ban\u5df2\u7ecf\u9ed8\u8ba4\u8ba9\u81ea\u52a8\u542f\u52a8\u751f\u6548\u3002\u5728\u57fa\u4e8eRed-Hat\u7684\u53d1\u884c\u7248\u4e2d\uff0c\u6309\u7167\u4e0b\u9762\u7684\u65b9\u5f0f\u8ba9\u81ea\u52a8\u542f\u52a8\u751f\u6548\u3002<\/p>\n \u5728 CentOS\/RHEL 6\u4e2d:<\/p>\n \u5728 Fedora \u6216 CentOS\/RHEL 7:<\/p>\n \u5728\u8be5\u6559\u7a0b\u4e2d\uff0c\u6211\u6f14\u793a\u4e86\u5982\u4f55\u5b89\u88c5\u5e76\u914d\u7f6efail2ban\u6765\u4fdd\u62a4\u4e00\u4e2aSSH\u670d\u52a1\u5668\u3002\u5f53\u7136fail2ban\u53ef\u4ee5\u7f13\u89e3\u66b4\u529b\u5bc6\u7801\u653b\u51fb\uff0c\u4f46\u662f\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u80fd\u4fdd\u62a4SSH\u670d\u52a1\u5668\u907f\u514d\u6765\u81ea\u590d\u6742\u7684\u5206\u5e03\u5f0f\u66b4\u529b\u7834\u89e3\u7ec4\u7ec7\uff0c\u8fd9\u4e9b\u653b\u51fb\u8005\u901a\u8fc7\u4f7f\u7528\u6210\u5343\u4e0a\u4e07\u4e2a\u673a\u5668\u63a7\u5236\u7684IP\u5730\u5740\u6765\u7ed5\u8fc7fail2ban\u7684\u9632\u5fa1\u673a\u5236\u3002<\/p>\n via: http:\/\/xmodulo.com\/how-to-protect-ssh-server-from-brute-force-attacks-using-fail2ban.html<\/a><\/p>\n \u4f5c\u8005\uff1aDan Nanni<\/a> \u8bd1\u8005\uff1atheo-l<\/a> \u6821\u5bf9\uff1awxy<\/a><\/p>\n \u672c\u6587\u7531 LCTT<\/a> \u539f\u521b\u7ffb\u8bd1\uff0cLinux\u4e2d\u56fd<\/a> \u8363\u8a89\u63a8\u51fa<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-5067-1.html<\/a><\/p>\n$ sudo yum install fail2ban\r\n<\/pre>\n
$ sudo yum install fail2ban\r\n<\/pre>\n
$ sudo apt-get install fail2ban\r\n<\/pre>\n
$ sudo vi \/etc\/fail2ban\/jail.local\r\n<\/pre>\n
[DEFAULT]\r\n# \u4ee5\u7a7a\u683c\u5206\u9694\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u662f IP \u5730\u5740\u3001CIDR \u524d\u7f00\u6216\u8005 DNS \u4e3b\u673a\u540d\r\n# \u7528\u4e8e\u6307\u5b9a\u54ea\u4e9b\u5730\u5740\u53ef\u4ee5\u5ffd\u7565 fail2ban \u9632\u5fa1\r\nignoreip = 127.0.0.1 172.31.0.0\/24 10.10.0.0\/24 192.168.0.0\/24\r\n\r\n# \u5ba2\u6237\u7aef\u4e3b\u673a\u88ab\u7981\u6b62\u7684\u65f6\u957f\uff08\u79d2\uff09\r\nbantime = 86400\r\n\r\n# \u5ba2\u6237\u7aef\u4e3b\u673a\u88ab\u7981\u6b62\u524d\u5141\u8bb8\u5931\u8d25\u7684\u6b21\u6570 \r\nmaxretry = 5\r\n\r\n# \u67e5\u627e\u5931\u8d25\u6b21\u6570\u7684\u65f6\u957f\uff08\u79d2\uff09\r\nfindtime = 600\r\n\r\nmta = sendmail\r\n\r\n[ssh-iptables]\r\nenabled = true\r\nfilter = sshd\r\naction = iptables[name=SSH, port=ssh, protocol=tcp]\r\nsendmail-whois[name=SSH, dest=your@email.com, sender=fail2ban@email.com]\r\n# Debian \u7cfb\u7684\u53d1\u884c\u7248 \r\nlogpath = \/var\/log\/auth.log\r\n# Red Hat \u7cfb\u7684\u53d1\u884c\u7248\r\nlogpath = \/var\/log\/secure\r\n# ssh \u670d\u52a1\u7684\u6700\u5927\u5c1d\u8bd5\u6b21\u6570 \r\nmaxretry = 3\r\n<\/pre>\n
$ sudo service fail2ban restart\r\n<\/pre>\n
$ sudo systemctl restart fail2ban\r\n<\/pre>\n
$ sudo fail2ban-client ping\r\nServer replied: pong\r\n<\/pre>\n
$ sudo tail -f \/var\/log\/fail2ban.log\r\n<\/pre>\n
$ sudo iptables --list -n\r\n<\/pre>\n
Chain INPUT (policy ACCEPT)\r\ntarget prot opt source destination\r\nfail2ban-SSH tcp -- 0.0.0.0\/0 0.0.0.0\/0 tcp dpt:22\r\n\r\nChain FORWARD (policy ACCEPT)\r\ntarget prot opt source destination\r\n\r\nChain OUTPUT (policy ACCEPT)\r\ntarget prot opt source destination\r\n\r\nChain fail2ban-SSH (1 references)\r\ntarget prot opt source destination\r\nDROP all -- 192.168.1.8 0.0.0.0\/0\r\nRETURN all -- 0.0.0.0\/0 0.0.0.0\/0\r\n<\/pre>\n
$ sudo iptables -D fail2ban-SSH -s 192.168.1.8 -j DROP\r\n<\/pre>\n
$ sudo fail2ban-client status\r\n<\/pre>\n
$ sudo fail2ban-client status ssh-iptables\r\n<\/pre>\n
$ sudo fail2ban-client set ssh-iptables unbanip 192.168.1.8\r\n<\/pre>\n
$ sudo chkconfig fail2ban on\r\n<\/pre>\n
$ sudo systemctl enable fail2ban\r\n<\/pre>\n
\n\n