66"""
77
88import os
9+ import shlex
910import sys
1011
1112from optparse import OptionError
1718from lib .core .common import checkSystemEncoding
1819from lib .core .common import expandMnemonics
1920from lib .core .common import getUnicode
21+ from lib .core .data import cmdLineOptions
22+ from lib .core .data import conf
2023from lib .core .data import logger
2124from lib .core .defaults import defaults
25+ from lib .core .enums import AUTOCOMPLETE_TYPE
26+ from lib .core .exception import SqlmapShellQuitException
2227from lib .core .settings import BASIC_HELP_ITEMS
2328from lib .core .settings import DUMMY_URL
2429from lib .core .settings import IS_WIN
2530from lib .core .settings import MAX_HELP_OPTION_LENGTH
2631from lib .core .settings import VERSION_STRING
32+ from lib .core .shell import autoCompletion
33+ from lib .core .shell import clearHistory
34+ from lib .core .shell import loadHistory
35+ from lib .core .shell import saveHistory
2736
2837def cmdLineParser ():
2938 """
@@ -693,6 +702,9 @@ def cmdLineParser():
693702 action = "store_true" ,
694703 help = "Conduct through tests only if positive heuristic(s)" )
695704
705+ miscellaneous .add_option ("--sqlmap-shell" , dest = "sqlmapShell" , action = "store_true" ,
706+ help = "Prompt for an interactive sqlmap shell" )
707+
696708 miscellaneous .add_option ("--wizard" , dest = "wizard" ,
697709 action = "store_true" ,
698710 help = "Simple wizard interface for beginner users" )
@@ -765,22 +777,25 @@ def _(self, *args):
765777 option = parser .get_option ("-h" )
766778 option .help = option .help .capitalize ().replace ("this help" , "basic help" )
767779
768- args = []
780+ argv = []
781+ prompt = False
769782 advancedHelp = True
770783
771784 for arg in sys .argv :
772- args .append (getUnicode (arg , system = True ))
785+ argv .append (getUnicode (arg , system = True ))
773786
774- checkDeprecatedOptions (args )
787+ checkDeprecatedOptions (argv )
775788
776789 # Hide non-basic options in basic help case
777790 for i in xrange (len (sys .argv )):
778- if sys .argv [i ] == ' -hh' :
779- sys .argv [i ] = '-h'
780- elif sys .argv [i ] == ' --version' :
791+ if sys .argv [i ] == " -hh" :
792+ sys .argv [i ] = "-h"
793+ elif sys .argv [i ] == " --version" :
781794 print VERSION_STRING
782795 raise SystemExit
783- elif sys .argv [i ] == '-h' :
796+ elif sys .argv [i ] == "--sqlmap-shell" :
797+ prompt = True
798+ elif sys .argv [i ] == "-h" :
784799 advancedHelp = False
785800 for group in parser .option_groups [:]:
786801 found = False
@@ -792,17 +807,56 @@ def _(self, *args):
792807 if not found :
793808 parser .option_groups .remove (group )
794809
810+ if prompt :
811+ cmdLineOptions .sqlmapShell = True
812+
813+ _ = ["x" , "q" , "exit" , "quit" , "clear" ]
814+ for group in parser .option_groups :
815+ for option in group .option_list :
816+ _ .extend (option ._long_opts )
817+ _ .extend (option ._short_opts )
818+
819+ autoCompletion (AUTOCOMPLETE_TYPE .SQLMAP , commands = _ )
820+
821+ while True :
822+ command = None
823+
824+ try :
825+ command = raw_input ("sqlmap-shell> " ).strip ()
826+ except (KeyboardInterrupt , EOFError ):
827+ print
828+ raise SqlmapShellQuitException
829+
830+ if not command :
831+ continue
832+ elif command .lower () == "clear" :
833+ clearHistory ()
834+ print "[i] history cleared"
835+ saveHistory ()
836+ elif command .lower () in ("x" , "q" , "exit" , "quit" ):
837+ raise SqlmapShellQuitException
838+ elif command [0 ] != '-' :
839+ print "[!] invalid option(s) provided"
840+ print "[i] proper example: '-u http://www.site.com/vuln.php?id=1 --banner'"
841+ else :
842+ saveHistory ()
843+ loadHistory ()
844+ break
845+
846+ for arg in shlex .split (command ):
847+ argv .append (getUnicode (arg , system = True ))
848+
795849 try :
796- (args , _ ) = parser .parse_args (args )
850+ (args , _ ) = parser .parse_args (argv )
797851 except SystemExit :
798- if '-h' in sys .argv and not advancedHelp :
852+ if "-h" in sys .argv and not advancedHelp :
799853 print "\n [!] to see full list of options run with '-hh'"
800854 raise
801855
802856 # Expand given mnemonic options (e.g. -z "ign,flu,bat")
803- for i in xrange (len (sys . argv ) - 1 ):
804- if sys . argv [i ] == '-z' :
805- expandMnemonics (sys . argv [i + 1 ], parser , args )
857+ for i in xrange (len (argv ) - 1 ):
858+ if argv [i ] == "-z" :
859+ expandMnemonics (argv [i + 1 ], parser , args )
806860
807861 if args .dummy :
808862 args .url = args .url or DUMMY_URL
0 commit comments