{"id":148,"date":"2023-03-27T08:18:08","date_gmt":"2023-03-27T00:18:08","guid":{"rendered":"https:\/\/mianka.xyz\/?p=147"},"modified":"2023-03-27T08:19:35","modified_gmt":"2023-03-27T00:19:35","slug":"python","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=148","title":{"rendered":"python\u600e\u4e48\u83b7\u53d6\u7535\u8111\u6240\u6709\u7684\u7f51\u7edc\u8bf7\u6c42"},"content":{"rendered":"<p>\u8981\u83b7\u53d6\u7535\u8111\u6240\u6709\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684pcapy\u5e93\u548cpyshark\u5e93\u3002\u8fd9\u4e24\u4e2a\u5e93\u53ef\u4ee5\u5728Python\u4e2d\u6355\u83b7\u548c\u5206\u6790\u7f51\u7edc\u6d41\u91cf\u3002<\/p>\n<p><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u7535\u8111\u6240\u6709\u7684\u7f51\u7edc\u8bf7\u6c42\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;pcapy\nimport&nbsp;pyshark\n#&nbsp;\u4f7f\u7528pcapy\u6355\u83b7\u7f51\u7edc\u6d41\u91cf\ncap&nbsp;=&nbsp;pcapy.open_live(&#39;eth0&#39;,&nbsp;65536,&nbsp;True,&nbsp;100)\nwhile&nbsp;True:\n&nbsp;&nbsp;&nbsp;&nbsp;try:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(header,&nbsp;payload)&nbsp;=&nbsp;cap.next()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(payload)\n&nbsp;&nbsp;&nbsp;&nbsp;except&nbsp;KeyboardInterrupt:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break\n#&nbsp;\u4f7f\u7528pyshark\u6355\u83b7\u7f51\u7edc\u6d41\u91cf\ncapture&nbsp;=&nbsp;pyshark.LiveCapture(interface=&#39;eth0&#39;)\nfor&nbsp;packet&nbsp;in&nbsp;capture.sniff_continuously():\n&nbsp;&nbsp;&nbsp;&nbsp;print(packet)<\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528pcapy\u548cpyshark\u5e93\u6355\u83b7\u7f51\u7edc\u6d41\u91cf\uff0c\u5e76\u6253\u5370\u6bcf\u4e2a\u6570\u636e\u5305\u7684\u5185\u5bb9\u3002\u5176\u4e2d\uff0c&#39;eth0&#39;\u662f\u7f51\u7edc\u63a5\u53e3\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u66f4\u6539\u3002\u6ce8\u610f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u83b7\u53d6\u7f51\u7edc\u8bf7\u6c42\u9700\u8981\u5177\u6709\u7279\u5b9a\u7684\u6743\u9650\uff0c\u901a\u5e38\u9700\u8981\u4ee5\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<p><\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u7f51\u7edc\u8bf7\u6c42\u7684\u539f\u59cb\u6570\u636e\u5305\uff0c\u5982\u679c\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u89e3\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528pyshark\u5e93\u4e2d\u7684\u5176\u4ed6\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f8b\u5982\u534f\u8bae(protocol)\u3001\u6e90\u5730\u5740(src)\u3001\u76ee\u6807\u5730\u5740(dst)\u7b49\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u83b7\u53d6\u7535\u8111\u6240\u6709\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684pcapy\u5e93\u548cpyshark\u5e93\u3002\u8fd9\u4e24\u4e2a\u5e93\u53ef\u4ee5\u5728Python\u4e2d\u6355\u83b7\u548c\u5206\u6790\u7f51\u7edc\u6d41\u91cf\u3002<\/p>\n<p><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u7535\u8111\u6240\u6709\u7684\u7f51\u7edc\u8bf7\u6c42\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;pcapy\nimport&nbsp;pyshark\n#&nbsp;\u4f7f\u7528pcapy\u6355\u83b7\u7f51\u7edc\u6d41\u91cf\ncap&nbsp;=&nbsp;pcapy.open_live(&#39;eth0&#39;,&nbsp;65536,&nbsp;True,&nbsp;100)\nwhile&nbsp;True:\n&nbsp;&nbsp;&nbsp;&nbsp;try:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(header,&nbsp;payload)&nbsp;=&nbsp;cap.next()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(payload)\n&nbsp;&nbsp;&nbsp;&nbsp;except&nbsp;KeyboardInterrupt:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break\n#&nbsp;\u4f7f\u7528pyshark\u6355\u83b7\u7f51\u7edc\u6d41\u91cf\ncapture&nbsp;=&nbsp;pyshark.LiveCapture(interface=&#39;eth0&#39;)\nfor&nbsp;packet&nbsp;in&nbsp;capture.sniff_continuously():\n&nbsp;&nbsp;&nbsp;&nbsp;print(packet)<\/pre>\n<p><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=148"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}