-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathEnterpriseLinuxLookup.cpp
234 lines (182 loc) · 4.98 KB
/
EnterpriseLinuxLookup.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "EnterpriseLinuxLookup.h"
#include "Utils.h"
#include <string>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
unordered_map<string, string> EnterpriseLinuxLookup::FindVulnerability(const string& cve, OpSys distrib, double ver)
{
unordered_map<string, string> vuln;
if (!ValidateCVE(cve))
{
log(ERR, "Specified CVE identifier '" + cve + "' is not syntactically valid.");
return vuln;
}
if (distrib != EnterpriseLinux && distrib != Fedora)
{
log(ERR, "Specified distribution is not supported by this instance.");
return vuln;
}
auto resp = getURL("https://bugzilla.redhat.com/show_bug.cgi?ctype=xml&id=" + cve);
if (get<2>(resp) != 200)
{
if (get<2>(resp) == -1)
{
log(ERR, "Failed to send HTTP request: " + get<1>(resp));
}
else
{
log(ERR, "Failed to get reply: HTTP response code was " + to_string(get<2>(resp)) + ".");
}
return vuln;
}
auto html = get<0>(resp);
// pkg -> name of the package
// dist -> distribution (rhel-7, rhscl-1, fedora-all, etc)
// status -> vulnerability status (affected, notaffected)
static regex tblrgx("(?<dist>(?:rhel|rhscl|fedora)-(?:\\d+|all))\\/(?<pkg>[^=]+)=(?<status>[^,]+)", regex::icase);
// res -> resolution (NOTABUG, ERRATA)
static regex stsrgx("<resolution>(?<res>[^<]+)<\\/resolution>", regex::icase);
// lst -> if res is ERRATA, comma-separated list of version numbers which fix the vulnerability
static regex cfirgx("<cf_fixed_in>(?<lst>[^<]+)<\\/cf_fixed_in>", regex::icase);
sregex_iterator srit(html.begin(), html.end(), tblrgx);
sregex_iterator end;
auto tdist = string(distrib == EnterpriseLinux ? "rhel" : "fedora");
auto tdall = tdist + "-all";
auto tdver = ver != 0 ? tdist + "-" + to_string(ver) : tdall;
for (; srit != end; ++srit)
{
auto m = *srit;
auto pkg = m["pkg"].str();
auto dist = m["dist"].str();
auto sts = m["status"].str();
if (dist != tdall && dist != tdver)
{
continue;
}
if (sts != "notaffected")
{
// placeholder for now
vuln.emplace(pkg, "");
}
}
smatch cfism;
if (regex_search(html, cfism, cfirgx))
{
auto lst = cfism["lst"].str();
vector<string> strs;
split(strs, lst, is_any_of(","));
for (auto& str : strs)
{
trim(str);
auto spc = str.find_last_of(" ");
if (spc == string::npos)
{
continue;
}
auto spkg = str.substr(0, spc);
auto sver = str.substr(spc + 1);
for (auto& fix : vuln)
{
if (fix.second != "" || fix.first != spkg)
{
continue;
}
fix.second = sver;
}
}
}
return vuln;
}
vector<pair<string, long>> EnterpriseLinuxLookup::GetChangelog(const string& pkg, OpSys distrib, double ver)
{
vector<pair<string, long>> updates;
if (distrib != EnterpriseLinux && distrib != Fedora)
{
log(ERR, "Specified distribution is not supported by this instance.");
return updates;
}
auto resp = getURL("https://www.rpmfind.net/linux/rpm2html/search.php?query=" + pkg + "&system=" + (distrib == Fedora ? "fedora" : "centos") + "&arch=x86_64");
if (get<2>(resp) != 200)
{
if (get<2>(resp) == -1)
{
log(ERR, "Failed to send HTTP request: " + get<1>(resp));
}
else
{
log(ERR, "Failed to get reply: HTTP response code was " + to_string(get<2>(resp)) + ".");
}
return updates;
}
string vertag = distrib == Fedora ? "fc" : "el";
if (ver == 0)
{
vertag += distrib == Fedora ? "24" : "7";
}
else
{
vertag += to_string(int(floor(ver)));
}
// pkg -> name of the package
// url -> location of the latest package changelog
regex chlrgx("<a href='(?<url>\\/linux\\/RPM\\/[^']*\\." + vertag + "\\.[^']*)'>(?<pkg>[^<]*)\\.html<\\/a>", regex::icase);
smatch chlurl;
if (!regex_search(get<0>(resp), chlurl, chlrgx))
{
log(ERR, "Failed get changelog location for the package " + pkg + ".");
return updates;
}
resp = getURL("https://www.rpmfind.net" + chlurl["url"].str());
if (get<2>(resp) != 200)
{
if (get<2>(resp) == -1)
{
log(ERR, "Failed to send HTTP request: " + get<1>(resp));
}
else
{
log(ERR, "Failed to get reply: HTTP response code was " + to_string(get<2>(resp)) + ".");
}
return updates;
}
// ver -> version number of the package
// date -> publication date of the package
static regex verrgx("^(?:<pre>)?\\* (?<date>(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun).*?\\d{4}).*?>(?: \\-)? (?<ver>[^$ ]+)", regex::icase);
istringstream iss(get<0>(resp));
for (string line; getline(iss, line); )
{
smatch verm;
if (regex_search(line, verm, verrgx))
{
updates.push_back(make_pair(verm["ver"].str(), dateToUnix(verm["date"].str(), "%a %b %d %Y")));
}
}
return updates;
}
string EnterpriseLinuxLookup::GetUpgradeCommand(const unordered_set<string>& pkgs, OpSys distrib, double ver)
{
if (pkgs.empty())
{
return "";
}
string cmd;
if (distrib == OpSys::Fedora && ver >= 22)
{
cmd = "sudo dnf update";
}
else
{
cmd = "sudo yum update";
}
for (auto& pkg : pkgs)
{
cmd += " " + pkg;
}
return cmd;
}
EnterpriseLinuxLookup::~EnterpriseLinuxLookup()
{
}