js注入代码 ,命令行:mitmdump -s js.py
Python
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
rom mitmproxy import ctxinjected_javascript = '''// overwrite the `languages` property to use a custom getterObject.defineProperty(navigator, "languages", { get: function() { return ["zh-CN","zh","zh-TW","en-US","en"]; }}); // Overwrite the `plugins` property to use a custom getter.Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5],}); // Pass the Webdriver testObject.defineProperty(navigator, 'webdriver', { get: () => false,}); // Pass the Chrome Test.// We can mock this in as much depth as we need for the test.window.navigator.chrome = { runtime: {}, // etc.}; // Pass the Permissions Test.const originalQuery = window.navigator.permissions.query;window.navigator.permissions.query = (parameters) => ( parameters.name === 'notifications' ? Promise.resolve({ state: Notification.permission }) : originalQuery(parameters));''' def response(flow): # Only process 200 responses of HTML content. if not flow.response.status_code == 200: return # Inject a script tag containing the JavaScript. html = flow.response.text html = html.replace('', '%s' % injected_javascript) flow.response.text = str(html) ctx.log.info('>>>> js代码插入成功 |