Issue
When i execute the code the file is sent successfully; i also tried to use tcpdump to check whether the file is actually transferring o not and it was but i can't find the file in destination vsftpd server(in kali linux)
#!/bin/python3
import ftplib
import socket
import os
def main():
file_path=input("enter the absolute_file_path of the file you want to upload",)
filesize = os.path.getsize(file_path)
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.190.130',21))
data = s.recv(1024)
print(data)
s.send('USER harsh'.encode('utf-8')+'\r\n'.encode('utf-8'))
data = s.recv(1024)
print(data)
s.send('PASS iacsd@123'.encode('utf-8')+'\r\n'.encode('utf-8'))
data = s.recv(1024)
print(data)
BUFFER_SIZE = 4096
s.send(f"{file_path}{filesize}\r\n".encode('utf-8'))
# Open the file in binary mode and send its content
with open(file_path, 'rb') as f:
while True:
bytes_read = f.read(BUFFER_SIZE)
if not bytes_read:
break
s.sendall(bytes_read)
print("File uploaded successfully.")
# s.send(f"{file_path}{filesize}".encode('utf-8'))
# with open(file_path, 'rb') as f:
# bytes_read = f.read()
# s.sendall(bytes_read)
# file=open(file_path,'r')
# data = file.read()
# s.send(str(data).encode('utf-8'))
# os.system('put file')
if __name__=='__main__':
main()
This is the python script i wrote in debian and in kali linux vsftpd is running with 21 port open/connected to debian machine
Solution
What you are implementing is not actually the "real" FTP protocol, but some custom protocol which resembles FTP for the first few commands (ie, logging in) but then totally diverges from "real" FTP for actually transferring data. Data transfers in FTP are done with a separate TCP connection, where the parameters and direction of this connection are exchanged using PORT
and PASV
commands (or EPRT
and EPSV
for IPv6), and then commands like STOR
and RETR
are used to perform the actual transfers.
I recommend that you use ftplib, which actually implements the real FTP protocol. If you really want to implement it by your own, please study the actual standard - RFC 959
Answered By - Steffen Ullrich Answer Checked By - Pedro (WPSolving Volunteer)