1

I tried to word my question as best I could since I'm not sure if that is exactly what I am looking for, sorry in advance for that.

I am parsing an RSS feed with feedparser.

Based on the documentation, I was able to parse my results down to just the link to a certain vulnerability + its RSS post title.

Here is my code:

import feedparser

# product variables
ie = ['Internet Explorer', 'IE', 'Explorer', 'Internet_Explorer']
nix = ['Linux', 'SUSE', 'Red Hat', 'RHEL', 'Ubuntu', 'Debian_Linux']
win = ['Windows_']

# NVD RSS feed variable
d = feedparser.parse('https://nvd.nist.gov/feeds/xml/cve/misc/nvd-rss-  analyzed.xml')
print("================================")
print(d['feed']['title'])
print("================================")

for entry in d.entries:
    print(entry.title, entry.link)

this will return results that look like this:

CVE-2018-8132 (windows_10, windows_server_2016) https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2018-8132

I also tried just parsing the raw data, which is:

for entry in d.entries:
    print(entry)

Now this shows me that title is a key in a dictionary (I think) and the value is CVE-2018-8132 (windows_10, windows_server_2016)

What I want to do, is an if statement that would be something like: if titlevalue contains a word (from the lists up top) then do something

I couldn't find in feedparser's documentation how to do this. I'd really appreciate any help.

EDIT (2018-06-15):

I figured it out.

Here is my code for whoever is looking for it in the future:

# library imports
import feedparser
import re
import smtplib
import datetime
import time

# NVD RSS feed variable
d = feedparser.parse('https://nvd.nist.gov/feeds/xml/cve/misc/nvd-rss-  analyzed.xml')
print("\n+++++++++++++++++++++++++++++++++++++++")
print(d['feed']['title'], 'Scanner')
print("+++++++++++++++++++++++++++++++++++++++\n")


# function for iterating through the entries and printing out how many vulns there are
def product_scan(product_name):

    # vulnerability links list
    vuln_list = []

    # counter for how many vulns per product
    count = 0
    for entry in d.entries:
        if product_name in entry.title:
            count += 1
            # here we append the hyperlinks of the CVEs to a pre-defined list so we can manipulate it later
            vuln_list.append(entry.link)


    # making it look nice
    if count == 1:
 print('===============================================================\nThere is', count, product_name,
          'related vulnerability:')
    elif count == 0:
        print('')
    else:
      print('===============================================================\nThere are', count, product_name,
          'related vulnerabilities:')

    # this for loop is for enumerating the links for each product CVE code
    for x in vuln_list:
    print(x)


# calling the function and searching for vulns based on keyword(s)
product_list = ['mysql', 'windows', 'linux', 'explorer', 'php', 'webex', 'firefox', 'norton', 'mcafee', 'symantec']
for product in product_list:
    product_scan(product)
Geronimo
  • 11
  • 2
  • 1
    Mixing the question and the answer only makes both more confusing. It is better if you leave the question as a question, write the answer as an answer and then accept it. – Stop harming Monica Jun 15 '18 at 13:23

1 Answers1

0

Geronimo, if you want to just check if a key is in the dictionary, you can use the in keyword so

d = {'abc':'123','bbg':'456','tig':'567'}
if 'abc' in d:
   print("key is in here")

If you want to do something more with filtered keys & values, you can create a separate dictionary using dictionary comprehension (similar to list comprehension but for dicts) Filter dict to contain only certain keys?

Linkx_lair
  • 569
  • 1
  • 9
  • 21
  • This is a great start! Thanks to this code now I get this: for entry in d.entries: if 'windows' in entry.title: print('there is a windows vuln',) and it returns how many times a vulnerability with the word 'windows' is on the feed. Now how could I print on the screen only the vulnerabilities that have windows in `d` ? – Geronimo Jun 14 '18 at 12:11
  • What are you trying to do exactly @Geronimo ? Are you trying to return value if a key is in the dictionary? or are you trying to see if a part of a key is in the dictionary keyset? If you are trying to do the latter, then a quick way to do that would be to make another list or dictionary of the keys, and use the 'in' keyword to see if the subfield/substring is in the list. See the classic substring in string problem https://stackoverflow.com/questions/16819222/how-to-return-dictionary-keys-as-a-list-in-python – Linkx_lair Jun 14 '18 at 15:41
  • And you can learn more about the 'in' operator in python https://www.tutorialspoint.com/python/membership_operators_example.htm Please mark the answer if it is helpful in addressing your issue – Linkx_lair Jun 14 '18 at 15:42
  • Thanks for answering again. I've edited my original post to show what I was looking for. I figured it out after some searching! – Geronimo Jun 15 '18 at 13:09