{"id":212504,"date":"2021-03-02T08:32:03","date_gmt":"2021-03-02T00:32:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=212504"},"modified":"2021-02-26T10:33:34","modified_gmt":"2021-02-26T02:33:34","slug":"iptables-record-log","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/iptables-record-log.html","title":{"rendered":"iptables\u9632\u706b\u5899\u5982\u4f55\u8bb0\u5f55\u65e5\u5fd7"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f53\u4f60\u7684iptables\u89c4\u5219\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u65f6\uff0c\u53ef\u80fd\u5e0c\u671b\u8bb0\u5f55iptables\u7684\u6570\u636e\u5305\u4ee5\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528iptables\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5757\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4f8b\u5982\uff1a\u8bb0\u5f55\u6240\u6709ssh\u670d\u52a1\u7684\u767b\u5f55\u7684\u65e5\u5fd7<\/strong><\/div>\n

\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5982\u4f55\u5c06\u6240\u6709\u7684iptables\u7684INPUT\u94fe\u6570\u636e\u5305\u8bb0\u5f55\u5230\/var\/log\/messages<\/code>\u4e2d\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e00\u4e9biptables\u89c4\u5219\u4e86\uff0c\u90a3\u4e48\u5c06\u8bb0\u5f55\u65e5\u5fd7\u7684\u89c4\u5219\u653e\u5728\u8fd9\u4e9b\u89c4\u5219\u7684\u9876\u90e8\uff0c\u5982\u679c\u653e\u5728\u89c4\u5219\u7684\u540e\u9762\uff0c\u5c06\u4e0d\u4f1a\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n

\r\niptables -A INPUT -p tcp --dport 22 -m state --state NEW -m limit --limit 4\/min -j LOG --log-prefix \"Iptables-SSH-IN: \" --log-level 4\r\niptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT\r\niptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT\r\niptables -P INPUT DROP\r\n<\/pre>\n

\"\"
\n\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5b83\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n

    \n
  • \u7b2c\u4e00\u6761\u89c4\u5219\u8bb0\u5f55\u6240\u6709\u8bbf\u95ee\u76ee\u7684\u7aef\u53e3\u4e3a22\u7684\uff0c\u5c06\u8bb0\u5f55\u65e5\u5fd7\uff0c\u9650\u5236\u65e5\u5fd7\u6bcf\u5206\u949f4\u6761\uff0c\u65e5\u5fd7\u524d\u7f00\u4e3a\u201cIptables-SSH-IN: \u201d\uff0c\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\u4e3aWarning\u3002<\/li>\n
  • \u7b2c\u4e8c\u6761\u89c4\u5219\u5141\u8bb8\u6240\u6709\u6e90\u5730\u5740\u7684\u65b0\u8fde\u63a5\u8bbf\u95ee\u76ee\u7684\u7aef\u53e322\u3002<\/li>\n
  • \u7b2c\u4e09\u6761\u89c4\u5219\u5141\u8bb8\u6240\u6709\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\u8bbf\u95ee\u672c\u673a\u3002<\/li>\n
  • \u8bbe\u7f6eINPUT\u94fe\u7684\u9ed8\u8ba4\u89c4\u5219\u4e3aDROP\u3002<\/li>\n<\/ul>\n

    \u4e0a\u9762\u7b2c\u4e00\u6761\u89c4\u5219\u89e3\u91ca\uff1a<\/p>\n

      \n
    • -m limit\uff1a\u4f7f\u7528limit\u6a21\u5757\u3002\u4f7f\u7528\u6b64\u9009\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528--limit\u9009\u9879\u9650\u5236\u8bbf\u95ee\u901f\u7387\u3002<\/li>\n
    • --limit 4\/min\uff1a\u8fd9\u8868\u793a\u8bb0\u5f55\u7684\u6700\u5927\u5e73\u5747\u8bbf\u95ee\u901f\u7387\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5bf9\u4e8e\u7c7b\u4f3c\u7684\u6570\u636e\u5305\uff0c\u5b83\u5c06\u65e5\u5fd7\u8bb0\u5f55\u9650\u5236\u4e3a\u6bcf\u5206\u949f4\u4e2a\u3002\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u4e3a2\/second, 2\/minute, 2\/hour, 2\/day\u3002<\/li>\n
    • -j LOG\uff1a\u8fd9\u8868\u793a\u6b64\u6570\u636e\u5305\u7684\u76ee\u6807\u662fLOG\u3002\u5373\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n
    • --log-prefix \"Iptables-SSH-IN: \u201d \u53ef\u4ee5\u6307\u5b9a\u4efb\u4f55\u65e5\u5fd7\u524d\u7f00\uff0c\u8fd9\u4e9b\u524d\u7f00\u5c06\u5199\u5165\u5230\/var\/log\/messages<\/code>\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/li>\n
    • --log-level 4\u8fd9\u662f\u6807\u51c6\u7684\u7cfb\u7edf\u65e5\u5fd7\u7ea7\u522b\u30024\u662f\u8b66\u544a(warning)\u3002\u53ef\u4ee5\u4f7f\u75280\u52307\u4e4b\u95f4\u7684\u6570\u5b57\u30020\u662femerg,1\u662falert\uff0c2\u662fcrit\uff0c3\u662ferr\uff0c4\u662fwarning\uff0c5\u662fnotice\uff0c6\u662finfo\uff0c7\u662fdebug\u3002<\/li>\n<\/ul>\n
      \u4fee\u6539iptables\u65e5\u5fd7\u5b58\u653e\u4f4d\u7f6e<\/strong><\/div>\n

      \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ciptables\u5c06\u4f7f\u7528\/var\/log\/messages<\/code>\u8bb0\u5f55\u6240\u6709\u6d88\u606f\u3002\u5982\u679c\u8981\u5c06\u5176\u66f4\u6539\u4e3a\u81ea\u5df1\u7684\u5b9a\u5236\u65e5\u5fd7\u6587\u4ef6\uff0c\u5c06\u4e0b\u9762\u547d\u4ee4\u6dfb\u52a0\u5230\/etc\/rsyslog.conf<\/code>\u4e2d\u3002\u524d\u63d0\u9700\u8981\u5b89\u88c5rsyslog\u670d\u52a1\uff1a<\/p>\n

      \r\n# \u5b89\u88c5rsyslog\u670d\u52a1\r\n[root@localhost ~]# yum -y install rsyslog\r\n# \u8bbe\u7f6e\u5f00\u673a\u542f\u52a8\u3001\u5e76\u7acb\u5373\u542f\u52a8\r\n[root@localhost ~]# systemctl enable rsyslog --now\r\n# \u7f16\u8f91\/etc\/rsyslog.conf\u6587\u4ef6\uff0c\u6dfb\u52a0\u4e0b\u9762\u5185\u5bb9\u5230\u6587\u4ef6\u5e95\u90e8\r\n[root@localhost ~]# vim \/etc\/rsyslog.conf \r\nkern.warning \/var\/log\/iptables.log\r\n<\/pre>\n

      \"\"
      \n\u67e5\u770b\u521a\u624d\u8bbe\u7f6e\u7684\u65e5\u5fd7\u6587\u4ef6\uff1a<\/p>\n

      \r\n[root@localhost ~]# ll -h \/var\/log\/iptables.log \r\n-rw-------. 1 root root 2.0K Feb  9 10:46 \/var\/log\/iptables.log\r\n[root@localhost ~]# tail -f \/var\/log\/iptables.log \r\n<\/pre>\n

      \"\"<\/p>\n

      \u5982\u4f55\u8bfb\u53d6IPTables\u65e5\u5fd7\uff1f<\/strong><\/div>\n
      \r\n[root@localhost ~]# cat \/var\/log\/iptables.log \r\n\r\nFeb  9 10:46:20 localhost kernel: Iptables-SSH-IN: IN=ens160 OUT= MAC=00:0c:29:ae:7d:09:00:50:56:c0:00:08:08:00 SRC=192.168.43.1 DST=192.168.43.137 LEN=52 TOS=0x00 PREC=0x00 TTL=128 ID=49928 DF PROTO=TCP SPT=16512 DPT=22 WINDOW=64240 RES=0x00 SYN URGP=0\r\n<\/pre>\n
        \n
      • Iptables-SSH-IN: \u901a\u8fc7\u6307\u5b9a--log-prefix\u9009\u9879\uff0c\u8fd9\u662f\u6211\u4eec\u5728\u65e5\u5fd7\u8bb0\u5f55\u4e2d\u4f7f\u7528\u7684\u524d\u7f00<\/li>\n
      • IN=ens160 OUT=: \u8fd9\u8868\u793a\u4ece\u8be5\u63a5\u53e3\u4f20\u5165\u6570\u636e\u5305\u3002\u5bf9\u4e8e\u4f20\u51fa\u6570\u636e\u5305\u5c06\u4e3a\u7a7a\u3002<\/li>\n
      • IN= OUT=: \u8fd9\u8868\u793a\u4ece\u8be5\u63a5\u53e3\u4f20\u51fa\u6570\u636e\u5305\u3002\u5bf9\u4e8e\u4f20\u5165\u7684\u6570\u636e\u5305\u5c06\u4e3a\u7a7a\u3002<\/li>\n
      • MAC=: 00:0c:29:ae:7d:09\u5c0f\u65f6\u76ee\u6807MAC\u5730\u5740\uff0c:00:50:56:c0:00:08\u4e3a\u6e90MAC\u5730\u5740\uff0c08:00\u4e3a\u4e0a\u5c42\u534f\u8bae\u4ee3\u7801\uff0c\u8868\u793aIP\u534f\u8bae\u3002<\/li>\n
      • SRC=: \u6e90IP\u5730\u5740<\/li>\n
      • DST=:\u76ee\u7684IP\u5730\u5740<\/li>\n
      • LEN=: \u6570\u636e\u5305\u7684\u957f\u5ea6<\/li>\n
      • PROTO=: \u4f7f\u7528\u4ec0\u4e48\u7c7b\u578b\u534f\u8bae<\/li>\n
      • SPT=: \u6e90\u7aef\u53e3<\/li>\n
      • DPT=: \u76ee\u6807\u7aef\u53e3<\/li>\n<\/ul>\n
        \u603b\u7ed3<\/strong><\/div>\n

        \u5f53\u4f60\u7684iptables\u89c4\u5219\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u65f6\uff0c\u53ef\u80fd\u5e0c\u671b\u8bb0\u5f55iptables\u7684\u6570\u636e\u5305\u4ee5\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528iptables\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5982\u4f55\u5c06\u6240\u6709\u7684iptables\u7684INPUT\u94fe\u6570\u636e\u5305\u8bb0\u5f55\u5230\/var\/log\/messages\u4e2d\u3002 […]<\/p>\n","protected":false},"author":309,"featured_media":212505,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-212504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=212504"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212504\/revisions"}],"predecessor-version":[{"id":212511,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212504\/revisions\/212511"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/212505"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=212504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=212504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=212504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}