Skip to content

Commit 37a55cc

Browse files
committed
Abstracted version comparison code
1 parent 31cdd75 commit 37a55cc

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-47
lines changed

lib/msf/http/wordpress/version.rb

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,7 @@ def check_theme_version_from_style(theme_name, fixed_version = nil, vuln_introdu
6868
# No style.css file present
6969
return Msf::Exploit::CheckCode::Unknown if res.nil? || res.code != 200
7070

71-
# Try to extract version from style.css
72-
# Example line:
73-
# Version: 1.5.2
74-
version = res.body.to_s[/(?:Version):\s*([0-9a-z.-]+)/i, 1]
75-
76-
# style.css present, but no version number
77-
return Msf::Exploit::CheckCode::Detected if version.nil?
78-
79-
vprint_status("#{peer} - Found version #{version} of the theme")
80-
81-
if fixed_version.nil?
82-
if vuln_introduced_version.nil?
83-
# All versions are vulnerable
84-
return Msf::Exploit::CheckCode::Appears
85-
elsif Gem::Version.new(version) >= Gem::Version.new(vuln_introduced_version)
86-
# Newer or equal to the version it was introduced
87-
return Msf::Exploit::CheckCode::Appears
88-
else
89-
return Msf::Exploit::CheckCode::Safe
90-
end
91-
else
92-
# Version older than fixed version
93-
if Gem::Version.new(version) < Gem::Version.new(fixed_version)
94-
if vuln_introduced_version.nil?
95-
# All previous versions are vulnerable
96-
return Msf::Exploit::CheckCode::Appears
97-
# vuln_introduced_version provided, check if version is newer
98-
elsif Gem::Version.new(version) >= Gem::Version.new(vuln_introduced_version)
99-
return Msf::Exploit::CheckCode::Appears
100-
else
101-
# Not in range, nut vulnerable
102-
return Msf::Exploit::CheckCode::Safe
103-
end
104-
# version newer than fixed version
105-
else
106-
return Msf::Exploit::CheckCode::Safe
107-
end
108-
end
71+
return extract_and_check_version(res.body.to_s, :style, :theme, fixed_version, vuln_introduced_version)
10972
end
11073

11174
# Checks a readme for a vulnerable version
@@ -156,20 +119,70 @@ def check_version_from_readme(type, name, fixed_version, vuln_introduced_version
156119
'uri' => readme_url,
157120
'method' => 'GET'
158121
)
122+
end
123+
124+
if res.nil? || res.code != 200
125+
# No readme.txt or Readme.txt present for plugin
126+
return Msf::Exploit::CheckCode::Unknown if type == :plugin
127+
128+
# Try again using the style.css file
129+
return check_theme_version_from_style(name, fixed_version, vuln_introduced_version) if type == :theme
130+
end
131+
132+
version_res = extract_and_check_version(res.body.to_s, :readme, type, fixed_version, vuln_introduced_version)
133+
if version_res == Msf::Exploit::CheckCode::Detected && type == :theme
134+
# If no version could be found in readme.txt for a theme, try style.css
135+
return check_theme_version_from_style(name, fixed_version, vuln_introduced_version)
136+
else
137+
return version_res
138+
end
139+
end
140+
141+
def extract_and_check_version(body, type, item_type, fixed_version = nil, vuln_introduced_version = nil)
142+
case type
143+
when :readme
144+
# Try to extract version from readme
145+
# Example line:
146+
# Stable tag: 2.6.6
147+
version = body[/(?:stable tag|version):\s*(?!trunk)([0-9a-z.-]+)/i, 1]
148+
when :style
149+
# Try to extract version from style.css
150+
# Example line:
151+
# Version: 1.5.2
152+
version = body[/(?:Version):\s*([0-9a-z.-]+)/i, 1]
153+
else
154+
fail("Unknown file type #{type}")
155+
end
159156

160-
# no Readme.txt present
161-
return Msf::Exploit::CheckCode::Unknown if res.nil? || res.code != 200
157+
version_res = extract_and_check_version(res.body.to_s, :readme, type, fixed_version, vuln_introduced_version)
158+
if version_res == Msf::Exploit::CheckCode::Detected && type == :theme
159+
# If no version could be found in readme.txt for a theme, try style.css
160+
return check_theme_version_from_style(name, fixed_version, vuln_introduced_version)
161+
else
162+
return version_res
162163
end
164+
end
163165

164-
# try to extract version from readme
165-
# Example line:
166-
# Stable tag: 2.6.6
167-
version = res.body.to_s[/(?:stable tag|version):\s*(?!trunk)([0-9a-z.-]+)/i, 1]
166+
def extract_and_check_version(body, type, item_type, fixed_version = nil, vuln_introduced_version = nil)
167+
case type
168+
when :readme
169+
# Try to extract version from readme
170+
# Example line:
171+
# Stable tag: 2.6.6
172+
version = body[/(?:stable tag|version):\s*(?!trunk)([0-9a-z.-]+)/i, 1]
173+
when :style
174+
# Try to extract version from style.css
175+
# Example line:
176+
# Version: 1.5.2
177+
version = body[/(?:Version):\s*([0-9a-z.-]+)/i, 1]
178+
else
179+
fail("Unknown file type #{type}")
180+
end
168181

169-
# readme present, but no version number
182+
# Could not identify version number
170183
return Msf::Exploit::CheckCode::Detected if version.nil?
171184

172-
vprint_status("#{peer} - Found version #{version} of the #{type}")
185+
vprint_status("#{peer} - Found version #{version} of the #{item_type}")
173186

174187
if fixed_version.nil?
175188
if vuln_introduced_version.nil?

modules/auxiliary/admin/http/wp_wplms_privilege_escalation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def initialize(info = {})
4444
end
4545

4646
def check
47-
check_theme_version_from_style('wplms', '1.8.4.2', '1.5.2')
47+
check_theme_version_from_readme('wplms', '1.8.4.2', '1.5.2')
4848
end
4949

5050
def username

0 commit comments

Comments
 (0)