Skip to content

Commit fc37fbb

Browse files
Merge pull request #837 from jhelmold/GEA-626_Apply_new_CPE_matching_in_CVE_scans
Add: Added functions for the new CPE matching in CVE scans.
2 parents 1bc955d + 0397ba0 commit fc37fbb

File tree

4 files changed

+210
-83
lines changed

4 files changed

+210
-83
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ if (BUILD_TESTS AND NOT SKIP_SRC)
230230
add_custom_target (tests
231231
DEPENDS array-test alivedetection-test boreas_error-test boreas_io-test
232232
cli-test cpeutils-test cvss-test ping-test sniffer-test util-test networking-test
233-
passwordbasedauthentication-test xmlutils-test version-test osp-test
234-
versionutils-test nvti-test hosts-test jsonpull-test)
233+
passwordbasedauthentication-test xmlutils-test version-test versionutils-test
234+
osp-test nvti-test hosts-test jsonpull-test)
235235

236236
endif (BUILD_TESTS AND NOT SKIP_SRC)
237237

util/CMakeLists.txt

+18-18
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ endif (BUILD_WITH_LDAP)
112112
include_directories (${GLIB_INCLUDE_DIRS} ${GPGME_INCLUDE_DIRS} ${GCRYPT_INCLUDE_DIRS}
113113
${LIBXML2_INCLUDE_DIRS})
114114

115-
set (FILES cpeutils.c passwordbasedauthentication.c compressutils.c fileutils.c gpgmeutils.c kb.c ldaputils.c
116-
nvticache.c mqtt.c radiusutils.c serverutils.c sshutils.c uuidutils.c versionutils.c
117-
xmlutils.c jsonpull.c)
115+
set (FILES cpeutils.c passwordbasedauthentication.c compressutils.c fileutils.c gpgmeutils.c jsonpull.c kb.c
116+
ldaputils.c nvticache.c mqtt.c radiusutils.c serverutils.c sshutils.c uuidutils.c versionutils.c
117+
xmlutils.c)
118118

119-
set (HEADERS cpeutils.h passwordbasedauthentication.h authutils.h compressutils.h fileutils.h gpgmeutils.h kb.h
120-
ldaputils.h nvticache.h mqtt.h radiusutils.h serverutils.h sshutils.h
121-
uuidutils.h xmlutils.h jsonpull.h versionutils.h)
119+
set (HEADERS cpeutils.h passwordbasedauthentication.h authutils.h compressutils.h fileutils.h gpgmeutils.h
120+
jsonpull.h kb.h ldaputils.h nvticache.h mqtt.h radiusutils.h serverutils.h sshutils.h
121+
uuidutils.h versionutils.h xmlutils.h)
122122

123123
if (BUILD_STATIC)
124124
add_library (gvm_util_static STATIC ${FILES})
@@ -190,10 +190,10 @@ if (BUILD_TESTS)
190190

191191
target_link_libraries (cpeutils-test ${CGREEN_LIBRARIES}
192192
${GLIB_LDFLAGS} ${GIO_LDFLAGS} ${GPGME_LDFLAGS} ${ZLIB_LDFLAGS}
193-
${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS}
194-
${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS}
195-
${LIBXML2_LDFLAGS} ${UUID_LDFLAGS}
196-
${LINKER_HARDENING_FLAGS})
193+
${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS}
194+
${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS}
195+
${LIBXML2_LDFLAGS} ${UUID_LDFLAGS}
196+
${LINKER_HARDENING_FLAGS})
197197

198198
add_custom_target (tests-cpeutils
199199
DEPENDS cpeutils-test)
@@ -208,10 +208,10 @@ if (BUILD_TESTS)
208208

