#NEWLINE# def main( ) : #NEWLINE# #INDENT# parser = argparse . ArgumentParser ( description = 'Test accesses of files over HTTP versus filesystem' ) #NEWLINE# parser . add_argument ( 'id' , action = 'store' , type = int , help = 'Input image id' ) #NEWLINE# parser . add_argument ( 'ip' , action = 'store' , help = 'IP address of emulated image' ) #NEWLINE# parser . add_argument ( 'log' , action = 'store' , help = 'Output list of accessible URLs' ) #NEWLINE# parser . add_argument ( 'sql' , action = 'store' , default = '127.0.0.1' , nargs = '?' , help = 'Hostname of SQL server' ) #NEWLINE# parser . add_argument ( '-p' , action = 'store' , dest = 'pattern' , default = '/www/' , help = 'Filename pattern of files to access' ) #NEWLINE# cmd = parser . parse_args ( ) #NEWLINE# db = psycopg2 . connect ( database = 'firmware' , user = 'firmadyne' , password = 'firmadyne' , host = cmd . sql ) #NEWLINE# files = [ ] #NEWLINE# try : #NEWLINE# #INDENT# cur = db . cursor ( ) #NEWLINE# cur . execute ( 'SELECT filename FROM object_to_image WHERE iid=%s AND filename LIKE %s' , ( cmd . id , ( ( '%' + cmd . pattern ) + '%' ) ) ) #NEWLINE# files = cur . fetchall ( ) #NEWLINE# #UNINDENT# except BaseException : #NEWLINE# #INDENT# traceback . print_exc ( ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if cur : #NEWLINE# #INDENT# cur . close ( ) #NEWLINE# #UNINDENT# if db : #NEWLINE# #INDENT# db . close ( ) #NEWLINE# #UNINDENT# #UNINDENT# accessible = [ ] #NEWLINE# for file in files : #NEWLINE# #INDENT# ( head , sep , tail ) = file [ 0 ] . partition ( cmd . pattern ) #NEWLINE# if ( tail and ( ( '.' not in tail ) or any ( ( tail . endswith ( ext ) for ext in [ '.htm' , '.html' , '.cgi' , '.asp' , '.php' , '.bin' , '.xml' , '.rg' ] ) ) ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# url = urllib . parse . urlunsplit ( ( 'http' , cmd . ip , tail , None , None ) ) #NEWLINE# print ( ( 'Accessing: %s...' % url ) ) #NEWLINE# req = urllib . request . urlopen ( url , timeout = 5 ) #NEWLINE# data = req . read ( ) #NEWLINE# if ( ( b'location.href' in data ) or ( b'window.location' in data ) ) : #NEWLINE# #INDENT# print ( '-> Redirect' ) #NEWLINE# accessible . append ( ( tail + ' (REDIR)' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# accessible . append ( tail ) #NEWLINE# #UNINDENT# #UNINDENT# except socket . timeout as exc : #NEWLINE# #INDENT# print ( ( '-> Socket Timeout: %s' % exc ) ) #NEWLINE# #UNINDENT# except http . client . IncompleteRead as exc : #NEWLINE# #INDENT# data = exc . partial #NEWLINE# #UNINDENT# except urllib . error . HTTPError as exc : #NEWLINE# #INDENT# print ( ( '-> HTTPError: %d' % exc . code ) ) #NEWLINE# #UNINDENT# except urllib . error . URLError as exc : #NEWLINE# #INDENT# print ( ( '-> URLError: %s' % exc . reason ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if tail : #NEWLINE# #INDENT# print ( ( 'Skipping: %s...' % tail ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# with open ( cmd . log , 'w' ) as file : #NEWLINE# #INDENT# for url in accessible : #NEWLINE# #INDENT# file . write ( ( url + '\n' ) )