{"id":1509,"date":"2024-07-02T18:06:02","date_gmt":"2024-07-02T10:06:02","guid":{"rendered":"https:\/\/aichh.com\/?p=1509"},"modified":"2024-07-02T18:07:19","modified_gmt":"2024-07-02T10:07:19","slug":"%e4%b8%8d%e7%94%a8%e7%a6%81%e7%94%a8-iptables-%e6%9d%a5%e8%a7%a3%e5%86%b3-ufw-%e5%92%8c-docker-%e7%9a%84%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/aichh.com\/1509.html","title":{"rendered":"\u4e0d\u7528\u7981\u7528 iptables \u6765\u89e3\u51b3 UFW \u548c Docker \u7684\u5b89\u5168\u95ee\u9898"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><h2><a id=\"user-content-\u95ee\u9898\" class=\"anchor\" href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E9%97%AE%E9%A2%98\" aria-hidden=\"true\"><\/a>\u95ee\u9898<\/h2>\n<p>UFW \u662f Ubuntu \u4e0a\u5f88\u6d41\u884c\u7684\u4e00\u4e2a iptables \u524d\u7aef\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u7ba1\u7406\u9632\u706b\u5899\u7684\u89c4\u5219\u3002\u4f46\u662f\u5f53\u5b89\u88c5\u4e86 Docker\uff0cUFW \u65e0\u6cd5\u7ba1\u7406 Docker \u53d1\u5e03\u51fa\u6765\u7684\u7aef\u53e3\u4e86\u3002<\/p>\n<p>\u5177\u4f53\u73b0\u8c61\u662f\uff1a<\/p>\n<ol>\n<li>\u5728\u4e00\u4e2a\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u7684\u670d\u52a1\u5668\u4e0a\u542f\u7528\u4e86 UFW\uff0c\u5e76\u4e14\u9ed8\u8ba4\u963b\u6b62\u6240\u6709\u672a\u88ab\u5141\u8bb8\u7684\u4f20\u5165\u8fde\u63a5\u3002<\/li>\n<li>\u8fd0\u884c\u4e86\u4e00\u4e2a Docker \u5bb9\u5668\uff0c\u5e76\u4e14\u4f7f\u7528\u00a0<code>-p<\/code>\u00a0\u9009\u9879\u6765\u628a\u8be5\u5bb9\u5668\u7684\u67d0\u4e2a\u7aef\u53e3\u53d1\u5e03\u5230\u670d\u52a1\u5668\u7684\u6240\u6709 IP \u5730\u5740\u4e0a\u3002\u6bd4\u5982\uff1a<code>docker run -d --name httpd -p 0.0.0.0:8080:80 httpd:alpine<\/code>\u00a0\u5c06\u4f1a\u8fd0\u884c\u4e00\u4e2a httpd \u670d\u52a1\uff0c\u5e76\u4e14\u5c06\u5bb9\u5668\u7684\u00a0<code>80<\/code>\u00a0\u7aef\u53e3\u53d1\u5e03\u5230\u670d\u52a1\u5668\u7684\u00a0<code>8080<\/code>\u00a0\u7aef\u53e3\u4e0a\u3002<\/li>\n<li>UFW \u5c06\u4e0d\u4f1a\u963b\u6b62\u6240\u6709\u5bf9\u00a0<code>8080<\/code>\u00a0\u7aef\u53e3\u8bbf\u95ee\u7684\u8bf7\u6c42\uff0c\u7528\u547d\u4ee4\u00a0<code>ufw deny 8080<\/code>\u00a0\u4e5f\u65e0\u6cd5\u963b\u6b62\u5916\u90e8\u8bbf\u95ee\u8fd9\u4e2a\u7aef\u53e3\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5176\u5b9e\u633a\u4e25\u91cd\u7684\uff0c\u8fd9\u610f\u5473\u7740\u672c\u6765\u53ea\u662f\u4e3a\u4e86\u5728\u5185\u90e8\u63d0\u4f9b\u670d\u52a1\u7684\u4e00\u4e2a\u7aef\u53e3\u88ab\u66b4\u9732\u5728\u516c\u5171\u7f51\u7edc\u4e0a\u3002<\/p>\n<p>\u5728\u7f51\u7edc\u4e0a\u641c\u7d22 \u201cufw docker\u201d \u53ef\u4ee5\u53d1\u73b0\u5f88\u591a\u7684\u8ba8\u8bba\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/moby\/moby\/issues\/4737\">https:\/\/github.com\/moby\/moby\/issues\/4737<\/a><\/li>\n<li><a href=\"https:\/\/forums.docker.com\/t\/running-multiple-docker-containers-with-ufw-and-iptables-false\/8953\" rel=\"nofollow\">https:\/\/forums.docker.com\/t\/running-multiple-docker-containers-with-ufw-and-iptables-false\/8953<\/a><\/li>\n<li><a href=\"https:\/\/www.techrepublic.com\/article\/how-to-fix-the-docker-and-ufw-security-flaw\/\" rel=\"nofollow\">https:\/\/www.techrepublic.com\/article\/how-to-fix-the-docker-and-ufw-security-flaw\/<\/a><\/li>\n<li><a href=\"https:\/\/blog.viktorpetersson.com\/2014\/11\/03\/the-dangers-of-ufw-docker.html\" rel=\"nofollow\">https:\/\/blog.viktorpetersson.com\/2014\/11\/03\/the-dangers-of-ufw-docker.html<\/a><\/li>\n<li><a href=\"https:\/\/askubuntu.com\/questions\/652556\/uncomplicated-firewall-ufw-is-not-blocking-anything-when-using-docker\" rel=\"nofollow\">https:\/\/askubuntu.com\/questions\/652556\/uncomplicated-firewall-ufw-is-not-blocking-anything-when-using-docker<\/a><\/li>\n<li><a href=\"https:\/\/chjdev.com\/2016\/06\/08\/docker-ufw\/\" rel=\"nofollow\">https:\/\/chjdev.com\/2016\/06\/08\/docker-ufw\/<\/a><\/li>\n<li><a href=\"https:\/\/askubuntu.com\/questions\/652556\/uncomplicated-firewall-ufw-is-not-blocking-anything-when-using-docker\" rel=\"nofollow\">https:\/\/askubuntu.com\/questions\/652556\/uncomplicated-firewall-ufw-is-not-blocking-anything-when-using-docker<\/a><\/li>\n<li><a href=\"https:\/\/my.oschina.net\/abcfy2\/blog\/539485\" rel=\"nofollow\">https:\/\/my.oschina.net\/abcfy2\/blog\/539485<\/a><\/li>\n<li><a href=\"https:\/\/www.v2ex.com\/amp\/t\/466666\" rel=\"nofollow\">https:\/\/www.v2ex.com\/amp\/t\/466666<\/a><\/li>\n<li><a href=\"https:\/\/blog.36web.rocks\/2016\/07\/08\/docker-behind-ufw.html\" rel=\"nofollow\">https:\/\/blog.36web.rocks\/2016\/07\/08\/docker-behind-ufw.html<\/a><\/li>\n<li>\u2026<\/li>\n<\/ul>\n<p>\u57fa\u672c\u4e0a\u53ef\u4ee5\u627e\u5230\u7684\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u9996\u5148\u7981\u7528 docker \u7684 iptables \u529f\u80fd\uff0c\u4f46\u8fd9\u4e5f\u610f\u5473\u7740\u653e\u5f03\u4e86 docker \u7684\u7f51\u7edc\u7ba1\u7406\u529f\u80fd\uff0c\u5f88\u5178\u578b\u7684\u73b0\u8c61\u5c31\u662f\u5bb9\u5668\u5c06\u65e0\u6cd5\u8bbf\u95ee\u5916\u90e8\u7f51\u7edc\u3002\u5728\u6709\u7684\u6587\u7ae0\u4e2d\u4e5f\u63d0\u5230\u4e86\u53ef\u4ee5\u5728 UFW \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u624b\u5de5\u6dfb\u52a0\u4e00\u6761\u89c4\u5219\uff0c\u6bd4\u5982\u00a0<code>-A POSTROUTING ! -o docker0 -s 172.17.0.0\/16 -j MASQUERADE<\/code>\u3002\u4f46\u8fd9\u4e5f\u53ea\u662f\u5141\u8bb8\u4e86\u00a0<code>172.17.0.0\/16<\/code>\u00a0\u8fd9\u4e2a\u7f51\u7edc\u3002\u5982\u679c\u6709\u4e86\u65b0\u589e\u7684\u7f51\u7edc\uff0c\u6211\u4eec\u4e5f\u5fc5\u987b\u624b\u5de5\u518d\u4e3a\u65b0\u589e\u7684\u7f51\u7edc\u6dfb\u52a0\u8fd9\u6837\u7c7b\u4f3c\u7684 iptables \u89c4\u5219\u3002<\/p>\n<h2><a id=\"user-content-\u671f\u671b\u7684\u76ee\u6807\" class=\"anchor\" href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E6%9C%9F%E6%9C%9B%E7%9A%84%E7%9B%AE%E6%A0%87\" aria-hidden=\"true\"><\/a>\u671f\u671b\u7684\u76ee\u6807<\/h2>\n<p>\u76ee\u524d\u7f51\u7edc\u4e0a\u7684\u89e3\u51b3\u65b9\u6848\u90fd\u975e\u5e38\u7c7b\u4f3c\uff0c\u800c\u4e14\u4e5f\u4e0d\u4f18\u96c5\uff0c\u6211\u5e0c\u671b\u4e00\u4e2a\u65b0\u7684\u89e3\u51b3\u65b9\u6848\u53ef\u4ee5\uff1a<\/p>\n<ol>\n<li>\u4e0d\u8981\u7981\u7528 Docker \u7684 iptables\uff0c\u50cf\u5f80\u5e38\u4e00\u6837\u7531 Docker \u6765\u7ba1\u7406\u81ea\u5df1\u7684\u7f51\u7edc\u3002\u8fd9\u6837\u6709\u4efb\u4f55\u65b0\u589e\u7684 Docker \u7f51\u7edc\u65f6\u90fd\u65e0\u9700\u624b\u5de5\u7ef4\u62a4 iptables \u89c4\u5219\uff0c\u4e5f\u907f\u514d\u4e86\u5728 Docker \u4e2d\u7981\u7528 iptables \u4e4b\u540e\u53ef\u80fd\u5e26\u6765\u7684\u526f\u4f5c\u7528\u3002<\/li>\n<li>\u516c\u5171\u7f51\u7edc\u4e0d\u53ef\u4ee5\u8bbf\u95ee Docker \u53d1\u5e03\u51fa\u6765\u7684\u7aef\u53e3\uff0c\u5373\u4f7f\u662f\u4f7f\u7528\u7c7b\u4f3c\u00a0<code>-p 0.0.0.0:8080:80<\/code>\u00a0\u7684\u9009\u9879\u628a\u7aef\u53e3\u53d1\u5e03\u5728\u6240\u6709\u7684 IP \u5730\u5740\u4e0a\u3002\u5bb9\u5668\u4e4b\u95f4\u3001\u5185\u90e8\u7f51\u7edc\u4e4b\u95f4\u90fd\u53ef\u4ee5\u6b63\u5e38\u4e92\u76f8\u8bbf\u95ee\uff0c\u53ea\u6709\u516c\u5171\u7f51\u7edc\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u3002 \u867d\u7136\u53ef\u4ee5\u8ba9 Docker \u628a\u5bb9\u5668\u7684\u67d0\u4e00\u4e2a\u7aef\u53e3\u6620\u5c04\u5230\u670d\u52a1\u5668\u7684\u79c1\u6709 IP \u5730\u5740\u4e0a\uff0c\u8fd9\u6837\u516c\u5171\u7f51\u7edc\u4e0a\u5c06\u4e0d\u4f1a\u8bbf\u95ee\u5230\u8fd9\u4e2a\u7aef\u53e3\u3002\u4f46\u662f\u8fd9\u4e2a\u670d\u52a1\u5668\u53ef\u80fd\u6709\u591a\u4e2a\u79c1\u6709 IP \u5730\u5740\uff0c\u8fd9\u4e9b\u79c1\u6709 IP \u5730\u5740\u53ef\u80fd\u4e5f\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/li>\n<li>\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5141\u8bb8\u516c\u5171\u7f51\u7edc\u76f4\u63a5\u8bbf\u95ee\u67d0\u4e2a\u5bb9\u5668\u7684\u7aef\u53e3\uff0c\u800c\u65e0\u9700\u989d\u5916\u7684\u8f6f\u4ef6\u548c\u914d\u7f6e\u3002\u5c31\u50cf\u662f\u7528\u00a0<code>ufw allow 8080<\/code>\u00a0\u8fd9\u6837\u5141\u8bb8\u5916\u90e8\u8bbf\u95ee 8080 \u7aef\u53e3\uff0c\u7136\u540e\u7528\u00a0<code>ufw delete allow 8080<\/code>\u00a0\u5c31\u4e0d\u518d\u5141\u8bb8\u5916\u90e8\u8bbf\u95ee\u3002<\/li>\n<\/ol>\n<h2><a id=\"user-content-\u5982\u4f55\u505a\" class=\"anchor\" href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E5%A6%82%E4%BD%95%E5%81%9A\" aria-hidden=\"true\"><\/a>\u5982\u4f55\u505a\uff1f<\/h2>\n<h3><a id=\"user-content-\u64a4\u9500\u539f\u5148\u7684\u4fee\u6539\" class=\"anchor\" href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E6%92%A4%E9%94%80%E5%8E%9F%E5%85%88%E7%9A%84%E4%BF%AE%E6%94%B9\" aria-hidden=\"true\"><\/a>\u64a4\u9500\u539f\u5148\u7684\u4fee\u6539<\/h3>\n<p>\u5982\u679c\u5df2\u7ecf\u6309\u7167\u76ee\u524d\u7f51\u7edc\u4e0a\u641c\u7d22\u5230\u89e3\u51b3\u65b9\u6848\u4fee\u6539\u8fc7\u4e86\uff0c\u8bf7\u5148\u4fee\u6539\u56de\u6765\uff0c\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u542f\u7528 Docker \u7684 iptables \u529f\u80fd\uff0c\u5220\u9664\u6240\u6709\u7c7b\u4f3c\u00a0<code>--iptables=false<\/code>\u00a0\u7684\u4fee\u6539\uff0c\u5305\u62ec\u00a0<code>\/etc\/docker\/daemon.json<\/code>\u00a0\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>UFW \u7684\u9ed8\u8ba4\u00a0<code>FORWARD<\/code>\u00a0\u89c4\u5219\u6539\u56de\u9ed8\u8ba4\u7684\u00a0<code>DROP<\/code>\uff0c\u800c\u975e\u00a0<code>ACCEPT<\/code>\u3002<\/li>\n<li>\u5220\u9664 UFW \u914d\u7f6e\u6587\u4ef6\u00a0<code>\/etc\/ufw\/after.rules<\/code>\u00a0\u4e2d\u4e0e Docker \u7f51\u7edc\u76f8\u5173\u7684\u89c4\u5219\u3002<\/li>\n<li>\u5982\u679c\u4fee\u6539\u4e86 Docker \u76f8\u5173\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u91cd\u542f Docker\u3002\u7a0d\u540e\u8fd8\u8981\u4fee\u6539 UFW \u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u4e00\u5e76\u91cd\u542f\u3002<\/li>\n<\/ol>\n<h3><a id=\"%E8%A7%A3%E5%86%B3-ufw-%E5%92%8C-docker-%E7%9A%84%E9%97%AE%E9%A2%98\" class=\"anchor\" href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E8%A7%A3%E5%86%B3-ufw-%E5%92%8C-docker-%E7%9A%84%E9%97%AE%E9%A2%98\" aria-hidden=\"true\"><\/a>\u89e3\u51b3 UFW \u548c Docker \u7684\u95ee\u9898<\/h3>\n<p>\u76ee\u524d\u65b0\u7684\u89e3\u51b3\u65b9\u6848\u53ea\u9700\u8981\u4fee\u6539\u4e00\u4e2a UFW \u914d\u7f6e\u6587\u4ef6\u5373\u53ef\uff0cDocker \u7684\u6240\u6709\u914d\u7f6e\u548c\u9009\u9879\u90fd\u4fdd\u6301\u9ed8\u8ba4\u3002<\/p>\n<p>\u4fee\u6539 UFW \u7684\u914d\u7f6e\u6587\u4ef6\u00a0<code>\/etc\/ufw\/after.rules<\/code>\uff0c\u5728\u6700\u540e\u6dfb\u52a0\u4e0a\u5982\u4e0b\u89c4\u5219\uff1a<\/p>\n<pre># BEGIN UFW AND DOCKER\r\n*filter\r\n:ufw-user-forward - [0:0]\r\n:DOCKER-USER - [0:0]\r\n-A DOCKER-USER -j RETURN -s 10.0.0.0\/8\r\n-A DOCKER-USER -j RETURN -s 172.16.0.0\/12\r\n-A DOCKER-USER -j RETURN -s 192.168.0.0\/16\r\n\r\n-A DOCKER-USER -j ufw-user-forward\r\n\r\n-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 192.168.0.0\/16\r\n-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 10.0.0.0\/8\r\n-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 172.16.0.0\/12\r\n-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 192.168.0.0\/16\r\n-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 10.0.0.0\/8\r\n-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 172.16.0.0\/12\r\n\r\n-A DOCKER-USER -j RETURN\r\nCOMMIT\r\n# END UFW AND DOCKER\r\n<\/pre>\n<p>\u7136\u540e\u91cd\u542f UFW\uff0c<code>sudo systemctl restart ufw<\/code>\u3002\u73b0\u5728\u5916\u90e8\u5c31\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee Docker \u53d1\u5e03\u51fa\u6765\u7684\u4efb\u4f55\u7aef\u53e3\u4e86\uff0c\u4f46\u662f\u5bb9\u5668\u5185\u90e8\u4ee5\u53ca\u79c1\u6709\u7f51\u7edc\u5730\u5740\u4e0a\u53ef\u4ee5\u6b63\u5e38\u4e92\u76f8\u8bbf\u95ee\uff0c\u800c\u4e14\u5bb9\u5668\u4e5f\u53ef\u4ee5\u6b63\u5e38\u8bbf\u95ee\u5916\u90e8\u7684\u7f51\u7edc\u3002<strong>\u53ef\u80fd\u7531\u4e8e\u67d0\u4e9b\u672a\u77e5\u539f\u56e0\uff0c\u91cd\u542f UFW \u4e4b\u540e\u89c4\u5219\u4e5f\u65e0\u6cd5\u751f\u6548\uff0c\u8bf7\u91cd\u542f\u670d\u52a1\u5668\u3002<\/strong><\/p>\n<p>\u5982\u679c\u5e0c\u671b\u5141\u8bb8\u5916\u90e8\u7f51\u7edc\u8bbf\u95ee Docker \u5bb9\u5668\u63d0\u4f9b\u7684\u670d\u52a1\uff0c\u6bd4\u5982\u6709\u4e00\u4e2a\u5bb9\u5668\u7684\u670d\u52a1\u7aef\u53e3\u662f\u00a0<code>80<\/code>\u3002\u90a3\u5c31\u53ef\u4ee5\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5141\u8bb8\u5916\u90e8\u7f51\u7edc\u8bbf\u95ee\u8fd9\u4e2a\u670d\u52a1\uff1a<\/p>\n<pre class=\"brush: text; gutter: false\">ufw route allow proto tcp from any to any port 80\r\n<\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5141\u8bb8\u5916\u90e8\u7f51\u7edc\u8bbf\u95ee\u6240\u6709\u7528 Docker \u53d1\u5e03\u51fa\u6765\u7684\u5e76\u4e14\u5185\u90e8\u670d\u52a1\u7aef\u53e3\u4e3a\u00a0<code>80<\/code>\u00a0\u7684\u6240\u6709\u670d\u52a1\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u7aef\u53e3\u00a0<code>80<\/code>\u00a0\u662f\u5bb9\u5668\u7684\u7aef\u53e3\uff0c\u800c\u975e\u4f7f\u7528\u00a0<code>-p 0.0.0.0:8080:80<\/code>\u00a0\u9009\u9879\u53d1\u5e03\u5728\u670d\u52a1\u5668\u4e0a\u7684\u00a0<code>8080<\/code>\u00a0\u7aef\u53e3\u3002<\/p>\n<p>\u5982\u679c\u6709\u591a\u4e2a\u5bb9\u5668\u7684\u670d\u52a1\u7aef\u53e3\u4e3a 80\uff0c\u4f46\u53ea\u5e0c\u671b\u5916\u90e8\u7f51\u7edc\u8bbf\u95ee\u67d0\u4e2a\u7279\u5b9a\u7684\u5bb9\u5668\u3002\u6bd4\u5982\u8be5\u5bb9\u5668\u7684\u79c1\u6709\u5730\u5740\u4e3a\u00a0<code>172.17.0.2<\/code>\uff0c\u5c31\u7528\u7c7b\u4f3c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush: bash; gutter: true\">ufw route allow proto tcp from any to 172.17.0.2 port 80\r\n<\/pre>\n<p>\u5982\u679c\u4e00\u4e2a\u5bb9\u5668\u7684\u670d\u52a1\u662f UDP \u534f\u8bae\uff0c\u5047\u5982\u662f DNS \u670d\u52a1\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u5141\u8bb8\u5916\u90e8\u7f51\u7edc\u8bbf\u95ee\u6240\u6709\u53d1\u5e03\u51fa\u6765\u7684 DNS \u670d\u52a1\uff1a<\/p>\n<pre class=\"brush: bash; gutter: true\">ufw route allow proto udp from any to any port 53\r\n<\/pre>\n<p>\u540c\u6837\u7684\uff0c\u5982\u679c\u53ea\u9488\u5bf9\u4e00\u4e2a\u7279\u5b9a\u7684\u5bb9\u5668\uff0c\u6bd4\u5982 IP \u5730\u5740\u4e3a 172.17.0.2\uff1a<\/p>\n<pre class=\"brush: bash; gutter: true\">ufw route allow proto udp from any to 172.17.0.2 port 53\r\n<\/pre>\n<h3><a id=\"user-content-\u89e3\u91ca\" class=\"anchor\" href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E8%A7%A3%E9%87%8A\" aria-hidden=\"true\"><\/a>\u89e3\u91ca<\/h3>\n<p>\u5728\u65b0\u589e\u7684\u8fd9\u6bb5\u89c4\u5219\u4e2d\uff0c\u4e0b\u9762\u8fd9\u6bb5\u89c4\u5219\u662f\u4e3a\u4e86\u8ba9\u79c1\u6709\u7f51\u7edc\u5730\u5740\u53ef\u4ee5\u4e92\u76f8\u8bbf\u95ee\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u79c1\u6709\u7f51\u7edc\u662f\u6bd4\u516c\u5171\u7f51\u7edc\u66f4\u4fe1\u4efb\u7684\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">-A DOCKER-USER -j RETURN -s 10.0.0.0\/8\r\n-A DOCKER-USER -j RETURN -s 172.16.0.0\/12\r\n-A DOCKER-USER -j RETURN -s 192.168.0.0\/16\r\n<\/pre>\n<p>\u4e0b\u9762\u7684\u89c4\u5219\u662f\u4e3a\u4e86\u53ef\u4ee5\u7528 UFW \u6765\u7ba1\u7406\u5916\u90e8\u7f51\u7edc\u662f\u5426\u5141\u8bb8\u8bbf\u95ee Docker \u5bb9\u5668\u63d0\u4f9b\u7684\u670d\u52a1\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u4e00\u4e2a\u5730\u65b9\u6765\u7ba1\u7406\u9632\u706b\u5899\u7684\u89c4\u5219\u4e86\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">-A DOCKER-USER -j ufw-user-forward\r\n<\/pre>\n<p>\u4e0b\u9762\u7684\u89c4\u5219\u963b\u6b62\u4e86\u6240\u6709\u5916\u90e8\u7f51\u7edc\u53d1\u8d77\u7684\u8fde\u63a5\u8bf7\u6c42\uff0c\u4f46\u662f\u5141\u8bb8\u5185\u90e8\u7f51\u7edc\u8bbf\u95ee\u5916\u90e8\u7f51\u7edc\u3002\u5bf9\u4e8e TCP \u534f\u8bae\uff0c\u662f\u963b\u6b62\u4e86\u4ece\u5916\u90e8\u7f51\u7edc\u4e3b\u52a8\u5efa\u7acb TCP \u8fde\u63a5\u3002\u5bf9\u4e8e UDP\uff0c\u662f\u963b\u6b62\u4e86\u6240\u6709\u5c0f\u4f59\u7aef\u53e3 32767 \u7684\u8bbf\u95ee\u3002\u4e3a\u4ec0\u4e48\u662f\u8fd9\u4e2a\u7aef\u53e3\u7684\uff1f\u7531\u4e8e UDP \u534f\u8bae\u662f\u65e0\u72b6\u6001\u7684\uff0c\u65e0\u6cd5\u50cf TCP \u90a3\u6837\u963b\u6b62\u53d1\u8d77\u5efa\u7acb\u8fde\u63a5\u8bf7\u6c42\u7684\u63e1\u624b\u4fe1\u53f7\u3002\u5728 GNU\/Linux \u4e0a\u67e5\u770b\u6587\u4ef6\u00a0<code>\/proc\/sys\/net\/ipv4\/ip_local_port_range<\/code>\u00a0\u53ef\u4ee5\u770b\u5230\u53d1\u51fa TCP\/UDP \u6570\u636e\u540e\uff0c\u672c\u5730\u6e90\u7aef\u53e3\u7684\u8303\u56f4\uff0c\u9ed8\u8ba4\u4e3a\u00a0<code>32768 60999<\/code>\u3002\u5f53\u4ece\u4e00\u4e2a\u8fd0\u884c\u7684\u5bb9\u5668\u5bf9\u5916\u8bbf\u95ee\u4e00\u4e2a UDP \u534f\u8bae\u7684\u670d\u52a1\u65f6\uff0c\u672c\u5730\u7aef\u53e3\u5c06\u4f1a\u4ece\u8fd9\u4e2a\u7aef\u53e3\u8303\u56f4\u91cc\u9762\u968f\u673a\u9009\u62e9\u4e00\u4e2a\uff0c\u670d\u52a1\u5668\u5c06\u4f1a\u628a\u6570\u636e\u8fd4\u56de\u5230\u8fd9\u4e2a\u968f\u673a\u7aef\u53e3\u4e0a\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u5b9a\u6240\u6709\u5bb9\u5668\u5185\u90e8\u7684 UDP \u534f\u8bae\u7684\u76d1\u542c\u7aef\u53e3\u90fd\u5c0f\u4f59 32768\uff0c\u4e0d\u5141\u8bb8\u5916\u90e8\u7f51\u7edc\u4e3b\u52a8\u8fde\u63a5\u5c0f\u4f59 32768 \u7684 UDP \u7aef\u53e3\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 192.168.0.0\/16\r\n-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 10.0.0.0\/8\r\n-A DOCKER-USER -j DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 172.16.0.0\/12\r\n-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 192.168.0.0\/16\r\n-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 10.0.0.0\/8\r\n-A DOCKER-USER -j DROP -p udp -m udp --dport 0:32767 -d 172.16.0.0\/12\r\n\r\n-A DOCKER-USER -j RETURN<\/pre>\n<p>\u6765\u6e90\uff1a<a href=\"https:\/\/chaifeng.com\/to-fix-ufw-and-docker-security-flaw-without-disabling-iptables\/#%E8%A7%A3%E5%86%B3-ufw-%E5%92%8C-docker-%E7%9A%84%E9%97%AE%E9%A2%98\" target=\"_blank\" rel=\"noopener\">\u00a0\u4e09\u5206\u70ed\u5ea6:\/\/\u67f4\u950b<\/a><\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u95ee\u9898 UFW \u662f Ubuntu \u4e0a\u5f88\u6d41\u884c\u7684\u4e00\u4e2a iptables \u524d\u7aef\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u7ba1\u7406\u9632\u706b\u5899\u7684\u89c4\u5219\u3002\u4f46\u662f\u5f53\u5b89\u88c5\u4e86 Docker\uff0cUFW \u65e0\u6cd5\u7ba1\u7406 Docker \u53d1\u5e03\u51fa\u6765\u7684\u7aef\u53e3\u4e86\u3002 \u5177\u4f53\u73b0\u8c61\u662f\uff1a \u5728 \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[64,403,57],"class_list":["post-1509","post","type-post","status-publish","format-standard","hentry","category-jiaocheng","tag-docker","tag-ufw","tag-57"],"views":204,"_links":{"self":[{"href":"https:\/\/aichh.com\/api\/wp\/v2\/posts\/1509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aichh.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aichh.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aichh.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aichh.com\/api\/wp\/v2\/comments?post=1509"}],"version-history":[{"count":2,"href":"https:\/\/aichh.com\/api\/wp\/v2\/posts\/1509\/revisions"}],"predecessor-version":[{"id":1511,"href":"https:\/\/aichh.com\/api\/wp\/v2\/posts\/1509\/revisions\/1511"}],"wp:attachment":[{"href":"https:\/\/aichh.com\/api\/wp\/v2\/media?parent=1509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aichh.com\/api\/wp\/v2\/categories?post=1509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aichh.com\/api\/wp\/v2\/tags?post=1509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}