Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. const CDP = require('chrome-remote-interface');
  2. const findProcess = require('find-process');
  3.  
  4. const processCmdRegex = /Slack\.app\/Contents\/MacOS\/Slack( .*)?$/;
  5. const browserViewJs = `
  6. alert('hi');
  7. `;
  8. const mainProcessJs = `(() => {
  9. // Although require() is not exposed in global scope, you can find some interesting modules
  10. // by traversing from process.mainModule
  11. function findModule(rootModule, pattern) {
  12. let queue = [rootModule];
  13. const visited = new WeakSet(queue);
  14.  
  15. while (queue.length > 0) {
  16. const module = queue.shift();
  17. if (module.filename && module.filename.match(pattern)) {
  18. return module;
  19. }
  20.  
  21. if (module.children) {
  22. const uniqueChildren = module.children.filter(child => !visited.has(child));
  23. uniqueChildren.forEach(child => visited.add(child));
  24. queue = queue.concat(uniqueChildren);
  25. }
  26. }
  27.  
  28. return null;
  29. }
  30. function req(pattern) {
  31. const root = global.process.mainModule;
  32. const module = findModule(root, pattern);
  33. return module && module.exports;
  34. }
  35.  
  36. /////////////////////////
  37.  
  38. const electron = req(/electron\.js$/);
  39. const BrowserView = electron.BrowserView;
  40.  
  41. // We don't know what the id of the browserview will be, but it's usually small.
  42. // For the sake of this demo just take the first one we find.
  43. let bv = null;
  44. for (let i = 1; i < 10; i++) {
  45. try {
  46. bv = BrowserView.fromId(i);
  47. if (bv) {
  48. break;
  49. }
  50. } catch (err) {}
  51. }
  52.  
  53. // Execute JS in the browserview
  54. const browserViewJs = ${JSON.stringify(browserViewJs)};
  55. bv.webContents.executeJavaScript(browserViewJs);
  56.  
  57. bv.webContents.openDevTools();
  58.  
  59. console.log('hi');
  60. global.xxx = true;
  61. })();`;
  62. const defaultPort = 9229;
  63.  
  64. async function main() {
  65. // Find process
  66. const processes = await findProcess('name', '');
  67. const proc = processes.filter(p => p.cmd.match(processCmdRegex))[0];
  68. if (!proc) {
  69. throw new Error('cant find process');
  70. }
  71. const pid = proc.pid;
  72. console.log('found process', pid, proc.cmd);
  73.  
  74. // Enable debugger
  75. process.kill(pid, 'SIGUSR1');
  76.  
  77. // Connect debugger
  78. const client = await CDP({port: defaultPort, host: 'localhost'});
  79.  
  80. // Inject code
  81. const result = await client.Runtime.evaluate({expression: mainProcessJs});
  82. }
  83.  
  84. main().then(() => process.exit(0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement