Skip to content

Commit 463e77f

Browse files
committed
VulnAPI Implementation
1 parent 84422b1 commit 463e77f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1125
-222
lines changed

.rubocop.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ ClassVars:
88
Enabled: false
99
LineLength:
1010
Max: 120
11+
Lint/UriEscapeUnescape:
12+
Enabled: false
1113
MethodLength:
1214
Max: 20
1315
Exclude:
1416
- 'app/controllers/enumeration/cli_options.rb'
15-
Lint/UriEscapeUnescape:
16-
Enabled: false
1717
Metrics/AbcSize:
1818
Max: 25
1919
Metrics/BlockLength:
@@ -29,3 +29,6 @@ Style/Documentation:
2929
Enabled: false
3030
Style/FormatStringToken:
3131
Enabled: false
32+
Style/NumericPredicate:
33+
Exclude:
34+
- 'app/controllers/vuln_api.rb'

app/controllers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative 'controllers/core'
4-
require_relative 'controllers/api_token'
4+
require_relative 'controllers/vuln_api'
55
require_relative 'controllers/custom_directories'
66
require_relative 'controllers/wp_version'
77
require_relative 'controllers/main_theme'

app/controllers/api_token.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/controllers/vuln_api.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module WPScan
4+
module Controller
5+
# Controller to handle the API token
6+
class VulnApi < CMSScanner::Controller::Base
7+
def cli_options
8+
[
9+
OptString.new(['--api-token TOKEN', 'The WPVulnDB API Token to display vulnerability data'])
10+
]
11+
end
12+
13+
def before_scan
14+
return unless ParsedCli.api_token
15+
16+
DB::VulnApi.token = ParsedCli.api_token
17+
18+
api_status = DB::VulnApi.status
19+
20+
raise Error::InvalidApiToken if api_status['error']
21+
raise Error::ApiLimitReached if api_status['requests_remaining'] == 0
22+
raise api_status['http_error'] if api_status['http_error']
23+
end
24+
25+
def after_scan
26+
output('status', status: DB::VulnApi.status, api_requests: WPScan.api_requests)
27+
end
28+
end
29+
end
30+
end

app/models/plugin.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ def initialize(slug, blog, opts = {})
1515
@uri = Addressable::URI.parse(blog.url(path_from_blog))
1616
end
1717

18-
# @return [ JSON ]
18+
# Retrieve the metadata from the vuln API if available (and a valid token is given),
19+
# or the local metadata db otherwise
20+
# @return [ Hash ]
1921
def metadata
20-
@metadata ||= DB::Plugin.metadata_at(slug)
22+
@metadata ||= db_data.empty? ? DB::Plugin.metadata_at(slug) : db_data
23+
end
24+
25+
# @return [ Hash ]
26+
def db_data
27+
@db_data ||= DB::VulnApi.plugin_data(slug)
2128
end
2229

2330
# @param [ Hash ] opts

app/models/theme.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ def initialize(slug, blog, opts = {})
2121
parse_style
2222
end
2323

24+
# Retrieve the metadata from the vuln API if available (and a valid token is given),
25+
# or the local metadata db otherwise
2426
# @return [ JSON ]
2527
def metadata
26-
@metadata ||= DB::Theme.metadata_at(slug)
28+
@metadata ||= db_data.empty? ? DB::Theme.metadata_at(slug) : db_data
29+
end
30+
31+
# @return [ Hash ]
32+
def db_data
33+
@db_data ||= DB::VulnApi.theme_data(slug)
2734
end
2835

2936
# @param [ Hash ] opts

app/models/wp_item.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ def vulnerabilities
3939

4040
@vulnerabilities = []
4141

42-
# TODO Get them from API
43-
#[*db_data['vulnerabilities']].each do |json_vuln|
44-
# vulnerability = Vulnerability.load_from_json(json_vuln)
45-
# @vulnerabilities << vulnerability if vulnerable_to?(vulnerability)
46-
#end
42+
[*db_data['vulnerabilities']].each do |json_vuln|
43+
vulnerability = Vulnerability.load_from_json(json_vuln)
44+
@vulnerabilities << vulnerability if vulnerable_to?(vulnerability)
45+
end
4746

4847
@vulnerabilities
4948
end
@@ -67,7 +66,7 @@ def latest_version
6766
# Not used anywhere ATM
6867
# @return [ Boolean ]
6968
def popular?
70-
@popular ||= metadata['popular']
69+
@popular ||= metadata['popular'] ? true : false
7170
end
7271

7372
# @return [ String ]

app/models/wp_version.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ def self.all
3535
@all_numbers.sort! { |a, b| Gem::Version.new(b) <=> Gem::Version.new(a) }
3636
end
3737

38-
# @return [ JSON ]
38+
# Retrieve the metadata from the vuln API if available (and a valid token is given),
39+
# or the local metadata db otherwise
40+
# @return [ Hash ]
3941
def metadata
40-
@metadata ||= DB::Version.metadata_at(number)
42+
@metadata ||= db_data.empty? ? DB::Version.metadata_at(number) : db_data
43+
end
44+
45+
# @return [ Hash ]
46+
def db_data
47+
@db_data ||= DB::VulnApi.wordpress_data(number)
4148
end
4249

4350
# @return [ Array<Vulnerability> ]
@@ -46,10 +53,9 @@ def vulnerabilities
4653

4754
@vulnerabilities = []
4855

49-
# TODO get them from API
50-
#[*db_data['vulnerabilities']].each do |json_vuln|
51-
# @vulnerabilities << Vulnerability.load_from_json(json_vuln)
52-
#end
56+
[*db_data['vulnerabilities']].each do |json_vuln|
57+
@vulnerabilities << Vulnerability.load_from_json(json_vuln)
58+
end
5359

5460
@vulnerabilities
5561
end

app/views/cli/vuln_api/status.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<% unless @status.empty? -%>
2+
<% if @status['http_error'] -%>
3+
<%= critical_icon %> WPVulnDB API, <%= @status['http_error'].to_s %>
4+
<% else -%>
5+
<%= info_icon %> WPVulnDB API OK
6+
| Plan: <%= @status['plan'] %>
7+
| Requests Done (during the scan): <%= @api_requests %>
8+
| Requests Remaining: <%= @status['requests_remaining'] %>
9+
<% end -%>
10+
<% else -%>
11+
<%= warning_icon %> No WPVulnDB API Token given, as a result vulnerability data has not been output.
12+
<%= warning_icon %> You can get a free API token with 50 daily requests by registering at https://wpvulndb.com/register.
13+
<% end -%>

app/views/json/vuln_api/status.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"vuln_api": {
2+
<% unless @status.empty? -%>
3+
<% if @status['http_error'] -%>
4+
"http_error": <%= @status['http_error'].to_s.to_json %>
5+
<% else -%>
6+
"plan": <%= @status['plan'].to_json %>,
7+
"requests_done_during_scan": <%= @api_requests.to_json %>,
8+
"requests_remaining": <%= @status['requests_remaining'].to_json %>
9+
<% end -%>
10+
<% else -%>
11+
"error": "No WPVulnDB API Token given, as a result vulnerability data has not been output.\nYou can get a free API token with 50 daily requests by registering at https://wpvulndb.com/register."
12+
<% end -%>
13+
},

0 commit comments

Comments
 (0)