Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const CDP = require('chrome-remote-interface');
- const findProcess = require('find-process');
- const processCmdRegex = /Slack\.app\/Contents\/MacOS\/Slack( .*)?$/;
- const browserViewJs = `
- alert('hi');
- `;
- const mainProcessJs = `(() => {
- // Although require() is not exposed in global scope, you can find some interesting modules
- // by traversing from process.mainModule
- function findModule(rootModule, pattern) {
- let queue = [rootModule];
- const visited = new WeakSet(queue);
- while (queue.length > 0) {
- const module = queue.shift();
- if (module.filename && module.filename.match(pattern)) {
- return module;
- }
- if (module.children) {
- const uniqueChildren = module.children.filter(child => !visited.has(child));
- uniqueChildren.forEach(child => visited.add(child));
- queue = queue.concat(uniqueChildren);
- }
- }
- return null;
- }
- function req(pattern) {
- const root = global.process.mainModule;
- const module = findModule(root, pattern);
- return module && module.exports;
- }
- /////////////////////////
- const electron = req(/electron\.js$/);
- const BrowserView = electron.BrowserView;
- // We don't know what the id of the browserview will be, but it's usually small.
- // For the sake of this demo just take the first one we find.
- let bv = null;
- for (let i = 1; i < 10; i++) {
- try {
- bv = BrowserView.fromId(i);
- if (bv) {
- break;
- }
- } catch (err) {}
- }
- // Execute JS in the browserview
- const browserViewJs = ${JSON.stringify(browserViewJs)};
- bv.webContents.executeJavaScript(browserViewJs);
- bv.webContents.openDevTools();
- console.log('hi');
- global.xxx = true;
- })();`;
- const defaultPort = 9229;
- async function main() {
- // Find process
- const processes = await findProcess('name', '');
- const proc = processes.filter(p => p.cmd.match(processCmdRegex))[0];
- if (!proc) {
- throw new Error('cant find process');
- }
- const pid = proc.pid;
- console.log('found process', pid, proc.cmd);
- // Enable debugger
- process.kill(pid, 'SIGUSR1');
- // Connect debugger
- const client = await CDP({port: defaultPort, host: 'localhost'});
- // Inject code
- const result = await client.Runtime.evaluate({expression: mainProcessJs});
- }
- main().then(() => process.exit(0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement