{"id":204092,"date":"2025-05-29T12:50:55","date_gmt":"2025-05-29T04:50:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204092\/"},"modified":"2025-05-29T12:50:55","modified_gmt":"2025-05-29T04:50:55","slug":"%e4%b8%ba%e5%ae%b6%e5%ba%ad%e7%bd%91%e7%bb%9c%e6%9e%84%e5%bb%ba%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e5%9f%ba%e4%ba%8e-python-%e7%9a%84%e9%98%b2%e7%81%ab%e5%a2%99","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204092\/","title":{"rendered":"\u4e3a\u5bb6\u5ead\u7f51\u7edc\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e Python \u7684\u9632\u706b\u5899"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e3a\u5bb6\u5ead\u7f51\u7edc\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e Python \u7684\u9632\u706b\u5899<\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u5bb6\u5ead\u7f51\u7edc\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e Python \u7684\u9632\u706b\u5899\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730026646671e1c9699a3a.jpg\" class=\"aligncenter\"><\/p>\n<p><em>\u4f5c\u8005\uff1a\u7279\u91cc\u514b\u65af\u00b7\u8d5b\u52d2\u65af<\/em><\/p>\n<p>waymap\u6e17\u900f\u6d4b\u8bd5\u5de5\u5177\uff1a\u70b9\u51fb\u8fd9\u91cc<br \/> trixsec github\uff1a\u70b9\u51fb\u8fd9\u91cc<\/p>\n<p><strong>\u5148\u51b3\u6761\u4ef6<\/strong><br \/> \u5728\u6df1\u5165\u5b9e\u65bd\u4e4b\u524d\uff0c\u60a8\u9700\u8981\uff1a<\/p>\n<p><em>python \u7f16\u7a0b\u57fa\u7840\u77e5\u8bc6\u3002<\/em><br \/><em>\u60a8\u7684\u7cfb\u7edf\u4e0a\u5df2\u5b89\u88c5 python 3\u3002<\/em><br \/><em>\u7528\u4e8e\u6570\u636e\u5305\u64cd\u4f5c\u7684 scapy \u5e93\uff08\u4f7f\u7528 pip install scapy \u5b89\u88c5\uff09\u3002<\/em><br \/><em>\u60a8\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u7f51\u7edc\u547d\u4ee4\u7684\u7ba1\u7406\u6743\u9650\u3002<\/em><\/p>\n<p><strong>\u4e86\u89e3\u9632\u706b\u5899\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n<p>\u9632\u706b\u5899\u5145\u5f53\u5bb6\u5ead\u7f51\u7edc\u548c\u4e92\u8054\u7f51\u4e4b\u95f4\u7684\u5c4f\u969c\u3002\u5b83\u6839\u636e\u9884\u5b9a\u4e49\u7684\u5b89\u5168\u89c4\u5219\u8fc7\u6ee4\u4f20\u5165\u548c\u4f20\u51fa\u7684\u6d41\u91cf\u3002\u9632\u706b\u5899\u53ef\u4ee5\u963b\u6b62\u6076\u610f\u6d41\u91cf\u5e76\u5141\u8bb8\u5408\u6cd5\u6d41\u91cf\uff0c\u4ece\u800c\u63d0\u4f9b\u4e00\u5c42\u5b89\u5168\u6027\u3002<\/p>\n<p>\u8bbe\u7f6e\u4f60\u7684 python \u9632\u706b\u5899<\/p>\n<p><strong>1\u3002\u5bfc\u5165\u6240\u9700\u7684\u5e93<\/strong><br \/> \u9996\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<pre>from scapy.all import *\n<\/pre>\n<p><strong>2\u3002\u5b9a\u4e49\u6570\u636e\u5305\u8fc7\u6ee4\u89c4\u5219<\/strong><br \/> \u60a8\u53ef\u4ee5\u6839\u636e ip \u5730\u5740\u3001\u534f\u8bae\u548c\u7aef\u53e3\u521b\u5efa\u8fc7\u6ee4\u89c4\u5219\u5217\u8868\u3002\u8fd9\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre># list of allowed ips\nallowed_ips = ['192.168.1.1', '192.168.1.2']  # add your trusted ips here\n\n# function to check if the packet is allowed\ndef is_allowed(packet):\n    if ip in packet:\n        return packet[ip].src in allowed_ips\n    return false\n<\/pre>\n<p><strong>3\u3002\u6570\u636e\u5305\u55c5\u63a2\u548c\u8fc7\u6ee4<\/strong><br \/> \u4f7f\u7528 scapy\uff0c\u60a8\u53ef\u4ee5\u55c5\u63a2\u6570\u636e\u5305\u5e76\u5e94\u7528\u8fc7\u6ee4\u89c4\u5219\uff1a<\/p>\n<pre>def packet_callback(packet):\n    if is_allowed(packet):\n        print(f\"allowed packet: {packet.summary()}\")\n    else:\n        print(f\"blocked packet: {packet.summary()}\")\n\n# start sniffing the packets\nsniff(prn=packet_callback, filter=\"ip\", store=0)\n<\/pre>\n<p><strong>4\u3002\u8fd0\u884c\u9632\u706b\u5899<\/strong><br \/> \u8981\u8fd0\u884c\u9632\u706b\u5899\uff0c\u8bf7\u5c06\u811a\u672c\u4fdd\u5b58\u4e3a simple_firewall.py \u5e76\u4f7f\u7528\u7ba1\u7406\u6743\u9650\u6267\u884c\u5b83\uff1a<\/p>\n<pre>sudo python3 simple_firewall.py\n<\/pre>\n<p><strong>5\u3002\u6d4b\u8bd5\u9632\u706b\u5899<\/strong><br \/> \u60a8\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5 ping \u5141\u8bb8\u548c\u963b\u6b62\u7684 ip \u5730\u5740\u6765\u6d4b\u8bd5\u9632\u706b\u5899\u3002\u68c0\u67e5\u63a7\u5236\u53f0\u8f93\u51fa\uff0c\u770b\u770b\u6570\u636e\u5305\u662f\u5426\u6839\u636e\u60a8\u7684\u89c4\u5219\u88ab\u5141\u8bb8\u6216\u963b\u6b62\u3002<\/p>\n<p><strong>\u9650\u5236\u548c\u6ce8\u610f\u4e8b\u9879<\/strong><br \/> \u8fd9\u4e2a\u7b80\u5355\u7684\u9632\u706b\u5899\u53ea\u662f\u7528\u4e8e\u6559\u80b2\u76ee\u7684\u7684\u57fa\u672c\u5b9e\u73b0\u3002\u4e00\u4e9b\u9650\u5236\u5305\u62ec\uff1a<\/p>\n<p><em>\u65e0\u72b6\u6001\u68c0\u67e5\uff1a<\/em>\u6b64\u9632\u706b\u5899\u4e0d\u7ef4\u62a4\u8fde\u63a5\u72b6\u6001\u3002<br \/><em>\u89c4\u5219\u590d\u6742\u5ea6\u6709\u9650\uff1a<\/em>\u53ea\u80fd\u6839\u636eip\u5730\u5740\u8fdb\u884c\u8fc7\u6ee4\uff0c\u6dfb\u52a0\u66f4\u590d\u6742\u7684\u89c4\u5219\u9700\u8981\u989d\u5916\u7f16\u7801\u3002<br \/><em>\u6027\u80fd\uff1a<\/em>\u4e0e\u4e13\u7528\u9632\u706b\u5899\u89e3\u51b3\u65b9\u6848\u76f8\u6bd4\uff0cpython \u53ef\u80fd\u65e0\u6cd5\u6709\u6548\u5730\u5904\u7406\u9ad8\u6d41\u91cf\u3002<\/p>\n<p><em>~trixsec<\/em><\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4e3a\u5bb6\u5ead\u7f51\u7edc\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e Python \u7684\u9632\u706b\u5899\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u5bb6\u5ead\u7f51\u7edc\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e P&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204092","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204092"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204092\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}