Skip to content

Commit 1646c8a

Browse files
committed
gateway: Improve check_ftp function
1 parent c1537c4 commit 1646c8a

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

gateway.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ def detect_ssh(self, verbose = 1, interactive = True, contimeout = 2, aux_port =
726726
self.passw = passw
727727
ftp_en = self.check_tcp_connect(self.ip_addr, 21, contimeout = 1)
728728
if ftp_en:
729-
if self.check_ftp() == 0:
729+
if self.check_ftp(check_upload = True) == 0:
730730
self.use_ftp = True
731731
return 23
732732

@@ -831,20 +831,30 @@ def get_telnet(self, verbose = 0, password = None):
831831
die("Can't login to TELNET (IP: {})".format(self.ip_addr))
832832
return None
833833

834-
def check_ftp(self, timeout = None):
834+
def check_ftp(self, check_upload = False, timeout = None):
835835
ret = -1
836836
if not timeout:
837837
timeout = self.timeout
838838
ftp = None
839839
try:
840840
ftp = ftplib.FTP(self.ip_addr, user=self.login, passwd=self.passw, timeout=timeout)
841841
ftp.voidcmd("NOOP")
842+
if check_upload:
843+
import io
844+
fp = io.BytesIO(b"HELLO_123")
845+
remote_fn = '/tmp/_test_payload.bin'
846+
ftp.storbinary(f'STOR {remote_fn}', fp)
847+
ftp.delete(remote_fn)
842848
ret = 0
849+
except ftplib.error_perm as e:
850+
ret = -3
851+
if '500 Unknown command' in str(e):
852+
ret = -11
843853
except ftplib.error_proto as e:
844854
ret = -2
845855
if 'unrecognized option: w' in str(e):
846856
ret = -10
847-
except Exception:
857+
except Exception as e:
848858
ret = -1
849859
finally:
850860
if ftp:
@@ -1099,7 +1109,7 @@ def post_connect(self, exec_cmd, contimeout = 20, passw = 'root'):
10991109
self.run_cmd(r'/etc/init.d/inetd enable')
11001110
self.run_cmd(r'/etc/init.d/inetd restart')
11011111
ftp_en = self.check_ftp(timeout = 5)
1102-
if ftp_en == -10:
1112+
if ftp_en <= -10:
11031113
print(f'WARNING: FTP server is running, but upload mode is blocked!')
11041114
elif ftp_en != 0:
11051115
print(f"WARNING: FTP server not responding (IP: {self.ip_addr})")
@@ -1136,7 +1146,11 @@ def create_gateway(timeout = 4, die_if_sshOk = True, die_if_ftpOk = True, web_lo
11361146
if ret == 23:
11371147
if gw.use_ftp and die_if_ftpOk:
11381148
die("Telnet and FTP servers already running!")
1139-
print("Telnet server already running, but FTP server not respond")
1149+
ftp_err = gw.check_ftp(check_upload = True)
1150+
if ftp_err <= -10:
1151+
print("Telnet server already running, but upload mode on FTP server is blocked")
1152+
else:
1153+
print("Telnet server already running, but FTP server not respond")
11401154
elif ret > 0:
11411155
if die_if_sshOk:
11421156
die(0, "SSH server already installed and running")

0 commit comments

Comments
 (0)