<s> import cmd <EOL> import sqlite3 <EOL> import sys <EOL> import os <EOL> import requests <EOL> import argparse <EOL> from requests import ConnectionError <EOL> from ConfigParser import ConfigParser <EOL> from core . database import CMEDatabase <EOL> from core . helpers import validate_ntlm <EOL> requests . packages . urllib3 . disable_warnings ( ) <EOL> class CMEDatabaseNavigator ( cmd . Cmd ) : <EOL> def __init__ ( self ) : <EOL> cmd . Cmd . __init__ ( self ) <EOL> self . prompt = '<STR_LIT>' <EOL> try : <EOL> conn = sqlite3 . connect ( '<STR_LIT>' , check_same_thread = False ) <EOL> conn . text_factory = str <EOL> conn . isolation_level = None <EOL> self . db = CMEDatabase ( conn ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" . format ( e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> self . config = ConfigParser ( ) <EOL> self . config . read ( '<STR_LIT>' ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" . format ( e ) <EOL> def display_creds ( self , creds ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for cred in creds : <EOL> credID = cred [ <NUM_LIT:0> ] <EOL> credType = cred [ <NUM_LIT:1> ] <EOL> domain = cred [ <NUM_LIT:2> ] <EOL> username = cred [ <NUM_LIT:3> ] <EOL> password = cred [ <NUM_LIT:4> ] <EOL> links = self . db . get_links ( credID = credID ) <EOL> print u"<STR_LIT>" . format ( '<STR_LIT>' . format ( credID ) , <EOL> '<STR_LIT>' . format ( str ( len ( links ) ) + '<STR_LIT>' ) , <EOL> '<STR_LIT>' . format ( credType ) , <EOL> u'<STR_LIT>' . format ( domain . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( username . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( password . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> print "<STR_LIT>" <EOL> def display_hosts ( self , hosts ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for host in hosts : <EOL> hostID = host [ <NUM_LIT:0> ] <EOL> ip = host [ <NUM_LIT:1> ] <EOL> hostname = host [ <NUM_LIT:2> ] <EOL> domain = host [ <NUM_LIT:3> ] <EOL> os = host [ <NUM_LIT:4> ] <EOL> links = self . db . get_links ( hostID = hostID ) <EOL> print u"<STR_LIT>" . format ( '<STR_LIT>' . format ( hostID ) , <EOL> '<STR_LIT>' . format ( str ( len ( links ) ) + '<STR_LIT>' ) , <EOL> '<STR_LIT>' . format ( ip ) , <EOL> u'<STR_LIT>' . format ( hostname . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( domain . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> '<STR_LIT>' . format ( os ) ) <EOL> print "<STR_LIT>" <EOL> def do_exit ( self , line ) : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def do_import ( self , line ) : <EOL> if not line : <EOL> return <EOL> if line == '<STR_LIT>' : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> payload = { '<STR_LIT:username>' : self . config . get ( '<STR_LIT>' , '<STR_LIT:username>' ) , <EOL> '<STR_LIT:password>' : self . config . get ( '<STR_LIT>' , '<STR_LIT:password>' ) } <EOL> base_url = '<STR_LIT>' . format ( self . config . get ( '<STR_LIT>' , '<STR_LIT>' ) , self . config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> try : <EOL> r = requests . post ( base_url + '<STR_LIT>' , json = payload , headers = headers , verify = False ) <EOL> if r . status_code == <NUM_LIT:200> : <EOL> token = r . json ( ) [ '<STR_LIT>' ] <EOL> url_params = { '<STR_LIT>' : token } <EOL> r = requests . get ( base_url + '<STR_LIT>' , headers = headers , params = url_params , verify = False ) <EOL> creds = r . json ( ) <EOL> for cred in creds [ '<STR_LIT>' ] : <EOL> if cred [ '<STR_LIT>' ] == '<STR_LIT>' or cred [ '<STR_LIT>' ] == '<STR_LIT>' or cred [ '<STR_LIT:username>' ] . endswith ( '<STR_LIT:$>' ) : <EOL> continue <EOL> self . db . add_credential ( cred [ '<STR_LIT>' ] , cred [ '<STR_LIT>' ] , cred [ '<STR_LIT:username>' ] , cred [ '<STR_LIT:password>' ] ) <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> except ConnectionError as e : <EOL> print "<STR_LIT>" . format ( e ) <EOL> def complete_import ( self , text , line , begidx , endidx ) : <EOL> "<STR_LIT>" <EOL> commands = [ "<STR_LIT>" ] <EOL> mline = line . partition ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:2> ] <EOL> offs = len ( mline ) - len ( text ) <EOL> return [ s [ offs : ] for s in commands if s . startswith ( mline ) ] <EOL> def do_hosts ( self , line ) : <EOL> filterTerm = line . strip ( ) <EOL> if filterTerm == "<STR_LIT>" : <EOL> hosts = self . db . get_hosts ( ) <EOL> self . display_hosts ( hosts ) <EOL> else : <EOL> hosts = self . db . get_hosts ( filterTerm = filterTerm ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> hostIDList = [ ] <EOL> for host in hosts : <EOL> hostID = host [ <NUM_LIT:0> ] <EOL> hostIDList . append ( hostID ) <EOL> ip = host [ <NUM_LIT:1> ] <EOL> hostname = host [ <NUM_LIT:2> ] <EOL> domain = host [ <NUM_LIT:3> ] <EOL> os = host [ <NUM_LIT:4> ] <EOL> print u"<STR_LIT>" . format ( '<STR_LIT>' . format ( hostID ) , <EOL> '<STR_LIT>' . format ( ip ) , <EOL> u'<STR_LIT>' . format ( hostname . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( domain . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> '<STR_LIT>' . format ( os ) ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for hostID in hostIDList : <EOL> links = self . db . get_links ( hostID = hostID ) <EOL> for link in links : <EOL> linkID , credID , hostID = link <EOL> creds = self . db . get_credentials ( filterTerm = credID ) <EOL> for cred in creds : <EOL> credID = cred [ <NUM_LIT:0> ] <EOL> credType = cred [ <NUM_LIT:1> ] <EOL> domain = cred [ <NUM_LIT:2> ] <EOL> username = cred [ <NUM_LIT:3> ] <EOL> password = cred [ <NUM_LIT:4> ] <EOL> print u"<STR_LIT>" . format ( '<STR_LIT>' . format ( credID ) , <EOL> '<STR_LIT>' . format ( credType ) , <EOL> u'<STR_LIT>' . format ( domain . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( username . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( password . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> print "<STR_LIT>" <EOL> def do_creds ( self , line ) : <EOL> filterTerm = line . strip ( ) <EOL> if filterTerm == "<STR_LIT>" : <EOL> creds = self . db . get_credentials ( ) <EOL> self . display_creds ( creds ) <EOL> elif filterTerm . split ( ) [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> args = filterTerm . split ( ) [ <NUM_LIT:1> : ] <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> domain , username , password = args <EOL> if validate_ntlm ( password ) : <EOL> self . db . add_credential ( "<STR_LIT>" , domain , username , password ) <EOL> else : <EOL> self . db . add_credential ( "<STR_LIT>" , domain , username , password ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> return <EOL> elif filterTerm . split ( ) [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> args = filterTerm . split ( ) [ <NUM_LIT:1> : ] <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> return <EOL> else : <EOL> self . db . remove_credentials ( args ) <EOL> self . db . remove_links ( credIDs = args ) <EOL> elif filterTerm . split ( ) [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> creds = self . db . get_credentials ( credtype = "<STR_LIT>" ) <EOL> self . display_creds ( creds ) <EOL> elif filterTerm . split ( ) [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> creds = self . db . get_credentials ( credtype = "<STR_LIT>" ) <EOL> self . display_creds ( creds ) <EOL> else : <EOL> creds = self . db . get_credentials ( filterTerm = filterTerm ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> credIDList = [ ] <EOL> for cred in creds : <EOL> credID = cred [ <NUM_LIT:0> ] <EOL> credIDList . append ( credID ) <EOL> credType = cred [ <NUM_LIT:1> ] <EOL> domain = cred [ <NUM_LIT:2> ] <EOL> username = cred [ <NUM_LIT:3> ] <EOL> password = cred [ <NUM_LIT:4> ] <EOL> print u"<STR_LIT>" . format ( '<STR_LIT>' . format ( credID ) , <EOL> '<STR_LIT>' . format ( credType ) , <EOL> u'<STR_LIT>' . format ( domain . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( username . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( password . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for credID in credIDList : <EOL> links = self . db . get_links ( credID = credID ) <EOL> for link in links : <EOL> linkID , credID , hostID = link <EOL> hosts = self . db . get_hosts ( hostID ) <EOL> for host in hosts : <EOL> hostID = host [ <NUM_LIT:0> ] <EOL> ip = host [ <NUM_LIT:1> ] <EOL> hostname = host [ <NUM_LIT:2> ] <EOL> domain = host [ <NUM_LIT:3> ] <EOL> os = host [ <NUM_LIT:4> ] <EOL> print u"<STR_LIT>" . format ( '<STR_LIT>' . format ( hostID ) , <EOL> '<STR_LIT>' . format ( ip ) , <EOL> u'<STR_LIT>' . format ( hostname . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> u'<STR_LIT>' . format ( domain . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> '<STR_LIT>' . format ( os ) ) <EOL> print "<STR_LIT>" <EOL> def complete_creds ( self , text , line , begidx , endidx ) : <EOL> "<STR_LIT>" <EOL> commands = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> mline = line . partition ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:2> ] <EOL> offs = len ( mline ) - len ( text ) <EOL> return [ s [ offs : ] for s in commands if s . startswith ( mline ) ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT:path>" , nargs = '<STR_LIT:?>' , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> if not os . path . exists ( args . path ) : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> cmedbnav = CMEDatabaseNavigator ( ) <EOL> cmedbnav . cmdloop ( ) <EOL> except KeyboardInterrupt : <EOL> pass </s>
