mdestroy

Captcha Solving code

Aug 29th, 2020 (edited)
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // NodeJS code for automating Puppeteer to register an account on Reddit avoiding Captcha challenge
  2. // Install node 10.x repository to the system
  3. // curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
  4. // download & install nodejs 10.x along with npm
  5. // $ sudo apt install nodejs
  6. // Install puppeteer
  7. // $ mkdir your-project
  8. // $ cd your-project
  9. // $ npm install puppeteer
  10. // $ npm install request-promise-native
  11. // $ npm install promise-poller
  12. // create a file index.js in your folder, then paste the following code
  13. // More information on https://homputersecurity.com/2020/08/31/comment-contourner-les-challenges-captcha-avec-le-service-2captcha/
  14.  
  15. const puppeteer = require('puppeteer');
  16. const chromeOptions = {
  17. headless:false,
  18. defaultViewport: null,
  19. slowMo:10
  20. };
  21. const siteDetails = {
  22. sitekey: '6LeTnxkTAAAAAN9QEuDZRpn90WwKk_R1TRW_g-JC',
  23. pageurl: 'https://old.reddit.com/login'
  24. };
  25. const request = require('request-promise-native');
  26. const poll = require('promise-poller').default;
  27. const apiKey = '$user-apikey';
  28. const Username = '$intented-username';
  29. const Password = '$intended-password';
  30. const Email = '$intended-email';
  31.  
  32. (async function main() {
  33.  
  34. try {
  35. const browser = await puppeteer.launch(chromeOptions);
  36. const page = await browser.newPage();
  37.  
  38. page.on('error', async function(err) {
  39. console.log('PageOnError: caught!');
  40. });
  41.  
  42. await new Promise(async function(resolve, reject) {
  43. try {
  44. await page.goto('https://old.reddit.com/login');
  45. const requestId = await initiateCaptchaRequest(apiKey);
  46. await page.type('#user_reg', Username);
  47. await page.type('#passwd_reg', Password);
  48. await page.type('#passwd2_reg', Password);
  49. await page.type('#email_reg', Email);
  50. await page.click('#rem_reg');
  51. await page.click('#newsletter_subscribe');
  52. const response = await pollForRequestResults(apiKey, requestId);
  53. console.log(`Entering recaptcha response ${response}`);
  54. await page.evaluate(`document.getElementById("g-recaptcha-response").innerHTML="${response}";`);
  55.  
  56. page.click('#register-form button[type=submit]');
  57. } catch (err) {
  58. reject(err);
  59. }
  60. });
  61. } catch (err) {
  62. console.log(err);
  63. }
  64. })();
  65.  
  66. async function initiateCaptchaRequest(apiKey) {
  67. const formData = {
  68. method: 'userrecaptcha',
  69. googlekey: siteDetails.sitekey,
  70. key: apiKey,
  71. pageurl: siteDetails.pageurl,
  72. json: 1
  73. };
  74. const response = await request.post('http://2captcha.com/in.php', {form: formData});
  75. return JSON.parse(response).request;
  76. }
  77.  
  78. async function pollForRequestResults(key, id, retries = 100, interval = 1500, delay = 15000) {
  79. await timeout(delay);
  80. return poll({
  81. taskFn: requestCaptchaResults(key, id),
  82. interval,
  83. retries
  84. });
  85. }
  86.  
  87. function requestCaptchaResults(apiKey, requestId) {
  88. const url = `http://2captcha.com/res.php?key=${apiKey}&action=get&id=${requestId}&json=1`;
  89. return async function() {
  90. return new Promise(async function(resolve, reject){
  91. console.log(`Polling for response...`);
  92. const rawResponse = await request.get(url);
  93. const resp = JSON.parse(rawResponse);
  94. console.log(resp);
  95. if (resp.status === 0) return reject(resp.request);
  96. console.log('Response received');
  97. resolve(resp.request);
  98. });
  99. }
  100. }
  101.  
  102. const timeout = millis => new Promise(resolve => setTimeout(resolve, millis))
  103. ;
RAW Paste Data