如何尝试打印为其中有警报的URL时解决并发问题?(How to solve concurrency

2019-10-21 06:20发布

我试图通过一对夫妇的URL使用PhantomJS循环,打开网页,并检查是否有在任一页面的警报。 我也印刷发生警报的页面的URL。

该代码是如下:

var page = require('webpage');

var u = ["http://127.0.0.1/DVWA-1.0.8/vulnerabilities/xss_r/?name=<SCRIPT>alert('XSS');</SCRIPT>", "http://127.0.0.1/DVWA-1.0.8/vulnerabilities/xss_r/?name=abcd"]
var url = "";

for(var i = 0; i < u.length; i++) {
  url = u[i];

  var webpage = page.create();

  phantom.addCookie({
    'name':'PHPSESSID',
    'value':'00885b45d9ddda3e757371b177c5959b',
    'domain':'127.0.0.1'
  });

  webpage.onAlert = function(alertMessage){
    console.log("Alert URL: " + webpage.url);
    console.log("Alert occured with message: " + alertMessage);
  }

  webpage.open(url, function (status) {
    console.log("Opening URL:  " + webpage.url);
    phantom.exit();
  });
}

我希望输出的部分是:

Alert URL: http://127.0.0.1/DVWA-1.0.8/vulnerabilities/xss_r/?name=<SCRIPT>alert('XSS');</SCRIPT>
Alert occured with message: XSS

但是,相反,它每次都不同,像显示不正确的输出:

Alert URL: http://127.0.0.1/DVWA-1.0.8/vulnerabilities/xss_r/?name=abcd
Alert occured with message: XSS

它看起来像这种情况是因为回调的并发性。

有没有一种方法,以处理这个以确保如预期的输出? 或者是不是意味着这个库像这样使用,我应该尝试别的东西吗?

Answer 1:

您可以在回路基本上是一个新的标签只要PhantomJS运行而言创造了新的一页。 这将同时进行。 要添加结构和重现性的脚本,你必须写一个小链接。

像这样的东西应该工作:

var page = require('webpage');

var u = ["http://127.0.0.1/DVWA-1.0.8/vulnerabilities/xss_r/?name=<SCRIPT>alert('XSS');</SCRIPT>", "http://127.0.0.1/DVWA-1.0.8/vulnerabilities/xss_r/?name=abcd"]

function each(list, func, done){
  var len = list.length,
      previous = done;
  for(var i = len - 1; i >= 0; i--) {
    (function(item, done){ // IIFE
      previous = function(){
        func(item, done);
      };
    })(list[i], previous);
  }
  previous(); // start the chain
}

each(u, function(url, done){
  var webpage = page.create();

  phantom.addCookie({
    'name':'PHPSESSID',
    'value':'00885b45d9ddda3e757371b177c5959b',
    'domain':'127.0.0.1'
  });

  webpage.onAlert = function(alertMessage){
    console.log("Alert URL: " + webpage.url);
    console.log("Alert occured with message: " + alertMessage);
  }

  webpage.open(url, function (status) {
    console.log("Opening URL:  " + webpage.url);
    done();
  });
}, function(){
  phantom.exit();
});


文章来源: How to solve concurrency problems when trying to print the URLs for which there was an alert?