<s> from kippo_extra . utils import ExtendedHoneyPotCommand , ModifiedOptionParser , OptionParsingError , OptionParsingExit <EOL> commands = { } <EOL> class command_iptables ( ExtendedHoneyPotCommand ) : <EOL> resolve_args = False <EOL> APP_NAME = "<STR_LIT>" <EOL> APP_VERSION = "<STR_LIT>" <EOL> DEFAULT_TABLE = "<STR_LIT>" <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . args ) == <NUM_LIT:0> : <EOL> self . no_command ( ) <EOL> return <EOL> def optional_arg ( arg_default ) : <EOL> def func ( option , opt_str , value , parser ) : <EOL> if parser . rargs and not parser . rargs [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:->' ) : <EOL> val = parser . rargs [ <NUM_LIT:0> ] <EOL> parser . rargs . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> val = arg_default <EOL> setattr ( parser . values , option . dest , val ) <EOL> return func <EOL> parser = ModifiedOptionParser ( add_help_option = False ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:version>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" , default = command_iptables . DEFAULT_TABLE ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT>" , callback = optional_arg ( True ) ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT>" , callback = optional_arg ( True ) ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT>" , callback = optional_arg ( True ) ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:list>" , action = "<STR_LIT>" , callback = optional_arg ( True ) ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:replace>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:source>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT:state>" , action = "<STR_LIT:store>" ) <EOL> try : <EOL> ( opts , args ) = parser . parse_args ( list ( self . args ) ) <EOL> except OptionParsingError , e : <EOL> self . bad_argument ( self . args [ <NUM_LIT:0> ] ) <EOL> return <EOL> except OptionParsingExit , e : <EOL> self . unknown_option ( e ) <EOL> return <EOL> if not self . setup_table ( opts . table ) : <EOL> return <EOL> if opts . help : <EOL> self . show_help ( ) <EOL> return <EOL> elif opts . version : <EOL> self . show_version ( ) <EOL> return <EOL> elif opts . flush : <EOL> self . flush ( "<STR_LIT>" if opts . flush == True else opts . flush ) <EOL> return <EOL> elif opts . list : <EOL> self . list ( "<STR_LIT>" if opts . list == True else opts . list ) <EOL> return <EOL> elif opts . list_rules : <EOL> self . list_rules ( "<STR_LIT>" if opts . list_rules == True else opts . list_rules ) <EOL> return <EOL> self . exit ( ) <EOL> def setup_table ( self , table ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self . honeypot . env , '<STR_LIT>' ) : <EOL> setattr ( self . honeypot . env , '<STR_LIT>' , { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] <EOL> } <EOL> } ) <EOL> self . tables = getattr ( self . honeypot . env , '<STR_LIT>' ) <EOL> if not self . is_valid_table ( table ) : <EOL> return False <EOL> self . current_table = self . tables [ table ] <EOL> return True <EOL> def is_valid_table ( self , table ) : <EOL> if self . user_is_root ( ) : <EOL> if not table in self . tables . iterkeys ( ) : <EOL> self . writeln ( """<STR_LIT>""" % ( command_iptables . APP_NAME , table ) ) <EOL> self . exit ( ) <EOL> else : <EOL> return True <EOL> else : <EOL> self . no_permission ( ) <EOL> return False <EOL> def is_valid_chain ( self , chain ) : <EOL> if not chain in self . current_table . iterkeys ( ) : <EOL> self . writeln ( "<STR_LIT>" % command_iptables . APP_NAME ) <EOL> self . exit ( ) <EOL> return False <EOL> return True <EOL> def show_version ( self ) : <EOL> """<STR_LIT>""" <EOL> self . writeln_and_exit ( '<STR_LIT>' % ( command_iptables . APP_NAME , command_iptables . APP_VERSION ) ) <EOL> def show_help ( self ) : <EOL> """<STR_LIT>""" <EOL> self . writeln_and_exit ( """<STR_LIT>""" % ( <EOL> command_iptables . APP_NAME , command_iptables . APP_VERSION ) ) <EOL> def list_rules ( self , chain ) : <EOL> """<STR_LIT>""" <EOL> if self . user_is_root ( ) : <EOL> if len ( chain ) > <NUM_LIT:0> : <EOL> print chain <EOL> if not self . is_valid_chain ( chain ) : <EOL> return <EOL> chains = [ chain ] <EOL> else : <EOL> chains = self . current_table . iterkeys ( ) <EOL> output = [ ] <EOL> for chain in chains : <EOL> output . append ( "<STR_LIT>" % chain ) <EOL> self . writeln_and_exit ( output ) <EOL> else : <EOL> self . no_permission ( ) <EOL> def list ( self , chain ) : <EOL> """<STR_LIT>""" <EOL> if self . user_is_root ( ) : <EOL> if len ( chain ) > <NUM_LIT:0> : <EOL> print chain <EOL> if not self . is_valid_chain ( chain ) : <EOL> return <EOL> chains = [ chain ] <EOL> else : <EOL> chains = self . current_table . iterkeys ( ) <EOL> output = [ ] <EOL> for chain in chains : <EOL> chain_output = [ <EOL> "<STR_LIT>" % chain , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for rule in self . current_table [ chain ] : <EOL> chain_output . append ( <EOL> "<STR_LIT>" % rule , <EOL> ) <EOL> output . append ( "<STR_LIT:\n>" . join ( chain_output ) ) <EOL> self . writeln_and_exit ( "<STR_LIT>" . join ( output ) ) <EOL> else : <EOL> self . no_permission ( ) <EOL> def flush ( self , chain ) : <EOL> """<STR_LIT>""" <EOL> if self . user_is_root ( ) : <EOL> if len ( chain ) > <NUM_LIT:0> : <EOL> if not self . is_valid_chain ( chain ) : <EOL> return <EOL> chains = [ chain ] <EOL> else : <EOL> chains = self . current_table . iterkeys ( ) <EOL> for chain in chains : <EOL> self . current_table [ chain ] = [ ] <EOL> self . exit ( ) <EOL> else : <EOL> self . no_permission ( ) <EOL> def no_permission ( self ) : <EOL> self . writeln_and_exit ( """<STR_LIT>""" % ( <EOL> command_iptables . APP_NAME , command_iptables . APP_VERSION ) ) <EOL> def no_command ( self ) : <EOL> """<STR_LIT>""" <EOL> self . writeln_and_exit ( """<STR_LIT>""" % ( <EOL> command_iptables . APP_NAME , command_iptables . APP_VERSION ) ) <EOL> def unknown_option ( self , option ) : <EOL> """<STR_LIT>""" <EOL> self . writeln_and_exit ( """<STR_LIT>""" % ( <EOL> command_iptables . APP_NAME , command_iptables . APP_VERSION , option ) ) <EOL> def bad_argument ( self , argument ) : <EOL> """<STR_LIT>""" <EOL> self . writeln_and_exit ( """<STR_LIT>""" % argument ) <EOL> commands [ '<STR_LIT>' ] = command_iptables </s>
