2019-09-18 16:09:53 +02:00
import os
import sys
import argparse
2019-09-28 12:03:51 -04:00
import datetime
2019-09-18 16:09:53 +02:00
try :
import glob
except ImportError :
print ( " Please install glob package " )
sys . exit ( )
2019-09-28 12:03:51 -04:00
if sys . version_info [ 0 ] < 3 :
raise Exception ( " Must be using Python 3 " )
parser = argparse . ArgumentParser ( description = ' This script identifies issues with module documentation, including missing documentation, and incorrect file names. Output is in markdown format. ' ,
prefix_chars = ' -- ' , )
2019-09-18 16:09:53 +02:00
parser . add_argument ( ' -m ' , ' --modules ' , type = str , default = ' auxiliary/scanner ' , help = ' Choose the modules category to work with. Respect the module category names as in metasploit-framework. Only one category should be passed, e.g. " auxiliary/admin " , " exploits/android/browser " or " encoders " are valid entries. ' )
parser . add_argument ( ' --show_all ' , action = " store_true " , default = False , help = ' Show the complete list of items. In default mode, modules with documentation are marked " [x] " and modules without are marked " [ ] " . In issues mode, documentation files without module are marked " [ ] " and documentation files with module are marked " [x] " . ' )
parser . add_argument ( ' --show_issues ' , action = " store_true " , default = False , help = ' Show the list of documentation files without modules instead of modules withouth documentation file. ' )
2019-09-28 12:03:51 -04:00
parser . add_argument ( ' -o ' , ' --output ' , help = " Writes to a file. " )
2019-09-18 16:09:53 +02:00
args = parser . parse_args ( )
module_type = args . modules
show_all = args . show_all
show_issues = args . show_issues
modules = [ ]
docs = [ ]
path = os . path . abspath ( os . path . join ( os . path . realpath ( __file__ ) , " .. " , " .. " , " .. " ) )
2019-09-28 12:03:51 -04:00
if os . path . exists ( os . path . join ( path , ' modules ' , module_type ) ) :
list_docs = glob . glob ( os . path . join ( path , ' documentation/modules ' , module_type , ' **/*.md ' ) , recursive = True )
list_modules = glob . glob ( os . path . join ( path , ' modules ' , module_type , ' **/*.* ' ) , recursive = True )
2019-09-18 16:09:53 +02:00
else :
2019-09-28 12:03:51 -04:00
print ( " Path doesn ' t exist. Maybe you have passed a wrong module category or maybe there isn ' t any documentation file yet. " )
sys . exit ( )
2019-10-02 21:03:00 -04:00
2019-09-18 16:09:53 +02:00
for doc in list_docs :
docs . append ( doc . split ( ' . ' ) [ 0 ] . replace ( ' /documentation/ ' , ' / ' ) )
for module in list_modules :
modules . append ( module . split ( ' . ' ) [ 0 ] )
2019-10-02 21:03:00 -04:00
missings = 0
problems = 0
2019-09-18 16:09:53 +02:00
count = 0
2019-10-02 21:03:00 -04:00
root = ' metasploit-framework '
2019-10-06 18:13:52 -04:00
url_root = ' https://github.com/rapid7/metasploit-framework/blob/master/modules/ '
2019-09-18 16:09:53 +02:00
2019-09-28 12:03:51 -04:00
if args . output :
o = open ( args . output , ' w ' )
def print_or_write ( line ) :
if args . output :
o . write ( " %s \n " % ( line ) )
return
print ( line )
2019-10-06 18:13:52 -04:00
def make_link ( line ) :
# first we wenat to get the extension back
for m in list_modules :
if " %s . " % ( line ) in m :
ext = m
break
link = ext . split ( " /modules " ) [ 1 ]
#link = ext.replace("/modules", url_root)
return " [ %s %s ]( %s %s ) " % ( root , line , url_root , link )
2019-10-02 21:03:00 -04:00
print_or_write ( ' # Documentation Issue Finder \n ' )
print_or_write ( ' Generated: %s \n ' % ( datetime . datetime . now ( ) . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ) )
2019-09-28 12:03:51 -04:00
if not ( show_all ) :
if not ( show_issues ) :
print_or_write ( ' ## Modules Without Documentation \n ' )
2019-10-02 21:03:00 -04:00
for i in sorted ( modules ) :
2019-09-28 12:03:51 -04:00
if i not in docs :
2019-10-02 21:03:00 -04:00
missings + = 1
2019-10-06 18:13:52 -04:00
print_or_write ( ' + [ ] %s ' % ( make_link ( i . split ( ' metasploit-framework ' ) [ 1 ] ) ) )
2019-10-02 21:03:00 -04:00
print_or_write ( ' \n %s modules have no documentation. ' % ( missings ) )
2019-09-28 12:03:51 -04:00
else :
print_or_write ( ' ## Docs Without Modules \n ' )
2019-10-02 21:03:00 -04:00
for i in sorted ( docs ) :
2019-09-28 12:03:51 -04:00
if i not in modules :
2019-10-02 21:03:00 -04:00
problems + = 1
2019-10-06 18:13:52 -04:00
print_or_write ( ' + [ ] %s ' % ( make_link ( i . split ( ' metasploit-framework ' ) [ 1 ] ) ) )
2019-10-02 21:03:00 -04:00
print_or_write ( ' \n %s doc files do not correspond to any module. ' % ( problems ) )
2019-09-18 16:09:53 +02:00
else :
2019-09-28 12:03:51 -04:00
count = 0
if not ( show_issues ) :
print_or_write ( ' ## Modules Without Documentation \n ' )
for i in sorted ( modules ) :
if i in docs :
2019-10-06 18:13:52 -04:00
print_or_write ( ' + [x] %s ' % ( make_link ( i . split ( ' metasploit-framework ' ) [ 1 ] ) ) )
2019-09-28 12:03:51 -04:00
else :
2019-10-06 18:13:52 -04:00
print_or_write ( ' + [ ] %s ' % ( make_link ( i . split ( ' metasploit-framework ' ) [ 1 ] ) ) )
2019-09-28 12:03:51 -04:00
count + = 1
2019-10-02 21:03:00 -04:00
print_or_write ( ' \n %s modules out of %s ( %d %% ) have no documentation. ' % ( count , len ( modules ) , float ( count ) / len ( modules ) * 100.0 ) )
2019-09-28 12:03:51 -04:00
else :
print_or_write ( ' ## Docs Without Modules \n ' )
for i in sorted ( docs ) :
if i in modules :
2019-10-06 18:13:52 -04:00
print_or_write ( ' + [x] %s ' % ( make_link ( i . split ( ' metasploit-framework ' ) [ 1 ] ) ) )
2019-09-28 12:03:51 -04:00
else :
2019-10-06 18:13:52 -04:00
print_or_write ( ' + [ ] %s ' % ( make_link ( i . split ( ' metasploit-framework ' ) [ 1 ] ) ) )
2019-09-28 12:03:51 -04:00
count + = 1
2019-10-02 21:03:00 -04:00
print_or_write ( ' \n %s doc files out of %s do not correspond to any module. ' % ( count , len ( docs ) ) )
2019-09-18 16:09:53 +02:00
2019-09-28 12:03:51 -04:00
if args . output :
o . close ( )