From e0279a5cb3eaf4201120e6c8ebb65793d47ac264 Mon Sep 17 00:00:00 2001 From: pegom96 Date: Tue, 18 Jul 2017 16:25:59 +0200 Subject: [PATCH 1/5] Update_ftp.py 1.Added support for LIST command. The names of the files that the user specifies in the pynetsim.conf will be used. Since the STOR and RETR command are not supported yet, only the names of the files are actually available, not the files themselves, for this reason the LIST command does not return the file size or date/time, this can be easily adapted once downloading files becomes possible. 2.Added more commands in the FEAT command values. 3.Added support for the TYPE command. --- pynetsim/protocols/ftp.py | 61 ++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/pynetsim/protocols/ftp.py b/pynetsim/protocols/ftp.py index eb86cda..9545dd3 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 @@ -67,6 +74,18 @@ def get_response(self, payload): if command == req: response = self.get_proto_command(command) if response: + if 'LIST' in command : + self.handle_list_command() + elif 'TYPE' in command : + if re.search('ASCII',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',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) + else : + pass response = bytes(response.format(**self.proto_vars), encoding="ascii") break return response @@ -87,6 +106,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): """ @@ -101,3 +133,18 @@ def guess_protocol_from_payload(cls, payload, config, addr): if payload.startswith((b"USER ",)): identified_protocol = FTP return identified_protocol + + +#NOTE 1 : +""" +The files_rights_list should be adapt to the desire of the user : + +10 chars : d : the files type ; rwx : owner permissions ; rwx : group permissions ; rwx : all other users permissions + +Chars : + +- : File cannot be read +r : can be read +w : can be modifeid +x: can be executed +""" From 34c4c5f1b5232a470dadb8b6e90bf99bcb6213d8 Mon Sep 17 00:00:00 2001 From: pegom96 Date: Mon, 24 Jul 2017 12:00:04 +0200 Subject: [PATCH 2/5] Update ftp.py --- pynetsim/protocols/ftp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pynetsim/protocols/ftp.py b/pynetsim/protocols/ftp.py index 9545dd3..6486c31 100644 --- a/pynetsim/protocols/ftp.py +++ b/pynetsim/protocols/ftp.py @@ -26,7 +26,7 @@ class FTP(TCP): 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\n200 End.\r\n'", - SIZE="213 Size:{RANDOM_SIZE}bytes.\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", From 59fe6f6a219df8abcd6ea90e6d35fbe6de5ade65 Mon Sep 17 00:00:00 2001 From: pegom96 Date: Tue, 25 Jul 2017 10:41:53 +0200 Subject: [PATCH 3/5] Update ftp.py change detection of LIST and TYPE commands to recv() function instead of putting in the get_response() function. --- pynetsim/protocols/ftp.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pynetsim/protocols/ftp.py b/pynetsim/protocols/ftp.py index 6486c31..ed28ae8 100644 --- a/pynetsim/protocols/ftp.py +++ b/pynetsim/protocols/ftp.py @@ -74,18 +74,6 @@ def get_response(self, payload): if command == req: response = self.get_proto_command(command) if response: - if 'LIST' in command : - self.handle_list_command() - elif 'TYPE' in command : - if re.search('ASCII',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',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) - else : - pass response = bytes(response.format(**self.proto_vars), encoding="ascii") break return response @@ -94,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',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',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 From 52c5a5ab2c779c1b650ca12e02610766d4d0c422 Mon Sep 17 00:00:00 2001 From: pegom96 Date: Tue, 25 Jul 2017 10:42:39 +0200 Subject: [PATCH 4/5] Update ftp.py #NOTE 1 : """ The files_rights_list should be adapt to the desire of the user : 10 chars : d : the files type ; rwx : owner permissions ; rwx : group permissions ; rwx : all other users permissions Chars : - : File cannot be read r : can be read w : can be modifeid x: can be executed """ --- pynetsim/protocols/ftp.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/pynetsim/protocols/ftp.py b/pynetsim/protocols/ftp.py index ed28ae8..81353aa 100644 --- a/pynetsim/protocols/ftp.py +++ b/pynetsim/protocols/ftp.py @@ -131,18 +131,3 @@ def guess_protocol_from_payload(cls, payload, config, addr): if payload.startswith((b"USER ",)): identified_protocol = FTP return identified_protocol - - -#NOTE 1 : -""" -The files_rights_list should be adapt to the desire of the user : - -10 chars : d : the files type ; rwx : owner permissions ; rwx : group permissions ; rwx : all other users permissions - -Chars : - -- : File cannot be read -r : can be read -w : can be modifeid -x: can be executed -""" From 9a1e42d5d64b8cfa2bcb24e4bef55eb8aa78a2a3 Mon Sep 17 00:00:00 2001 From: pegom96 Date: Thu, 27 Jul 2017 15:03:17 +0000 Subject: [PATCH 5/5] Update ftp.py --- pynetsim/protocols/ftp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pynetsim/protocols/ftp.py b/pynetsim/protocols/ftp.py index 81353aa..5acbc2e 100644 --- a/pynetsim/protocols/ftp.py +++ b/pynetsim/protocols/ftp.py @@ -85,9 +85,9 @@ def recv(self): if 'LIST' in command : self.handle_list_command() elif 'TYPE' in command : - if re.search('ASCII',str(command_parts),re.I) is not None: # used re to handle upper or lower case. + 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',str(command_parts),re.I) is not None : #not case sensitive + handles bin/binary + 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"