· last year · Feb 25, 2024, 02:55 AM
1/*
2
3fazenda.gov.br bypass example with Anti Captcha.
4
5IMPORTANT!
61. Make sure "const userAgent" is same as in your Chromium instance
72. Set your API key ("apiKey")
83. Set correct form password value ("formPassword")
94. The page is using Hcaptcha Enterprise. This is why we do several attempts to bypass the screen (howManyTestsToMake value).
105. If you want to see what's happening, replace "headless: true" with "headless: false".
11
12Install dependencies:
13
14npm install @antiadmin/anticaptchaofficial puppeteer
15
16
17* */
18
19
20const pup = require('puppeteer')
21
22const anticaptcha = require("@antiadmin/anticaptchaofficial");
23
24//screen with captcha
25const captchaUrl = 'https://www.nfe.fazenda.gov.br/portal/consultaRecaptcha.aspx?tipoConsulta=resumo&tipoConteudo=7PhJ+gAVw2g=';
26
27//address behind captcha
28const checkUrl = 'https://www.nfe.fazenda.gov.br/portal/consultaResumo.aspx?tipoConteudo=7PhJ+gAVw2g=';
29
30//Anti-captcha.com API key
31const apiKey = 'API_KEY_HERE';
32
33const formPassword = '44_DIGIT_CODE_HERE_44_DIGIT_CODE_HERE_44_DIG';
34
35const howManyTestsToMake = 10;
36
37let browser = null;
38let page = null;
39let success = 0;
40let fail = 0;
41let userAgent = '';
42
43// STOP! IMPORTANT! Shared proxy services won't work!
44// Use ONLY self-installed proxies on your own infrastructure! Instruction: https://anti-captcha.com/apidoc/articles/how-to-install-squid
45const proxyAddress = '1.2.3.4';
46const proxyPort = 1234;
47const proxyLogin = 'login';
48const proxyPassword = 'pass';
49
50(async () => {
51
52 anticaptcha.setAPIKey(apiKey);
53 anticaptcha.shutUp(); //comment for verbose captcha recognition
54 const balance = await anticaptcha.getBalance();
55 if (balance <= 0) {
56 console.log('Topup your anti-captcha.com balance!');
57 return;
58 } else {
59 console.log('API key balance is '+balance+', continuing');
60 }
61
62 for (let i=0;i<howManyTestsToMake;i++) {
63
64 console.log("\nSolving HCaptcha with Anti-Captcha.Com ..");
65
66 let hcaptchaResponse = null;
67 try {
68 hcaptchaResponse = await anticaptcha.solveHCaptchaProxyOn(
69 captchaUrl,
70 'e72d2f82-9594-4448-a875-47ded9a1898a',
71 'http',
72 proxyAddress,
73 proxyPort,
74 proxyLogin,
75 proxyPassword,
76 '',
77 '',
78 {}
79 );
80 userAgent = anticaptcha.getHcaptchaUserAgent();
81 } catch (e) {
82 console.error("could not solve captcha: " + e.toString());
83 return;
84 }
85 console.log('hcaptchaResponse:', hcaptchaResponse);
86
87
88 try {
89 console.log('opening browser ..');
90
91
92 let options = {
93 headless: false,
94 ignoreDefaultArgs: ["--disable-extensions", "--enable-automation"],
95 devtools: true,
96 args: [
97 `--proxy-server=${proxyAddress}:${proxyPort}`,
98 '--disable-web-security',
99 '--disable-features=IsolateOrigins,site-per-process',
100 '--allow-running-insecure-content',
101 '--disable-blink-features=AutomationControlled',
102 '--no-sandbox',
103 '--mute-audio',
104 '--no-zygote',
105 '--no-xshm',
106 '--window-size=1920,1080',
107 '--no-first-run',
108 '--no-default-browser-check',
109 '--disable-dev-shm-usage',
110 '--disable-gpu',
111 '--enable-webgl',
112 '--ignore-certificate-errors',
113 '--lang=en-US,en;q=0.9',
114 '--password-store=basic',
115 '--disable-gpu-sandbox',
116 '--disable-software-rasterizer',
117 '--disable-background-timer-throttling',
118 '--disable-backgrounding-occluded-windows',
119 '--disable-renderer-backgrounding',
120 '--disable-infobars',
121 '--disable-breakpad',
122 '--disable-canvas-aa',
123 '--disable-2d-canvas-clip-aa',
124 '--disable-gl-drawing-for-tests',
125 '--enable-low-end-device-mode',
126 '--no-sandbox'
127 ]
128 };
129 browser = await pup.launch(options);
130
131
132 console.log('creating new page ..');
133 page = await browser.newPage();
134
135 console.log('setting browser user agent to ',userAgent);
136 await page.setUserAgent(userAgent);
137
138 await page.evaluateOnNewDocument(() => {
139 delete navigator.__proto__.webdriver;
140 });
141
142 if (proxyPassword && proxyLogin) {
143 console.log(`setting proxy authentication ${proxyLogin}:${proxyPassword}`);
144 await page.authenticate({
145 username: proxyLogin,
146 password: proxyPassword,
147 });
148 }
149 } catch (e) {
150 console.error("could not open browser: " + e);
151 return false;
152 }
153
154 //screen size
155 await page.setViewport({width: 1360, height: 1000});
156
157
158 try {
159 await page.goto(captchaUrl, {
160 waitUntil: "networkidle0",
161 timeout: 300000
162 });
163 } catch (e) {
164 console.log('err while loading the page: ' + e);
165 }
166
167 console.log('Filling h-captcha-response');
168 await page.evaluate(async (hcaptchaResponse) => {
169
170 document.getElementsByName('h-captcha-response')[0].value = hcaptchaResponse;
171
172 }, hcaptchaResponse);
173
174 console.log('Filling password ',formPassword);
175 await page.type('#ctl00_ContentPlaceHolder1_txtChaveAcessoResumo', formPassword);
176
177
178 console.log('Waiting 3 seconds...');
179 await delay(3000);
180
181 console.log('Submitting form');
182 await page.click('#ctl00_ContentPlaceHolder1_btnConsultarHCaptcha');
183
184
185 console.log('Waiting 3 seconds...');
186 await delay(3000);
187
188
189 console.log('Checking if we are on correct page');
190 let currentUrl = await page.evaluate(async () => {
191 return new Promise((resolve => {
192 resolve(document.location.href);
193 }))
194 });
195 if (currentUrl === checkUrl) {
196 console.log('SUCCESS: We are on the target page ' + checkUrl);
197 success++;
198 } else {
199 console.log('FAIL: We are not on the target page: ' + currentUrl);
200 await anticaptcha.reportIncorrectHcaptcha();
201 fail++;
202 }
203
204 console.log('success = ',success);
205 console.log('fail = ',fail);
206
207
208 await delay(3000);
209
210 await browser.close();
211
212 }
213
214
215
216})();
217
218function delay(time) {
219 return new Promise(function(resolve) {
220 setTimeout(resolve, time)
221 });
222}