Mercurial > farol > farolluz
comparison farolluz/parsers/cpe.py @ 48:3826f2701ff2
CPE: Add the possibility to add ourself integrally to the product tree
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Tue, 30 Dec 2014 12:30:19 +0100 |
parents | bb1dd2a55643 |
children |
comparison
equal
deleted
inserted
replaced
47:652f59fbea3a | 48:3826f2701ff2 |
---|---|
33 CPE is a trademark of The MITRE Corporation. | 33 CPE is a trademark of The MITRE Corporation. |
34 | 34 |
35 """ | 35 """ |
36 | 36 |
37 import re | 37 import re |
38 | |
39 from ..producttree import CVRFFullProductName, CVRFRelationship | |
40 | |
41 def capitalize(s): | |
42 """ A custom version of string.capwords that split on _, and join on ' ' | |
43 """ | |
44 s = s.replace('\\', '') | |
45 return ' '.join(c.capitalize() for c in s.split('_')) | |
46 | |
38 | 47 |
39 PCT_MAP ={'!': "%21", '"': "%22", '#': "%23", '$': "%24", '%': "%25", '&': "%26", | 48 PCT_MAP ={'!': "%21", '"': "%22", '#': "%23", '$': "%24", '%': "%25", '&': "%26", |
40 "'": "%27", '(': "%28", ')': "%29", '*': "%2a", '+': "%2b", ',': "%2c", | 49 "'": "%27", '(': "%28", ')': "%29", '*': "%2a", '+': "%2b", ',': "%2c", |
41 '/': "%2f", ':': "%3a", ';': "%3b", '<': "%3c", "=": "%3d", '>': "%3e", | 50 '/': "%2f", ':': "%3a", ';': "%3b", '<': "%3c", "=": "%3d", '>': "%3e", |
42 '?': "%3f", '@': "%40", '[': "%5b", '\\': "%5c","]": "%5d", '^': "%5e", | 51 '?': "%3f", '@': "%40", '[': "%5b", '\\': "%5c","]": "%5d", '^': "%5e", |
280 elif idx == 11: | 289 elif idx == 11: |
281 self.target_hw = v | 290 self.target_hw = v |
282 elif idx == 12: | 291 elif idx == 12: |
283 self.other = v | 292 self.other = v |
284 | 293 |
294 def addToDoc(self, document, finalProduct=True): | |
295 """ Add the CPE value as full producttree in the document | |
296 If finalProduct is false, only the elements leading to the product | |
297 will be added. | |
298 """ | |
299 ptree = document._producttree | |
300 if ptree is None: | |
301 ptree = document.createProductTree() | |
302 | |
303 def next_prodid(): | |
304 """ A handy function to generate the next available productid """ | |
305 prods = document._producttree._products | |
306 if len(prods) > 0: | |
307 last_prodid = prods[-1]._productid | |
308 numlen = 0 | |
309 while last_prodid[- (numlen + 1)] in "0123456789": | |
310 numlen += 1 | |
311 if numlen != 0: | |
312 return last_prodid[:-numlen] + str(int(last_prodid[-numlen:]) + 1) | |
313 return document.getDocId() + '-P0' | |
314 | |
315 # Create the main product tree | |
316 tree = [] | |
317 for value, valtype in [(self.vendor, 'Vendor'), | |
318 (self.product, 'Product Name'), | |
319 (self.version, 'Product Version'), | |
320 (self.update, 'Patch Level'), | |
321 (self.language, 'Language'), | |
322 (self.target_hw, 'Architecture')]: | |
323 if value.value is not None: | |
324 tree.append((valtype, capitalize(value.value))) | |
325 | |
326 # Import it | |
327 last_branch = ptree.importTree(tree) | |
328 # Add a product there | |
329 if self.target_sw.value is None: | |
330 if not finalProduct: | |
331 return last_branch | |
332 product = CVRFFullProductName(next_prodid(), str(self), last_branch, self.bind_to_fs()) | |
333 ptree.addProduct(product) | |
334 return product | |
335 else: | |
336 product = CVRFFullProductName(next_prodid(), str(self), last_branch) | |
337 ptree.addProduct(product) | |
338 | |
339 # We do have a target software, we need to create a relationship ! | |
340 os = CVRFFullProductName(next_prodid(), self.target_sw.value, ptree) | |
341 ptree.addProduct(os) | |
342 | |
343 rel = CVRFRelationship(product._productid, 'Installed On', os._productid) | |
344 ptree.addRelationship(rel) | |
345 if not finalProduct: | |
346 return rel | |
347 | |
348 final_prod = CVRFFullProductName(next_prodid(), ptree.getNameOfRelationship(rel), rel, self.bind_to_fs()) | |
349 ptree.addProduct(final_prod) | |
350 return final_prod | |
351 | |
352 def __str__(self): | |
353 res = [] | |
354 if self.product.value: | |
355 res.append(capitalize(self.product.value)) | |
356 if self.version.value: | |
357 res.append(capitalize(self.version.value)) | |
358 if not res: | |
359 return capitalize(self.vendor.value) | |
360 return ' '.join(res) | |
361 | |
285 def parse(s): | 362 def parse(s): |
286 cpe = CPE() | 363 cpe = CPE() |
287 if s[:5] == 'cpe:/': | 364 if s[:5] == 'cpe:/': |
288 cpe.unbind_URI(s) | 365 cpe.unbind_URI(s) |
289 elif s[:8] == 'cpe:2.3:': | 366 elif s[:8] == 'cpe:2.3:': |