Class: Wordstress::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/wordstress/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "") ⇒ Site

Returns a new instance of Site.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wordstress/site.rb', line 7

def initialize(name="")
  begin
    @uri      = URI(name)
    @raw_name = name
    @valid    = true
  rescue
    @valid = false
  end

  @robots_txt   = get(@raw_name + "/robots.txt")
  @readme_html  = get(@raw_name + "/readme.html")
  @homepage     = get(@raw_name)
  @version      = detect_version

  @wp_vuln_json = get_wp_vulnerabilities  unless @version[:version] == "0.0.0"
  @wp_vuln_json = Hash.new.to_json        if @version[:version] == "0.0.0"
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/wordstress/site.rb', line 6

def version
  @version
end

#wp_vuln_jsonObject (readonly)

Returns the value of attribute wp_vuln_json.



6
7
8
# File 'lib/wordstress/site.rb', line 6

def wp_vuln_json
  @wp_vuln_json
end

Instance Method Details

#detect_versionObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wordstress/site.rb', line 36

def detect_version

  #
  # 1. trying to detect wordpress version from homepage body meta generator
  # tag

  v_meta = ""
  doc = Nokogiri::HTML(@homepage.body)
  doc.xpath("//meta[@name='generator']/@content").each do |attr|
    v_meta = attr.value.split(' ')[1]
  end

  #
  # 2. trying to detect wordpress version from readme.html in the root
  # directory

  v_readme = ""
  doc = Nokogiri::HTML(@readme_html.body)
  v_readme = doc.at_css('h1').children.last.text.chop.lstrip.split(' ')[1]

  v_rss = ""
  rss_doc = Nokogiri::HTML(@homepage.body)
  rss = Nokogiri::HTML(get(rss_doc.css('link[type="application/rss+xml"]').first.attr('href')).body)

  v_rss= rss.css('generator').text.split('=')[1]

  return {:version => v_meta, :accuracy => 1.0} if v_meta == v_readme && v_meta == v_rss
  return {:version => v_meta, :accuracy => 0.8} if v_meta == v_readme || v_meta == v_rss

  # we failed detecting wordpress version
  return {:version => "0.0.0", :accuracy => 0}
end

#get(page) ⇒ Object



69
70
71
72
# File 'lib/wordstress/site.rb', line 69

def get(page)
  return get_http(page)   if @uri.scheme == "http"
  return get_https(page)  if @uri.scheme == "https"
end

#get_wp_vulnerabilitiesObject



25
26
27
# File 'lib/wordstress/site.rb', line 25

def get_wp_vulnerabilities
  get_https("https://wpvulndb.com/api/v1/wordpresses/#{version_pad(@version[:version])}").body
end

#is_valid?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/wordstress/site.rb', line 74

def is_valid?
  return @valid
end

#version_pad(version) ⇒ Object



29
30
31
32
33
34
# File 'lib/wordstress/site.rb', line 29

def version_pad(version)
  # 3.2.1 => 321
  # 4.0 => 400
  return version.gsub('.', '')      if version.split('.').count == 3
  return version.gsub('.', '')+'0'  if version.split('.').count == 2
end