Skip to content

Commit

Permalink
Merge pull request #68 from mbrg/feature/windows-puppeteer-support
Browse files Browse the repository at this point in the history
Feature/windows puppeteer support copilot hunter
  • Loading branch information
ofrin-zen authored Sep 29, 2024
2 parents a143881 + 68059b7 commit 604d75a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/powerpwn/copilot_studio/modules/deep_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,10 @@ def run_pup_commands(existing_bots: List[str]):
:param existing_bots: The list of bot urls needed to check
"""
pup_path = get_project_file_path("tools/pup_is_webchat_live", "is_chat_live.js")
if os.name == "nt": # Windows
pup_path = get_project_file_path("tools/pup_is_webchat_live", "is_chat_live_windows.js")
else:
pup_path = get_project_file_path("tools/pup_is_webchat_live", "is_chat_live.js")
# Construct the command to run the Node.js script
open_bots_path = get_project_file_path("final_results/", "chat_exists_output.txt")
# Empty the file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const puppeteer = require('puppeteer'); // v22.0.0 or later
const fs = require('fs').promises;
const path = require('path'); // Import the path module
const chalk = require('chalk'); // for colors in terminal texts
// todo: install chalk > 4

function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time)
});
}

(async () => {
const targetPageUrl = process.argv[2];
if (!targetPageUrl) {
console.error("Please provide the target page URL as an argument.");
process.exit(1);
}

const orange = chalk.hex('#FFA500'); // Define a custom orange color
let browser;
try {
browser = await puppeteer.launch({ headless: true, executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', args: ['--incognito']});
} catch(e) {
browser = await puppeteer.launch({ headless: true, args: ['--incognito']});
}
const [page] = await browser.pages();
const timeout = 30000;
page.setDefaultTimeout(timeout);

await page.setViewport({
width: 1920,
height: 1080
});

try {
await page.goto(targetPageUrl, {
waitUntil: 'networkidle2'
});

await delay(7000); // Wait for 7 second to avoid sync issues
await page.evaluate(() => { // Find and click the div button that says hello to start the chat
const button = Array.from(document.querySelectorAll('div')).find(el => el.textContent.trim() === 'Hello');
if (button) {
button.click();
}
});
await page.waitForSelector('div.webchat__bubble__content > div', { timeout: timeout });
const chatTexts = await page.evaluate(() => {
const elements = document.querySelectorAll('div.webchat__bubble__content > div')
return Array.from(elements).map(element => element.innerText);
});

const stringsToCheck = ["I’ll need you to sign in", "Error code:"];

const allStringsAbsent = stringsToCheck.every(str => chatTexts.every(text => !text.includes(str)));

if (allStringsAbsent) {
process.stdout.write(chalk.red(`Found open chatbot at: ${targetPageUrl}\n`));
const outputPath = path.resolve(__dirname, '../../final_results/chat_exists_output.txt');
await fs.appendFile(outputPath, targetPageUrl + '\n');
} else {
process.stdout.write(chalk.green("Found inaccessible chatbot.\n"));
}
} catch (e) {
if (e.name === 'TimeoutError') {
process.stdout.write(chalk.yellow(`Timeout occurred for URL: ${targetPageUrl}, rerun or test manually\n`));
} else {
process.stdout.write(orange("Error occurred while trying to find chat texts:", e));
}
}

await browser.close();
})().catch(err => {
console.error(err);
process.exit(1);
});

0 comments on commit 604d75a

Please sign in to comment.