1.启动设置

const browser = await puppeteer.launch({
    headless: true,
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-blink-features=AutomationControlled',
    ],
    dumpio: false,
});

2.webdriver

// webdriver
await page.evaluateOnNewDocument(() => {
    const newProto = navigator.__proto__;
    delete newProto.webdriver; //删除 navigator.webdriver字段
    navigator.__proto__ = newProto;
});

3.window.chrome

// 添加 window.chrome字段,向内部填充一些值
await page.evaluateOnNewDocument(() => {
    window.chrome = {};
    window.chrome.app = {
        InstallState: 'hehe',
        RunningState: 'haha',
        getDetails: 'xixi',
        getIsInstalled: 'ohno',
    };
    window.chrome.csi = function () {};
    window.chrome.loadTimes = function () {};
    window.chrome.runtime = function () {};
});

4.userAgent

// userAgent设置
await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'userAgent', {
        //userAgent在无头模式下有headless字样,所以需覆盖
        get: () =>
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
    });
});

5.plugins

// plugins设置
await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'plugins', {
        //伪装真实的插件信息
        get: () => [
        {
            0: {
            type: 'application/x-google-chrome-pdf',
            suffixes: 'pdf',
            description: 'Portable Document Format',
            enabledPlugin: Plugin,
            },
            description: 'Portable Document Format',
            filename: 'internal-pdf-viewer',
            length: 1,
            name: 'Chrome PDF Plugin',
        },
        {
            0: {
            type: 'application/pdf',
            suffixes: 'pdf',
            description: '',
            enabledPlugin: Plugin,
            },
            description: '',
            filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai',
            length: 1,
            name: 'Chrome PDF Viewer',
        },
        {
            0: {
            type: 'application/x-nacl',
            suffixes: '',
            description: 'Native Client Executable',
            enabledPlugin: Plugin,
            },
            1: {
            type: 'application/x-pnacl',
            suffixes: '',
            description: 'Portable Native Client Executable',
            enabledPlugin: Plugin,
            },
            description: '',
            filename: 'internal-nacl-plugin',
            length: 2,
            name: 'Native Client',
        },
        ],
    });
});

6.languages

// languages设置
await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'languages', {
        //添加语言
        get: () => ['zh-CN', 'zh', 'en'],
    });
});

7.permissions

// permissions设置
await page.evaluateOnNewDocument(() => {
    const originalQuery = window.navigator.permissions.query; //notification伪装
    window.navigator.permissions.query = (parameters) =>
        parameters.name === 'notifications'
        ? Promise.resolve({ state: Notification.permission })
        : originalQuery(parameters);
});

8.WebGL

// WebGL设置
await page.evaluateOnNewDocument(() => {
    const getParameter = WebGLRenderingContext.getParameter;
    WebGLRenderingContext.prototype.getParameter = function (parameter) {
        // UNMASKED_VENDOR_WEBGL
        if (parameter === 37445) {
            return 'Intel Inc.';
        }
        // UNMASKED_RENDERER_WEBGL
        if (parameter === 37446) {
            return 'Intel(R) Iris(TM) Graphics 6100';
        }
        return getParameter(parameter);
    };
});

来源:https://blog.csdn.net/Revivedsun/article/details/81785000

https://intoli.com/blog/not-possible-to-block-chrome-headless/
https://intoli.com/blog/making-chrome-headless-undetectable/
https://github.com/infosimples/detect-headless
https://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today

作者 铁血 汉子 2022年2月17日
2024/12/03/05:19:34pm 2022/2/17/9:39:08
0 2970