209209
target_link_libraries (versionutils-test ${CGREEN_LIBRARIES}
210210
${GLIB_LDFLAGS} ${GIO_LDFLAGS} ${GPGME_LDFLAGS} ${ZLIB_LDFLAGS}
211-
${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS}
212-
${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS}
213-
${LIBXML2_LDFLAGS} ${UUID_LDFLAGS}
214-
${LINKER_HARDENING_FLAGS})
211+
${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS}
212+
${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS}
213+
${LIBXML2_LDFLAGS} ${UUID_LDFLAGS}
214+
${LINKER_HARDENING_FLAGS})
215215

216216
add_custom_target (tests-versionutils
217217
DEPENDS versionutils-test)
@@ -226,10 +226,10 @@ if (BUILD_TESTS)
226226

227227
target_link_libraries (xmlutils-test ${CGREEN_LIBRARIES}
228228
${GLIB_LDFLAGS} ${GIO_LDFLAGS} ${GPGME_LDFLAGS} ${ZLIB_LDFLAGS}
229-
${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS}
230-
${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS}
231-
${LIBXML2_LDFLAGS} ${UUID_LDFLAGS}
232-
${LINKER_HARDENING_FLAGS})
229+
${RADIUS_LDFLAGS} ${LIBSSH_LDFLAGS} ${GNUTLS_LDFLAGS}
230+
${GCRYPT_LDFLAGS} ${LDAP_LDFLAGS} ${REDIS_LDFLAGS}
231+
${LIBXML2_LDFLAGS} ${UUID_LDFLAGS}
232+
${LINKER_HARDENING_FLAGS})
233233

234234
add_custom_target (tests-xmlutils
235235
DEPENDS xmlutils-test)

util/cpeutils.c

+177
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,69 @@
2929
*/
3030
#define G_LOG_DOMAIN "libgvm util"
3131

