Skip to content

Commit c3500a3

Browse files
committed
Initial commit
0 parents  commit c3500a3

Some content is hidden

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

41 files changed

+1449
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
db/*
2+
!db/.gitkeep
3+
nvd_data/*
4+
!nvd_data/.gitkeep
5+
log/*
6+
!log/.gitkeep
7+
tmp/*
8+
!tmp/.gitkeep
9+
coverage

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--colour
2+
--backtrace
3+
--format documentation

.ruby-gemset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cve_server

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.2.3

Capfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'capistrano/setup'
2+
require 'capistrano/deploy'
3+
require 'capistrano/puma'
4+
require 'capistrano/puma'
5+
require 'capistrano/rvm'
6+
require 'capistrano/bundler'
7+
8+
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Gemfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'sinatra', '~> 1.4.6'
4+
gem 'sinatra-json', '~> 0.1.0'
5+
gem 'nokogiri', '~> 1.6.6.2'
6+
gem 'mongo', '~> 2.1.0'
7+
gem 'puma', '~> 2.13.4'
8+
9+
group :production do
10+
gem 'capistrano', '~> 3.4.0'
11+
gem 'capistrano-bundler', '~> 1.1.4'
12+
gem 'capistrano-rvm', '~> 0.1.2'
13+
gem 'capistrano3-puma', '~> 1.2.1'
14+
end
15+
16+
group :development, :test do
17+
gem 'pry', '~> 0.10.1'
18+
gem 'rspec', '~> 3.3.0'
19+
gem 'rack-test', '~> 0.6.3'
20+
gem 'simplecov', '~> 0.10.0', :require => false
21+
end

Gemfile.lock

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
bson (3.2.4)
5+
capistrano (3.4.0)
6+
i18n
7+
rake (>= 10.0.0)
8+
sshkit (~> 1.3)
9+
capistrano-bundler (1.1.4)
10+
capistrano (~> 3.1)
11+
sshkit (~> 1.2)
12+
capistrano-rvm (0.1.2)
13+
capistrano (~> 3.0)
14+
sshkit (~> 1.2)
15+
capistrano3-puma (1.2.1)
16+
capistrano (~> 3.0)
17+
puma (>= 2.6)
18+
coderay (1.1.0)
19+
colorize (0.7.7)
20+
diff-lcs (1.2.5)
21+
docile (1.1.5)
22+
i18n (0.7.0)
23+
json (1.8.3)
24+
method_source (0.8.2)
25+
mini_portile (0.6.2)
26+
mongo (2.1.0)
27+
bson (~> 3.0)
28+
multi_json (1.11.1)
29+
net-scp (1.2.1)
30+
net-ssh (>= 2.6.5)
31+
net-ssh (2.9.2)
32+
nokogiri (1.6.6.2)
33+
mini_portile (~> 0.6.0)
34+
pry (0.10.1)
35+
coderay (~> 1.1.0)
36+
method_source (~> 0.8.1)
37+
slop (~> 3.4)
38+
puma (2.13.4)
39+
rack (1.6.2)
40+
rack-protection (1.5.3)
41+
rack
42+
rack-test (0.6.3)
43+
rack (>= 1.0)
44+
rake (10.4.2)
45+
rspec (3.3.0)
46+
rspec-core (~> 3.3.0)
47+
rspec-expectations (~> 3.3.0)
48+
rspec-mocks (~> 3.3.0)
49+
rspec-core (3.3.1)
50+
rspec-support (~> 3.3.0)
51+
rspec-expectations (3.3.0)
52+
diff-lcs (>= 1.2.0, < 2.0)
53+
rspec-support (~> 3.3.0)
54+
rspec-mocks (3.3.1)
55+
diff-lcs (>= 1.2.0, < 2.0)
56+
rspec-support (~> 3.3.0)
57+
rspec-support (3.3.0)
58+
simplecov (0.10.0)
59+
docile (~> 1.1.0)
60+
json (~> 1.8)
61+
simplecov-html (~> 0.10.0)
62+
simplecov-html (0.10.0)
63+
sinatra (1.4.6)
64+
rack (~> 1.4)
65+
rack-protection (~> 1.4)
66+
tilt (>= 1.3, < 3)
67+
sinatra-json (0.1.0)
68+
multi_json (~> 1.0)
69+
sinatra (~> 1.0)
70+
slop (3.6.0)
71+
sshkit (1.7.1)
72+
colorize (>= 0.7.0)
73+
net-scp (>= 1.1.2)
74+
net-ssh (>= 2.8.0)
75+
tilt (2.0.1)
76+
77+
PLATFORMS
78+
ruby
79+
80+
DEPENDENCIES
81+
capistrano (~> 3.4.0)
82+
capistrano-bundler (~> 1.1.4)
83+
capistrano-rvm (~> 0.1.2)
84+
capistrano3-puma (~> 1.2.1)
85+
mongo (~> 2.1.0)
86+
nokogiri (~> 1.6.6.2)
87+
pry (~> 0.10.1)
88+
puma (~> 2.13.4)
89+
rack-test (~> 0.6.3)
90+
rspec (~> 3.3.0)
91+
simplecov (~> 0.10.0)
92+
sinatra (~> 1.4.6)
93+
sinatra-json (~> 0.1.0)
94+
95+
BUNDLED WITH
96+
1.10.6

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# CVEServer
2+
3+
Simple REST-style web service for the CVE searching
4+
5+
# Requirements
6+
7+
* Ruby 2.x.x
8+
* Mongo
9+
* Ruby bundler
10+
11+
# Installation
12+
13+
* Clone our repository
14+
15+
$ git clone https://github.com/SpiderLabs/cve_server.git
16+
17+
* Install the ruby dependencies
18+
19+
$ bundle install
20+
21+
* Download the raw data from the National Vulnerability Database
22+
23+
$ ./bin/nvd_downloader
24+
25+
* Configure your database
26+
27+
$ vi config/database.yml
28+
29+
* Create and populate the database for you environment
30+
31+
$ RACK_ENV=development ./bin/seed
32+
33+
* Create and populate the database
34+
35+
$ RACK_ENV=development ./bin/seed
36+
37+
* Start the server
38+
39+
$ RACK_ENV=development puma
40+
41+
# Using the API
42+
43+
* Search for an specific CVE using its ID
44+
45+
http://localhost:port/v1/cve/CVE-ID
46+
47+
* Search for CVEs related to any cpe
48+
49+
http://localhost:port/v1/cpe/php:php
50+
51+
* List all the available CPEs
52+
53+
http://localhost:port/v1/cpe/
54+
55+
http://localhost:port/v1/cpe/microsoft:windows

bin/nvd_downloader

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env ruby
2+
$LOAD_PATH.unshift File.expand_path(File.join('..', '..', '/lib'), __FILE__)
3+
require 'nokogiri'
4+
require 'net/http'
5+
require 'cve_server'
6+
7+
def fetch_page(url)
8+
uri = URI.parse(url)
9+
use_ssl = uri.scheme == 'https'
10+
Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
11+
response = http.request_get(uri)
12+
case response
13+
when Net::HTTPSuccess then
14+
response
15+
when Net::HTTPRedirection then
16+
location = response['location']
17+
fail "redirected to #{location}"
18+
else
19+
response.value
20+
end
21+
end
22+
end
23+
24+
def download_file(url, dest_path)
25+
uri = URI.parse(url)
26+
use_ssl = uri.scheme == 'https'
27+
Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
28+
http.request_get(uri.path) do |response|
29+
case response
30+
when Net::HTTPSuccess then
31+
f = File.open(dest_path, 'w')
32+
response.read_body do |seg|
33+
f << seg
34+
sleep 0.005
35+
end
36+
f.close
37+
when Net::HTTPRedirection then
38+
location = response['location']
39+
fail "redirected to #{location}"
40+
else
41+
fail "Unable to download #{url}"
42+
end
43+
end
44+
end
45+
end
46+
47+
def dest_path(link)
48+
filename = link.split('/').last
49+
File.join(CVEServer::Boot.config.raw_data_path, filename)
50+
end
51+
52+
response = fetch_page('https://nvd.nist.gov/download.cfm')
53+
54+
if response.is_a?(Net::HTTPSuccess)
55+
@doc = Nokogiri::HTML(response.body)
56+
xml_file_path = '//td[@class="xml-file-type file-20"]'
57+
@doc.xpath('//html').xpath(xml_file_path).each do |td|
58+
link = td.xpath('a').first['href']
59+
next unless link =~ /.gz$/
60+
61+
dest_path = dest_path(link)
62+
downloaded_path = [dest_path, '.download'].join
63+
64+
puts "Downloading file from #{link}.."
65+
download_file(link, downloaded_path)
66+
67+
if File.exist?(downloaded_path) && File.size?(downloaded_path)
68+
File.delete(dest_path) if File.exist?(dest_path)
69+
File.rename(downloaded_path, dest_path)
70+
end
71+
end
72+
end

bin/seed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env ruby
2+
$LOAD_PATH.unshift File.expand_path(File.join('..', '..', '/lib'), __FILE__)
3+
require 'zlib'
4+
require 'cve_server'
5+
require 'cve_server/nvd/reader'
6+
7+
files = File.join(CVEServer::Boot.config.raw_data_path, '*.xml.gz')
8+
9+
CVEServer::Cve.drop_all
10+
Dir.glob(files).sort.each do |infile|
11+
puts "Uncompressing #{infile}"
12+
input = Zlib::GzipReader.open(infile).read
13+
@doc = CVEServer::NVD::Reader.new(input)
14+
puts 'Exporting data into the CVE collection'
15+
CVEServer::Cve.bulk_create(@doc.all_cve)
16+
end
17+
puts "Reducing the cpe list"
18+
CVEServer::Cve.reduce_cpes

config.ru

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2+
require 'cve_server/app'
3+
4+
run CVEServer::App

config/database.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
production:
2+
database: cves_production
3+
adapter: mongo
4+
host: 127.0.0.1
5+
port: 27017
6+
7+
development:
8+
database: cves_development
9+
adapter: mongo
10+
host: 127.0.0.1
11+
port: 27017
12+
13+
test:
14+
database: cves_test
15+
adapter: mongo
16+
host: 127.0.0.1
17+
port: 27017

config/deploy.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
lock '3.4.0'
2+
set :application, 'cve_server'
3+
set :repo_url, '[email protected]:SpiderLabs/cve_server.git'
4+
set :branch, 'master' # Default branch is :master
5+
set :deploy_to, '/home/deployer/cve_server'
6+
set :stage, :production
7+
set :pty, false
8+
set :linked_dirs, fetch(:linked_dirs, []).push('nvd_data', 'log', 'tmp')
9+
10+
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
11+
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
12+
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
13+
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock" #accept array for multi-bind
14+
set :puma_default_control_app, "unix://#{shared_path}/tmp/sockets/pumactl.sock"
15+
set :puma_conf, "#{shared_path}/puma.rb"
16+
set :puma_access_log, "#{shared_path}/log/puma_access.log"
17+
set :puma_error_log, "#{shared_path}/log/puma_error.log"
18+
set :puma_role, :app
19+
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
20+
set :puma_threads, [0, 16]
21+
set :puma_workers, 0
22+
set :puma_worker_timeout, nil
23+
set :puma_init_active_record, false
24+
set :puma_preload_app, true
25+
26+
namespace :deploy do
27+
28+
namespace :symlink do
29+
desc 'Symlink linked directories'
30+
task :linked_dirs do
31+
next unless any? :linked_dirs
32+
on release_roles :all do
33+
execute :mkdir, '-pv', linked_dir_parents(shared_path)
34+
execute :mkdir, '-pv', shared_path.join('tmp/sockets')
35+
execute :mkdir, '-pv', shared_path.join('tmp/pids')
36+
37+
fetch(:linked_dirs).each do |dir|
38+
target = release_path.join(dir)
39+
source = shared_path.join(dir)
40+
unless test "[ -L #{target} ]"
41+
if Dir.exist?(target)
42+
execute :rm, '-rf', target
43+
end
44+
execute :ln, '-s', source, target
45+
end
46+
end
47+
end
48+
end
49+
end
50+
51+
desc 'download the nvd reports'
52+
task :download_nvd_reports do
53+
on fetch(:bundle_servers) do
54+
within release_path do
55+
with fetch(:bundle_env_variables, {}) do
56+
execute :bundle, 'exec', './bin/nvd_downloader'
57+
end
58+
end
59+
end
60+
end
61+
62+
desc 'reload the database with seed data'
63+
task :seed do
64+
on fetch(:bundle_servers) do
65+
within release_path do
66+
with fetch(:bundle_env_variables, {}) do
67+
execute :bundle, 'exec', "./bin/seed RACK_ENV=#{fetch(:rack_env,{})}"
68+
end
69+
end
70+
end
71+
end
72+
end

config/deploy/production.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server 'YourIP', user: 'deployer', roles: %w{app}
2+
set :rack_env, 'production'

0 commit comments

Comments
 (0)