Path: blob/master/app/finders/wp_items/urls_in_page.rb
485 views
# frozen_string_literal: true12module WPScan3module Finders4module WpItems5# URLs In Homepage Module to use in plugins & themes finders6module UrlsInPage7# @param [ String ] type plugins / themes8# @param [ Boolean ] uniq Wether or not to apply the #uniq on the results9#10# @return [ Array<String> ] The plugins/themes detected in the href, src attributes of the page11def items_from_links(type, uniq: true)12found = []13xpath = format(14'(//@href|//@src|//@data-src)[contains(., "%s")]',15type == 'plugins' ? target.plugins_dir : target.content_dir16)1718target.in_scope_uris(page_res, xpath) do |uri|19next unless uri.to_s =~ item_attribute_pattern(type)2021slug = Regexp.last_match[1]&.strip2223found << slug unless slug&.empty?24end2526uniq ? found.uniq.sort : found.sort27end2829# @param [ String ] type plugins / themes30# @param [ Boolean ] uniq Wether or not to apply the #uniq on the results31#32# @return [Array<String> ] The plugins/themes detected in the javascript/style of the homepage33def items_from_codes(type, uniq: true)34found = []3536page_res.html.xpath('//script[not(@src)]|//style[not(@src)]').each do |tag|37code = tag.text.to_s38next if code.empty?3940code.scan(item_code_pattern(type)).flatten.uniq.each { |slug| found << slug }41end4243uniq ? found.uniq.sort : found.sort44end4546# @param [ String ] type47#48# @return [ Regexp ]49def item_attribute_pattern(type)50@item_attribute_pattern ||= %r{#{item_url_pattern(type)}([^/]+)/}i51end5253# @param [ String ] type54#55# @return [ Regexp ]56def item_code_pattern(type)57@item_code_pattern ||= %r{["'( ]#{item_url_pattern(type)}([^\\/)"']+)}i58end5960# @param [ String ] type61#62# @return [ Regexp ]63def item_url_pattern(type)64item_dir = type == 'plugins' ? target.plugins_dir : target.content_dir65item_url = type == 'plugins' ? target.plugins_url : target.content_url6667url = /#{item_url.gsub(/\A(?:https?)/i, 'https?').gsub('/', '\\\\\?\/')}/i68item_dir = %r{(?:#{url}|\\?/#{item_dir.gsub('/', '\\\\\?\/')}\\?/)}i6970type == 'plugins' ? item_dir : %r{#{item_dir}#{type}\\?/}i71end72end73end74end75end767778