32+
static enum set_relation
33+
compare_component (const char *, const char *);
34+
35+
static enum set_relation
36+
compare_strings (const char *, const char *);
37+
38+
static int
39+
count_escapes (const char *, int, int);
40+
41+
static gboolean
42+
is_even_wildcards (const char *, int);
43+
44+
static gboolean
45+
has_wildcards (const char *);
46+
47+
static int
48+
index_of (const char *, const char *, int);
49+
50+
static gboolean
51+
is_string (const char *);
52+
53+
static char *
54+
get_uri_component (const char *, int);
55+
56+
static char *
57+
decode_uri_component (const char *);
58+
59+
static void
60+
unpack_sixth_uri_component (const char *, cpe_struct_t *);
61+
62+
static char *
63+
get_fs_component (const char *, int);
64+
65+
static char *
66+
unbind_fs_component (char *);
67+
68+
static char *
69+
add_quoting (const char *);
70+
71+
static char *
72+
bind_cpe_component_for_uri (const char *);
73+
74+
static char *
75+
transform_for_uri (const char *);
76+
77+
static char *
78+
pack_sixth_uri_component (const cpe_struct_t *);
79+
80+
static char *
81+
bind_cpe_component_for_fs (const char *);
82+
83+
static char *
84+
process_quoted_chars (const char *);
85+
86+
static void
87+
trim_pct (char *);
88+
89+
static void
90+
get_code (char *, const char *);
91+
92+
static void
93+
str_cpy (char **, const char *, int);
94+
3295
/**
3396
* @brief Convert a URI CPE to a formatted string CPE.
3497
*
@@ -49,6 +112,26 @@ uri_cpe_to_fs_cpe (const char *uri_cpe)
49112
return (fs_cpe);
50113
}
51114

115+
/**
116+
* @brief Convert a URI CPE to a formatted string product.
117+
*
118+
* @param[in] uri_cpe A CPE v2.2-conformant URI.
119+
*
120+
* @return A formatted string product.
121+
*/
122+
char *
123+
uri_cpe_to_fs_product (const char *uri_cpe)
124+
{
125+
cpe_struct_t cpe;
126+
char *fs_cpe;
127+
128+
cpe_struct_init (&cpe);
129+
uri_cpe_to_cpe_struct (uri_cpe, &cpe);
130+
fs_cpe = cpe_struct_to_fs_product (&cpe);
131+
cpe_struct_free (&cpe);
132+
return (fs_cpe);
133+
}
134+
52135
/**
53136
* @brief Convert a formatted string CPE to a URI CPE.
54137
*
@@ -69,6 +152,26 @@ fs_cpe_to_uri_cpe (const char *fs_cpe)
69152
return (uri_cpe);
70153
}
71154

155+
/**
156+
* @brief Convert a formatted string CPE to an URI product.
157+
*
158+
* @param[in] fs_cpe A formatted string CPE.
159+
*
160+
* @return An URI product.
161+
*/
162+
char *
163+
fs_cpe_to_uri_product (const char *fs_cpe)
164+
{
165+
cpe_struct_t cpe;
166+
char *uri_cpe;
167+
168+
cpe_struct_init (&cpe);
169+
fs_cpe_to_cpe_struct (fs_cpe, &cpe);
170+
uri_cpe = cpe_struct_to_uri_product (&cpe);
171+
cpe_struct_free (&cpe);
172+
return (uri_cpe);
173+
}
174+
72175
/**
73176
* @brief Read a URI CPE into the CPE struct.
74177
*
@@ -171,6 +274,44 @@ cpe_struct_to_uri_cpe (const cpe_struct_t *cpe)
171274
return (result);
172275
}
173276

277+
/**
278+
* @brief Convert a CPE struct into a URI product.
279+
*
280+
* @param[in] cpe A pointer to the CPE struct.
281+
*
282+
* @return A CPE v2.2-conformant URI product.
283+
*/
284+
char *
285+
cpe_struct_to_uri_product (const cpe_struct_t *cpe)
286+
{
287+
GString *uri_cpe;
288+
char *bind_cpe_component;
289+
uri_cpe = g_string_new ("cpe:/");
290+
291+
bind_cpe_component = bind_cpe_component_for_uri (cpe->part);
292+
if (bind_cpe_component)
293+
{
294+
g_string_append_printf (uri_cpe, "%s:", bind_cpe_component);
295+
g_free (bind_cpe_component);
296+
}
297+
bind_cpe_component = bind_cpe_component_for_uri (cpe->vendor);
298+
if (bind_cpe_component)
299+
{
300+
g_string_append_printf (uri_cpe, "%s:", bind_cpe_component);
301+
g_free (bind_cpe_component);
302+
}
303+
bind_cpe_component = bind_cpe_component_for_uri (cpe->product);
304+
if (bind_cpe_component)
305+
{
306+
g_string_append_printf (uri_cpe, "%s:", bind_cpe_component);
307+
g_free (bind_cpe_component);
308+
}
309+
310+
char *result = g_string_free (uri_cpe, FALSE);
311+
trim_pct (result);
312+
return (result);
313+
}
314+
174315
/**
175316
* @brief Read a formatted string CPE into the CPE struct.
176317
*
@@ -301,6 +442,42 @@ cpe_struct_to_fs_cpe (const cpe_struct_t *cpe)
301442
return (g_string_free (fs_cpe, FALSE));
302443
}
303444

445+
/**
446+
* @brief Convert a CPE struct into a formatted string product.
447+
*
448+
* @param[in] cpe A pointer to the CPE struct.
449+
*
450+
* @return A formatted string product.
451+
*/
452+
char *
453+
cpe_struct_to_fs_product (const cpe_struct_t *cpe)
454+
{
455+
GString *fs_cpe;
456+
char *bind_cpe_component;
457+
458+
fs_cpe = g_string_new ("cpe:2.3:");
459+
460+
bind_cpe_component = bind_cpe_component_for_fs (cpe->part);
461+
if (bind_cpe_component)
462+
{
463+
g_string_append_printf (fs_cpe, "%s:", bind_cpe_component);
464+
g_free (bind_cpe_component);
465+
}
466+
bind_cpe_component = bind_cpe_component_for_fs (cpe->vendor);
467+
if (bind_cpe_component)
468+
{
469+
g_string_append_printf (fs_cpe, "%s:", bind_cpe_component);
470+
g_free (bind_cpe_component);
471+
}
472+
bind_cpe_component = bind_cpe_component_for_fs (cpe->product);
473+
if (bind_cpe_component)
474+
{
475+
g_string_append_printf (fs_cpe, "%s:", bind_cpe_component);
476+
g_free (bind_cpe_component);
477+
}
478+
return (g_string_free (fs_cpe, FALSE));
479+
}
480+
304481
/**
305482
* @brief Get the indexth component of a URI CPE.
306483
*

util/cpeutils.h

+13-63
Original file line numberDiff line numberDiff line change
@@ -37,68 +37,41 @@ typedef struct
3737
char *
3838
uri_cpe_to_fs_cpe (const char *);
3939

40+
char *
41+
uri_cpe_to_fs_product (const char *);
42+
4043
char *
4144
fs_cpe_to_uri_cpe (const char *);
4245

46+
char *
47+
fs_cpe_to_uri_product (const char *);
48+
4349
void
4450
uri_cpe_to_cpe_struct (const char *, cpe_struct_t *);
4551

4652
char *
4753
cpe_struct_to_uri_cpe (const cpe_struct_t *);
4854

55+
char *
56+
cpe_struct_to_uri_product (const cpe_struct_t *);
57+
4958
void
5059
fs_cpe_to_cpe_struct (const char *, cpe_struct_t *);
5160

5261
char *
5362
cpe_struct_to_fs_cpe (const cpe_struct_t *);
5463

55-
static char *
56-
get_uri_component (const char *, int);
57-
58-
static char *
59-
decode_uri_component (const char *);
60-
61-
static void
62-
unpack_sixth_uri_component (const char *, cpe_struct_t *);
63-
64-
static char *
65-
get_fs_component (const char *, int);
66-
67-
static char *
68-
unbind_fs_component (char *);
69-
70-
static char *
71-
add_quoting (const char *);
72-
73-
static char *
74-
bind_cpe_component_for_uri (const char *);
75-
76-
static char *
77-
transform_for_uri (const char *);
78-
79-
static char *
80-
pack_sixth_uri_component (const cpe_struct_t *);
81-
82-
static char *
83-
bind_cpe_component_for_fs (const char *);
84-
85-
static char *
86-
process_quoted_chars (const char *);
64+
char *
65+
cpe_struct_to_fs_product (const cpe_struct_t *);
8766

8867
void
8968
cpe_struct_init (cpe_struct_t *);
9069

9170
void
9271
cpe_struct_free (cpe_struct_t *);
9372

94-
static void
95-
trim_pct (char *);
96-
97-
static void
98-
get_code (char *, const char *);
99-
100-
static void
101-
str_cpy (char **, const char *, int);
73+
gboolean
74+
cpe_struct_match (cpe_struct_t source, cpe_struct_t target);
10275

10376
enum set_relation
10477
{
@@ -109,27 +82,4 @@ enum set_relation
10982
UNDEFINED
11083
};
11184

112-
gboolean
113-
cpe_struct_match (cpe_struct_t source, cpe_struct_t target);
114-
115-
static enum set_relation
116-
compare_component (const char *, const char *);
117-
118-
static enum set_relation
119-
compare_strings (const char *, const char *);
120-
121-
static int
122-
count_escapes (const char *, int, int);
123-
124-
static gboolean
125-
is_even_wildcards (const char *, int);
126-
127-
static gboolean
128-
has_wildcards (const char *);
129-
130-
static int
131-
index_of (const char *, const char *, int);
132-
133-
static gboolean
134-
is_string (const char *);
13585
#endif

0 commit comments

Comments
 (0)