-
Notifications
You must be signed in to change notification settings - Fork 7
/
arch-vulns
executable file
·145 lines (111 loc) · 2.71 KB
/
arch-vulns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env ruby
############################################################################
require 'epitools'
require 'open-uri'
############################################################################
class Time
def elapsed
Time.now - self
end
end
############################################################################
class Package < Struct.new(:name, :version_str)
def version
parse_version(version_str)
end
def to_s
"<15>#{name} <13>#{version_str}".colorize
end
end
############################################################################
class Vuln
def initialize(json)
@json = json
end
def affected
parse_version @json["affected"]
end
def fixed
parse_version @json["fixed"]
end
def names
@json["packages"]
end
def covers?(package)
if (affected.nil? and fixed.nil?) or package.version.nil?
true
else
(package.version >= affected) and (fixed ? package.version < fixed : true)
end
end
def type
@json["type"]
end
def severity
@json["severity"]
end
def codes
@json["issues"].join(", ")
end
def status
case s = @json["status"]
when "Vulnerable"
"<12>#{s}"
when "Fixed", "Not affected"
"<10>#{s}"
else
"<14>#{s}"
end
end
def url
"https://security.archlinux.org/#{@json["name"]}"
end
def to_s
"<11>#{@json["affected"]}<8>...<11>#{@json["fixed"]} <8>(<10>#{status}<8>) <3>=> <14>#{severity}<7>: <9>#{type} <8>(<7>#{codes}<8>)".colorize
end
end
############################################################################
def parse_version(str)
str && SemanticVersion.new(str)
end
############################################################################
# initialize the cache
cache_dir = Path["~/.cache/upm/"]
cache_dir.mkdir_p unless cache_dir.exists?
# download the json
json = cache_dir/"pacman-vulns.json"
if !json.exists? or (json.mtime.elapsed > 20.minutes)
open("https://security.archlinux.org/issues/all.json") do |io|
json.write io.read
end
end
# parse the json
vulns = Hash.of_arrays
json.parse.each do |json|
vuln = Vuln.new(json)
vuln.names.each do |name|
vulns[name] << vuln
end
end
# parse the installed pacakges
installed_packages = `pacman -Q`.each_line.map do |line|
Package.new(*line.strip.split)
end
# find vulnerable packages
vulnerable_packages = Hash.of_arrays
installed_packages.each do |package|
vulns[package.name].each do |vuln|
if vuln.covers?(package)
vulnerable_packages[package] << vuln
end
end
end
# print vulnerable packages
vulnerable_packages.each do |package, vulns|
puts package
vulns.each do |vuln|
puts " #{vuln}"
puts " #{vuln.url}".green
end
puts
end