diff --git a/pynetsim/protocols/ftp.py b/pynetsim/protocols/ftp.py index eb86cda..5acbc2e 100644 --- a/pynetsim/protocols/ftp.py +++ b/pynetsim/protocols/ftp.py @@ -5,26 +5,31 @@ import select import time import logging +import pynetsim.lib.core as core +import re from pynetsim.protocols.tcp import TCP log = logging.getLogger(__name__) - class FTP(TCP): name = "ftp" + config = core.get_config() + #Files specified in the pynetsim.conf file in the main folder---------------- + ftp_files = config.get("ftp").get("file_list",['picture.jpg','backup01_5','user_rights.txt']) + #---------------------------------------------------------------------------- proto_commands = dict(USER="331 Password required for {USER}\r\n", PASS="230 User {USER} logged in.\r\n", SYST="215 UNIX Type: L8\r\n", CWD="""250 CWD command successful. "{CWD}" is current directory.\r\n""", PWD="""257 "{CWD}" is current directory.\r\n""", - TYPE="200 Type set to {TYPE}.\r\n", - SIZE="213 {RANDOM_SIZE}\r\n", - PASV="500 PASV command not supported.\r\n", - LIST="", - FEAT="211-Extensions Supported:\r\nSIZE\r\nCLNT\r\n211 End.\r\n", + TYPE="200 Type set to: {TYPE}\r\n200 End.\r\n'", + SIZE="213 Size:{RANDOM_SIZE}bytes.\r\n", + PASV="PASV command not supported.\r\n", + LIST="212-Files on {CWD}:\r\n{LIST}\r\n212 End.\r\n", + FEAT="211-Extensions Supported:\r\nSIZE\r\nSYST\r\nCWD\r\nTYPE\r\n211 End.\r\n", CLNT="""200 "{CLNT}" noted.""", RETR="{RANDOM_DATA}", MKD="""257 "{MKD}": The directory was successfully created.\r\n""", @@ -35,7 +40,9 @@ class FTP(TCP): proto_vars = dict(CWD="/", RANDOM_SIZE=123453, - RANDOM_DATA="a"*123453) + RANDOM_DATA="a"*123, + TYPE ='', + LIST='',) def run(self): load = self.payload @@ -75,6 +82,16 @@ def recv(self): data = None s = select.select([self.socket], [], [], 10) if s[0]: + if 'LIST' in command : + self.handle_list_command() + elif 'TYPE' in command : + if re.search('ASCII|A',str(command_parts),re.I) is not None: # used re to handle upper or lower case. + self.handle_type_command('ASCII') + elif re.search('BIN|BINARY|B',str(command_parts),re.I) is not None : #not case sensitive + handles bin/binary + self.handle_type_command('BIN') + else : + expl = "Unsupported type. Supported types : ASCII/BINARY\r\nExample : TYPE ASCII" + self.handle_type_command(expl) data = self.socket.recv(self.recv_size) return data @@ -87,6 +104,19 @@ def handle_port_command(self): def handle_pasv_command(self): raise RuntimeError("Currently Not Implemented") + def handle_list_command(self) : + response_additional = '' + #SEE NOTE 1 FOR ADDITIONAL EXPLANATIONS ABOUT files_rights_list + files_rights_list = ['drwxr--r--'] + for item in self.ftp_files : + item = files_rights_list[0]+' ftp file_size date/hour_here '+item+'\r\n' + response_additional = response_additional+item + self.proto_vars['LIST'] = response_additional + + def handle_type_command(self,type1) : + self.type = type1 + self.proto_vars['TYPE'] = self.type + @classmethod def guess_protocol_from_payload(cls, payload, config, addr): """