• How to generate an IP range list in Python?

    How to generate an IP range list in Python?

    Here’s a short Python snippet that generates a list of IP addresses based on IP range. Nothing fancy, but could come in handy if you ever need it somewhere in your code. Enjoy!

  • Merging multiple Nessus scans (Python script)

    nessus

    If you for any reason have the need to merge / combine a few Nessus scans into a single *.nessus file, you can do so using this simple Python script. Since *.nessus files are basically just XML files with a different extension, what this script does is it finds all the *.nessus files in the current folder, finds all the “ReportHost” XML nodes accumulating them into a single report.nessus file which is then exported to nss_report folder.

    Note that scans must be of the same type (same plugins must be used), but they can be from different subnets or different parts of the same subnet.

    How to use it? – Put the script and all your *.nessus files into a same folder, run the script, import nss_report/report.nessus into Nessus – and there you have it, all the hosts are in one place..

    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
    #! /usr/bin/env python3.2

    import xml.etree.ElementTree as etree
    import shutil
    import os

    first = 1
    for fileName in os.listdir("."):
       if ".nessus" in fileName:
          print(":: Parsing", fileName)
          if first:
             mainTree = etree.parse(fileName)
             report = mainTree.find('Report')
             first = 0
          else:
             tree = etree.parse(fileName)
             for element in tree.findall('.//ReportHost'):
                report.append(element)
          print(":: => done.")

    if "nss_report" in os.listdir("."):
       shutil.rmtree("nss_report")

    os.mkdir("nss_report")
    mainTree.write("nss_report/report.nessus", encoding="utf-8", xml_declaration=True)

    If you have any questions, just drop a comment bellow..

    edit: mastahyeti made some improvements to this script, you can get it at his github

  • Removing multiple elements from a list in Python

    Removing multiple elements from a list in Python

    Let’s say you need to filter a Python list and remove all elements that match a given criteria. If you wanted to remove a single element, you could just use ‘del list[i]’ (example). But if you wanted to remove multiple elements this might be a problem since you would be modifying and iterating over the list at the same time (keeping track of list indexes can become very confusing, very fast).

    A simple solution to this problem would be to keep record of all list elements (indexes) that need to be removed, and to remove tham afterwards. Also, one other thing to be wary about is that the removal process should be done in reverse because otherwise you would shift all the elements to the ‘left’ every time you removed an element from a list and you would (again) have to keep track of list indexes.

    A simple function example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    matchingFilter = ['criteria1', 'criteria2']

    def criteriaFiltering(aList, matchingFilter):
       deletionIndexes = []
       i = 0

       for listLine in aList:
          for match in matchingFilter:
             if match in str(listLine):
                continue             
             else:
                deletionIndexes.append(i)
                break
          i += 1

       for number in reversed(deletionIndexes):
          del listLines[number]

       return aList

    This might not be the fastest ‘algorithm’ to do the job, but it works well enough for me..

Back to top