pythonʹÓÃmitmproxy½Ì³Ì
mitmproxyÊÇÒ»¿îÓÃpython±àдµÄ¸ß¼¶httpÊðÀí¹¤¾ß£¬ÌṩÁËÇ¿Ê¢µÄapiÀ´ÊµÏÖ×Ô½ç˵µÄÊðÀíÐÐΪ£º×°ÖúÍÉèÖãºÊ¹ÓÃpip×°ÖÃmitmproxy£¬È»ºóͨ¹ýmitmproxyÏÂÁîÆô¶¯Ð§ÀÍ¡£×èµ²Á÷Á¿£º½¨Éèpython¾ç±¾²¢Ê¹ÓÃmitmproxy apiµÄrequest()ºÍresponse()º¯ÊýÀ´×èµ²ºÍ´¦ÀíhttpÇëÇóºÍÏìÓ¦¡£Ð޸ĺÍÖØ·ÅÇëÇó£ºÔÚrequest()º¯ÊýÖиüÐÂÇëÇ󹤾ßÒÔÐÞ¸ÄÇëÇ󣬲¢Ê¹ÓÃreplay()º¯ÊýÖØ·ÅÇëÇ󡣸߼¶¹¦Ð§£ºmitmproxy»¹ÌṩÁËwebsocketsÖ§³Ö¡¢t
Python ʹÓà mitmproxy ½Ì³Ì
ÏÈÈÝ
mitmproxy ÊÇÒ»¿îÇ¿Ê¢µÄ HTTP ÊðÀí¹¤¾ß£¬¿ÉÓÃÓÚ×èµ²¡¢Ð޸ĺÍÖØ·Å HTTP Á÷Á¿¡£ËüÔÚ Python Öбàд£¬Îª¹¹½¨×Ô½ç˵ÊðÀí¾ç±¾ÌṩÁ˸߶ȵĿɶ¨ÖÆÐÔ¡£±¾ÎĽ«Ö¸µ¼ÄúÔõÑùʹÓà Python ÉèÖúÍʹÓà mitmproxy¡£
×°ÖÃ
Á¬Ã¦Ñ§Ï°¡°PythonÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ʹÓà pip ×°Öà mitmproxy£º
pip install mitmproxy
µÇ¼ºó¸´ÖÆ
ÉèÖÃÊðÀí
ÒªÉèÖÃÊðÀí£¬ÇëÔËÐÐÒÔÏÂÏÂÁ
mitmproxy
µÇ¼ºó¸´ÖÆ
Õ⽫Æô¶¯ mitmproxy ЧÀÍ£¬²¢½«ÆäÓÃ×÷ÍâµØÊðÀí¡£Äú¿ÉÒÔÔÚä¯ÀÀÆ÷»òÆäËûÓ¦ÓóÌÐòÖÐÉèÖÃÊðÀíÉèÖÃÀ´Ê¹ÓôËÊðÀí¡£
×èµ²ÇëÇóºÍÏìÓ¦
mitmproxy ÌṩÁËÇ¿Ê¢µÄ API£¬ÓÃÓÚ×èµ²ºÍÐÞ¸Ä HTTP ÇëÇóºÍÏìÓ¦¡£Äú¿ÉÒÔ±àд×Ô½ç˵½ÅÔÀ´ÊµÏÖËùÐèµÄ²Ù×÷¡£
ÒªÔÚ Python ÖÐ×èµ²Á÷Á¿£¬Ç뽨ÉèÒÔϾ籾£º
from mitmproxy import http def request(flow: http.HTTPFlow): # ÔÚ´ËËæ´¦ÀíÇëÇó print(flow.request.url) def response(flow: http.HTTPFlow): # ÔÚ´ËËæ´¦ÀíÏìÓ¦ print(flow.response.text)
µÇ¼ºó¸´ÖÆ
½«´Ë¾ç±¾ÉúÑÄÔÚÎļþÖУ¬ÀýÈç my_script.py£¬È»ºóͨ¹ýÒÔÏÂÏÂÁîÔËÐÐ mitmproxy ²¢¼ÓÔؾ籾£º
mitmproxy --set addons=my_script.py
µÇ¼ºó¸´ÖÆ
Ð޸ĺÍÖØ·ÅÇëÇó
³ýÁË×èµ²Á÷Á¿£¬Äú»¹¿ÉÒÔʹÓà mitmproxy Ð޸ĺÍÖØ·ÅÇëÇó¡£
ÒªÐÞ¸ÄÇëÇó£¬ÇëÔÚ request() º¯ÊýÖиüÐÂÇëÇ󹤾ߣº
def request(flow: http.HTTPFlow): flow.request.url = "https://example.com/"
µÇ¼ºó¸´ÖÆ
ÒªÖØ·ÅÇëÇó£¬ÇëʹÓà replay() º¯Êý£º
from mitmproxy import script class ReplayFlow(script.Abstraction): def replay(self, flow): self.master.replay_request(flow)
µÇ¼ºó¸´ÖÆ
¸ß¼¶¹¦Ð§
mitmproxy ÌṩÁËÆÕ±éµÄ¸ß¼¶¹¦Ð§£¬°üÀ¨£º
- WebSockets Ö§³Ö
- TCP ÊðÀí
- WebSocket ÖÕ¶Ë
- Á÷Á¿×ª´¢ºÍÖز¥
Äú¿ÉÒÔͨ¹ý¹Ù·½ÎĵµÉó²éÕâЩ¹¦Ð§µÄÏêϸÐÅÏ¢£ºhttps://docs.mitmproxy.org/
½áÂÛ
Python ÖÐµÄ mitmproxy Ϊ¿ª·¢×Ô½ç˵ HTTP ÊðÀí¾ç±¾ÌṩÁËÇ¿Ê¢µÄ¹¤¾ß¡£±¾½Ì³ÌÌṩÁËÉèÖúÍʹÓà mitmproxy µÄ»ù´¡ÖªÊ¶£¬ÏêϸÏÈÈÝÁË×èµ²¡¢Ð޸ĺÍÖØ·ÅÁ÷Á¿µÄÊÖÒÕ¡£ÕÆÎÕÁËÕâЩ»ù´¡ÖªÊ¶£¬Äú¾Í¿ÉÒÔ¹¹½¨ÖØ´óµÄÊðÀí½ÅÔÀ´Öª×ãÖÖÖÖÐèÇó¡£
ÒÔÉϾÍÊÇpythonʹÓÃmitmproxy½Ì³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