安装: pip install mitmproxy
新建一个脚本
脚本代码:
1 from mitmproxy import ctx 2 3 injected_javascript = ‘‘‘ 4 // overwrite the `languages` property to use a custom getter 5 Object.defineProperty(navigator, "languages", { 6 get: function() { 7 return ["zh-CN","zh","zh-TW","en-US","en"]; 8 } 9 });10 // Overwrite the `plugins` property to use a custom getter.11 Object.defineProperty(navigator, ‘plugins‘, {12 get: () => [1, 2, 3, 4, 5],13 });14 // Pass the Webdriver test15 Object.defineProperty(navigator, ‘webdriver‘, {16 get: () => false,17 });18 // Pass the Chrome Test.19 // We can mock this in as much depth as we need for the test.20 window.navigator.chrome = {21 runtime: {},22 // etc.23 };24 // Pass the Permissions Test.25 const originalQuery = window.navigator.permissions.query;26 window.navigator.permissions.query = (parameters) => (27 parameters.name === ‘notifications‘ ?28 Promise.resolve({ state: Notification.permission }) :29 originalQuery(parameters)30 );31 ‘‘‘32 33 def response(flow):34 # Only process 200 responses of HTML content.35 if not flow.response.status_code == 200:36 return37 38 # Inject a script tag containing the JavaScript.39 html = flow.response.text40 html = html.replace(‘<head>‘, ‘<head><script>%s</script>‘ % injected_javascript)41 flow.response.text = str(html)42 ctx.log.info(‘插入成功 ‘)43
然后运行mitmdump
mitmdump -s xxxx.py
默认为8080端口,将webDriver设置代理127.0.0.1:8080
然后就能监听和修改selenium特征了。